<commit_before>package net.kemuridama.kafcon.service

import org.apache.zookeeper.{ZooKeeper, Watcher, WatchedEvent}
import org.apache.zookeeper.data.Stat

import net.kemuridama.kafcon.model.ConnectionState
import net.kemuridama.kafcon.util.{UsesApplicationConfig, MixinApplicationConfig}

trait ZooKeeperService
  extends UsesApplicationConfig {

  import collection.JavaConversions._

  private val sessionTimeout = 10 * 1000
  private val charset = "UTF-8"

  private val watcher = new Watcher {
    def process(event: WatchedEvent) = { /* Ignore all watched event */ }
  }

  private lazy val zookeeperServers = applicationConfig.cluster.getStringList("zookeeperServers").toList
  private lazy val zookeeper = new ZooKeeper(zookeeperServers.mkString(","), sessionTimeout, watcher)

  private var connectionState: ConnectionState = ConnectionState.Disconnected

  def getAll: List[String] = zookeeperServers
  def getConnectionState: ConnectionState = connectionState

  def getChildren(path: String): List[String] = try {
    zookeeper.getChildren(path, false).toList
  } catch {
    case _: Exception => {
      connectionState = ConnectionState.Disconnected
      List.empty[String]
    }
  }

  def getData(path: String): String = try {
    new String(zookeeper.getData(path, false, new Stat), charset)
  } catch {
    case _: Exception => {
      connectionState = ConnectionState.Disconnected
      ""
    }
  }

}

private[service] object ZooKeeperService
  extends ZooKeeperService
  with MixinApplicationConfig

trait UsesZooKeeperService {
  val zookeeperService: ZooKeeperService
}
<commit_msg>Update getting zookeeper connection routine<commit_after>package net.kemuridama.kafcon.service

import org.apache.zookeeper.{ZooKeeper, Watcher, WatchedEvent}
import org.apache.zookeeper.data.Stat

import net.kemuridama.kafcon.model.ConnectionState
import net.kemuridama.kafcon.util.{UsesApplicationConfig, MixinApplicationConfig}

trait ZooKeeperService
  extends UsesApplicationConfig {

  import collection.JavaConversions._

  private val sessionTimeout = 10 * 1000
  private val charset = "UTF-8"

  private val watcher = new Watcher {
    def process(event: WatchedEvent) = { /* Ignore all watched event */ }
  }

  private lazy val zookeeperServers = applicationConfig.cluster.getStringList("zookeeperServers").toList
  private var zookeeper: Option[ZooKeeper] = None

  private var connectionState: ConnectionState = ConnectionState.Disconnected

  private def connect: Option[ZooKeeper] = {
    connectionState = ConnectionState.Connected
    Some(new ZooKeeper(zookeeperServers.mkString(","), sessionTimeout, watcher))
  }

  private def disconnect: Unit = {
    connectionState = ConnectionState.Disconnected
    zookeeper.map(_.close)
  }

  private def getConnection: Option[ZooKeeper] = zookeeper match {
    case Some(zk) => Some(zk)
    case _ => {
      zookeeper = connect
      zookeeper
    }
  }

  private def withConnection[T](func: ZooKeeper => T): Option[T] = getConnection.flatMap { zk =>
    try {
      Some(func(zk))
    } catch {
      case _: Exception => {
        disconnect
        None
      }
    }
  }

  def getAll: List[String] = zookeeperServers
  def getConnectionState: ConnectionState = connectionState

  def getChildren(path: String): List[String] = withConnection { zk =>
    zk.getChildren(path, false).toList
  } getOrElse(List.empty[String])

  def getData(path: String): String = withConnection { zk =>
    new String(zk.getData(path, false, new Stat), charset)
  } getOrElse("")

}

private[service] object ZooKeeperService
  extends ZooKeeperService
  with MixinApplicationConfig

trait UsesZooKeeperService {
  val zookeeperService: ZooKeeperService
}

{'fraction_non_alphanumeric': 0.08914624293024509, 'fraction_numerical': 0.0037705359547535685, 'mean_word_length': 4.320916905444126, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '2893029', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1123, 'n_words': 253}