diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b30b38d50..6cf5619712d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,34 @@ Inited :) * `Versions`: * `kotlinx.serialization`: `1.0.0-RC2` -> `1.0.0` +* `Pagination` + * `Common` + * Function `calculatePage` for calculating page based on size of page and index of first element + * Extension `List#createPaginationResult` which use index of first element to calculate current page and other + info in `PaginationResult` object + * Factory `emptyPaginationResult` for empty `PaginationResult` + * Extensions `paginate` for creating of `PaginationResult` which use as source one of next types: + * `Iterable` + * `List` + * `Set` * `Repos` * `Common` + * Interfaces related to `OneToManyKeyValueRepo` were renamed with convenience to `Read`/`Write` modifier before name + * All subclasses were renamed + * Interfaces related to `StandartKeyValueRepo` were renamed with convenience to `Read`/`Write` modifier before name + * All subclasses were renamed * Extensions `doForAll` and `getAll` were added for all current types of repos: * `ReadStandardCRUDRepo` - * `StandardReadKeyValueRepo` - * `OneToManyReadKeyValueRepo` - * `StandardReadKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false` + * `ReadStandardKeyValueRepo` + * `ReadOneToManyKeyValueRepo` + * `ReadStandardKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false` + * New `Flow`'s in `WriteOneToManyKeyValueRepo`: + * `onNewValue` + * `onValueRemoved` + * `onDataCleared` + * New function `ReadStandardCRUDRepo#count` + * `In Memory` + * Package has been created:) you can connect it via `implementation "dev.inmo:micro_utils.repos.inmemory"` + * `MapCRUDRepo` class as implementation of `StandardCRUDRepo` on top of `MutableMap` has been added + * `MapKeyValueRepo` class as implementation of `StandardKeyValueRepo` on top of `MutableMap` has been added + * `MapOneToManyKeyValueRepo` class as implementation of `OneToManyKeyValueRepo` on top of `MutableMap` has been added diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt index a3fd510198c..53d8958dc42 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/Pagination.kt @@ -1,6 +1,7 @@ package dev.inmo.micro_utils.pagination import kotlin.math.ceil +import kotlin.math.floor /** * Base interface of pagination @@ -50,3 +51,12 @@ fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int = datasetSize.toLong(), pageSize ) + +/** + * @return calculated page number which can be correctly used in [PaginationResult] as [PaginationResult.page] value + */ +fun calculatePage(firstIndex: Int, resultsSize: Int): Int = if (resultsSize > 0) { + floor(firstIndex.toFloat() / resultsSize).toInt() +} else { + 0 +} diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt index ebca7b51ef8..35c3f3939a8 100644 --- a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/PaginationResult.kt @@ -10,6 +10,8 @@ data class PaginationResult( override val size: Int ) : Pagination +fun emptyPaginationResult() = PaginationResult(0, 0, emptyList(), 0) + fun List.createPaginationResult( pagination: Pagination, commonObjectsNumber: Long @@ -23,6 +25,19 @@ fun List.createPaginationResult( pagination.size ) +fun List.createPaginationResult( + firstIndex: Int, + commonObjectsNumber: Long +) = PaginationResult( + calculatePage(firstIndex, size), + calculatePagesNumber( + commonObjectsNumber, + size + ), + this, + size +) + fun Pair>.createPaginationResult( pagination: Pagination ) = second.createPaginationResult(pagination, first) diff --git a/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt new file mode 100644 index 00000000000..4b6a5d97240 --- /dev/null +++ b/pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt @@ -0,0 +1,35 @@ +package dev.inmo.micro_utils.pagination.utils + +import dev.inmo.micro_utils.pagination.* + +fun Iterable.paginate(with: Pagination): PaginationResult { + var i = 0 + val result = mutableListOf() + val lowerIndex = with.firstIndex + val greatestIndex = with.lastIndex + for (item in this) { + when { + i < lowerIndex || i > greatestIndex -> i++ + else -> { + result.add(item) + i++ + } + } + } + + return result.createPaginationResult(with, i.toLong()) +} + +fun List.paginate(with: Pagination): PaginationResult { + return subList(with.firstIndex, with.lastIndex + 1).createPaginationResult( + with, + size.toLong() + ) +} + +fun Set.paginate(with: Pagination): PaginationResult { + return this.drop(with.firstIndex).take(with.size).createPaginationResult( + with, + size.toLong() + ) +} 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 8cd84aa7bb8..4c6b71e6b9b 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 @@ -2,8 +2,9 @@ package dev.inmo.micro_utils.repos import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult +import kotlinx.coroutines.flow.Flow -interface OneToManyReadKeyValueRepo : Repo { +interface ReadOneToManyKeyValueRepo : Repo { suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult suspend fun contains(k: Key): Boolean @@ -11,11 +12,19 @@ interface OneToManyReadKeyValueRepo : Repo { suspend fun count(k: Key): Long suspend fun count(): Long } +@Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo")) +typealias OneToManyReadKeyValueRepo = ReadOneToManyKeyValueRepo + +interface WriteOneToManyKeyValueRepo : Repo { + val onNewValue: Flow> + val onValueRemoved: Flow> + val onDataCleared: Flow -interface OneToManyWriteKeyValueRepo : Repo { suspend fun add(k: Key, v: Value) suspend fun remove(k: Key, v: Value) suspend fun clear(k: Key) } +@Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo")) +typealias OneToManyWriteKeyValueRepo = WriteOneToManyKeyValueRepo -interface OneToManyKeyValueRepo : OneToManyReadKeyValueRepo, OneToManyWriteKeyValueRepo \ No newline at end of file +interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt index c8c193c5f29..28ad1ac34c7 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/StandartCRUDRepo.kt @@ -8,6 +8,7 @@ interface ReadStandardCRUDRepo : Repo { suspend fun getByPagination(pagination: Pagination): PaginationResult suspend fun getById(id: IdType): ObjectType? suspend fun contains(id: IdType): Boolean + suspend fun count(): Long } typealias UpdatedValuePair = Pair 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 6cc2c6a03f7..a2666571db6 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 @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult import kotlinx.coroutines.flow.Flow -interface StandardReadKeyValueRepo : Repo { +interface ReadStandardKeyValueRepo : Repo { suspend fun get(k: Key): Value? suspend fun values(pagination: Pagination, reversed: Boolean = false): PaginationResult suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult @@ -12,7 +12,7 @@ interface StandardReadKeyValueRepo : Repo { suspend fun count(): Long } -interface StandardWriteKeyValueRepo : Repo { +interface WriteStandardKeyValueRepo : Repo { val onNewValue: Flow> val onValueRemoved: Flow @@ -20,4 +20,4 @@ interface StandardWriteKeyValueRepo : Repo { suspend fun unset(k: Key) } -interface StandardKeyValueRepo : StandardReadKeyValueRepo, StandardWriteKeyValueRepo \ No newline at end of file +interface StandardKeyValueRepo : ReadStandardKeyValueRepo, WriteStandardKeyValueRepo \ No newline at end of file diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt index 8bd84afdce0..4442571948c 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/KeyValuePaginationExtensions.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.pagination import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.* -suspend inline fun > REPO.doForAll( +suspend inline fun > REPO.doForAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") methodCaller: suspend REPO.(Pagination) -> PaginationResult, block: (List>) -> Unit @@ -15,11 +15,11 @@ suspend inline fun > REP } } -suspend inline fun > REPO.doForAll( +suspend inline fun > REPO.doForAll( block: (List>) -> Unit ) = doForAll({ keys(it, false) }, block) -suspend inline fun > REPO.getAll( +suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") methodCaller: suspend REPO.(Pagination) -> PaginationResult ): List> { diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt index 313721d170b..0c3de2def3c 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/OneToManyPaginationExtensions.kt @@ -3,7 +3,7 @@ package dev.inmo.micro_utils.repos.pagination import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.* -suspend inline fun > REPO.doForAll( +suspend inline fun > REPO.doForAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") methodCaller: suspend REPO.(Pagination) -> PaginationResult, block: (List>>) -> Unit @@ -25,11 +25,11 @@ suspend inline fun > RE } } -suspend inline fun > REPO.doForAll( +suspend inline fun > REPO.doForAll( block: (List>>) -> Unit ) = doForAll({ keys(it, false) }, block) -suspend inline fun > REPO.getAll( +suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") methodCaller: suspend REPO.(Pagination) -> PaginationResult ): List>> { diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt index af9e7c268e4..916e0a80512 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/AbstractExposedReadCRUDRepo.kt @@ -33,4 +33,6 @@ abstract class AbstractExposedReadCRUDRepo( override suspend fun contains(id: IdType): Boolean = transaction(db = database) { select { selectById(id) }.limit(1).any() } + + override suspend fun count(): Long = transaction(db = database) { selectAll().count() } } diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt index 83ca592d8ee..867f7439cd8 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt @@ -1,7 +1,7 @@ package dev.inmo.micro_utils.repos.exposed.keyvalue import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction @@ -9,7 +9,7 @@ abstract class AbstractExposedReadKeyValueRepo( protected val database: Database, protected val keyColumn: Column, protected val valueColumn: Column -) : StandardReadKeyValueRepo, Table() { +) : ReadStandardKeyValueRepo, Table() { override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn) override suspend fun get(k: Key): Value? = transaction(database) { diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedOneToManyKeyValueRepo.kt similarity index 66% rename from repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedKeyValueRepo.kt rename to repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedOneToManyKeyValueRepo.kt index 8553c12e611..f0f5e45aa1e 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedOneToManyKeyValueRepo.kt @@ -4,11 +4,11 @@ import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction -abstract class AbstractOneToManyExposedKeyValueRepo( +abstract class AbstractExposedOneToManyKeyValueRepo( keyColumnAllocator: ColumnAllocator, valueColumnAllocator: ColumnAllocator, database: Database -) : OneToManyKeyValueRepo, AbstractOneToManyExposedReadKeyValueRepo( +) : OneToManyKeyValueRepo, AbstractExposedReadOneToManyKeyValueRepo( keyColumnAllocator, valueColumnAllocator, database @@ -30,3 +30,6 @@ abstract class AbstractOneToManyExposedKeyValueRepo( transaction(database) { deleteWhere { keyColumn.eq(k) } } } } + +@Deprecated("Renamed", ReplaceWith("AbstractExposedOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.AbstractExposedOneToManyKeyValueRepo")) +typealias AbstractOneToManyExposedKeyValueRepo = AbstractExposedOneToManyKeyValueRepo diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedReadKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadOneToManyKeyValueRepo.kt similarity index 77% rename from repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedReadKeyValueRepo.kt rename to repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadOneToManyKeyValueRepo.kt index 72ccc7ae84f..445ad25a04f 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractOneToManyExposedReadKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/AbstractExposedReadOneToManyKeyValueRepo.kt @@ -1,17 +1,17 @@ package dev.inmo.micro_utils.repos.exposed.onetomany import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.transaction typealias ColumnAllocator = Table.() -> Column -abstract class AbstractOneToManyExposedReadKeyValueRepo( +abstract class AbstractExposedReadOneToManyKeyValueRepo( keyColumnAllocator: ColumnAllocator, valueColumnAllocator: ColumnAllocator, protected val database: Database -) : OneToManyReadKeyValueRepo, Table() { +) : ReadOneToManyKeyValueRepo, Table() { protected val keyColumn: Column = keyColumnAllocator() protected val valueColumn: Column = valueColumnAllocator() @@ -45,3 +45,6 @@ abstract class AbstractOneToManyExposedReadKeyValueRepo( select { keyColumn.eq(k).and(valueColumn.eq(v)) }.limit(1).any() } } + +@Deprecated("Renamed", ReplaceWith("AbstractExposedReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.exposed.onetomany.AbstractExposedReadOneToManyKeyValueRepo")) +typealias AbstractOneToManyExposedReadKeyValueRepo = AbstractExposedReadOneToManyKeyValueRepo diff --git a/repos/inmemory/build.gradle b/repos/inmemory/build.gradle new file mode 100644 index 00000000000..162e4af700e --- /dev/null +++ b/repos/inmemory/build.gradle @@ -0,0 +1,17 @@ +plugins { + id "org.jetbrains.kotlin.multiplatform" + id "org.jetbrains.kotlin.plugin.serialization" +} + +apply from: "$mppProjectWithSerializationPresetPath" + +kotlin { + sourceSets { + commonMain { + dependencies { + api internalProject("micro_utils.repos.common") + api internalProject("micro_utils.coroutines") + } + } + } +} \ No newline at end of file diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt new file mode 100644 index 00000000000..a2194f0b96a --- /dev/null +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt @@ -0,0 +1,96 @@ +package dev.inmo.micro_utils.repos + +import dev.inmo.micro_utils.coroutines.BroadcastFlow +import dev.inmo.micro_utils.pagination.* +import kotlinx.coroutines.flow.Flow + +class ReadMapCRUDRepo( + private val map: Map = emptyMap() +) : ReadStandardCRUDRepo { + override suspend fun getByPagination(pagination: Pagination): PaginationResult { + return map.keys.drop(pagination.firstIndex).take(pagination.size).mapNotNull { + map[it] + }.createPaginationResult( + pagination, + count() + ) + } + + override suspend fun getById(id: IdType): ObjectType? = map[id] + + override suspend fun contains(id: IdType): Boolean = map.containsKey(id) + + override suspend fun count(): Long = map.size.toLong() +} + +abstract class WriteMapCRUDRepo( + private val map: MutableMap = mutableMapOf() +) : WriteStandardCRUDRepo { + private val _newObjectsFlow: BroadcastFlow = BroadcastFlow() + override val newObjectsFlow: Flow + get() = _newObjectsFlow + private val _updatedObjectsFlow: BroadcastFlow = BroadcastFlow() + override val updatedObjectsFlow: Flow + get() = _updatedObjectsFlow + private val _deletedObjectsIdsFlow: BroadcastFlow = BroadcastFlow() + override val deletedObjectsIdsFlow: Flow + get() = _deletedObjectsIdsFlow + + protected abstract suspend fun updateObject(newValue: InputValueType, id: IdType, old: ObjectType): ObjectType + protected abstract suspend fun createObject(newValue: InputValueType): Pair + + override suspend fun create(values: List): List { + return values.map { + val (id, newObject) = createObject(it) + map[id] = newObject + newObject.also { _ -> + _newObjectsFlow.send(newObject) + } + } + } + + override suspend fun update(id: IdType, value: InputValueType): ObjectType? { + val newValue = updateObject(value, id, map[id] ?: return null) + + return newValue.also { + map[id] = it + _updatedObjectsFlow.send(it) + } + } + + override suspend fun update(values: List>): List { + return values.mapNotNull { update(it.first, it.second) } + } + + override suspend fun deleteById(ids: List) { + ids.forEach { + map.remove(it) ?.also { _ -> _deletedObjectsIdsFlow.send(it) } + } + } + +} + +abstract class MapCRUDRepo( + map: MutableMap +) : StandardCRUDRepo, + ReadStandardCRUDRepo by ReadMapCRUDRepo(map), + WriteMapCRUDRepo(map) + +fun MapCRUDRepo( + map: MutableMap, + updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, + createCallback: suspend (newValue: InputValueType) -> Pair +) = object : MapCRUDRepo(map) { + override suspend fun updateObject( + newValue: InputValueType, + id: IdType, + old: ObjectType + ): ObjectType = updateCallback(newValue, id, old) + + override suspend fun createObject(newValue: InputValueType): Pair = createCallback(newValue) +} + +fun MutableMap.asCrudRepo( + updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, + createCallback: suspend (newValue: InputValueType) -> Pair +) = MapCRUDRepo(this, updateCallback, createCallback) 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 new file mode 100644 index 00000000000..506033e6c70 --- /dev/null +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt @@ -0,0 +1,74 @@ +package dev.inmo.micro_utils.repos + +import dev.inmo.micro_utils.coroutines.BroadcastFlow +import dev.inmo.micro_utils.pagination.* +import kotlinx.coroutines.flow.Flow + +class ReadMapKeyValueRepo( + private val map: Map = emptyMap() +) : ReadStandardKeyValueRepo { + override suspend fun get(k: Key): Value? = map[k] + + override suspend fun values( + pagination: Pagination, + reversed: Boolean + ): PaginationResult { + val firstIndex: Int = if (reversed) { + val size = map.size + (size - pagination.lastIndex).let { if (it < 0) 0 else it } + } else { + pagination.firstIndex + } + + return map.keys.drop(firstIndex).take(pagination.size).mapNotNull { map[it] }.createPaginationResult( + firstIndex, + count() + ) + } + + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult { + val firstIndex: Int = if (reversed) { + val size = map.size + (size - pagination.lastIndex).let { if (it < 0) 0 else it } + } else { + pagination.firstIndex + } + + return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult( + firstIndex, + count() + ) + } + + override suspend fun contains(key: Key): Boolean = map.containsKey(key) + + override suspend fun count(): Long = map.size.toLong() +} + +class WriteMapKeyValueRepo( + private val map: MutableMap = mutableMapOf() +) : WriteStandardKeyValueRepo { + private val _onNewValue: BroadcastFlow> = BroadcastFlow() + override val onNewValue: Flow> + get() = _onNewValue + private val _onValueRemoved: BroadcastFlow = BroadcastFlow() + override val onValueRemoved: Flow + get() = _onValueRemoved + + override suspend fun set(k: Key, v: Value) { + map[k] = v + _onNewValue.send(k to v) + } + + override suspend fun unset(k: Key) { + map.remove(k) ?.also { _onValueRemoved.send(k) } + } +} + +class MapKeyValueRepo( + private val map: MutableMap = mutableMapOf() +) : StandardKeyValueRepo, + ReadStandardKeyValueRepo by ReadMapKeyValueRepo(map), + WriteStandardKeyValueRepo by WriteMapKeyValueRepo(map) + +fun MutableMap.asKeyValueRepo(): StandardKeyValueRepo = MapKeyValueRepo(this) 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 new file mode 100644 index 00000000000..266f98c3baa --- /dev/null +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt @@ -0,0 +1,82 @@ +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 kotlinx.coroutines.flow.Flow + +class MapReadOneToManyKeyValueRepo( + private val map: Map> = emptyMap() +) : ReadOneToManyKeyValueRepo { + override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult { + val list = map[k] ?: return emptyPaginationResult() + + return list.paginate( + if (reversed) { + val firstIndex = (map.size - pagination.lastIndex).let { if (it < 0) 0 else it } + SimplePagination(firstIndex, pagination.size) + } else { + pagination + } + ) + } + + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult { + val firstIndex: Int = if (reversed) { + val size = map.size + (size - pagination.lastIndex).let { if (it < 0) 0 else it } + } else { + pagination.firstIndex + } + + return map.keys.drop(firstIndex).take(pagination.size).createPaginationResult( + firstIndex, + count() + ) + } + + override suspend fun contains(k: Key): Boolean = map.containsKey(k) + + override suspend fun contains(k: Key, v: Value): Boolean = map[k] ?.contains(v) == true + + override suspend fun count(k: Key): Long = map[k] ?.size ?.toLong() ?: 0L + + override suspend fun count(): Long = map.size.toLong() +} + +class MapWriteOneToManyKeyValueRepo( + private val map: MutableMap> = mutableMapOf() +) : WriteOneToManyKeyValueRepo { + private val _onNewValue: BroadcastFlow> = BroadcastFlow() + override val onNewValue: Flow> + get() = _onNewValue + private val _onValueRemoved: BroadcastFlow> = BroadcastFlow() + override val onValueRemoved: Flow> + get() = _onValueRemoved + private val _onDataCleared: BroadcastFlow = BroadcastFlow() + override val onDataCleared: Flow + get() = _onDataCleared + + override suspend fun add(k: Key, v: Value) { + map.getOrPut(k) { mutableListOf() }.add(v) + _onNewValue.send(k to v) + } + + override suspend fun remove(k: Key, v: Value) { + map[k] ?.remove(v) ?.also { _onValueRemoved.send(k to v) } + } + + override suspend fun clear(k: Key) { + map.remove(k) ?.also { _onDataCleared.send(k) } + } +} + +class MapOneToManyKeyValueRepo( + private val map: MutableMap> = mutableMapOf() +) : OneToManyKeyValueRepo, + ReadOneToManyKeyValueRepo by MapReadOneToManyKeyValueRepo(map), + WriteOneToManyKeyValueRepo by MapWriteOneToManyKeyValueRepo(map) + +fun MutableMap>.asOneToManyKeyValueRepo(): OneToManyKeyValueRepo = MapOneToManyKeyValueRepo( + map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap() +) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt index 082e72a83fb..1a3d3a2bf67 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadStandardCrudRepo.kt @@ -47,4 +47,12 @@ class KtorReadStandardCrudRepo ( ), Boolean.serializer() ) + + override suspend fun count(): Long = client.uniget( + buildStandardUrl( + baseUrl, + countRouting + ), + Long.serializer() + ) } diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartReadKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt similarity index 85% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartReadKeyValueRepo.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt index 588768223d3..f7c0af2e256 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartReadKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorReadStandardKeyValueRepo.kt @@ -6,19 +6,19 @@ import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.asUrlQueryParts -import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.* import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer -class KtorStandartReadKeyValueRepo ( +class KtorReadStandardKeyValueRepo ( private var baseUrl: String, private var client: HttpClient = HttpClient(), private var keySerializer: KSerializer, private var valueSerializer: KSerializer, private var valueNullableSerializer: KSerializer, -) : StandardReadKeyValueRepo { +) : ReadStandardKeyValueRepo { override suspend fun get(k: Key): Value? = client.uniget( buildStandardUrl( baseUrl, @@ -70,4 +70,7 @@ class KtorStandartReadKeyValueRepo ( ), Long.serializer() ) -} \ No newline at end of file +} + +@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/KtorStandartKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt index d81700e52f9..9c7164ab49f 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartKeyValueRepo.kt @@ -1,8 +1,8 @@ package dev.inmo.micro_utils.repos.ktor.client.key_value import dev.inmo.micro_utils.repos.StandardKeyValueRepo -import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo -import dev.inmo.micro_utils.repos.StandardWriteKeyValueRepo +import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo +import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import io.ktor.client.* import kotlinx.serialization.KSerializer @@ -14,14 +14,14 @@ class KtorStandartKeyValueRepo ( valueSerializer: KSerializer, valueNullableSerializer: KSerializer ) : StandardKeyValueRepo, - StandardReadKeyValueRepo by KtorStandartReadKeyValueRepo( + ReadStandardKeyValueRepo by KtorReadStandardKeyValueRepo( "$baseUrl/$baseSubpart", client, keySerializer, valueSerializer, valueNullableSerializer ), - StandardWriteKeyValueRepo by KtorStandartWriteKeyValueRepo( + WriteStandardKeyValueRepo by KtorWriteStandardKeyValueRepo( "$baseUrl/$baseSubpart", client, keySerializer, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartWriteKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt similarity index 80% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartWriteKeyValueRepo.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt index e8f11ecebc7..043f21155db 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorStandartWriteKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/KtorWriteStandardKeyValueRepo.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.ktor.client.BodyPair import dev.inmo.micro_utils.ktor.client.createStandardWebsocketFlow import dev.inmo.micro_utils.ktor.client.unipost import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.repos.StandardWriteKeyValueRepo +import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.client.* import kotlinx.coroutines.flow.Flow @@ -12,12 +12,12 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.PairSerializer import kotlinx.serialization.builtins.serializer -class KtorStandartWriteKeyValueRepo ( +class KtorWriteStandardKeyValueRepo ( private var baseUrl: String, private var client: HttpClient = HttpClient(), private var keySerializer: KSerializer, private var valueSerializer: KSerializer, -) : StandardWriteKeyValueRepo { +) : WriteStandardKeyValueRepo { override val onNewValue: Flow> = client.createStandardWebsocketFlow( buildStandardUrl(baseUrl, onNewValueRoute), deserializer = PairSerializer(keySerializer, valueSerializer) @@ -45,4 +45,7 @@ class KtorStandartWriteKeyValueRepo ( BodyPair(keySerializer, k), Unit.serializer() ) -} \ No newline at end of file +} + +@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/KtorOneToManyKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt index d6107824816..8dc20ae25ad 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyKeyValueRepo.kt @@ -1,8 +1,8 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many import dev.inmo.micro_utils.repos.OneToManyKeyValueRepo -import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo -import dev.inmo.micro_utils.repos.OneToManyWriteKeyValueRepo +import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo +import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer @@ -13,13 +13,13 @@ class KtorOneToManyKeyValueRepo( keySerializer: KSerializer, valueSerializer: KSerializer, ) : OneToManyKeyValueRepo, - OneToManyReadKeyValueRepo by KtorOneToManyReadKeyValueRepo ( + ReadOneToManyKeyValueRepo by KtorReadOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", client, keySerializer, valueSerializer, ), - OneToManyWriteKeyValueRepo by KtorOneToManyWriteKeyValueRepo ( + WriteOneToManyKeyValueRepo by KtorWriteOneToManyKeyValueRepo ( "$baseUrl/$baseSubpart", client, keySerializer, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyReadKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt similarity index 95% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyReadKeyValueRepo.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt index 0df6f253bf8..5dd4e23c9b8 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyReadKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorReadOneToManyKeyValueRepo.kt @@ -6,18 +6,18 @@ import dev.inmo.micro_utils.ktor.common.buildStandardUrl import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.asUrlQueryParts -import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.client.HttpClient import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer -class KtorOneToManyReadKeyValueRepo ( +class KtorReadOneToManyKeyValueRepo ( private val baseUrl: String, private val client: HttpClient = HttpClient(), private val keySerializer: KSerializer, private val valueSerializer: KSerializer, -) : OneToManyReadKeyValueRepo { +) : ReadOneToManyKeyValueRepo { private val paginationValueResultSerializer = PaginationResult.serializer(valueSerializer) private val paginationKeyResultSerializer = PaginationResult.serializer(keySerializer) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyWriteKeyValueRepo.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt similarity index 56% rename from repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyWriteKeyValueRepo.kt rename to repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt index d714855ae25..3022463da8a 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorOneToManyWriteKeyValueRepo.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/KtorWriteOneToManyKeyValueRepo.kt @@ -1,22 +1,36 @@ package dev.inmo.micro_utils.repos.ktor.client.one_to_many -import dev.inmo.micro_utils.ktor.client.BodyPair -import dev.inmo.micro_utils.ktor.client.unipost +import dev.inmo.micro_utils.coroutines.broadcastStateFlow +import dev.inmo.micro_utils.ktor.client.* import dev.inmo.micro_utils.ktor.common.buildStandardUrl -import dev.inmo.micro_utils.repos.OneToManyWriteKeyValueRepo +import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.client.HttpClient +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.Flow import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.PairSerializer import kotlinx.serialization.builtins.serializer -class KtorOneToManyWriteKeyValueRepo ( +class KtorWriteOneToManyKeyValueRepo ( private val baseUrl: String, private val client: HttpClient = HttpClient(), private val keySerializer: KSerializer, - private val valueSerializer: KSerializer, -) : OneToManyWriteKeyValueRepo { + private val valueSerializer: KSerializer +) : WriteOneToManyKeyValueRepo { private val keyValueSerializer = PairSerializer(keySerializer, valueSerializer) + override val onNewValue: Flow> = client.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onNewValueRoute), + deserializer = keyValueSerializer + ) + override val onValueRemoved: Flow> = client.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onValueRemovedRoute), + deserializer = keyValueSerializer + ) + override val onDataCleared: Flow = client.createStandardWebsocketFlow( + buildStandardUrl(baseUrl, onDataClearedRoute), + deserializer = keySerializer + ) override suspend fun add(k: Key, v: Value) = client.unipost( buildStandardUrl( diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt index dc44163c26e..00348cc409d 100644 --- a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/crud/CrudReadRoutes.kt @@ -3,3 +3,4 @@ package dev.inmo.micro_utils.repos.ktor.common.crud const val getByPaginationRouting = "getByPagination" const val getByIdRouting = "getById" const val containsRouting = "contains" +const val countRouting = "count" diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt index ed56f7ced53..637edd9bda1 100644 --- a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/one_to_many/OneToManyRoutes.kt @@ -7,6 +7,10 @@ const val containsByKeyValueRoute = "containsByKeyValue" const val countByKeyRoute = "countByKey" const val countRoute = "count" +const val onNewValueRoute = "onNewValue" +const val onValueRemovedRoute = "onValueRemoved" +const val onDataClearedRoute = "onDataCleared" + const val addRoute = "add" const val removeRoute = "remove" const val clearRoute = "clear" \ No newline at end of file diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt index d876840b033..c270f8e47b9 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadStandardCrudRepo.kt @@ -2,12 +2,10 @@ package dev.inmo.micro_utils.repos.ktor.server.crud import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError import dev.inmo.micro_utils.ktor.server.unianswer -import dev.inmo.micro_utils.repos.ktor.common.crud.containsRouting -import dev.inmo.micro_utils.repos.ktor.common.crud.getByIdRouting -import dev.inmo.micro_utils.repos.ktor.common.crud.getByPaginationRouting import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadStandardCRUDRepo +import dev.inmo.micro_utils.repos.ktor.common.crud.* import io.ktor.application.call import io.ktor.routing.Route import io.ktor.routing.get @@ -54,4 +52,11 @@ fun Route.configureReadStandardCrudRepoRoutes( originalRepo.contains(id) ) } + + get(countRouting) { + call.unianswer( + Long.serializer(), + originalRepo.count() + ) + } } diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt index d55d3a57b63..ab52eba53f0 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartReadKeyValueRepo.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError import dev.inmo.micro_utils.ktor.server.unianswer import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.StandardReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.application.* import io.ktor.routing.* @@ -12,7 +12,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer fun Route.configureReadStandartKeyValueRepoRoutes ( - originalRepo: StandardReadKeyValueRepo, + originalRepo: ReadStandardKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, valueNullableSerializer: KSerializer, diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt index e075249b1bc..249bcbc4741 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key_value/KtorStandartWriteKeyValueRepo.kt @@ -2,7 +2,7 @@ package dev.inmo.micro_utils.repos.ktor.server.key_value import dev.inmo.micro_utils.ktor.server.includeWebsocketHandling import dev.inmo.micro_utils.ktor.server.uniload -import dev.inmo.micro_utils.repos.StandardWriteKeyValueRepo +import dev.inmo.micro_utils.repos.WriteStandardKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.key_value.* import io.ktor.application.* import io.ktor.routing.* @@ -10,7 +10,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.PairSerializer fun Route.configureWriteStandartKeyValueRepoRoutes ( - originalRepo: StandardWriteKeyValueRepo, + originalRepo: WriteStandardKeyValueRepo, keySerializer: KSerializer, valueSerializer: KSerializer, ) { diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt index 0ef8beaeca0..07976b2e78e 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyReadKeyValueRepoRoutes.kt @@ -5,7 +5,7 @@ import dev.inmo.micro_utils.ktor.server.decodeUrlQueryValueOrSendError import dev.inmo.micro_utils.ktor.server.unianswer import dev.inmo.micro_utils.pagination.PaginationResult import dev.inmo.micro_utils.pagination.extractPagination -import dev.inmo.micro_utils.repos.OneToManyReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.application.call import io.ktor.routing.Route @@ -14,7 +14,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.builtins.serializer fun Route.configureOneToManyReadKeyValueRepoRoutes( - originalRepo: OneToManyReadKeyValueRepo, + originalRepo: ReadOneToManyKeyValueRepo, keySerializer: KSerializer, valueSealizer: KSerializer, ) { diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt index f39e0ee3b48..788ea78a16a 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/one_to_many/ConfigureOneToManyWriteKeyValueRepoRoutes.kt @@ -1,8 +1,7 @@ package dev.inmo.micro_utils.repos.ktor.server.one_to_many -import dev.inmo.micro_utils.ktor.server.unianswer -import dev.inmo.micro_utils.ktor.server.uniload -import dev.inmo.micro_utils.repos.OneToManyWriteKeyValueRepo +import dev.inmo.micro_utils.ktor.server.* +import dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo import dev.inmo.micro_utils.repos.ktor.common.one_to_many.* import io.ktor.application.call import io.ktor.routing.Route @@ -12,12 +11,28 @@ import kotlinx.serialization.builtins.PairSerializer import kotlinx.serialization.builtins.serializer fun Route.configureOneToManyWriteKeyValueRepoRoutes( - originalRepo: OneToManyWriteKeyValueRepo, + originalRepo: WriteOneToManyKeyValueRepo, keySerializer: KSerializer, valueSealizer: KSerializer, ) { val keyValueSerializer = PairSerializer(keySerializer, valueSealizer) + includeWebsocketHandling( + onNewValueRoute, + originalRepo.onNewValue, + keyValueSerializer + ) + includeWebsocketHandling( + onValueRemovedRoute, + originalRepo.onValueRemoved, + keyValueSerializer + ) + includeWebsocketHandling( + onDataClearedRoute, + originalRepo.onDataCleared, + keySerializer + ) + post(addRoute) { val obj = call.uniload( keyValueSerializer diff --git a/settings.gradle b/settings.gradle index 333d1080497..54fb4ed3371 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,6 +9,7 @@ String[] includes = [ ":mime_types", ":repos:common", ":repos:exposed", + ":repos:inmemory", ":repos:ktor:client", ":repos:ktor:common", ":repos:ktor:server",