From f1678ef7cf5576eb0131bb63a61b31f915a4e4c1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 15:36:30 +0600 Subject: [PATCH 1/9] start 0.17.5 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f698e2d57ee..5fbbfc661b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.17.5 + ## 0.17.4 * `Serialization`: diff --git a/gradle.properties b/gradle.properties index 79aae800ee7..9fbde391eee 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,5 +14,5 @@ crypto_js_version=4.1.1 # Project data group=dev.inmo -version=0.17.4 -android_code_version=186 +version=0.17.5 +android_code_version=187 From 0e21699cd130c5e223982ff3047159476bb8fe59 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 15:45:02 +0600 Subject: [PATCH 2/9] several updates --- CHANGELOG.md | 6 ++++ .../common/PrimitivesConversations.kt | 28 +++++++++++++++++++ .../KeyValuePaginationExtensions.kt | 9 +++++- 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PrimitivesConversations.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fbbfc661b5..1df2f2c7799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.17.5 +* `Common`: + * Conversations of number primitives with bounds care +* `Repos`: + * `Common`: + * By default, `getAll` for repos will take all the size of repo as page size + ## 0.17.4 * `Serialization`: diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PrimitivesConversations.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PrimitivesConversations.kt new file mode 100644 index 00000000000..fb3a39d19e8 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/PrimitivesConversations.kt @@ -0,0 +1,28 @@ +package dev.inmo.micro_utils.common + +/** + * Convert [this] [Long] to [Int] with bounds of [Int.MIN_VALUE] and [Int.MAX_VALUE] + */ +fun Long.toCoercedInt(): Int = coerceIn(Int.MIN_VALUE.toLong(), Int.MAX_VALUE.toLong()).toInt() +/** + * Convert [this] [Long] to [Short] with bounds of [Short.MIN_VALUE] and [Short.MAX_VALUE] + */ +fun Long.toCoercedShort(): Short = coerceIn(Short.MIN_VALUE.toLong(), Short.MAX_VALUE.toLong()).toShort() +/** + * Convert [this] [Long] to [Byte] with bounds of [Byte.MIN_VALUE] and [Byte.MAX_VALUE] + */ +fun Long.toCoercedByte(): Byte = coerceIn(Byte.MIN_VALUE.toLong(), Byte.MAX_VALUE.toLong()).toByte() + +/** + * Convert [this] [Int] to [Short] with bounds of [Short.MIN_VALUE] and [Short.MAX_VALUE] + */ +fun Int.toCoercedShort(): Short = coerceIn(Short.MIN_VALUE.toInt(), Short.MAX_VALUE.toInt()).toShort() +/** + * Convert [this] [Int] to [Byte] with bounds of [Byte.MIN_VALUE] and [Byte.MAX_VALUE] + */ +fun Int.toCoercedByte(): Byte = coerceIn(Byte.MIN_VALUE.toInt(), Byte.MAX_VALUE.toInt()).toByte() + +/** + * Convert [this] [Short] to [Byte] with bounds of [Byte.MIN_VALUE] and [Byte.MAX_VALUE] + */ +fun Short.toCoercedByte(): Byte = coerceIn(Byte.MIN_VALUE.toShort(), Byte.MAX_VALUE.toShort()).toByte() 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 14fe3068416..1af608a9d54 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 @@ -1,15 +1,22 @@ package dev.inmo.micro_utils.repos.pagination +import dev.inmo.micro_utils.common.toCoercedInt import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.ReadKeyValueRepo suspend inline fun > REPO.getAll( + pagination: Pagination, @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult -): List> = getAllWithNextPaging { +): List> = getAllWithNextPaging(pagination) { val result = methodCaller(it) result.changeResultsUnchecked( result.results.mapNotNull { it to (get(it) ?: return@mapNotNull null) } ) } + +suspend inline fun > REPO.getAll( + @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") + crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult +): List> = getAll(FirstPagePagination(count().toCoercedInt()), methodCaller) From 486515eddde7e9aa4356239464f2ed83d0de67b7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 16:09:33 +0600 Subject: [PATCH 3/9] update dokka --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f066efbc714..141d7dbb4b8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,7 +8,7 @@ kslog = "1.0.0" jb-compose = "1.3.1" jb-exposed = "0.41.1" -jb-dokka = "1.7.20" +jb-dokka = "1.8.10" klock = "3.4.0" uuid = "0.7.0" From afc6aeea159d88a076e1f975ef63c8c5c99c1b16 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 16:14:51 +0600 Subject: [PATCH 4/9] fixes --- .../repos/pagination/CRUDPaginationExtensions.kt | 9 ++++++++- .../repos/pagination/OneToManyPaginationExtensions.kt | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt index 52bb8231a54..45b2c86c497 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt @@ -1,12 +1,19 @@ package dev.inmo.micro_utils.repos.pagination +import dev.inmo.micro_utils.common.toCoercedInt import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.ReadCRUDRepo suspend inline fun > REPO.getAll( + pagination: Pagination, @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult -): List = getAllWithNextPaging { +): List = getAllWithNextPaging(pagination) { methodCaller(this, it) } + +suspend inline fun > REPO.getAll( + @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") + crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult +): List = getAll(FirstPagePagination(count().toCoercedInt()), methodCaller) 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 5e825390297..ca14bfd4312 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 @@ -1,13 +1,15 @@ package dev.inmo.micro_utils.repos.pagination +import dev.inmo.micro_utils.common.toCoercedInt import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.repos.ReadKeyValuesRepo suspend inline fun > REPO.getAll( + pagination: Pagination, @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult -): List>> = getAllWithNextPaging { +): List>> = getAllWithNextPaging(pagination) { val keysResult = methodCaller(it) keysResult.changeResultsUnchecked( keysResult.results.map { k -> @@ -15,3 +17,8 @@ suspend inline fun > REPO.getAl } ) } + +suspend inline fun > REPO.getAll( + @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") + crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult +): List>> = getAll(FirstPagePagination(count().toCoercedInt()), methodCaller) From eeebbff70dfea7bb91d5fdb233f1074a0f9f6323 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 18:37:48 +0600 Subject: [PATCH 5/9] getAll --- CHANGELOG.md | 2 ++ .../inmo/micro_utils/repos/cache/CRUDCacheRepo.kt | 7 +++++++ .../micro_utils/repos/cache/KeyValueCacheRepo.kt | 6 ++++++ .../cache/fallback/crud/AutoRecacheReadCRUDRepo.kt | 8 ++++++++ .../fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt | 2 ++ .../repos/cache/full/FullCRUDCacheRepo.kt | 6 ++++++ .../repos/cache/full/FullKeyValueCacheRepo.kt | 7 ++++++- .../dev/inmo/micro_utils/repos/KeyValueRepo.kt | 12 ++++++++++++ .../dev/inmo/micro_utils/repos/StandartCRUDRepo.kt | 11 +++++++++++ .../inmo/micro_utils/repos/mappers/CRUDMappers.kt | 4 ++++ .../micro_utils/repos/mappers/KeyValueMappers.kt | 4 ++++ .../repos/pagination/CRUDPaginationExtensions.kt | 2 +- .../repos/pagination/KeyValuePaginationExtensions.kt | 2 +- .../repos/pagination/MaxPagePagination.kt | 11 +++++++++++ .../pagination/OneToManyPaginationExtensions.kt | 2 +- .../transforms/crud/ReadCRUDFromKeyValueRepo.kt | 2 ++ .../repos/transforms/kv/ReadKeyValueFromCRUDRepo.kt | 3 +++ .../transforms/kv/ReadKeyValueFromKeyValuesRepo.kt | 2 ++ .../dev/inmo/micro_utils/repos/FileKeyValueRepo.kt | 3 +++ .../repos/crud/AbstractAndroidCRUDRepo.kt | 12 ++++++++++++ .../inmo/micro_utils/repos/keyvalue/KeyValueStore.kt | 9 +++++++++ .../repos/exposed/AbstractExposedReadCRUDRepo.kt | 4 ++++ .../keyvalue/AbstractExposedReadKeyValueRepo.kt | 3 ++- .../kotlin/dev/inmo/micro_utils/repos/MapCRUDRepo.kt | 2 ++ .../dev/inmo/micro_utils/repos/MapKeyValueRepo.kt | 2 ++ .../repos/ktor/client/crud/KtorReadCRUDRepoClient.kt | 10 ++++++++++ .../client/key/value/KtorReadKeyValueRepoClient.kt | 9 +++++++++ .../micro_utils/repos/ktor/common/GetAllRoute.kt | 3 +++ .../repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt | 5 +++++ .../server/key/value/KtorReadKeyValueRepoRoutes.kt | 4 ++++ 30 files changed, 154 insertions(+), 5 deletions(-) create mode 100644 repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/MaxPagePagination.kt create mode 100644 repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/GetAllRoute.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 1df2f2c7799..581aef5e6a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ * `Repos`: * `Common`: * By default, `getAll` for repos will take all the size of repo as page size + * New extension for all built-in repos `maxPagePagination` + * All the repos got `getAll` functions ## 0.17.4 diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt index 264e727ed1c..4b58af9f4e2 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CRUDCacheRepo.kt @@ -2,6 +2,7 @@ package dev.inmo.micro_utils.repos.cache import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.cache.cache.KVCache +import dev.inmo.micro_utils.repos.cache.util.actualizeAll import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.* @@ -15,6 +16,12 @@ open class ReadCRUDCacheRepo( kvCache.set(id, it) }) + override suspend fun getAll(): Map { + return kvCache.getAll().takeIf { it.size.toLong() == count() } ?: parentRepo.getAll().also { + kvCache.actualizeAll(true) { it } + } + } + override suspend fun contains(id: IdType): Boolean = kvCache.contains(id) || parentRepo.contains(id) override suspend fun invalidate() = kvCache.clear() diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt index 8b9c36196d1..53203664e43 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt @@ -24,6 +24,12 @@ open class ReadKeyValueCacheRepo( } } + override suspend fun getAll(): Map = kvCache.getAll().takeIf { + it.size.toLong() == count() + } ?: parentRepo.getAll().also { + kvCache.set(it) + } + override suspend fun invalidate() = kvCache.clear() } diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/crud/AutoRecacheReadCRUDRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/crud/AutoRecacheReadCRUDRepo.kt index edca199fe97..7b1b915e240 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/crud/AutoRecacheReadCRUDRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/crud/AutoRecacheReadCRUDRepo.kt @@ -52,6 +52,14 @@ open class AutoRecacheReadCRUDRepo( kvCache.contains(id) } + override suspend fun getAll(): Map = actionWrapper.wrap { + originalRepo.getAll() + }.onSuccess { + kvCache.actualizeAll(clear = true) { it } + }.getOrElse { + kvCache.getAll() + } + override suspend fun count(): Long = actionWrapper.wrap { originalRepo.count() }.getOrElse { diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt index 74711f920c0..4a530047a6b 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt @@ -52,6 +52,8 @@ open class AutoRecacheReadKeyValueRepo( kvCache.contains(key) } + override suspend fun getAll(): Map = kvCache.getAll() + override suspend fun count(): Long = actionWrapper.wrap { originalRepo.count() }.getOrElse { diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt index 0b18d436001..7d8c236c6ad 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt @@ -58,6 +58,12 @@ open class FullReadCRUDCacheRepo( { if (it) parentRepo.getById(id) ?.let { set(id, it) } } ) + override suspend fun getAll(): Map = doOrTakeAndActualize( + { getAll().takeIf { it.isNotEmpty() }.optionalOrAbsentIfNull }, + { getAll() }, + { kvCache.actualizeAll(clear = true) { it } } + ) + override suspend fun getById(id: IdType): ObjectType? = doOrTakeAndActualize( { get(id) ?.optional ?: Optional.absent() }, { getById(id) }, diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt index 81927bb1b80..8a985d017be 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt @@ -9,7 +9,6 @@ import dev.inmo.micro_utils.repos.cache.util.actualizeAll import dev.inmo.micro_utils.repos.pagination.getAll import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job import kotlinx.coroutines.flow.* open class FullReadKeyValueCacheRepo( @@ -59,6 +58,12 @@ open class FullReadKeyValueCacheRepo( { if (it) parentRepo.get(key) ?.also { kvCache.set(key, it) } } ) + override suspend fun getAll(): Map = doOrTakeAndActualize( + { getAll().takeIf { it.isNotEmpty() }.optionalOrAbsentIfNull }, + { getAll() }, + { kvCache.actualizeAll(clear = true) { it } } + ) + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = doOrTakeAndActualize( { keys(pagination, reversed).takeIf { it.results.isNotEmpty() }.optionalOrAbsentIfNull }, { keys(pagination, reversed) }, diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt index 97298e6d234..e789865423f 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/KeyValueRepo.kt @@ -2,8 +2,10 @@ package dev.inmo.micro_utils.repos import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.doAllWithCurrentPaging +import dev.inmo.micro_utils.pagination.utils.getAllByWithNextPaging import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging import dev.inmo.micro_utils.pagination.utils.paginate +import dev.inmo.micro_utils.repos.pagination.maxPagePagination import kotlinx.coroutines.flow.Flow /** @@ -51,6 +53,16 @@ interface ReadKeyValueRepo : Repo { */ suspend fun contains(key: Key): Boolean + suspend fun getAll(): Map = getAllByWithNextPaging(maxPagePagination()) { + keys(it).let { + it.changeResultsUnchecked( + it.results.mapNotNull { + it to (get(it) ?: return@mapNotNull null) + } + ) + } + }.toMap() + /** * @return count of all collection objects */ 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 5af7ec929fc..8ea1b8e8f92 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 @@ -2,6 +2,9 @@ package dev.inmo.micro_utils.repos import dev.inmo.micro_utils.pagination.Pagination import dev.inmo.micro_utils.pagination.PaginationResult +import dev.inmo.micro_utils.pagination.changeResultsUnchecked +import dev.inmo.micro_utils.pagination.utils.getAllWithCurrentPaging +import dev.inmo.micro_utils.repos.pagination.maxPagePagination import kotlinx.coroutines.flow.Flow interface ReadCRUDRepo : Repo { @@ -9,6 +12,14 @@ interface ReadCRUDRepo : Repo { suspend fun getIdsByPagination(pagination: Pagination): PaginationResult suspend fun getById(id: IdType): ObjectType? suspend fun contains(id: IdType): Boolean + suspend fun getAll(): Map = getAllWithCurrentPaging(maxPagePagination()) { + getIdsByPagination(it).let { + it.changeResultsUnchecked( + it.results.mapNotNull { it to (getById(it) ?: return@mapNotNull null) } + ) + } + }.toMap() + suspend fun count(): Long } typealias ReadStandardCRUDRepo = ReadCRUDRepo diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt index 4fb74e91f59..e13e4dd8bcd 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/CRUDMappers.kt @@ -32,6 +32,10 @@ open class MapperReadCRUDRepo( override suspend fun contains(id: FromId): Boolean = to.contains(id.toOutKey()) + override suspend fun getAll(): Map = to.getAll().asSequence().associate { (k, v) -> + k.toInnerKey() to v.toInnerValue() + } + override suspend fun getById(id: FromId): FromRegistered? = to.getById( id.toOutKey() ) ?.toInnerValue() diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt index 1afaeb8806f..15b9fb1c078 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt @@ -55,6 +55,10 @@ open class MapperReadKeyValueRepo( key.toOutKey() ) + override suspend fun getAll(): Map = to.getAll().map { (k, v) -> + k.toInnerKey() to v.toInnerValue() + }.toMap() + override suspend fun count(): Long = to.count() } diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt index 45b2c86c497..d3666d263fb 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/CRUDPaginationExtensions.kt @@ -16,4 +16,4 @@ suspend inline fun > REPO.getAll( suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult -): List = getAll(FirstPagePagination(count().toCoercedInt()), methodCaller) +): List = getAll(maxPagePagination(), methodCaller) 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 1af608a9d54..3e9a685ecc0 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 @@ -19,4 +19,4 @@ suspend inline fun > REPO.getAll suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult -): List> = getAll(FirstPagePagination(count().toCoercedInt()), methodCaller) +): List> = getAll(maxPagePagination(), methodCaller) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/MaxPagePagination.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/MaxPagePagination.kt new file mode 100644 index 00000000000..a55802be9d0 --- /dev/null +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/pagination/MaxPagePagination.kt @@ -0,0 +1,11 @@ +package dev.inmo.micro_utils.repos.pagination + +import dev.inmo.micro_utils.common.toCoercedInt +import dev.inmo.micro_utils.pagination.FirstPagePagination +import dev.inmo.micro_utils.repos.ReadCRUDRepo +import dev.inmo.micro_utils.repos.ReadKeyValueRepo +import dev.inmo.micro_utils.repos.ReadKeyValuesRepo + +suspend inline fun ReadCRUDRepo<*, *>.maxPagePagination() = FirstPagePagination(count().toCoercedInt()) +suspend inline fun ReadKeyValueRepo<*, *>.maxPagePagination() = FirstPagePagination(count().toCoercedInt()) +suspend inline fun ReadKeyValuesRepo<*, *>.maxPagePagination() = FirstPagePagination(count().toCoercedInt()) 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 ca14bfd4312..798b25bc923 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 @@ -21,4 +21,4 @@ suspend inline fun > REPO.getAl suspend inline fun > REPO.getAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") crossinline methodCaller: suspend REPO.(Pagination) -> PaginationResult -): List>> = getAll(FirstPagePagination(count().toCoercedInt()), methodCaller) +): List>> = getAll(maxPagePagination(), methodCaller) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/crud/ReadCRUDFromKeyValueRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/crud/ReadCRUDFromKeyValueRepo.kt index b158592bc26..5f2fa7fba02 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/crud/ReadCRUDFromKeyValueRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/crud/ReadCRUDFromKeyValueRepo.kt @@ -12,6 +12,8 @@ open class ReadCRUDFromKeyValueRepo( override suspend fun count(): Long = original.count() + override suspend fun getAll(): Map = original.getAll() + override suspend fun getByPagination(pagination: Pagination): PaginationResult = original.values(pagination) override suspend fun getIdsByPagination(pagination: Pagination): PaginationResult = original.keys(pagination) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromCRUDRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromCRUDRepo.kt index 7e05e152c8b..91edb7a1276 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromCRUDRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromCRUDRepo.kt @@ -11,6 +11,7 @@ import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ReadKeyValuesRepo +import dev.inmo.micro_utils.repos.pagination.getAll import dev.inmo.micro_utils.repos.transforms.kvs.ReadKeyValuesFromKeyValueRepo import kotlin.jvm.JvmInline @@ -40,6 +41,8 @@ value class ReadKeyValueFromCRUDRepo( } } + override suspend fun getAll(): Map = original.getAll() + override suspend fun count(): Long = original.count() override suspend fun contains(key: Key): Boolean = original.contains(key) diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromKeyValuesRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromKeyValuesRepo.kt index 9efbdd36403..283f9ae0d49 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromKeyValuesRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/transforms/kv/ReadKeyValueFromKeyValuesRepo.kt @@ -41,6 +41,8 @@ open class ReadKeyValueFromKeyValuesRepo( return original.contains(key) } + override suspend fun getAll(): Map> = original.getAll() + override suspend fun keys(v: List, pagination: Pagination, reversed: Boolean): PaginationResult { val keys = mutableSetOf() diff --git a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt index 92909c7949f..6bc619365fe 100644 --- a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt +++ b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt @@ -1,6 +1,7 @@ package dev.inmo.micro_utils.repos import dev.inmo.micro_utils.common.Warning +import dev.inmo.micro_utils.common.filename import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.reverse import kotlinx.coroutines.* @@ -94,6 +95,8 @@ class FileReadKeyValueRepo( return File(folder, key).exists() } + override suspend fun getAll(): Map = (folder.listFiles() ?.toList() ?: emptyList()).associateBy { it.filename.name } + override suspend fun count(): Long = folder.list() ?.size ?.toLong() ?: 0L } diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt index ae832c1a1a5..6ca69256ae1 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/crud/AbstractAndroidCRUDRepo.kt @@ -32,6 +32,18 @@ abstract class AbstractAndroidCRUDRepo( } } + override suspend fun getAll(): Map = helper.readableTransaction { + select( + tableName, + null, + "" + ).use { + it.map { + it.toId() to it.toObject() + } + } + }.toMap() + override suspend fun getById(id: IdType): ObjectType? = helper.readableTransaction { select( tableName, diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt index 1e5dda1a78f..92ada5a009c 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt @@ -104,6 +104,15 @@ class KeyValueStore internal constructor ( override suspend fun contains(key: String): Boolean = sharedPreferences.contains(key) + override suspend fun getAll(): Map { + val resultMap = mutableMapOf() + sharedPreferences.all.forEach { (k, v) -> + @Suppress("UNCHECKED_CAST") + resultMap[k] = (v as? T) ?: return@forEach + } + return resultMap.toMap() + } + override suspend fun count(): Long = sharedPreferences.all.size.toLong() override suspend fun set(toSet: Map) { 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 95b25277a26..793fddc0536 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 @@ -45,5 +45,9 @@ abstract class AbstractExposedReadCRUDRepo( select { selectById(id) }.limit(1).any() } + override suspend fun getAll(): Map = transaction(database) { + selectAll().associate { it.asId to it.asObject } + } + 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 428b3498bf5..37379a11d2d 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 @@ -5,7 +5,6 @@ import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.exposed.* import dev.inmo.micro_utils.repos.exposed.utils.selectPaginated import org.jetbrains.exposed.sql.* -import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.transactions.transaction abstract class AbstractExposedReadKeyValueRepo( @@ -32,6 +31,8 @@ abstract class AbstractExposedReadKeyValueRepo( select { selectById(key) }.limit(1).any() } + override suspend fun getAll(): Map = transaction(database) { selectAll().associate { it.asKey to it.asObject } } + override suspend fun count(): Long = transaction(database) { selectAll().count() } override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = transaction(database) { 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 bce6525676d..981b78b654d 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 @@ -26,6 +26,8 @@ class ReadMapCRUDRepo( override suspend fun contains(id: IdType): Boolean = map.containsKey(id) + override suspend fun getAll(): Map = map.toMap() + override suspend fun count(): Long = map.size.toLong() } 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 46fe85be252..328dccee38c 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 @@ -51,6 +51,8 @@ class ReadMapKeyValueRepo( } } + override suspend fun getAll(): Map = map.toMap() + override suspend fun contains(key: Key): Boolean = map.containsKey(key) override suspend fun count(): Long = map.size.toLong() diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt index 250192de745..9f9ac8a8655 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt @@ -5,6 +5,7 @@ import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.getAllRoute import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.client.HttpClient import io.ktor.client.call.body @@ -58,6 +59,15 @@ class KtorReadCRUDRepoClient ( contentType(contentType) }.body() + override suspend fun getAll(): Map = httpClient.get( + buildStandardUrl( + baseUrl, + getAllRoute + ) + ) { + contentType(contentType) + }.body() + override suspend fun count(): Long = httpClient.get( buildStandardUrl( baseUrl, diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt index c8c40e7ce30..b32e0baafb4 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt @@ -48,6 +48,15 @@ class KtorReadKeyValueRepoClient( contentType(contentType) }.body() + override suspend fun getAll(): Map = httpClient.get( + buildStandardUrl( + baseUrl, + getAllRoute + ) + ) { + contentType(contentType) + }.body() + override suspend fun values( pagination: Pagination, reversed: Boolean diff --git a/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/GetAllRoute.kt b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/GetAllRoute.kt new file mode 100644 index 00000000000..64173c5e9ff --- /dev/null +++ b/repos/ktor/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/common/GetAllRoute.kt @@ -0,0 +1,3 @@ +package dev.inmo.micro_utils.repos.ktor.common + +const val getAllRoute = "getAll" diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt index 4cc54433b87..7b295deefae 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/crud/KtorReadCRUDRepoRoutes.kt @@ -6,6 +6,7 @@ import dev.inmo.micro_utils.pagination.extractPagination import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ktor.common.countRouting import dev.inmo.micro_utils.repos.ktor.common.crud.* +import dev.inmo.micro_utils.repos.ktor.common.getAllRoute import dev.inmo.micro_utils.repos.ktor.common.idParameterName import io.ktor.http.HttpStatusCode import io.ktor.server.application.call @@ -53,6 +54,10 @@ inline fun Route.configureReadCRUDRepoRoute ) } + get(getAllRoute) { + call.respond(originalRepo.getAll()) + } + get(countRouting) { call.respond( originalRepo.count() diff --git a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt index abf2ca45731..418d87cae7e 100644 --- a/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt +++ b/repos/ktor/server/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/ktor/server/key/value/KtorReadKeyValueRepoRoutes.kt @@ -70,6 +70,10 @@ inline fun Route.configureReadKeyValueRepoRoutes ( call.respond(originalRepo.contains(key)) } + get(getAllRoute) { + call.respond(originalRepo.getAll()) + } + get(countRoute) { call.respond(originalRepo.count()) } From 5180d6fc3ed028199051ebeeb14e83141cc1b388 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 18:43:43 +0600 Subject: [PATCH 6/9] hotfixes --- .../dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt | 8 +++----- .../dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt index 61287183ff7..eb4db819b5c 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt @@ -28,7 +28,7 @@ suspend inline fun KVCache.actualizeAll( clear: Boolean = true, ) { actualizeAll(clear) { - repo.getAll { keys(it) }.toMap() + repo.getAll() } } @@ -37,7 +37,7 @@ suspend inline fun KVCache>.actualizeAll( clear: Boolean = true, ) { actualizeAll(clear) { - repo.getAll { keys(it) }.toMap() + repo.getAll() } } @@ -46,8 +46,6 @@ suspend inline fun KVCache.actualizeAll( clear: Boolean = true, ) { actualizeAll(clear) { - repo.getAllByWithNextPaging { - getIdsByPagination(it) - }.mapNotNull { it to (repo.getById(it) ?: return@mapNotNull null) }.toMap() + repo.getAll() } } diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt index 92ada5a009c..6cd00e7f151 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt @@ -106,9 +106,9 @@ class KeyValueStore internal constructor ( override suspend fun getAll(): Map { val resultMap = mutableMapOf() - sharedPreferences.all.forEach { (k, v) -> + for ((k, v) in sharedPreferences.all) { @Suppress("UNCHECKED_CAST") - resultMap[k] = (v as? T) ?: return@forEach + resultMap[k] = (v as? T) ?: continue } return resultMap.toMap() } From 022297ad3ffdcdc296880ae1b5c09051c717b134 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 19:37:20 +0600 Subject: [PATCH 7/9] fixes in AutoRecacheReadKeyValueRepo --- .../fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt index 4a530047a6b..c3fa08ddd27 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheReadKeyValueRepo.kt @@ -52,7 +52,13 @@ open class AutoRecacheReadKeyValueRepo( kvCache.contains(key) } - override suspend fun getAll(): Map = kvCache.getAll() + override suspend fun getAll(): Map = actionWrapper.wrap { + originalRepo.getAll() + }.onSuccess { + kvCache.actualizeAll(clear = true) { it } + }.getOrElse { + kvCache.getAll() + } override suspend fun count(): Long = actionWrapper.wrap { originalRepo.count() From f4c148bc58399fb2df49e160ef1f51fb40635b0e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 21:41:01 +0600 Subject: [PATCH 8/9] add map type info in ktor read kv and crud repos --- .../micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt | 1 + .../repos/ktor/client/crud/KtorReadCRUDRepoClient.kt | 4 +++- .../repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt index 811ce434cd1..641e0af28ac 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorCRUDRepoClient.kt @@ -44,6 +44,7 @@ class KtorCRUDRepoClient ( typeInfo>(), typeInfo>(), contentType, + typeInfo>(), idSerializer ), KtorWriteCrudRepoClient( diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt index 9f9ac8a8655..1f78f59b87d 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/crud/KtorReadCRUDRepoClient.kt @@ -22,6 +22,7 @@ class KtorReadCRUDRepoClient ( private val paginationObjectType: TypeInfo, private val paginationIdType: TypeInfo, private val contentType: ContentType, + private val mapTypeInfo: TypeInfo, private val idSerializer: suspend (IdType) -> String ) : ReadCRUDRepo { override suspend fun getByPagination(pagination: Pagination): PaginationResult = httpClient.get( @@ -66,7 +67,7 @@ class KtorReadCRUDRepoClient ( ) ) { contentType(contentType) - }.body() + }.body(mapTypeInfo) override suspend fun count(): Long = httpClient.get( buildStandardUrl( @@ -90,6 +91,7 @@ inline fun KtorReadCRUDRepoClient( typeInfo>(), typeInfo>(), contentType, + typeInfo>(), idSerializer ) diff --git a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt index b32e0baafb4..fbda0b8d833 100644 --- a/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt +++ b/repos/ktor/client/src/commonMain/kotlin/dev/inmo/micro_utils/repos/ktor/client/key/value/KtorReadKeyValueRepoClient.kt @@ -23,6 +23,7 @@ class KtorReadKeyValueRepoClient( private val objectType: TypeInfo, private val paginationResultObjectsTypeInfo: TypeInfo, private val paginationResultIdsTypeInfo: TypeInfo, + private val mapKeyValueTypeInfo: TypeInfo, private val idSerializer: suspend (Key) -> String, private val valueSerializer: suspend (Value) -> String ) : ReadKeyValueRepo { @@ -55,7 +56,7 @@ class KtorReadKeyValueRepoClient( ) ) { contentType(contentType) - }.body() + }.body(mapKeyValueTypeInfo) override suspend fun values( pagination: Pagination, @@ -112,6 +113,7 @@ inline fun KtorReadKeyValueRepoClient( typeInfo(), typeInfo>(), typeInfo>(), + typeInfo>(), idSerializer, valueSerializer ) From cc4224ea1fb3be06a2ee699cfa42d0ee945697b5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 22:08:35 +0600 Subject: [PATCH 9/9] update kv and crud tests --- repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt | 3 +++ repos/ktor/common/src/jvmTest/kotlin/KVTests.kt | 2 ++ 2 files changed, 5 insertions(+) diff --git a/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt index fdf2995b165..922a72289da 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/CRUDTests.kt @@ -83,6 +83,9 @@ class CRUDTests { assertEquals(map.size, 0) assertEquals(map.size.toLong(), crudClient.count()) assertEquals(0, crudClient.count()) + + assertEquals(map, crudClient.getAll()) + server.stop() } } diff --git a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt index 76702d189a2..2a0df3bff7f 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KVTests.kt @@ -133,6 +133,8 @@ class KVTests { crudClient.count() ) + assertEquals(map, crudClient.getAll()) + server.stop() } }