diff --git a/CHANGELOG.md b/CHANGELOG.md index cb396f6008a..c0c7930620e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 0.2.2 + +* `Repos` + * `Common` + * Several new methods `ReadOneToManyKeyValueRepo#getAll` + * Several new method `WriteOneToManyKeyValueRepo#add` and several extensions + * Several new method `WriteOneToManyKeyValueRepo#remove` and several extensions + ## 0.2.1 * `Pagination` diff --git a/gradle.properties b/gradle.properties index 03f3e2ae9bb..7103c62bf89 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,4 +19,4 @@ github_release_plugin_version=2.2.12 uuidVersion=0.2.2 group=dev.inmo -version=0.2.1 +version=0.2.2 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..d21048b6db0 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(reverseLists: Boolean = false): Map> = mutableMapOf>().also { map -> + doWithPagination { + keys(it).also { paginationResult -> + paginationResult.results.forEach { k -> + map[k] = getAll(k, reverseLists) + } + }.nextPageIfNotEmpty() + } + } } @Deprecated("Renamed", ReplaceWith("ReadOneToManyKeyValueRepo", "dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo")) typealias OneToManyReadKeyValueRepo = ReadOneToManyKeyValueRepo @@ -20,11 +43,51 @@ interface WriteOneToManyKeyValueRepo : Repo { val onValueRemoved: Flow> val onDataCleared: Flow + suspend fun add(toAdd: Map>) = toAdd.forEach { (k, values) -> + values.forEach { v -> + add(k, v) + } + } + @Deprecated("Will be extracted as extension for other add method") suspend fun add(k: Key, v: Value) + + suspend fun remove(toRemove: Map>) = toRemove.forEach { (k, values) -> + values.forEach { v -> + remove(k, v) + } + } + @Deprecated("Will be extracted as extension for other remove method") 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 : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo \ No newline at end of file +interface OneToManyKeyValueRepo : ReadOneToManyKeyValueRepo, WriteOneToManyKeyValueRepo + +suspend inline fun > REPO.add( + k: Key, + vararg v: Value +) = add(mapOf(k to v.toList())) + +suspend inline fun > REPO.add( + keysAndValues: List>> +) = add(keysAndValues.toMap()) + +suspend inline fun > REPO.add( + vararg keysAndValues: Pair> +) = add(keysAndValues.toMap()) + +suspend inline fun > REPO.remove( + k: Key, + vararg v: Value +) = remove(mapOf(k to v.toList())) + +suspend inline fun > REPO.remove( + keysAndValues: List>> +) = remove(keysAndValues.toMap()) + +suspend inline fun > REPO.remove( + vararg keysAndValues: Pair> +) = remove(keysAndValues.toMap()) 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 4442571948c..9a4b3391f2f 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,7 +1,7 @@ package dev.inmo.micro_utils.repos.pagination import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.ReadStandardKeyValueRepo suspend inline fun > REPO.doForAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE") 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 0c3de2def3c..28d3405e595 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,7 +1,7 @@ package dev.inmo.micro_utils.repos.pagination import dev.inmo.micro_utils.pagination.* -import dev.inmo.micro_utils.repos.* +import dev.inmo.micro_utils.repos.ReadOneToManyKeyValueRepo suspend inline fun > REPO.doForAll( @Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")