From abfda3627cee39059f70a2a1d9f9add4b7b19ae1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 9 Nov 2020 00:46:26 +0600 Subject: [PATCH] repos rework --- CHANGELOG.md | 15 ++++ .../dev/inmo/micro_utils/common/DiffUtils.kt | 15 ---- .../micro_utils/coroutines/BroadcastFlow.kt | 24 ----- .../coroutines/BroadcastStateFlow.kt | 68 --------------- .../dev/inmo/micro_utils/repos/MapperRepo.kt | 9 ++ .../repos/OneToManyKeyValueRepo.kt | 56 ++++++------ .../micro_utils/repos/StandartKeyValueRepo.kt | 24 ++--- .../repos/mappers/KeyValueMappers.kt | 84 ++++++++++++++++++ .../repos/mappers/OneToManyKeyValueMappers.kt | 87 +++++++++++++++++++ .../exposed/keyvalue/ExposedKeyValueRepo.kt | 26 ------ .../keyvalue/ExposedReadKeyValueRepo.kt | 3 - .../onetomany/ExposedOneToManyKeyValueRepo.kt | 16 ---- .../ExposedReadOneToManyKeyValueRepo.kt | 6 -- .../inmo/micro_utils/repos/MapKeyValueRepo.kt | 10 --- .../repos/MapOneToManyKeyValueRepo.kt | 27 +++--- .../key_value/KtorReadStandardKeyValueRepo.kt | 3 - .../KtorWriteStandardKeyValueRepo.kt | 5 -- .../KtorWriteOneToManyKeyValueRepo.kt | 9 -- 18 files changed, 254 insertions(+), 233 deletions(-) delete mode 100644 coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastFlow.kt delete mode 100644 coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastStateFlow.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 58f18307e57..59bbbe35d04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,22 @@ ## 0.3.0 +All deprecations has been removed + * `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`: * CRUD realizations replaced their channels to shared flows diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt index 7033edcfb6f..0c63e30c786 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt @@ -149,18 +149,3 @@ inline fun StrictDiff(old: Iterable, new: Iterable) = old.calculateDif inline fun Iterable.calculateStrictDiff( other: Iterable ) = calculateDiff(other, strictComparison = true) - -/** - * Compare one-to-one - */ -@Deprecated("Will be removed or replaced with some new function. Use calculateDiff instead") -inline fun Iterable.syncWith( - other: Iterable, - noinline removed: (List) -> Unit = {}, - noinline added: (List) -> Unit = {} -) { - calculateDiff(other).also { - removed(it.removed.map { it.value }) - added(it.added.map { it.value }) - } -} diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastFlow.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastFlow.kt deleted file mode 100644 index e0f7660bb75..00000000000 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastFlow.kt +++ /dev/null @@ -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 BroadcastFlow( - internalChannelSize: Int = Channel.BUFFERED -): BroadcastFlow { - val channel = BroadcastChannel(internalChannelSize) - - return BroadcastFlow( - channel, - channel.asFlow() - ) -} - -@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow") -class BroadcastFlow internal constructor( - private val channel: BroadcastChannel, - private val flow: Flow -): Flow by flow, SendChannel by channel diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastStateFlow.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastStateFlow.kt deleted file mode 100644 index 53d29ae713f..00000000000 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/BroadcastStateFlow.kt +++ /dev/null @@ -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 internal constructor( - parentFlow: Flow, - initial: T, - replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize, - replayScope: CoroutineScope -) : StateFlow, Flow by parentFlow { - private val deque = ArrayDeque(1).also { - it.add(initial) - } - override val replayCache: List - 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 BroadcastChannel.asStateFlow( - value: T, - scope: CoroutineScope, - replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize -): StateFlow = BroadcastStateFlow(asFlow(), value, replayCacheSize, scope) - -@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow") -fun BroadcastChannel.asStateFlow( - scope: CoroutineScope, - replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize -): StateFlow = asStateFlow(null, scope, replayCacheSize) - -@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow") -fun broadcastStateFlow( - initial: T, scope: CoroutineScope, - channelSize: Int = Channel.BUFFERED, - replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize -) = BroadcastChannel( - channelSize -).let { - it to it.asStateFlow(initial, scope, replayCacheSize) -} - -@Deprecated("Deprecated due to stabilization of SharedFlow and StateFlow") -fun broadcastStateFlow( - scope: CoroutineScope, - channelSize: Int = Channel.BUFFERED, - replayCacheSize: Int = defaultBroadcastStateFlowReplayCacheSize -) = broadcastStateFlow(null, scope, channelSize, replayCacheSize) - diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt new file mode 100644 index 00000000000..334077a007f --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapperRepo.kt @@ -0,0 +1,9 @@ +package dev.inmo.micro_utils.repos + +interface MapperRepo { + 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 +} \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt index bbc25228592..4a4e25f0c23 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/OneToManyKeyValueRepo.kt @@ -35,8 +35,6 @@ interface ReadOneToManyKeyValueRepo : Repo { } } } -@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo")) -typealias OneToManyReadKeyValueRepo = ReadOneToManyKeyValueRepo interface WriteOneToManyKeyValueRepo : Repo { val onNewValue: Flow> @@ -44,42 +42,44 @@ interface WriteOneToManyKeyValueRepo : Repo { val onDataCleared: Flow suspend fun add(toAdd: Map>) - @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>) - @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) } -@Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo")) -typealias OneToManyWriteKeyValueRepo = WriteOneToManyKeyValueRepo + +suspend inline fun > REPO.add( + keysAndValues: List>> +) = add(keysAndValues.toMap()) + +suspend inline fun > REPO.add( + vararg keysAndValues: Pair> +) = add(keysAndValues.toMap()) + +suspend inline fun WriteOneToManyKeyValueRepo.add( + k: Key, v: List +) = add(mapOf(k to v)) + +suspend inline fun WriteOneToManyKeyValueRepo.add( + k: Key, vararg v: Value +) = add(k, v.toList()) interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo -suspend inline fun > REPO.add( - k: Key, - vararg v: Value -) = add(mapOf(k to v.toList())) - -suspend inline fun > REPO.add( - keysAndValues: List>> -) = add(keysAndValues.toMap()) - -suspend inline fun > REPO.add( - vararg keysAndValues: Pair> -) = add(keysAndValues.toMap()) - -suspend inline fun > REPO.remove( - k: Key, - vararg v: Value -) = remove(mapOf(k to v.toList())) - -suspend inline fun > REPO.remove( +suspend inline fun WriteOneToManyKeyValueRepo.remove( keysAndValues: List>> ) = remove(keysAndValues.toMap()) -suspend inline fun > REPO.remove( +suspend inline fun WriteOneToManyKeyValueRepo.remove( vararg keysAndValues: Pair> ) = remove(keysAndValues.toMap()) + +suspend inline fun WriteOneToManyKeyValueRepo.remove( + k: Key, + v: List +) = remove(mapOf(k to v)) + +suspend inline fun WriteOneToManyKeyValueRepo.remove( + k: Key, + vararg v: Value +) = remove(k, v.toList()) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt index 62780db7012..d5f632739f9 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartKeyValueRepo.kt @@ -16,16 +16,20 @@ interface WriteStandardKeyValueRepo : Repo { val onNewValue: Flow> val onValueRemoved: Flow - @Deprecated("Realize set with map instead") - suspend fun set(k: Key, v: Value) - suspend fun set(toSet: Map) = toSet.forEach { (k, v) -> - set(k, v) - } - @Deprecated("Realize unset with list instead") - suspend fun unset(k: Key) - suspend fun unset(toUnset: List) = toUnset.forEach { - unset(it) - } + suspend fun set(toSet: Map) + suspend fun unset(toUnset: List) } +suspend inline fun WriteStandardKeyValueRepo.set( + vararg toSet: Pair +) = set(toSet.toMap()) + +suspend inline fun WriteStandardKeyValueRepo.set( + k: Key, v: Value +) = set(k to v) + +suspend inline fun WriteStandardKeyValueRepo.unset( + vararg k: Key +) = unset(k.toList()) + interface StandardKeyValueRepo : ReadStandardKeyValueRepo, WriteStandardKeyValueRepo \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt new file mode 100644 index 00000000000..75d4f860079 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt @@ -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( + private val to: ReadStandardKeyValueRepo, + mapper: MapperRepo +) : ReadStandardKeyValueRepo, MapperRepo by mapper { + override suspend fun get(k: FromKey): FromValue? = to.get( + k.toOutKey() + ) ?.toInnerValue() + + override suspend fun values( + pagination: Pagination, + reversed: Boolean + ): PaginationResult = 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 = 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( + private val to: WriteStandardKeyValueRepo, + mapper: MapperRepo +) : WriteStandardKeyValueRepo, MapperRepo by mapper { + override val onNewValue: Flow> = to.onNewValue.map { (k, v) -> + k.toInnerKey() to v.toInnerValue() + } + override val onValueRemoved: Flow = to.onValueRemoved.map { k -> + k.toInnerKey() + } + + override suspend fun set(toSet: Map) = to.set( + toSet.map { (k, v) -> + k.toOutKey() to v.toOutValue() + }.toMap() + ) + + override suspend fun unset(toUnset: List) = to.unset( + toUnset.map { k -> + k.toOutKey() + } + ) +} + +open class MapperStandardKeyValueRepo( + private val to: StandardKeyValueRepo, + mapper: MapperRepo +) : StandardKeyValueRepo, + MapperRepo by mapper, + ReadStandardKeyValueRepo by MapperReadStandardKeyValueRepo(to, mapper), + WriteStandardKeyValueRepo by MapperWriteStandardKeyValueRepo(to, mapper) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt new file mode 100644 index 00000000000..37c1602b203 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/OneToManyKeyValueMappers.kt @@ -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( + private val to: ReadOneToManyKeyValueRepo, + mapper: MapperRepo +) : ReadOneToManyKeyValueRepo, MapperRepo by mapper { + override suspend fun get( + k: FromKey, + pagination: Pagination, + reversed: Boolean + ): PaginationResult = 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 = 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( + private val to: WriteOneToManyKeyValueRepo, + mapper: MapperRepo +) : WriteOneToManyKeyValueRepo, MapperRepo by mapper { + override val onNewValue: Flow> = to.onNewValue.map { (k, v) -> + k.toInnerKey() to v.toInnerValue() + } + override val onValueRemoved: Flow> = to.onValueRemoved.map { (k, v) -> + k.toInnerKey() to v.toInnerValue() + } + override val onDataCleared: Flow = to.onDataCleared.map { k -> + k.toInnerKey() + } + + override suspend fun add(toAdd: Map>) = to.add( + toAdd.map { (k, v) -> + k.toOutKey() to v.map { it.toOutValue() } + }.toMap() + ) + + override suspend fun remove(toRemove: Map>) = 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( + private val to: OneToManyKeyValueRepo, + mapper: MapperRepo +) : OneToManyKeyValueRepo, + MapperRepo by mapper, + ReadOneToManyKeyValueRepo by MapperReadOneToManyKeyValueRepo(to, mapper), + WriteOneToManyKeyValueRepo by MapperWriteOneToManyKeyValueRepo(to, mapper) diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt index 3551e591e39..b303fa89feb 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt @@ -24,22 +24,6 @@ open class ExposedKeyValueRepo( override val onNewValue: Flow> = _onNewValue.asSharedFlow() override val onValueRemoved: Flow = _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) { transaction(database) { toSet.mapNotNull { (k, v) -> @@ -62,13 +46,6 @@ open class ExposedKeyValueRepo( } } - override suspend fun unset(k: Key) { - transaction(database) { - deleteWhere { keyColumn.eq(k) } - } - _onValueRemoved.emit(k) - } - override suspend fun unset(toUnset: List) { transaction(database) { toUnset.mapNotNull { @@ -83,6 +60,3 @@ open class ExposedKeyValueRepo( } } } - -@Deprecated("Renamed", ReplaceWith("ExposedKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo")) -typealias AbstractExposedKeyValueRepo = ExposedKeyValueRepo diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt index 6573c230e69..2137a329a02 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedReadKeyValueRepo.kt @@ -39,6 +39,3 @@ open class ExposedReadKeyValueRepo( } }.createPaginationResult(pagination, count()) } - -@Deprecated("Renamed", ReplaceWith("ExposedReadKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedReadKeyValueRepo")) -typealias AbstractExposedReadKeyValueRepo = ExposedReadKeyValueRepo diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedOneToManyKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedOneToManyKeyValueRepo.kt index f1b0aca4b1d..86a4cb020cd 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedOneToManyKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedOneToManyKeyValueRepo.kt @@ -1,6 +1,5 @@ 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.exposed.ColumnAllocator import kotlinx.coroutines.flow.* @@ -28,15 +27,6 @@ open class ExposedOneToManyKeyValueRepo( override val onDataCleared: Flow 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>) { transaction(database) { toAdd.keys.flatMap { k -> @@ -72,9 +62,3 @@ open class ExposedOneToManyKeyValueRepo( }.also { _onDataCleared.emit(k) } } } - -@Deprecated("Renamed", ReplaceWith("ExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo")) -typealias AbstractOneToManyExposedKeyValueRepo = ExposedOneToManyKeyValueRepo - -@Deprecated("Renamed", ReplaceWith("ExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedOneToManyKeyValueRepo")) -typealias AbstractExposedOneToManyKeyValueRepo = ExposedOneToManyKeyValueRepo diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadOneToManyKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadOneToManyKeyValueRepo.kt index ce9a2be494a..336fc7cd95e 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadOneToManyKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/ExposedReadOneToManyKeyValueRepo.kt @@ -47,9 +47,3 @@ open class ExposedReadOneToManyKeyValueRepo( 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 = ExposedReadOneToManyKeyValueRepo - -@Deprecated("Renamed", ReplaceWith("ExposedReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.ExposedReadOneToManyKeyValueRepo")) -typealias AbstractExposedReadOneToManyKeyValueRepo = ExposedReadOneToManyKeyValueRepo diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt index 2456e04d5b1..6b6fbfb77f5 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt @@ -1,6 +1,5 @@ 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.PaginationResult import dev.inmo.micro_utils.pagination.utils.paginate @@ -55,20 +54,11 @@ class WriteMapKeyValueRepo( override val onValueRemoved: Flow get() = _onValueRemoved - override suspend fun set(k: Key, v: Value) { - map[k] = v - _onNewValue.emit(k to v) - } - override suspend fun set(toSet: Map) { map.putAll(toSet) 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) { toUnset.forEach { k -> map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) } diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt index f9b681b9f8d..bf8758ac1ff 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt @@ -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.reverse import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableSharedFlow class MapReadOneToManyKeyValueRepo( private val map: Map> = emptyMap() @@ -45,32 +46,38 @@ class MapReadOneToManyKeyValueRepo( class MapWriteOneToManyKeyValueRepo( private val map: MutableMap> = mutableMapOf() ) : WriteOneToManyKeyValueRepo { - private val _onNewValue: BroadcastFlow> = BroadcastFlow() + private val _onNewValue: MutableSharedFlow> = MutableSharedFlow() override val onNewValue: Flow> get() = _onNewValue - private val _onValueRemoved: BroadcastFlow> = BroadcastFlow() + private val _onValueRemoved: MutableSharedFlow> = MutableSharedFlow() override val onValueRemoved: Flow> get() = _onValueRemoved - private val _onDataCleared: BroadcastFlow = BroadcastFlow() + private val _onDataCleared: MutableSharedFlow = MutableSharedFlow() override val onDataCleared: Flow get() = _onDataCleared override suspend fun add(toAdd: Map>) { - toAdd.keys.forEach { - map.getOrPut(it) { - mutableListOf() - }.addAll(toAdd[it] ?: return@forEach) + toAdd.keys.forEach { k -> + if (map.getOrPut(k) { mutableListOf() }.addAll(toAdd[k] ?: return@forEach)) { + toAdd[k] ?.forEach { v -> + _onNewValue.emit(k to v) + } + } } } override suspend fun remove(toRemove: Map>) { - toRemove.keys.forEach { - map[it] ?.removeAll(toRemove[it] ?: return@forEach) + toRemove.keys.forEach { k -> + if (map[k] ?.removeAll(toRemove[k] ?: return@forEach) == true) { + toRemove[k] ?.forEach { v -> + _onValueRemoved.emit(k to v) + } + } } } override suspend fun clear(k: Key) { - map.remove(k) ?.also { _onDataCleared.send(k) } + map.remove(k) ?.also { _onDataCleared.emit(k) } } } diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt index 16f0cfec1ef..4ad73bf32b4 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt @@ -69,6 +69,3 @@ class KtorReadStandardKeyValueRepo ( Long.serializer() ) } - -@Deprecated("Renamed", ReplaceWith("KtorReadStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorReadStandardKeyValueRepo")) -typealias KtorStandartReadKeyValueRepo = KtorReadStandardKeyValueRepo diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt index 440c22e8350..6b423797afc 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt @@ -35,7 +35,6 @@ class KtorWriteStandardKeyValueRepo ( BodyPair(keyValueMapSerializer, toSet), Unit.serializer() ) - override suspend fun set(k: K, v: V) = set(mapOf(k to v)) override suspend fun unset(toUnset: List) = client.unipost( buildStandardUrl( @@ -45,8 +44,4 @@ class KtorWriteStandardKeyValueRepo ( BodyPair(keysListSerializer, toUnset), 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 = KtorWriteStandardKeyValueRepo diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt index bc7de0bcd88..a231918430f 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt @@ -30,15 +30,6 @@ class KtorWriteOneToManyKeyValueRepo ( 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>) = client.unipost( buildStandardUrl( baseUrl,