From 0ba93d0e60807396849721a00f83d4ad3efa43fa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 14:22:58 +0600 Subject: [PATCH 1/9] add in memory map based crud implementation --- .../pagination/utils/IterableExtensions.kt | 35 ++++++++ repos/inmemory/build.gradle | 17 ++++ .../inmo/micro_utils/repos/HashMapCRUDRepo.kt | 89 +++++++++++++++++++ settings.gradle | 1 + 4 files changed, 142 insertions(+) create mode 100644 pagination/common/src/commonMain/kotlin/dev/inmo/micro_utils/pagination/utils/IterableExtensions.kt create mode 100644 repos/inmemory/build.gradle create mode 100644 repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/HashMapCRUDRepo.kt 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/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/HashMapCRUDRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/HashMapCRUDRepo.kt new file mode 100644 index 00000000000..9815088d9ca --- /dev/null +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/HashMapCRUDRepo.kt @@ -0,0 +1,89 @@ +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 MapCRUDRepo( + 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, + map.size.toLong() + ) + } + + override suspend fun getById(id: IdType): ObjectType? = map[id] + + override suspend fun contains(id: IdType): Boolean = map.containsKey(id) +} + +abstract class MutableMapCRUDRepo( + 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 HashMapCRUDRepo( + map: MutableMap +) : StandardCRUDRepo, + ReadStandardCRUDRepo by MapCRUDRepo(map), + MutableMapCRUDRepo(map) + +fun HashMapCRUDRepo( + map: MutableMap, + updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, + createCallback: suspend (newValue: InputValueType) -> Pair +) = object : HashMapCRUDRepo(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) +} 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", From af207d78a4fde8bcd80ec0dfd50de259be439d1c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 17:38:03 +0600 Subject: [PATCH 2/9] renames --- .../repos/{HashMapCRUDRepo.kt => MapCRUDRepo.kt} | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) rename repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/{HashMapCRUDRepo.kt => MapCRUDRepo.kt} (87%) diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/HashMapCRUDRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt similarity index 87% rename from repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/HashMapCRUDRepo.kt rename to repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt index 9815088d9ca..fcd14d7e3f4 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/HashMapCRUDRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt @@ -4,7 +4,7 @@ import dev.inmo.micro_utils.coroutines.BroadcastFlow import dev.inmo.micro_utils.pagination.* import kotlinx.coroutines.flow.Flow -class MapCRUDRepo( +class ReadMapCRUDRepo( private val map: Map = emptyMap() ) : ReadStandardCRUDRepo { override suspend fun getByPagination(pagination: Pagination): PaginationResult { @@ -21,7 +21,7 @@ class MapCRUDRepo( override suspend fun contains(id: IdType): Boolean = map.containsKey(id) } -abstract class MutableMapCRUDRepo( +abstract class WriteMapCRUDRepo( private val map: MutableMap = mutableMapOf() ) : WriteStandardCRUDRepo { private val _newObjectsFlow: BroadcastFlow = BroadcastFlow() @@ -68,17 +68,17 @@ abstract class MutableMapCRUDRepo( } -abstract class HashMapCRUDRepo( +abstract class MapCRUDRepo( map: MutableMap ) : StandardCRUDRepo, - ReadStandardCRUDRepo by MapCRUDRepo(map), - MutableMapCRUDRepo(map) + ReadStandardCRUDRepo by ReadMapCRUDRepo(map), + WriteMapCRUDRepo(map) -fun HashMapCRUDRepo( +fun MapCRUDRepo( map: MutableMap, updateCallback: suspend (newValue: InputValueType, id: IdType, old: ObjectType) -> ObjectType, createCallback: suspend (newValue: InputValueType) -> Pair -) = object : HashMapCRUDRepo(map) { +) = object : MapCRUDRepo(map) { override suspend fun updateObject( newValue: InputValueType, id: IdType, From e78961b597e0443a29c4048466b0afcd8638596c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 18:13:40 +0600 Subject: [PATCH 3/9] add MapKeyValueRepo.kt --- .../inmo/micro_utils/pagination/Pagination.kt | 5 ++ .../pagination/PaginationResult.kt | 13 ++++ .../micro_utils/repos/StandartCRUDRepo.kt | 1 + .../exposed/AbstractExposedReadCRUDRepo.kt | 2 + .../dev/inmo/micro_utils/repos/MapCRUDRepo.kt | 4 +- .../inmo/micro_utils/repos/MapKeyValueRepo.kt | 74 +++++++++++++++++++ .../client/crud/KtorReadStandardCrudRepo.kt | 8 ++ .../repos/ktor/common/crud/CrudReadRoutes.kt | 1 + .../server/crud/KtorReadStandardCrudRepo.kt | 11 ++- 9 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt 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..0bd7a87ace3 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 @@ -50,3 +50,8 @@ 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 = firstIndex / resultsSize 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..608f0aaf750 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 @@ -23,6 +23,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/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/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/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt index fcd14d7e3f4..96bbbfa18b2 100644 --- 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 @@ -12,13 +12,15 @@ class ReadMapCRUDRepo( map[it] }.createPaginationResult( pagination, - map.size.toLong() + 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( 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..ef86cf9ec7a --- /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() +) : StandardReadKeyValueRepo { + 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() +) : StandardWriteKeyValueRepo { + 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, + StandardReadKeyValueRepo by ReadMapKeyValueRepo(map), + StandardWriteKeyValueRepo by WriteMapKeyValueRepo(map) + +fun MutableMap.asKeyValueRepo(): StandardKeyValueRepo = MapKeyValueRepo(this) 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/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/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() + ) + } } From 8461ff4d386aff331b51c003c92b3166b6ce343a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 18:16:58 +0600 Subject: [PATCH 4/9] fix in calculating page fun --- .../kotlin/dev/inmo/micro_utils/pagination/Pagination.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 0bd7a87ace3..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 @@ -54,4 +55,8 @@ fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int = /** * @return calculated page number which can be correctly used in [PaginationResult] as [PaginationResult.page] value */ -fun calculatePage(firstIndex: Int, resultsSize: Int): Int = firstIndex / resultsSize +fun calculatePage(firstIndex: Int, resultsSize: Int): Int = if (resultsSize > 0) { + floor(firstIndex.toFloat() / resultsSize).toInt() +} else { + 0 +} From e22595339e1c5eeffceee8c2ee2d6173e0d5cc1c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 19:37:25 +0600 Subject: [PATCH 5/9] add MapOneToManyKeyValueRepo --- .../pagination/PaginationResult.kt | 2 + .../repos/OneToManyKeyValueRepo.kt | 5 ++ .../repos/MapOneToManyKeyValueRepo.kt | 82 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt 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 608f0aaf750..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 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..71d34a5b5ef 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,6 +2,7 @@ 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 { suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult @@ -13,6 +14,10 @@ interface OneToManyReadKeyValueRepo : Repo { } interface OneToManyWriteKeyValueRepo : Repo { + val onNewValue: Flow> + val onValueRemoved: Flow> + val onDataCleared: Flow + suspend fun add(k: Key, v: Value) suspend fun remove(k: Key, v: Value) suspend fun clear(k: Key) 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..2b182615901 --- /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 ReadOneToManyKeyValueRepo( + private val map: Map> = emptyMap() +) : OneToManyReadKeyValueRepo { + 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 WriteOneToManyKeyValueRepo( + private val map: MutableMap> = mutableMapOf() +) : OneToManyWriteKeyValueRepo { + 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, + OneToManyReadKeyValueRepo by ReadOneToManyKeyValueRepo(map), + OneToManyWriteKeyValueRepo by WriteOneToManyKeyValueRepo(map) + +fun MutableMap>.asOneToManyKeyValueRepo(): OneToManyKeyValueRepo = MapOneToManyKeyValueRepo( + map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap() +) From 3c393fdd216613c860490afea495f74adb594e9b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 21:31:26 +0600 Subject: [PATCH 6/9] add factory for MapCRUDRepo --- .../kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt | 5 +++++ 1 file changed, 5 insertions(+) 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 index 96bbbfa18b2..a2194f0b96a 100644 --- 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 @@ -89,3 +89,8 @@ fun MapCRUDRepo( 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) From de97872d4194b2b9ca38e0250722f259cc1dd2e2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 21:41:51 +0600 Subject: [PATCH 7/9] fill CHANGELOG.md --- CHANGELOG.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72b30b38d50..a4a6ae03ac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,16 @@ 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` * Extensions `doForAll` and `getAll` were added for all current types of repos: @@ -15,3 +25,13 @@ Inited :) * `StandardReadKeyValueRepo` * `OneToManyReadKeyValueRepo` * `StandardReadKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false` + * New `Flow`'s in `OneToManyWriteKeyValueRepo`: + * `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 From 22f5a59dfadb73f3ebd50c153a6fe0904af86bf3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 22:02:10 +0600 Subject: [PATCH 8/9] update onetomany naming and changelog --- CHANGELOG.md | 10 ++++--- .../repos/OneToManyKeyValueRepo.kt | 10 ++++--- .../OneToManyPaginationExtensions.kt | 6 ++--- ...> AbstractExposedOneToManyKeyValueRepo.kt} | 7 +++-- ...stractExposedReadOneToManyKeyValueRepo.kt} | 9 ++++--- .../repos/MapOneToManyKeyValueRepo.kt | 12 ++++----- .../one_to_many/KtorOneToManyKeyValueRepo.kt | 8 +++--- ...po.kt => KtorReadOneToManyKeyValueRepo.kt} | 6 ++--- ...o.kt => KtorWriteOneToManyKeyValueRepo.kt} | 26 ++++++++++++++----- .../common/one_to_many/OneToManyRoutes.kt | 4 +++ ...onfigureOneToManyReadKeyValueRepoRoutes.kt | 4 +-- ...nfigureOneToManyWriteKeyValueRepoRoutes.kt | 23 +++++++++++++--- 12 files changed, 85 insertions(+), 40 deletions(-) rename repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/{AbstractOneToManyExposedKeyValueRepo.kt => AbstractExposedOneToManyKeyValueRepo.kt} (66%) rename repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/onetomany/{AbstractOneToManyExposedReadKeyValueRepo.kt => AbstractExposedReadOneToManyKeyValueRepo.kt} (77%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/{KtorOneToManyReadKeyValueRepo.kt => KtorReadOneToManyKeyValueRepo.kt} (95%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/one_to_many/{KtorOneToManyWriteKeyValueRepo.kt => KtorWriteOneToManyKeyValueRepo.kt} (56%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4a6ae03ac1..a00799ecb03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,12 +20,14 @@ Inited :) * `Set` * `Repos` * `Common` + * Interfaces related to `OneToManyKeyValueRepo` 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` - * New `Flow`'s in `OneToManyWriteKeyValueRepo`: + * `ReadStandardKeyValueRepo` + * `ReadOneToManyKeyValueRepo` + * `ReadStandardKeyValueRepo` methods `values` and `keys` now have default value for `reversed` parameter `false` + * New `Flow`'s in `WriteOneToManyKeyValueRepo`: * `onNewValue` * `onValueRemoved` * `onDataCleared` 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 71d34a5b5ef..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 @@ -4,7 +4,7 @@ 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 @@ -12,8 +12,10 @@ 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 OneToManyWriteKeyValueRepo : Repo { +interface WriteOneToManyKeyValueRepo : Repo { val onNewValue: Flow> val onValueRemoved: Flow> val onDataCleared: Flow @@ -22,5 +24,7 @@ interface OneToManyWriteKeyValueRepo : Repo { 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/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/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/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapOneToManyKeyValueRepo.kt index 2b182615901..266f98c3baa 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,9 +5,9 @@ import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.paginate import kotlinx.coroutines.flow.Flow -class ReadOneToManyKeyValueRepo( +class MapReadOneToManyKeyValueRepo( private val map: Map> = emptyMap() -) : OneToManyReadKeyValueRepo { +) : ReadOneToManyKeyValueRepo { override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult { val list = map[k] ?: return emptyPaginationResult() @@ -44,9 +44,9 @@ class ReadOneToManyKeyValueRepo( override suspend fun count(): Long = map.size.toLong() } -class WriteOneToManyKeyValueRepo( +class MapWriteOneToManyKeyValueRepo( private val map: MutableMap> = mutableMapOf() -) : OneToManyWriteKeyValueRepo { +) : WriteOneToManyKeyValueRepo { private val _onNewValue: BroadcastFlow> = BroadcastFlow() override val onNewValue: Flow> get() = _onNewValue @@ -74,8 +74,8 @@ class WriteOneToManyKeyValueRepo( class MapOneToManyKeyValueRepo( private val map: MutableMap> = mutableMapOf() ) : OneToManyKeyValueRepo, - OneToManyReadKeyValueRepo by ReadOneToManyKeyValueRepo(map), - OneToManyWriteKeyValueRepo by WriteOneToManyKeyValueRepo(map) + 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/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/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/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 From 8f522509c05590d74ef72be743d0a443b9fd7ae5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 14 Oct 2020 22:29:03 +0600 Subject: [PATCH 9/9] renames in keyvalue --- CHANGELOG.md | 2 ++ .../inmo/micro_utils/repos/StandartKeyValueRepo.kt | 6 +++--- .../repos/pagination/KeyValuePaginationExtensions.kt | 6 +++--- .../keyvalue/AbstractExposedReadKeyValueRepo.kt | 4 ++-- .../dev/inmo/micro_utils/repos/MapKeyValueRepo.kt | 8 ++++---- ...eyValueRepo.kt => KtorReadStandardKeyValueRepo.kt} | 11 +++++++---- .../ktor/client/key_value/KtorStandartKeyValueRepo.kt | 8 ++++---- ...yValueRepo.kt => KtorWriteStandardKeyValueRepo.kt} | 11 +++++++---- .../server/key_value/KtorStandartReadKeyValueRepo.kt | 4 ++-- .../server/key_value/KtorStandartWriteKeyValueRepo.kt | 4 ++-- 10 files changed, 36 insertions(+), 28 deletions(-) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/{KtorStandartReadKeyValueRepo.kt => KtorReadStandardKeyValueRepo.kt} (85%) rename repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key_value/{KtorStandartWriteKeyValueRepo.kt => KtorWriteStandardKeyValueRepo.kt} (80%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a00799ecb03..6cf5619712d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ Inited :) * `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` * `ReadStandardKeyValueRepo` 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/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/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 ef86cf9ec7a..506033e6c70 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 @@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.Flow class ReadMapKeyValueRepo( private val map: Map = emptyMap() -) : StandardReadKeyValueRepo { +) : ReadStandardKeyValueRepo { override suspend fun get(k: Key): Value? = map[k] override suspend fun values( @@ -47,7 +47,7 @@ class ReadMapKeyValueRepo( class WriteMapKeyValueRepo( private val map: MutableMap = mutableMapOf() -) : StandardWriteKeyValueRepo { +) : WriteStandardKeyValueRepo { private val _onNewValue: BroadcastFlow> = BroadcastFlow() override val onNewValue: Flow> get() = _onNewValue @@ -68,7 +68,7 @@ class WriteMapKeyValueRepo( class MapKeyValueRepo( private val map: MutableMap = mutableMapOf() ) : StandardKeyValueRepo, - StandardReadKeyValueRepo by ReadMapKeyValueRepo(map), - StandardWriteKeyValueRepo by WriteMapKeyValueRepo(map) + ReadStandardKeyValueRepo by ReadMapKeyValueRepo(map), + WriteStandardKeyValueRepo by WriteMapKeyValueRepo(map) fun MutableMap.asKeyValueRepo(): StandardKeyValueRepo = MapKeyValueRepo(this) 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/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, ) {