From eeebbff70dfea7bb91d5fdb233f1074a0f9f6323 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 10 Mar 2023 18:37:48 +0600 Subject: [PATCH] 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()) }