jvmMain.MemoryKeyValueStore.kt Maven / Gradle / Ivy
The newest version!
package io.fluidsonic.raptor.keyvaluestore.memory
import io.fluidsonic.raptor.keyvaluestore.*
import java.util.concurrent.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
internal class MemoryKeyValueStore : RaptorKeyValueStore {
private val valuesByKey = ConcurrentHashMap()
override suspend fun clear() {
valuesByKey.clear()
}
override fun entries(): Flow> =
valuesByKey.entries
.asFlow()
.map { it.toPair() }
override fun keys(): Flow =
valuesByKey.keys.asFlow()
override fun values(): Flow =
valuesByKey.values.asFlow()
override suspend fun set(key: Key, value: Value) {
valuesByKey[key] = value
}
override suspend fun setIfAbsent(key: Key, value: Value): Boolean =
valuesByKey.putIfAbsent(key, value) == null
override suspend fun remove(key: Key): Boolean =
valuesByKey.remove(key) != null
override suspend fun get(key: Key): Value? =
valuesByKey[key]
}