From 6115c1bcac7fccf657eb3d06bbe98d95343ac17b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 25 Oct 2020 22:08:42 +0600 Subject: [PATCH] add extensions for OneToManyKeyValueRepo --- .../repos/OneToManyKeyValueRepo.kt | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) 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 4c6b71e6b9b..e1313311baf 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 @@ -1,7 +1,6 @@ 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.* import kotlinx.coroutines.flow.Flow interface ReadOneToManyKeyValueRepo : Repo { @@ -11,6 +10,30 @@ interface ReadOneToManyKeyValueRepo : Repo { suspend fun contains(k: Key, v: Value): Boolean suspend fun count(k: Key): Long suspend fun count(): Long + + suspend fun getAll(k: Key, reversed: Boolean = false): List = mutableListOf().also { list -> + doWithPagination { + get(k, it).also { + list.addAll(it.results) + }.nextPageIfNotEmpty() + } + if (reversed) { + list.reverse() + } + } + + /** + * WARNING!!! THIS METHOD PROBABLY IS NOT EFFICIENT, USE WITH CAUTION + */ + suspend fun getAll(): Map> = mutableMapOf>().also { map -> + doWithPagination { + keys(it).also { paginationResult -> + paginationResult.results.forEach { k -> + map[k] = getAll(k) + } + }.nextPageIfNotEmpty() + } + } } @Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo")) typealias OneToManyReadKeyValueRepo = ReadOneToManyKeyValueRepo @@ -20,8 +43,20 @@ interface WriteOneToManyKeyValueRepo : Repo { val onValueRemoved: Flow> val onDataCleared: Flow + @Deprecated("Will be extracted as extension for other add method") suspend fun add(k: Key, v: Value) + suspend fun add(toAdd: Map>) = toAdd.forEach { (k, values) -> + values.forEach { v -> + add(k, v) + } + } + @Deprecated("Will be extracted as extension for other remove method") suspend fun remove(k: Key, v: Value) + suspend fun remove(toRemove: Map>) = toRemove.forEach { (k, values) -> + values.forEach { v -> + remove(k, v) + } + } suspend fun clear(k: Key) } @Deprecated("Renamed", ReplaceWith("WriteOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.WriteOneToManyKeyValueRepo"))