WriteStandardKeyValueRepo#unsetWithValues

This commit is contained in:
InsanusMokrassar 2020-12-05 17:52:37 +06:00
parent 3ae9b3e576
commit 091cb38339
9 changed files with 74 additions and 7 deletions

View File

@ -4,6 +4,8 @@
* `Versions`:
* `Klock`: `2.0.0` -> `2.0.1`
* `Repo`
* Repo `WriteStandardKeyValueRepo` got new method `unsetWithValues`
## 0.4.9

View File

@ -1,7 +1,6 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.*
import kotlinx.coroutines.flow.Flow
interface ReadStandardKeyValueRepo<Key, Value> : Repo {
@ -20,6 +19,7 @@ interface WriteStandardKeyValueRepo<Key, Value> : Repo {
suspend fun set(toSet: Map<Key, Value>)
suspend fun unset(toUnset: List<Key>)
suspend fun unsetWithValues(toUnset: List<Value>)
}
typealias WriteKeyValueRepo<Key,Value> = WriteStandardKeyValueRepo<Key, Value>
@ -35,5 +35,13 @@ suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.unset(
vararg k: Key
) = unset(k.toList())
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value>
interface StandardKeyValueRepo<Key, Value> : ReadStandardKeyValueRepo<Key, Value>, WriteStandardKeyValueRepo<Key, Value> {
override suspend fun unsetWithValues(toUnset: List<Value>) = toUnset.forEach { v ->
doWithPagination {
keys(v, it).also {
unset(it.results)
}
}
}
}
typealias KeyValueRepo<Key,Value> = StandardKeyValueRepo<Key, Value>

View File

@ -105,6 +105,10 @@ open class MapperWriteStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
k.toOutKey()
}
)
override suspend fun unsetWithValues(toUnset: List<FromValue>) = to.unsetWithValues(
toUnset.map { it.toOutValue() }
)
}
@Suppress("NOTHING_TO_INLINE")

View File

@ -35,10 +35,10 @@ class KeyValueStore<T : Any> internal constructor (
}
private val onNewValueChannel = MutableSharedFlow<Pair<String, T>>()
private val onValueRemovedChannel = MutableSharedFlow<String>()
private val _onValueRemovedFlow = MutableSharedFlow<String>()
override val onNewValue: Flow<Pair<String, T>> = onNewValueChannel.asSharedFlow()
override val onValueRemoved: Flow<String> = onValueRemovedChannel.asSharedFlow()
override val onValueRemoved: Flow<String> = _onValueRemovedFlow.asSharedFlow()
init {
cachedData ?.let {
@ -136,6 +136,18 @@ class KeyValueStore<T : Any> internal constructor (
sharedPreferences.edit {
toUnset.forEach { remove(it) }
}
toUnset.forEach { onValueRemovedChannel.emit(it) }
toUnset.forEach { _onValueRemovedFlow.emit(it) }
}
override suspend fun unsetWithValues(toUnset: List<T>) {
val keysToRemove = sharedPreferences.all.mapNotNull { if (it.value in toUnset) it.key else null }
sharedPreferences.edit {
keysToRemove.map {
remove(it)
}
}
keysToRemove.forEach {
_onValueRemovedFlow.emit(it)
}
}
}

View File

@ -60,4 +60,16 @@ open class ExposedKeyValueRepo<Key, Value>(
_onValueRemoved.emit(it)
}
}
override suspend fun unsetWithValues(toUnset: List<Value>) {
transaction(database) {
toUnset.flatMap {
val keys = select { valueColumn.eq(it) }.mapNotNull { it[keyColumn] }
deleteWhere { keyColumn.inList(keys) }
keys
}
}.distinct().forEach {
_onValueRemoved.emit(it)
}
}
}

View File

@ -76,6 +76,15 @@ class WriteMapKeyValueRepo<Key, Value>(
map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) }
}
}
override suspend fun unsetWithValues(toUnset: List<Value>) {
map.forEach {
if (it.value in toUnset) {
map.remove(it.key)
_onValueRemoved.emit(it.key)
}
}
}
}
class MapKeyValueRepo<Key, Value>(

View File

@ -17,6 +17,7 @@ class KtorWriteStandardKeyValueRepo<K, V> (
) : WriteStandardKeyValueRepo<K, V> {
private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
private val keysListSerializer = ListSerializer(keySerializer)
private val valuesListSerializer = ListSerializer(valueSerializer)
constructor(
baseUrl: String,
@ -55,4 +56,13 @@ class KtorWriteStandardKeyValueRepo<K, V> (
BodyPair(keysListSerializer, toUnset),
Unit.serializer()
)
override suspend fun unsetWithValues(toUnset: List<V>) = unifiedRequester.unipost(
buildStandardUrl(
baseUrl,
unsetWithValuesRoute,
),
BodyPair(valuesListSerializer, toUnset),
Unit.serializer()
)
}

View File

@ -10,3 +10,4 @@ const val onNewValueRoute = "onNewValue"
const val onValueRemovedRoute = "onValueRemoved"
const val setRoute = "set"
const val unsetRoute = "unset"
const val unsetWithValuesRoute = "unsetWithValues"

View File

@ -19,6 +19,7 @@ fun <K, V> Route.configureWriteStandardKeyValueRepoRoutes (
) {
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
val keysListSerializer = ListSerializer(keySerializer)
val valuesListSerializer = ListSerializer(valueSerializer)
unifiedRouter.apply {
includeWebsocketHandling(
onNewValueRoute,
@ -50,6 +51,14 @@ fun <K, V> Route.configureWriteStandardKeyValueRepoRoutes (
unianswer(Unit.serializer(), originalRepo.unset(toUnset))
}
}
post(unsetWithValuesRoute) {
unifiedRouter.apply {
val toUnset = uniload(valuesListSerializer)
unianswer(Unit.serializer(), originalRepo.unsetWithValues(toUnset))
}
}
}
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (