mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-23 00:57:15 +00:00
add meppers for kv<->kvs repos
This commit is contained in:
parent
4f68459582
commit
a5f718e257
@ -0,0 +1,12 @@
|
||||
package dev.inmo.micro_utils.repos.transforms.kv
|
||||
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.KeyValuesRepo
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
||||
import kotlin.js.JsName
|
||||
import kotlin.jvm.JvmName
|
||||
|
||||
fun <K, V> ReadKeyValuesRepo<K, V>.asReadKeyValueRepo() = ReadKeyValueFromKeyValuesRepo(this)
|
||||
|
||||
fun <K, V> KeyValuesRepo<K, V>.asKeyValueRepo() = KeyValueFromKeyValuesRepo(this)
|
@ -0,0 +1,41 @@
|
||||
package dev.inmo.micro_utils.repos.transforms.kv
|
||||
|
||||
import dev.inmo.micro_utils.pagination.FirstPagePagination
|
||||
import dev.inmo.micro_utils.pagination.Pagination
|
||||
import dev.inmo.micro_utils.pagination.PaginationResult
|
||||
import dev.inmo.micro_utils.pagination.changeResults
|
||||
import dev.inmo.micro_utils.pagination.changeResultsUnchecked
|
||||
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
|
||||
import dev.inmo.micro_utils.pagination.utils.paginate
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.KeyValuesRepo
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
|
||||
import dev.inmo.micro_utils.repos.transforms.kvs.ReadKeyValuesFromKeyValueRepo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.flow.merge
|
||||
|
||||
open class KeyValueFromKeyValuesRepo<Key, Value>(
|
||||
private val original: KeyValuesRepo<Key, Value>
|
||||
) : KeyValueRepo<Key, List<Value>>, ReadKeyValueFromKeyValuesRepo<Key, Value>(original) {
|
||||
override val onNewValue: Flow<Pair<Key, List<Value>>> = merge(
|
||||
original.onNewValue,
|
||||
original.onValueRemoved
|
||||
).mapNotNull {
|
||||
it.first to (get(it.first) ?: return@mapNotNull null)
|
||||
}
|
||||
override val onValueRemoved: Flow<Key> = original.onDataCleared
|
||||
|
||||
override suspend fun unset(toUnset: List<Key>) {
|
||||
toUnset.forEach {
|
||||
original.clear(it)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun set(toSet: Map<Key, List<Value>>) {
|
||||
original.set(toSet)
|
||||
}
|
||||
}
|
@ -65,5 +65,3 @@ open class ReadKeyValueFromKeyValuesRepo<Key, Value>(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <K, V> ReadKeyValuesRepo<K, V>.asReadKeyValueRepo() = ReadKeyValueFromKeyValuesRepo(this)
|
||||
|
@ -0,0 +1,21 @@
|
||||
package dev.inmo.micro_utils.repos.transforms.kvs
|
||||
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.ReadKeyValueRepo
|
||||
import kotlin.js.JsName
|
||||
import kotlin.jvm.JvmName
|
||||
|
||||
|
||||
fun <K, V, VI : Iterable<V>> ReadKeyValueRepo<K, VI>.asReadKeyValuesRepo() = ReadKeyValuesFromKeyValueRepo(this)
|
||||
|
||||
fun <K, V, VI : Iterable<V>> KeyValueRepo<K, VI>.asKeyValuesRepo(
|
||||
listToValuesIterable: suspend (List<V>) -> VI
|
||||
): KeyValuesFromKeyValueRepo<K, V, VI> = KeyValuesFromKeyValueRepo(this, listToValuesIterable)
|
||||
|
||||
@JvmName("asListKeyValuesRepo")
|
||||
@JsName("asListKeyValuesRepo")
|
||||
fun <K, V> KeyValueRepo<K, List<V>>.asKeyValuesRepo(): KeyValuesFromKeyValueRepo<K, V, List<V>> = asKeyValuesRepo { it }
|
||||
|
||||
@JvmName("asSetKeyValuesRepo")
|
||||
@JsName("asSetKeyValuesRepo")
|
||||
fun <K, V> KeyValueRepo<K, Set<V>>.asKeyValuesRepo(): KeyValuesFromKeyValueRepo<K, V, Set<V>> = asKeyValuesRepo { it.toSet() }
|
@ -0,0 +1,77 @@
|
||||
package dev.inmo.micro_utils.repos.transforms.kvs
|
||||
|
||||
import dev.inmo.micro_utils.pagination.FirstPagePagination
|
||||
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||
import dev.inmo.micro_utils.repos.KeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.KeyValuesRepo
|
||||
import dev.inmo.micro_utils.repos.unset
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlin.js.JsName
|
||||
import kotlin.jvm.JvmName
|
||||
|
||||
open class KeyValuesFromKeyValueRepo<Key, Value, ValuesIterable : Iterable<Value>>(
|
||||
private val original: KeyValueRepo<Key, ValuesIterable>,
|
||||
private val listToValuesIterable: suspend (List<Value>) -> ValuesIterable
|
||||
) : KeyValuesRepo<Key, Value>, ReadKeyValuesFromKeyValueRepo<Key, Value, ValuesIterable>(original) {
|
||||
private val _onNewValue = MutableSharedFlow<Pair<Key, Value>>()
|
||||
private val _onValueRemoved = MutableSharedFlow<Pair<Key, Value>>()
|
||||
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
|
||||
override val onValueRemoved: Flow<Pair<Key, Value>> = _onValueRemoved.asSharedFlow()
|
||||
override val onDataCleared: Flow<Key> = original.onValueRemoved
|
||||
|
||||
override suspend fun clearWithValue(v: Value) {
|
||||
val keys = mutableSetOf<Key>()
|
||||
|
||||
doForAllWithNextPaging(FirstPagePagination(count().toInt())) {
|
||||
original.keys(it).also {
|
||||
it.results.forEach {
|
||||
if (contains(it, v)) {
|
||||
keys.add(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
original.unset(keys.toList())
|
||||
}
|
||||
|
||||
override suspend fun clear(k: Key) {
|
||||
original.unset(k)
|
||||
}
|
||||
|
||||
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
|
||||
original.set(
|
||||
toRemove.mapNotNull { (k, removing) ->
|
||||
val exists = original.get(k) ?: return@mapNotNull null
|
||||
k to listToValuesIterable(exists - removing).also {
|
||||
if (it.firstOrNull() == null) {
|
||||
original.unset(k)
|
||||
return@mapNotNull null
|
||||
}
|
||||
}
|
||||
}.toMap()
|
||||
)
|
||||
toRemove.forEach { (k, v) ->
|
||||
v.forEach {
|
||||
_onValueRemoved.emit(k to it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||
original.set(
|
||||
toAdd.mapNotNull { (k, adding) ->
|
||||
val exists = original.get(k) ?: emptyList()
|
||||
k to listToValuesIterable(exists + adding)
|
||||
}.toMap()
|
||||
)
|
||||
toAdd.forEach { (k, v) ->
|
||||
v.forEach {
|
||||
_onNewValue.emit(k to it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -70,5 +70,3 @@ open class ReadKeyValuesFromKeyValueRepo<Key, Value, ValuesIterable : Iterable<V
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <K, V, VI : Iterable<V>> ReadKeyValueRepo<K, VI>.asReadKeyValuesRepo() = ReadKeyValuesFromKeyValueRepo(this)
|
||||
|
Loading…
Reference in New Issue
Block a user