<commit_before>package models.persistence

import java.util.concurrent.TimeUnit

import scala.concurrent.Future
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.{Implicits => ExecutionContext}
import scala.concurrent.duration.FiniteDuration

import akka.actor.Actor
import akka.actor.Cancellable
import models.document.Document
import models.persistence.DocumentAccessorActor.CleanUp
import models.persistence.DocumentAccessorManagerActor.GetAccessor
import models.persistence.DocumentAccessorManagerActor.GetAllIds
import models.persistence.DocumentAccessorManagerActor.CacheDuration


/** Manages all DocumentAccessors of type [[T]].
 *
 * @tparam T type of the document Describe param
 */
class DocumentAccessorManagerActor[T <: Document] extends Actor { // scalastyle:ignore


  // TODO inject
  private val cacheDuration: CacheDuration = {
    val keepInCacheTime: Long = Duration(1, TimeUnit.HOURS).toMillis
    val keepActorAliveTime: Long = Duration(1, TimeUnit.DAYS).toMillis
    val keepActorAliveAfterDeleteTime: Long = Duration(1, TimeUnit.MINUTES).toMillis
    val cleanUpInterval: FiniteDuration = Duration(1, TimeUnit.MINUTES)

    CacheDuration(cleanUpInterval, keepInCacheTime, keepActorAliveTime, keepActorAliveAfterDeleteTime)
  }

  private val persistence: Persistence[T] = new CachePersistence[T] // TODO inject
  private val documentAccessorFactory: DocumentAccessorFactory = DocumentAccessorFactoryDefaultImpl // TODO inject


  private val cleanUpJob: Cancellable = {
    context.system.scheduler.schedule(cacheDuration.cleanUpInterval, cacheDuration.cleanUpInterval, self, CleanUp)(ExecutionContext.global)
  }

  /** Process received message.
   *
   * @return Receive
   */
  override def receive: Receive = {

    case GetAccessor(id) =>
      sender ! context.child(id).getOrElse {
        context.actorOf(documentAccessorFactory.props(persistence, cacheDuration), id)
      }

    case GetAllIds =>
      val originalSender = sender
      Future {
        originalSender ! persistence.readAllIds
      }(ExecutionContext.global)

    case CleanUp =>
      context.children.foreach(_.forward(CleanUp))

  }

  /** Process actor stopping. */
  override def postStop(): Unit = {
    cleanUpJob.cancel()
  }

}

/** Companion object of DocumentAccessorManager. */
object DocumentAccessorManagerActor {<commit_msg>changed execution context in DocumentAccessorManagerActor.scala to actor execution context<commit_after>package models.persistence

import java.util.concurrent.TimeUnit

import scala.concurrent.Future
import scala.concurrent.duration.Duration
import scala.concurrent.duration.FiniteDuration

import akka.actor.Actor
import akka.actor.Cancellable
import models.document.Document
import models.persistence.DocumentAccessorActor.CleanUp
import models.persistence.DocumentAccessorManagerActor.GetAccessor
import models.persistence.DocumentAccessorManagerActor.GetAllIds
import models.persistence.DocumentAccessorManagerActor.CacheDuration


/** Manages all DocumentAccessors of type [[T]].
 *
 * @tparam T type of the document Describe param
 */
class DocumentAccessorManagerActor[T <: Document] extends Actor { // scalastyle:ignore


  // TODO inject
  private val cacheDuration: CacheDuration = {
    val keepInCacheTime: Long = Duration(1, TimeUnit.HOURS).toMillis
    val keepActorAliveTime: Long = Duration(1, TimeUnit.DAYS).toMillis
    val keepActorAliveAfterDeleteTime: Long = Duration(1, TimeUnit.MINUTES).toMillis
    val cleanUpInterval: FiniteDuration = Duration(1, TimeUnit.MINUTES)

    CacheDuration(cleanUpInterval, keepInCacheTime, keepActorAliveTime, keepActorAliveAfterDeleteTime)
  }

  private val persistence: Persistence[T] = new CachePersistence[T] // TODO inject
  private val documentAccessorFactory: DocumentAccessorFactory = DocumentAccessorFactoryDefaultImpl // TODO inject

  private val cleanUpJob: Cancellable = {
    context.system.scheduler.schedule(cacheDuration.cleanUpInterval, cacheDuration.cleanUpInterval, self, CleanUp)(context.dispatcher)
  }

  /** Process received message.
   *
   * @return Receive
   */
  override def receive: Receive = {

    case GetAccessor(id) =>
      sender ! context.child(id).getOrElse {
        context.actorOf(documentAccessorFactory.props(persistence, cacheDuration), id)
      }

    case GetAllIds =>
      val originalSender = sender
      Future {
        originalSender ! persistence.readAllIds
      }(context.dispatcher)

    case CleanUp =>
      context.children.foreach(_.forward(CleanUp))

  }

  /** Process actor stopping. */
  override def postStop(): Unit = {
    cleanUpJob.cancel()
  }

}

/** Companion object of DocumentAccessorManager. */
object DocumentAccessorManagerActor {
{'fraction_non_alphanumeric': 0.08394853406454335, 'fraction_numerical': 0.0016874077198903185, 'mean_word_length': 4.713253012048193, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '2564666', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1258, 'n_words': 289}