<commit_before>package de.htwg.zeta.persistence.transient

import java.util.UUID
import java.util.concurrent.TimeUnit

import scala.collection.mutable
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.Duration

import akka.actor.ActorSystem
import de.htwg.zeta.persistence.general.TokenCache
import grizzled.slf4j.Logging

/**
 * Transient Implementation of TokenCache.
 */
class TransientTokenCache extends TokenCache with Logging {

  private case class Token(userId: UUID, lastUse: Long)

  private val tokens: mutable.HashMap[UUID, Token] = mutable.HashMap.empty

  private val cleaningInterval = Duration(1, TimeUnit.MINUTES)

  private val lifeTime: Long = Duration(1, TimeUnit.HOURS).toMillis

  /** Schedule the CleanUp job. */
  ActorSystem("TransientTokenCache").scheduler.schedule(cleaningInterval, cleaningInterval) {
    info("Cleaning expired tokens")
    val expired = System.currentTimeMillis - lifeTime // scalastyle:ignore
    tokens --= tokens.filter(n => n._2.lastUse > expired).keys
  }

  /**
   * Finds a token by its ID.
   *
   * @param id The unique token ID.
   * @return The found token or None if no token for the given ID could be found.
   */
  override def read(id: UUID): Future[UUID] = {
    Future {
      tokens(id).userId
    }
  }

  /**<commit_msg>replace HashMap in TransientTokenCache.scala with TrieMap<commit_after>package de.htwg.zeta.persistence.transient

import java.util.UUID
import java.util.concurrent.TimeUnit

import scala.collection.concurrent.TrieMap
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration.Duration

import akka.actor.ActorSystem
import de.htwg.zeta.persistence.general.TokenCache
import grizzled.slf4j.Logging

/**
 * Transient Implementation of TokenCache.
 */
class TransientTokenCache extends TokenCache with Logging {

  private case class Token(userId: UUID, lastUse: Long)

  private val tokens: TrieMap[UUID, Token] = TrieMap.empty

  private val cleaningInterval = Duration(10, TimeUnit.MINUTES)

  private val lifeTime: Long = Duration(1, TimeUnit.HOURS).toMillis

  /** Schedule the CleanUp job. */
  ActorSystem("TransientTokenCache").scheduler.schedule(cleaningInterval, cleaningInterval) {
    info("Cleaning expired tokens")
    val expired = System.currentTimeMillis - lifeTime // scalastyle:ignore
    tokens --= tokens.filter(n => n._2.lastUse > expired).keys
  }

  /**
   * Finds a token by its ID.
   *
   * @param id The unique token ID.
   * @return The found token or None if no token for the given ID could be found.
   */
  override def read(id: UUID): Future[UUID] = {
    Future {
      tokens(id).userId
    }
  }

  /**
{'fraction_non_alphanumeric': 0.09789702683103699, 'fraction_numerical': 0.0032632342277012327, 'mean_word_length': 4.0346715328467155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6748379', 'n_tokens_mistral': 843, 'n_tokens_neox': 829, 'n_words': 219}