<commit_before>package providers.hipache

import akka.actor.Actor

import scala.concurrent.Future
import scala.util._
import akka.actor.ActorRef
import akka.event.Logging
import akka.actor.ActorSystem
import net.nikore.etcd._

class HipacheClient(config: Hipache.ServerConfig)(implicit system: ActorSystem) {
  import scala.language.implicitConversions
  import play.api.libs.json._
  import play.api.libs.functional.syntax._
  import EtcdJsonProtocol._
  import system.dispatcher

  def all: Future[Map[Hipache.Frontend,Hipache.Backend]] =
    client.listDir(config.prefix) map { etcdListResponse =>
      etcdListResponse.node.nodes.getOrElse(Nil).flatMap {
        case NodeListElement(key, _, Some(value), _) =>
          (key -> Json.parse(value).as[Seq[String]]) match {
            case Entry(frontend, backend) => Some(frontend -> backend)
            case _ => None
          }
        case _ =>
          None
      }.toMap
    }

  def get(frontend: Hipache.Frontend): Future[Option[Hipache.Backend]] =
    client.getKey(keyFor(frontend))
      .map { etcdResponse =>
        etcdResponse.node.value.flatMap { value => 
          (keyFor(frontend) -> Json.parse(value).as[Seq[String]]) match {
            case Entry(`frontend`, backend) => Some(backend)
            case _ => None
          }
        }
      }.recover {
        case e: EtcdExceptions.KeyNotFoundException => None
      }

  def put(
      frontend: Hipache.Frontend,
      backend: Hipache.Backend): Future[Unit] = 
   client.setKey(
       keyFor(frontend), 
       Json.stringify(Json.arr(frontend.name, backend.toString))) map (_ => ())

  def delete(frontend: Hipache.Frontend): Future[Unit] =
    client.deleteKey(keyFor(frontend)) map (_ => ())

  lazy val keyPrefix = s"/${config.prefix.stripPrefix("/")}/frontend:"
  def keyFor(frontend: Hipache.Frontend) = keyPrefix + frontend.domain

  protected lazy val client = config.client

  object Entry {
    lazy val reBackend = """(\w+)://(.+):(\d+)""".r

    type HipachePair = (Hipache.Frontend, Hipache.Backend)

    def unapply(kvPair: (String, Seq[String])): Option[HipachePair] =
      kvPair match {
        case (k: String, Seq(name: String, backendStr: String))
            if k.startsWith(keyPrefix) =>
          val frontend = Hipache.Frontend(
              name,
              k.stripPrefix(keyPrefix))
          backendStr match {
            case reBackend(scheme, host, portStr) =>
              val backend =
                Hipache.Backend(host, Integer.parseInt(portStr), scheme)
              Some((frontend, backend))
            case _ => None
          }
        case _ =>
          None
      }
  }

  implicit protected def toFUnit[A](f: Future[A]): Future[Unit] = f.map(_ => ())

}
<commit_msg>Fixing leading slash issues with prefix.<commit_after>package providers.hipache

import akka.actor.Actor

import scala.concurrent.Future
import scala.util._
import akka.actor.ActorRef
import akka.event.Logging
import akka.actor.ActorSystem
import net.nikore.etcd._

class HipacheClient(config: Hipache.ServerConfig)(implicit system: ActorSystem) {
  import scala.language.implicitConversions
  import play.api.libs.json._
  import play.api.libs.functional.syntax._
  import EtcdJsonProtocol._
  import system.dispatcher

  def all: Future[Map[Hipache.Frontend,Hipache.Backend]] =
    client.listDir(config.prefix) map { etcdListResponse =>
      etcdListResponse.node.nodes.getOrElse(Nil).flatMap {
        case NodeListElement(key, _, Some(value), _) =>
          (key -> Json.parse(value).as[Seq[String]]) match {
            case Entry(frontend, backend) => Some(frontend -> backend)
            case _ => None
          }
        case _ =>
          None
      }.toMap
    }

  def get(frontend: Hipache.Frontend): Future[Option[Hipache.Backend]] =
    client.getKey(keyFor(frontend))
      .map { etcdResponse =>
        etcdResponse.node.value.flatMap { value => 
          (keyFor(frontend) -> Json.parse(value).as[Seq[String]]) match {
            case Entry(`frontend`, backend) => Some(backend)
            case _ => None
          }
        }
      }.recover {
        case e: EtcdExceptions.KeyNotFoundException => None
      }

  def put(
      frontend: Hipache.Frontend,
      backend: Hipache.Backend): Future[Unit] = 
   client.setKey(
       keyFor(frontend), 
       Json.stringify(Json.arr(frontend.name, backend.toString))) map (_ => ())

  def delete(frontend: Hipache.Frontend): Future[Unit] =
    client.deleteKey(keyFor(frontend)) map (_ => ())

  lazy val keyPrefix = s"${config.prefix.stripPrefix("/")}/frontend:"
  def keyFor(frontend: Hipache.Frontend) = keyPrefix + frontend.domain

  protected lazy val client = config.client

  object Entry {
    lazy val reBackend = """(\w+)://(.+):(\d+)""".r

    type HipachePair = (Hipache.Frontend, Hipache.Backend)

    def unapply(kvPair: (String, Seq[String])): Option[HipachePair] =
      kvPair match {
        case (k: String, Seq(name: String, backendStr: String))
            if k.startsWith("/"+keyPrefix) =>
          val frontend = Hipache.Frontend(
              name,
              k.stripPrefix("/"+keyPrefix))
          backendStr match {
            case reBackend(scheme, host, portStr) =>
              val backend =
                Hipache.Backend(host, Integer.parseInt(portStr), scheme)
              Some((frontend, backend))
            case _ => None
          }
        case _ =>
          None
      }
  }

  implicit protected def toFUnit[A](f: Future[A]): Future[Unit] = f.map(_ => ())

}

{'fraction_non_alphanumeric': 0.12883435582822086, 'fraction_numerical': 0.0, 'mean_word_length': 2.6087239583333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6437754', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1704, 'n_words': 333}