repos rework

This commit is contained in:
InsanusMokrassar 2020-11-09 00:46:26 +06:00
parent 5b5bfa02db
commit abfda3627c
18 changed files with 254 additions and 233 deletions

View File

@ -2,7 +2,22 @@
## 0.3.0 ## 0.3.0
All deprecations has been removed
* `Repos`: * `Repos`:
* `Common`:
* `KeyValue` and `OneToMany` repos lost their deprecated methods
* `OneToMany` write repos got additional extensions for mutation of repo
* `KeyValue` write repos got additional extensions for mutation of repo
* New interface `MapperRepo` and new classes which are using this:
* `KeyValue`
* `MapperReadStandardKeyValueRepo`
* `MapperWriteStandardKeyValueRepo`
* `MapperStandardKeyValueRepo`
* `OneToMany`
* `MapperReadOneToManyKeyValueRepo`
* `MapperWriteOneToManyKeyValueRepo`
* `MapperOneToManyKeyValueRepo`
* `Exposed`: * `Exposed`:
* CRUD realizations replaced their channels to shared flows * CRUD realizations replaced their channels to shared flows

View File

@ -149,18 +149,3 @@ inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDif
inline fun <T> Iterable<T>.calculateStrictDiff( inline fun <T> Iterable<T>.calculateStrictDiff(
other: Iterable<T> other: Iterable<T>
) = calculateDiff(other, strictComparison = true) ) = calculateDiff(other, strictComparison = true)
/**
* Compare one-to-one
*/
@Deprecated("Will be removed or replaced with some new function. Use calculateDiff instead")
inline fun <T> Iterable<T>.syncWith(
other: Iterable<T>,
noinline removed: (List<T>) -> Unit = {},
noinline added: (List<T>) -> Unit = {}
) {
calculateDiff(other).also {
removed(it.removed.map { it.value })
added(it.added.map { it.value })
}
}

View File

@ -1,24 +0,0 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
@Suppress("FunctionName")
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
fun <T> BroadcastFlow(
internalChannelSize: Int = Channel.BUFFERED
): BroadcastFlow<T> {
val channel = BroadcastChannel<T>(internalChannelSize)
return BroadcastFlow(
channel,
channel.asFlow()
)
}
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
class BroadcastFlow<T> internal constructor(
private val channel: BroadcastChannel<T>,
private val flow: Flow<T>
): Flow<T> by flow, SendChannel<T> by channel

View File

@ -1,68 +0,0 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
const val defaultBroadcastStateFlowReplayCacheSize = 1
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
class BroadcastStateFlow<T> internal constructor(
parentFlow: Flow<T>,
initial: T,
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize,
replayScope: CoroutineScope
) : StateFlow<T>, Flow<T> by parentFlow {
private val deque = ArrayDeque<T>(1).also {
it.add(initial)
}
override val replayCache: List<T>
get() = deque.toList()
override val value: T
get() = deque.last()
init {
if (replayCacheSize < 1) {
error("Replay cache size can't be less than 1, but was $replayCacheSize")
}
parentFlow.onEach {
deque.addLast(it)
if (deque.size > replayCacheSize) {
deque.removeFirst()
}
}.launchIn(replayScope)
}
}
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
fun <T> BroadcastChannel<T>.asStateFlow(
value: T,
scope: CoroutineScope,
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
): StateFlow<T> = BroadcastStateFlow(asFlow(), value, replayCacheSize, scope)
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
fun <T> BroadcastChannel<T?>.asStateFlow(
scope: CoroutineScope,
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
): StateFlow<T?> = asStateFlow(null, scope, replayCacheSize)
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
fun <T> broadcastStateFlow(
initial: T, scope: CoroutineScope,
channelSize: Int = Channel.BUFFERED,
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
) = BroadcastChannel<T>(
channelSize
).let {
it to it.asStateFlow(initial, scope, replayCacheSize)
}
@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow")
fun <T> broadcastStateFlow(
scope: CoroutineScope,
channelSize: Int = Channel.BUFFERED,
replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize
) = broadcastStateFlow<T?>(null, scope, channelSize, replayCacheSize)

View File

@ -0,0 +1,9 @@
package dev.inmo.micro_utils.repos
interface MapperRepo<FromKey, FromValue, ToKey, ToValue> {
suspend fun FromKey.toOutKey() = this as ToKey
suspend fun FromValue.toOutValue() = this as ToValue
suspend fun ToKey.toInnerKey() = this as FromKey
suspend fun ToValue.toInnerValue() = this as FromValue
}

View File

@ -35,8 +35,6 @@ interface ReadOneToManyKeyValueRepo<Key, Value> : Repo {
} }
} }
} }
@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo"))
typealias OneToManyReadKeyValueRepo<Key, Value> = ReadOneToManyKeyValueRepo<Key, Value>
interface WriteOneToManyKeyValueRepo<Key, Value> : Repo { interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
val onNewValue: Flow<Pair<Key, Value>> val onNewValue: Flow<Pair<Key, Value>>
@ -44,42 +42,44 @@ interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
val onDataCleared: Flow<Key> val onDataCleared: Flow<Key>
suspend fun add(toAdd: Map<Key, List<Value>>) suspend fun add(toAdd: Map<Key, List<Value>>)
@Deprecated("Will be extracted as extension for other add method")
suspend fun add(k: Key, v: Value) = add(mapOf(k to listOf(v)))
suspend fun remove(toRemove: Map<Key, List<Value>>) suspend fun remove(toRemove: Map<Key, List<Value>>)
@Deprecated("Will be extracted as extension for other remove method")
suspend fun remove(k: Key, v: Value) = remove(mapOf(k to listOf(v)))
suspend fun clear(k: Key) suspend fun clear(k: Key)
} }
@Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo"))
typealias OneToManyWriteKeyValueRepo<Key, Value> = WriteOneToManyKeyValueRepo<Key, Value> suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
keysAndValues: List<Pair<Key, List<Value>>>
) = add(keysAndValues.toMap())
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
vararg keysAndValues: Pair<Key, List<Value>>
) = add(keysAndValues.toMap())
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.add(
k: Key, v: List<Value>
) = add(mapOf(k to v))
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.add(
k: Key, vararg v: Value
) = add(k, v.toList())
interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value> interface OneToManyKeyValueRepo<Key, Value> : ReadOneToManyKeyValueRepo<Key, Value>, WriteOneToManyKeyValueRepo<Key, Value>
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add( suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
k: Key,
vararg v: Value
) = add(mapOf(k to v.toList()))
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
keysAndValues: List<Pair<Key, List<Value>>>
) = add(keysAndValues.toMap())
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
vararg keysAndValues: Pair<Key, List<Value>>
) = add(keysAndValues.toMap())
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
k: Key,
vararg v: Value
) = remove(mapOf(k to v.toList()))
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove(
keysAndValues: List<Pair<Key, List<Value>>> keysAndValues: List<Pair<Key, List<Value>>>
) = remove(keysAndValues.toMap()) ) = remove(keysAndValues.toMap())
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.remove( suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
vararg keysAndValues: Pair<Key, List<Value>> vararg keysAndValues: Pair<Key, List<Value>>
) = remove(keysAndValues.toMap()) ) = remove(keysAndValues.toMap())
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
k: Key,
v: List<Value>
) = remove(mapOf(k to v))
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
k: Key,
vararg v: Value
) = remove(k, v.toList())

View File

@ -16,16 +16,20 @@ interface WriteStandardKeyValueRepo<Key, Value> : Repo {
val onNewValue: Flow<Pair<Key, Value>> val onNewValue: Flow<Pair<Key, Value>>
val onValueRemoved: Flow<Key> val onValueRemoved: Flow<Key>
@Deprecated("Realize set with map instead") suspend fun set(toSet: Map<Key, Value>)
suspend fun set(k: Key, v: Value) suspend fun unset(toUnset: List<Key>)
suspend fun set(toSet: Map<Key, Value>) = toSet.forEach { (k, v) ->
set(k, v)
}
@Deprecated("Realize unset with list instead")
suspend fun unset(k: Key)
suspend fun unset(toUnset: List<Key>) = toUnset.forEach {
unset(it)
}
} }
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.set(
vararg toSet: Pair<Key, Value>
) = set(toSet.toMap())
suspend inline fun <Key, Value> WriteStandardKeyValueRepo<Key, Value>.set(
k: Key, v: Value
) = set(k to v)
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>

View File

@ -0,0 +1,84 @@
package dev.inmo.micro_utils.repos.mappers
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
open class MapperReadStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: ReadStandardKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : ReadStandardKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override suspend fun get(k: FromKey): FromValue? = to.get(
k.toOutKey()
) ?.toInnerValue()
override suspend fun values(
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromValue> = to.values(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerValue() },
it.size
)
}
override suspend fun keys(
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromKey> = to.keys(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
)
}
override suspend fun contains(key: FromKey): Boolean = to.contains(
key.toOutKey()
)
override suspend fun count(): Long = to.count()
}
open class MapperWriteStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: WriteStandardKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : WriteStandardKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override val onNewValue: Flow<Pair<FromKey, FromValue>> = to.onNewValue.map { (k, v) ->
k.toInnerKey() to v.toInnerValue()
}
override val onValueRemoved: Flow<FromKey> = to.onValueRemoved.map { k ->
k.toInnerKey()
}
override suspend fun set(toSet: Map<FromKey, FromValue>) = to.set(
toSet.map { (k, v) ->
k.toOutKey() to v.toOutValue()
}.toMap()
)
override suspend fun unset(toUnset: List<FromKey>) = to.unset(
toUnset.map { k ->
k.toOutKey()
}
)
}
open class MapperStandardKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: StandardKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : StandardKeyValueRepo<FromKey, FromValue>,
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
ReadStandardKeyValueRepo<FromKey, FromValue> by MapperReadStandardKeyValueRepo(to, mapper),
WriteStandardKeyValueRepo<FromKey, FromValue> by MapperWriteStandardKeyValueRepo(to, mapper)

View File

@ -0,0 +1,87 @@
package dev.inmo.micro_utils.repos.mappers
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.repos.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
open class MapperReadOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: ReadOneToManyKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : ReadOneToManyKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override suspend fun get(
k: FromKey,
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromValue> = to.get(
k.toOutKey(),
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerValue() },
it.size
)
}
override suspend fun keys(
pagination: Pagination,
reversed: Boolean
): PaginationResult<FromKey> = to.keys(
pagination,
reversed
).let {
PaginationResult(
it.page,
it.pagesNumber,
it.results.map { it.toInnerKey() },
it.size
)
}
override suspend fun contains(k: FromKey): Boolean = to.contains(k.toOutKey())
override suspend fun contains(k: FromKey, v: FromValue): Boolean = to.contains(k.toOutKey(), v.toOutValue())
override suspend fun count(): Long = to.count()
override suspend fun count(k: FromKey): Long = to.count(k.toOutKey())
}
open class MapperWriteOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: WriteOneToManyKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : WriteOneToManyKeyValueRepo<FromKey, FromValue>, MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper {
override val onNewValue: Flow<Pair<FromKey, FromValue>> = to.onNewValue.map { (k, v) ->
k.toInnerKey() to v.toInnerValue()
}
override val onValueRemoved: Flow<Pair<FromKey, FromValue>> = to.onValueRemoved.map { (k, v) ->
k.toInnerKey() to v.toInnerValue()
}
override val onDataCleared: Flow<FromKey> = to.onDataCleared.map { k ->
k.toInnerKey()
}
override suspend fun add(toAdd: Map<FromKey, List<FromValue>>) = to.add(
toAdd.map { (k, v) ->
k.toOutKey() to v.map { it.toOutValue() }
}.toMap()
)
override suspend fun remove(toRemove: Map<FromKey, List<FromValue>>) = to.remove(
toRemove.map { (k, v) ->
k.toOutKey() to v.map { it.toOutValue() }
}.toMap()
)
override suspend fun clear(k: FromKey) = to.clear(k.toOutKey())
}
open class MapperOneToManyKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
private val to: OneToManyKeyValueRepo<ToKey, ToValue>,
mapper: MapperRepo<FromKey, FromValue, ToKey, ToValue>
) : OneToManyKeyValueRepo<FromKey, FromValue>,
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
ReadOneToManyKeyValueRepo<FromKey, FromValue> by MapperReadOneToManyKeyValueRepo(to, mapper),
WriteOneToManyKeyValueRepo<FromKey, FromValue> by MapperWriteOneToManyKeyValueRepo(to, mapper)

View File

@ -24,22 +24,6 @@ open class ExposedKeyValueRepo<Key, Value>(
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow() override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
override val onValueRemoved: Flow<Key> = _onValueRemoved.asSharedFlow() override val onValueRemoved: Flow<Key> = _onValueRemoved.asSharedFlow()
override suspend fun set(k: Key, v: Value) {
transaction(database) {
if (select { keyColumn.eq(k) }.limit(1).any()) {
update({ keyColumn.eq(k) }) {
it[valueColumn] = v
}
} else {
insert {
it[keyColumn] = k
it[valueColumn] = v
}
}
}
_onNewValue.emit(k to v)
}
override suspend fun set(toSet: Map<Key, Value>) { override suspend fun set(toSet: Map<Key, Value>) {
transaction(database) { transaction(database) {
toSet.mapNotNull { (k, v) -> toSet.mapNotNull { (k, v) ->
@ -62,13 +46,6 @@ open class ExposedKeyValueRepo<Key, Value>(
} }
} }
override suspend fun unset(k: Key) {
transaction(database) {
deleteWhere { keyColumn.eq(k) }
}
_onValueRemoved.emit(k)
}
override suspend fun unset(toUnset: List<Key>) { override suspend fun unset(toUnset: List<Key>) {
transaction(database) { transaction(database) {
toUnset.mapNotNull { toUnset.mapNotNull {
@ -83,6 +60,3 @@ open class ExposedKeyValueRepo<Key, Value>(
} }
} }
} }
@Deprecated("Renamed", ReplaceWith("ExposedKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo"))
typealias AbstractExposedKeyValueRepo<Key, Value> = ExposedKeyValueRepo<Key, Value>

View File

@ -39,6 +39,3 @@ open class ExposedReadKeyValueRepo<Key, Value>(
} }
}.createPaginationResult(pagination, count()) }.createPaginationResult(pagination, count())
} }
@Deprecated("Renamed", ReplaceWith("ExposedReadKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedReadKeyValueRepo"))
typealias AbstractExposedReadKeyValueRepo<Key, Value> = ExposedReadKeyValueRepo<Key, Value>

View File

@ -1,6 +1,5 @@
package dev.inmo.micro_utils.repos.exposed.onetomany package dev.inmo.micro_utils.repos.exposed.onetomany
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.ColumnAllocator import dev.inmo.micro_utils.repos.exposed.ColumnAllocator
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
@ -28,15 +27,6 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
override val onDataCleared: Flow<Key> override val onDataCleared: Flow<Key>
get() = _onDataCleared get() = _onDataCleared
override suspend fun add(k: Key, v: Value) {
transaction(database) {
insert {
it[keyColumn] = k
it[valueColumn] = v
}
}.also { _onNewValue.emit(k to v) }
}
override suspend fun add(toAdd: Map<Key, List<Value>>) { override suspend fun add(toAdd: Map<Key, List<Value>>) {
transaction(database) { transaction(database) {
toAdd.keys.flatMap { k -> toAdd.keys.flatMap { k ->
@ -72,9 +62,3 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
}.also { _onDataCleared.emit(k) } }.also { _onDataCleared.emit(k) }
} }
} }
@Deprecated("Renamed", ReplaceWith("ExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo"))
typealias AbstractOneToManyExposedKeyValueRepo<Key, Value> = ExposedOneToManyKeyValueRepo<Key, Value>
@Deprecated("Renamed", ReplaceWith("ExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo"))
typealias AbstractExposedOneToManyKeyValueRepo<Key, Value> = ExposedOneToManyKeyValueRepo<Key, Value>

View File

@ -47,9 +47,3 @@ open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any() select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any()
} }
} }
@Deprecated("Renamed", ReplaceWith("ExposedReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedReadOneToManyKeyValueRepo"))
typealias AbstractOneToManyExposedReadKeyValueRepo<Key, Value> = ExposedReadOneToManyKeyValueRepo<Key, Value>
@Deprecated("Renamed", ReplaceWith("ExposedReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedReadOneToManyKeyValueRepo"))
typealias AbstractExposedReadOneToManyKeyValueRepo<Key, Value> = ExposedReadOneToManyKeyValueRepo<Key, Value>

View File

@ -1,6 +1,5 @@
package dev.inmo.micro_utils.repos package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.coroutines.BroadcastFlow
import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.PaginationResult
import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.paginate
@ -55,20 +54,11 @@ class WriteMapKeyValueRepo<Key, Value>(
override val onValueRemoved: Flow<Key> override val onValueRemoved: Flow<Key>
get() = _onValueRemoved get() = _onValueRemoved
override suspend fun set(k: Key, v: Value) {
map[k] = v
_onNewValue.emit(k to v)
}
override suspend fun set(toSet: Map<Key, Value>) { override suspend fun set(toSet: Map<Key, Value>) {
map.putAll(toSet) map.putAll(toSet)
toSet.forEach { (k, v) -> _onNewValue.emit(k to v) } toSet.forEach { (k, v) -> _onNewValue.emit(k to v) }
} }
override suspend fun unset(k: Key) {
map.remove(k) ?.also { _onValueRemoved.emit(k) }
}
override suspend fun unset(toUnset: List<Key>) { override suspend fun unset(toUnset: List<Key>) {
toUnset.forEach { k -> toUnset.forEach { k ->
map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) } map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) }

View File

@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
class MapReadOneToManyKeyValueRepo<Key, Value>( class MapReadOneToManyKeyValueRepo<Key, Value>(
private val map: Map<Key, List<Value>> = emptyMap() private val map: Map<Key, List<Value>> = emptyMap()
@ -45,32 +46,38 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
class MapWriteOneToManyKeyValueRepo<Key, Value>( class MapWriteOneToManyKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf() private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
) : WriteOneToManyKeyValueRepo<Key, Value> { ) : WriteOneToManyKeyValueRepo<Key, Value> {
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow() private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onNewValue: Flow<Pair<Key, Value>> override val onNewValue: Flow<Pair<Key, Value>>
get() = _onNewValue get() = _onNewValue
private val _onValueRemoved: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow() private val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
override val onValueRemoved: Flow<Pair<Key, Value>> override val onValueRemoved: Flow<Pair<Key, Value>>
get() = _onValueRemoved get() = _onValueRemoved
private val _onDataCleared: BroadcastFlow<Key> = BroadcastFlow() private val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
override val onDataCleared: Flow<Key> override val onDataCleared: Flow<Key>
get() = _onDataCleared get() = _onDataCleared
override suspend fun add(toAdd: Map<Key, List<Value>>) { override suspend fun add(toAdd: Map<Key, List<Value>>) {
toAdd.keys.forEach { toAdd.keys.forEach { k ->
map.getOrPut(it) { if (map.getOrPut(k) { mutableListOf() }.addAll(toAdd[k] ?: return@forEach)) {
mutableListOf() toAdd[k] ?.forEach { v ->
}.addAll(toAdd[it] ?: return@forEach) _onNewValue.emit(k to v)
}
}
} }
} }
override suspend fun remove(toRemove: Map<Key, List<Value>>) { override suspend fun remove(toRemove: Map<Key, List<Value>>) {
toRemove.keys.forEach { toRemove.keys.forEach { k ->
map[it] ?.removeAll(toRemove[it] ?: return@forEach) if (map[k] ?.removeAll(toRemove[k] ?: return@forEach) == true) {
toRemove[k] ?.forEach { v ->
_onValueRemoved.emit(k to v)
}
}
} }
} }
override suspend fun clear(k: Key) { override suspend fun clear(k: Key) {
map.remove(k) ?.also { _onDataCleared.send(k) } map.remove(k) ?.also { _onDataCleared.emit(k) }
} }
} }

View File

@ -69,6 +69,3 @@ class KtorReadStandardKeyValueRepo<Key, Value> (
Long.serializer() Long.serializer()
) )
} }
@Deprecated("Renamed", ReplaceWith("KtorReadStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorReadStandardKeyValueRepo"))
typealias KtorStandartReadKeyValueRepo<Key, Value> = KtorReadStandardKeyValueRepo<Key, Value>

View File

@ -35,7 +35,6 @@ class KtorWriteStandardKeyValueRepo<K, V> (
BodyPair(keyValueMapSerializer, toSet), BodyPair(keyValueMapSerializer, toSet),
Unit.serializer() Unit.serializer()
) )
override suspend fun set(k: K, v: V) = set(mapOf(k to v))
override suspend fun unset(toUnset: List<K>) = client.unipost( override suspend fun unset(toUnset: List<K>) = client.unipost(
buildStandardUrl( buildStandardUrl(
@ -45,8 +44,4 @@ class KtorWriteStandardKeyValueRepo<K, V> (
BodyPair(keysListSerializer, toUnset), BodyPair(keysListSerializer, toUnset),
Unit.serializer() Unit.serializer()
) )
override suspend fun unset(k: K) = unset(listOf(k))
} }
@Deprecated("Renamed", ReplaceWith("KtorWriteStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorWriteStandardKeyValueRepo"))
typealias KtorStandartWriteKeyValueRepo<K, V> = KtorWriteStandardKeyValueRepo<K, V>

View File

@ -30,15 +30,6 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
deserializer = keySerializer deserializer = keySerializer
) )
override suspend fun add(k: Key, v: Value) = client.unipost(
buildStandardUrl(
baseUrl,
addRoute,
),
BodyPair(keyValueSerializer, k to v),
Unit.serializer(),
)
override suspend fun remove(toRemove: Map<Key, List<Value>>) = client.unipost( override suspend fun remove(toRemove: Map<Key, List<Value>>) = client.unipost(
buildStandardUrl( buildStandardUrl(
baseUrl, baseUrl,