All Downloads are FREE. Search and download functionalities are using the official Maven repository.

flag.FlagConfigStorage.kt Maven / Gradle / Ivy

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