flag.FlagConfigStorage.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of experiment-jvm-server Show documentation
Show all versions of experiment-jvm-server Show documentation
Amplitude Experiment server-side SDK for JVM (Java, Kotlin)
The newest version!
package com.amplitude.experiment.flag
import com.amplitude.experiment.evaluation.EvaluationFlag
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write
internal interface FlagConfigStorage {
fun getFlagConfigs(): Map
fun putFlagConfig(flagConfig: EvaluationFlag)
fun removeIf(condition: (EvaluationFlag) -> Boolean)
fun update(put: List, remove: List)
}
internal class InMemoryFlagConfigStorage : FlagConfigStorage {
private val flagConfigs = mutableMapOf()
private val flagConfigsLock = ReentrantReadWriteLock()
override fun getFlagConfigs(): Map {
return flagConfigsLock.read { flagConfigs.toMap() }
}
override fun putFlagConfig(flagConfig: EvaluationFlag) {
flagConfigsLock.write {
flagConfigs.put(flagConfig.key, flagConfig)
}
}
override fun removeIf(condition: (EvaluationFlag) -> Boolean) {
return flagConfigsLock.write {
flagConfigs.entries.removeIf { condition(it.value) }
}
}
override fun update(put: List, remove: List) {
return flagConfigsLock.write {
flagConfigs.putAll(put.associateBy { it.key })
remove.forEach { flagConfigs.remove(it.key) }
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy