aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/storage/RequestStorage.scala
blob: d9651cad81eaa5b303fea06add6b7565cf44e858 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package xyz.driver.pdsuidomain.storage

import xyz.driver.pdsuicommon.domain.{LongId, UuidId}
import xyz.driver.pdsuicommon.logging._
import xyz.driver.pdsuidomain.entities.{Arm, Patient}

import scala.collection.concurrent.TrieMap

object RequestStorage {
  type Key = (UuidId[Patient], String)
  type Value = Set[LongId[Arm]]
}

class RequestStorage extends PhiLogging {
  import RequestStorage._

  private val storage = new TrieMap[Key, Value]()

  def put(patientId: UuidId[Patient],
          disease: String,
          ineligibleArms: Set[LongId[Arm]]): Unit = {
    logger.debug(phi"put($patientId, ${Unsafe(disease)}, $ineligibleArms")
    val key = (patientId, disease.toLowerCase)
    get(patientId, disease.toLowerCase) match {
      case Some(oldValue) =>
        logger.trace(phi"Requested ineligible arms=$oldValue, replace it")
        storage.replace(key, oldValue, ineligibleArms)
      case None =>
        logger.trace(phi"Put request into storage")
        storage.put(key, ineligibleArms)
    }
  }

  def get(patientId: UuidId[Patient], disease: String): Option[Value] = {
    logger.debug(phi"get($patientId,${Unsafe(disease)}")
    val key = (patientId, disease.toLowerCase)
    storage.get(key)
  }

  def contains(patientId: UuidId[Patient],
               disease: String,
               value: Set[LongId[Arm]]): Boolean = {
    logger.debug(phi"contains(key=($patientId,${Unsafe(disease)}),value=$value")
    get(patientId, disease.toLowerCase).contains(value)
  }

  def remove(patientId: UuidId[Patient], disease: String): Unit = {
    logger.debug(phi"remove($patientId,${Unsafe(disease)}")
    val key = (patientId, disease.toLowerCase)
    storage.remove(key)
  }
}