fixes in actualizeAll

This commit is contained in:
InsanusMokrassar 2023-01-29 22:51:39 +06:00
parent 6142022283
commit 2162e83bce

View File

@ -2,16 +2,17 @@ package dev.inmo.micro_utils.repos.cache.util
import dev.inmo.micro_utils.pagination.FirstPagePagination import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.pagination.utils.getAllByWithNextPaging
import dev.inmo.micro_utils.repos.ReadCRUDRepo import dev.inmo.micro_utils.repos.ReadCRUDRepo
import dev.inmo.micro_utils.repos.ReadKeyValueRepo import dev.inmo.micro_utils.repos.ReadKeyValueRepo
import dev.inmo.micro_utils.repos.ReadKeyValuesRepo import dev.inmo.micro_utils.repos.ReadKeyValuesRepo
import dev.inmo.micro_utils.repos.cache.cache.KVCache import dev.inmo.micro_utils.repos.cache.cache.KVCache
import dev.inmo.micro_utils.repos.pagination.getAll
import dev.inmo.micro_utils.repos.set import dev.inmo.micro_utils.repos.set
suspend inline fun <K, V> KVCache<K, V>.actualizeAll( suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
getAll: () -> Map<K, V> getAll: () -> Map<K, V>
) { ) {
clear()
set( set(
getAll().also { getAll().also {
clear() clear()
@ -22,56 +23,25 @@ suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
suspend inline fun <K, V> KVCache<K, V>.actualizeAll( suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
repo: ReadKeyValueRepo<K, V> repo: ReadKeyValueRepo<K, V>
) { ) {
var cleared = false actualizeAll {
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE repo.getAll { keys(it) }.toMap()
val initPagination = FirstPagePagination(count)
doForAllWithNextPaging(initPagination) {
keys(it).also {
if (!cleared) {
clear()
cleared = true
}
set(
it.results.mapNotNull { k -> repo.get(k) ?.let { k to it } }
)
}
} }
} }
suspend inline fun <K, V> KVCache<K, List<V>>.actualizeAll( suspend inline fun <K, V> KVCache<K, List<V>>.actualizeAll(
repo: ReadKeyValuesRepo<K, V> repo: ReadKeyValuesRepo<K, V>
) { ) {
var cleared = false actualizeAll {
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE repo.getAll { keys(it) }.toMap()
val initPagination = FirstPagePagination(count)
doForAllWithNextPaging(initPagination) {
keys(it).also {
if (!cleared) {
clear()
cleared = true
}
set(
it.results.associateWith { k -> repo.getAll(k) }
)
}
} }
} }
suspend inline fun <K, V> KVCache<K, V>.actualizeAll( suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
repo: ReadCRUDRepo<V, K> repo: ReadCRUDRepo<V, K>
) { ) {
var cleared = false actualizeAll {
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE repo.getAllByWithNextPaging {
val initPagination = FirstPagePagination(count) getIdsByPagination(it)
doForAllWithNextPaging(initPagination) { }.mapNotNull { it to (repo.getById(it) ?: return@mapNotNull null) }.toMap()
keys(it).also {
if (!cleared) {
clear()
cleared = true
}
set(
it.results.mapNotNull { k -> repo.getById(k) ?.let { k to it } }
)
}
} }
} }