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