mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-22 16:47:15 +00:00
commit
aeee41680e
21
CHANGELOG.md
21
CHANGELOG.md
@ -1,5 +1,26 @@
|
||||
# Changelog
|
||||
|
||||
## 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
|
||||
|
||||
## 0.2.7
|
||||
|
||||
* `Versions`:
|
||||
|
@ -149,18 +149,3 @@ inline fun <T> StrictDiff(old: Iterable<T>, new: Iterable<T>) = old.calculateDif
|
||||
inline fun <T> Iterable<T>.calculateStrictDiff(
|
||||
other: Iterable<T>
|
||||
) = 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 })
|
||||
}
|
||||
}
|
||||
|
@ -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
|
@ -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)
|
||||
|
@ -19,4 +19,4 @@ github_release_plugin_version=2.2.12
|
||||
uuidVersion=0.2.2
|
||||
|
||||
group=dev.inmo
|
||||
version=0.2.7
|
||||
version=0.3.0
|
||||
|
@ -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
|
||||
}
|
@ -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 {
|
||||
val onNewValue: Flow<Pair<Key, Value>>
|
||||
@ -44,42 +42,44 @@ interface WriteOneToManyKeyValueRepo<Key, Value> : Repo {
|
||||
val onDataCleared: Flow<Key>
|
||||
|
||||
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>>)
|
||||
@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<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>
|
||||
|
||||
suspend inline fun <Key, Value, REPO : WriteOneToManyKeyValueRepo<Key, Value>> REPO.add(
|
||||
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(
|
||||
suspend inline fun <Key, Value> WriteOneToManyKeyValueRepo<Key, Value>.remove(
|
||||
keysAndValues: List<Pair<Key, List<Value>>>
|
||||
) = 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>>
|
||||
) = 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())
|
||||
|
@ -16,16 +16,20 @@ interface WriteStandardKeyValueRepo<Key, Value> : Repo {
|
||||
val onNewValue: Flow<Pair<Key, Value>>
|
||||
val onValueRemoved: Flow<Key>
|
||||
|
||||
@Deprecated("Realize set with map instead")
|
||||
suspend fun set(k: Key, v: Value)
|
||||
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 fun set(toSet: Map<Key, Value>)
|
||||
suspend fun unset(toUnset: List<Key>)
|
||||
}
|
||||
|
||||
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>
|
@ -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)
|
@ -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)
|
@ -1,9 +1,7 @@
|
||||
package dev.inmo.micro_utils.repos.exposed
|
||||
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
|
||||
abstract class AbstractExposedCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
flowsChannelsSize: Int = Channel.BUFFERED,
|
||||
flowsChannelsSize: Int = 0,
|
||||
tableName: String = ""
|
||||
) :
|
||||
AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
|
@ -3,28 +3,27 @@ package dev.inmo.micro_utils.repos.exposed
|
||||
import dev.inmo.micro_utils.repos.UpdatedValuePair
|
||||
import dev.inmo.micro_utils.repos.WriteStandardCRUDRepo
|
||||
import kotlinx.coroutines.channels.BroadcastChannel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.*
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.statements.InsertStatement
|
||||
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
flowsChannelsSize: Int = 64,
|
||||
flowsChannelsSize: Int = 0,
|
||||
tableName: String = ""
|
||||
) :
|
||||
AbstractExposedReadCRUDRepo<ObjectType, IdType>(tableName),
|
||||
ExposedCRUDRepo<ObjectType, IdType>,
|
||||
WriteStandardCRUDRepo<ObjectType, IdType, InputValueType>
|
||||
{
|
||||
protected val newObjectsChannel = BroadcastChannel<ObjectType>(flowsChannelsSize)
|
||||
protected val updateObjectsChannel = BroadcastChannel<ObjectType>(flowsChannelsSize)
|
||||
protected val deleteObjectsIdsChannel = BroadcastChannel<IdType>(flowsChannelsSize)
|
||||
protected val newObjectsChannel = MutableSharedFlow<ObjectType>(flowsChannelsSize)
|
||||
protected val updateObjectsChannel = MutableSharedFlow<ObjectType>(flowsChannelsSize)
|
||||
protected val deleteObjectsIdsChannel = MutableSharedFlow<IdType>(flowsChannelsSize)
|
||||
|
||||
override val newObjectsFlow: Flow<ObjectType> = newObjectsChannel.asFlow()
|
||||
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asFlow()
|
||||
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asFlow()
|
||||
override val newObjectsFlow: Flow<ObjectType> = newObjectsChannel.asSharedFlow()
|
||||
override val updatedObjectsFlow: Flow<ObjectType> = updateObjectsChannel.asSharedFlow()
|
||||
override val deletedObjectsIdsFlow: Flow<IdType> = deleteObjectsIdsChannel.asSharedFlow()
|
||||
|
||||
abstract val InsertStatement<Number>.asObject: ObjectType
|
||||
abstract val selectByIds: SqlExpressionBuilder.(List<out IdType>) -> Op<Boolean>
|
||||
@ -45,7 +44,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
values.map { value -> createWithoutNotification(value) }
|
||||
}.also {
|
||||
it.forEach {
|
||||
newObjectsChannel.send(it)
|
||||
newObjectsChannel.emit(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -75,7 +74,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
onBeforeUpdate(listOf(id to value))
|
||||
return updateWithoutNotification(id, value).also {
|
||||
if (it != null) {
|
||||
updateObjectsChannel.send(it)
|
||||
updateObjectsChannel.emit(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -89,7 +88,7 @@ abstract class AbstractExposedWriteCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
} as List<ObjectType>
|
||||
).also {
|
||||
it.forEach {
|
||||
updateObjectsChannel.send(it)
|
||||
updateObjectsChannel.emit(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,22 +24,6 @@ open class ExposedKeyValueRepo<Key, Value>(
|
||||
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.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>) {
|
||||
transaction(database) {
|
||||
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>) {
|
||||
transaction(database) {
|
||||
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>
|
||||
|
@ -39,6 +39,3 @@ open class ExposedReadKeyValueRepo<Key, Value>(
|
||||
}
|
||||
}.createPaginationResult(pagination, count())
|
||||
}
|
||||
|
||||
@Deprecated("Renamed", ReplaceWith("ExposedReadKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedReadKeyValueRepo"))
|
||||
typealias AbstractExposedReadKeyValueRepo<Key, Value> = ExposedReadKeyValueRepo<Key, Value>
|
||||
|
@ -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<Key, Value>(
|
||||
override val onDataCleared: Flow<Key>
|
||||
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>>) {
|
||||
transaction(database) {
|
||||
toAdd.keys.flatMap { k ->
|
||||
@ -72,9 +62,3 @@ open class ExposedOneToManyKeyValueRepo<Key, Value>(
|
||||
}.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>
|
||||
|
@ -47,9 +47,3 @@ open class ExposedReadOneToManyKeyValueRepo<Key, Value>(
|
||||
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>
|
||||
|
@ -1,8 +1,7 @@
|
||||
package dev.inmo.micro_utils.repos
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
||||
import dev.inmo.micro_utils.pagination.*
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
class ReadMapCRUDRepo<ObjectType, IdType>(
|
||||
private val map: Map<IdType, ObjectType> = emptyMap()
|
||||
@ -26,15 +25,12 @@ class ReadMapCRUDRepo<ObjectType, IdType>(
|
||||
abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
private val map: MutableMap<IdType, ObjectType> = mutableMapOf()
|
||||
) : WriteStandardCRUDRepo<ObjectType, IdType, InputValueType> {
|
||||
private val _newObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
|
||||
override val newObjectsFlow: Flow<ObjectType>
|
||||
get() = _newObjectsFlow
|
||||
private val _updatedObjectsFlow: BroadcastFlow<ObjectType> = BroadcastFlow()
|
||||
override val updatedObjectsFlow: Flow<ObjectType>
|
||||
get() = _updatedObjectsFlow
|
||||
private val _deletedObjectsIdsFlow: BroadcastFlow<IdType> = BroadcastFlow()
|
||||
override val deletedObjectsIdsFlow: Flow<IdType>
|
||||
get() = _deletedObjectsIdsFlow
|
||||
private val _newObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
|
||||
override val newObjectsFlow: Flow<ObjectType> = _newObjectsFlow.asSharedFlow()
|
||||
private val _updatedObjectsFlow: MutableSharedFlow<ObjectType> = MutableSharedFlow()
|
||||
override val updatedObjectsFlow: Flow<ObjectType> = _updatedObjectsFlow.asSharedFlow()
|
||||
private val _deletedObjectsIdsFlow: MutableSharedFlow<IdType> = MutableSharedFlow()
|
||||
override val deletedObjectsIdsFlow: Flow<IdType> = _deletedObjectsIdsFlow.asSharedFlow()
|
||||
|
||||
protected abstract suspend fun updateObject(newValue: InputValueType, id: IdType, old: ObjectType): ObjectType
|
||||
protected abstract suspend fun createObject(newValue: InputValueType): Pair<IdType, ObjectType>
|
||||
@ -44,7 +40,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
val (id, newObject) = createObject(it)
|
||||
map[id] = newObject
|
||||
newObject.also { _ ->
|
||||
_newObjectsFlow.send(newObject)
|
||||
_newObjectsFlow.emit(newObject)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -54,7 +50,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
|
||||
return newValue.also {
|
||||
map[id] = it
|
||||
_updatedObjectsFlow.send(it)
|
||||
_updatedObjectsFlow.emit(it)
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,7 +60,7 @@ abstract class WriteMapCRUDRepo<ObjectType, IdType, InputValueType>(
|
||||
|
||||
override suspend fun deleteById(ids: List<IdType>) {
|
||||
ids.forEach {
|
||||
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.send(it) }
|
||||
map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.emit(it) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<Key, Value>(
|
||||
override val onValueRemoved: Flow<Key>
|
||||
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>) {
|
||||
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<Key>) {
|
||||
toUnset.forEach { k ->
|
||||
map.remove(k) ?.also { _ -> _onValueRemoved.emit(k) }
|
||||
|
@ -1,10 +1,9 @@
|
||||
package dev.inmo.micro_utils.repos
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.BroadcastFlow
|
||||
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.*
|
||||
|
||||
class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||
private val map: Map<Key, List<Value>> = emptyMap()
|
||||
@ -45,32 +44,35 @@ class MapReadOneToManyKeyValueRepo<Key, Value>(
|
||||
class MapWriteOneToManyKeyValueRepo<Key, Value>(
|
||||
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf()
|
||||
) : WriteOneToManyKeyValueRepo<Key, Value> {
|
||||
private val _onNewValue: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
||||
override val onNewValue: Flow<Pair<Key, Value>>
|
||||
get() = _onNewValue
|
||||
private val _onValueRemoved: BroadcastFlow<Pair<Key, Value>> = BroadcastFlow()
|
||||
override val onValueRemoved: Flow<Pair<Key, Value>>
|
||||
get() = _onValueRemoved
|
||||
private val _onDataCleared: BroadcastFlow<Key> = BroadcastFlow()
|
||||
override val onDataCleared: Flow<Key>
|
||||
get() = _onDataCleared
|
||||
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||
override val onNewValue: Flow<Pair<Key, Value>> = _onNewValue.asSharedFlow()
|
||||
private val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MutableSharedFlow()
|
||||
override val onValueRemoved: Flow<Pair<Key, Value>> = _onValueRemoved.asSharedFlow()
|
||||
private val _onDataCleared: MutableSharedFlow<Key> = MutableSharedFlow()
|
||||
override val onDataCleared: Flow<Key> = _onDataCleared.asSharedFlow()
|
||||
|
||||
override suspend fun add(toAdd: Map<Key, List<Value>>) {
|
||||
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<Key, List<Value>>) {
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -69,6 +69,3 @@ class KtorReadStandardKeyValueRepo<Key, Value> (
|
||||
Long.serializer()
|
||||
)
|
||||
}
|
||||
|
||||
@Deprecated("Renamed", ReplaceWith("KtorReadStandardKeyValueRepo", "dev.inmo.micro_utils.repos.ktor.client.key_value.KtorReadStandardKeyValueRepo"))
|
||||
typealias KtorStandartReadKeyValueRepo<Key, Value> = KtorReadStandardKeyValueRepo<Key, Value>
|
||||
|
@ -35,7 +35,6 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
||||
BodyPair(keyValueMapSerializer, toSet),
|
||||
Unit.serializer()
|
||||
)
|
||||
override suspend fun set(k: K, v: V) = set(mapOf(k to v))
|
||||
|
||||
override suspend fun unset(toUnset: List<K>) = client.unipost(
|
||||
buildStandardUrl(
|
||||
@ -45,8 +44,4 @@ class KtorWriteStandardKeyValueRepo<K, V> (
|
||||
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<K, V> = KtorWriteStandardKeyValueRepo<K, V>
|
||||
|
@ -30,15 +30,6 @@ class KtorWriteOneToManyKeyValueRepo<Key, Value> (
|
||||
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(
|
||||
buildStandardUrl(
|
||||
baseUrl,
|
||||
|
Loading…
Reference in New Issue
Block a user