mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 03:58:45 +00:00
WriteStandardKeyValueRepo#unsetWithValues
This commit is contained in:
parent
3ae9b3e576
commit
091cb38339
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
* `Klock`: `2.0.0` -> `2.0.1`
|
* `Klock`: `2.0.0` -> `2.0.1`
|
||||||
|
* `Repo`
|
||||||
|
* Repo `WriteStandardKeyValueRepo` got new method `unsetWithValues`
|
||||||
|
|
||||||
## 0.4.9
|
## 0.4.9
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package dev.inmo.micro_utils.repos
|
package dev.inmo.micro_utils.repos
|
||||||
|
|
||||||
import dev.inmo.micro_utils.pagination.Pagination
|
import dev.inmo.micro_utils.pagination.*
|
||||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
interface ReadStandardKeyValueRepo<Key, Value> : Repo {
|
interface ReadStandardKeyValueRepo<Key, Value> : Repo {
|
||||||
@ -20,6 +19,7 @@ interface WriteStandardKeyValueRepo<Key, Value> : Repo {
|
|||||||
|
|
||||||
suspend fun set(toSet: Map<Key, Value>)
|
suspend fun set(toSet: Map<Key, Value>)
|
||||||
suspend fun unset(toUnset: List<Key>)
|
suspend fun unset(toUnset: List<Key>)
|
||||||
|
suspend fun unsetWithValues(toUnset: List<Value>)
|
||||||
}
|
}
|
||||||
typealias WriteKeyValueRepo<Key,Value> = WriteStandardKeyValueRepo<Key, Value>
|
typealias WriteKeyValueRepo<Key,Value> = WriteStandardKeyValueRepo<Key, Value>
|
||||||
|
|
||||||
@ -35,5 +35,13 @@ suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.unset(
|
|||||||
vararg k: Key
|
vararg k: Key
|
||||||
) = unset(k.toList())
|
) = 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>
|
typealias KeyValueRepo<Key,Value> = StandardKeyValueRepo<Key, Value>
|
||||||
|
@ -105,6 +105,10 @@ open class MapperWriteStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
|
|||||||
k.toOutKey()
|
k.toOutKey()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun unsetWithValues(toUnset: List<FromValue>) = to.unsetWithValues(
|
||||||
|
toUnset.map { it.toOutValue() }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
@ -35,10 +35,10 @@ class KeyValueStore<T : Any> internal constructor (
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val onNewValueChannel = MutableSharedFlow<Pair<String, T>>()
|
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 onNewValue: Flow<Pair<String, T>> = onNewValueChannel.asSharedFlow()
|
||||||
override val onValueRemoved: Flow<String> = onValueRemovedChannel.asSharedFlow()
|
override val onValueRemoved: Flow<String> = _onValueRemovedFlow.asSharedFlow()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
cachedData ?.let {
|
cachedData ?.let {
|
||||||
@ -136,6 +136,18 @@ class KeyValueStore<T : Any> internal constructor (
|
|||||||
sharedPreferences.edit {
|
sharedPreferences.edit {
|
||||||
toUnset.forEach { remove(it) }
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,4 +60,16 @@ open class ExposedKeyValueRepo<Key, Value>(
|
|||||||
_onValueRemoved.emit(it)
|
_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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,15 @@ class WriteMapKeyValueRepo<Key, Value>(
|
|||||||
map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) }
|
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>(
|
class MapKeyValueRepo<Key, Value>(
|
||||||
|
@ -17,6 +17,7 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
|||||||
) : WriteStandardKeyValueRepo<K, V> {
|
) : WriteStandardKeyValueRepo<K, V> {
|
||||||
private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
private val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
||||||
private val keysListSerializer = ListSerializer(keySerializer)
|
private val keysListSerializer = ListSerializer(keySerializer)
|
||||||
|
private val valuesListSerializer = ListSerializer(valueSerializer)
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
baseUrl: String,
|
baseUrl: String,
|
||||||
@ -55,4 +56,13 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
|||||||
BodyPair(keysListSerializer, toUnset),
|
BodyPair(keysListSerializer, toUnset),
|
||||||
Unit.serializer()
|
Unit.serializer()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
override suspend fun unsetWithValues(toUnset: List<V>) = unifiedRequester.unipost(
|
||||||
|
buildStandardUrl(
|
||||||
|
baseUrl,
|
||||||
|
unsetWithValuesRoute,
|
||||||
|
),
|
||||||
|
BodyPair(valuesListSerializer, toUnset),
|
||||||
|
Unit.serializer()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
@ -10,3 +10,4 @@ const val onNewValueRoute = "onNewValue"
|
|||||||
const val onValueRemovedRoute = "onValueRemoved"
|
const val onValueRemovedRoute = "onValueRemoved"
|
||||||
const val setRoute = "set"
|
const val setRoute = "set"
|
||||||
const val unsetRoute = "unset"
|
const val unsetRoute = "unset"
|
||||||
|
const val unsetWithValuesRoute = "unsetWithValues"
|
@ -19,6 +19,7 @@ fun <K, V> Route.configureWriteStandardKeyValueRepoRoutes (
|
|||||||
) {
|
) {
|
||||||
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
val keyValueMapSerializer = MapSerializer(keySerializer, valueSerializer)
|
||||||
val keysListSerializer = ListSerializer(keySerializer)
|
val keysListSerializer = ListSerializer(keySerializer)
|
||||||
|
val valuesListSerializer = ListSerializer(valueSerializer)
|
||||||
unifiedRouter.apply {
|
unifiedRouter.apply {
|
||||||
includeWebsocketHandling(
|
includeWebsocketHandling(
|
||||||
onNewValueRoute,
|
onNewValueRoute,
|
||||||
@ -50,6 +51,14 @@ fun <K, V> Route.configureWriteStandardKeyValueRepoRoutes (
|
|||||||
unianswer(Unit.serializer(), originalRepo.unset(toUnset))
|
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 (
|
fun <K, V> Route.configureWriteStandartKeyValueRepoRoutes (
|
||||||
|
Loading…
Reference in New Issue
Block a user