improve actualizeAll to lazily clear repo

This commit is contained in:
InsanusMokrassar 2023-01-29 22:46:32 +06:00
parent e6d9c8250f
commit 6142022283

View File

@ -12,17 +12,25 @@ suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
getAll: () -> Map<K, V> getAll: () -> Map<K, V>
) { ) {
clear() clear()
set(getAll()) set(
getAll().also {
clear()
}
)
} }
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>
) { ) {
clear() var cleared = false
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE
val initPagination = FirstPagePagination(count) val initPagination = FirstPagePagination(count)
doForAllWithNextPaging(initPagination) { doForAllWithNextPaging(initPagination) {
keys(it).also { keys(it).also {
if (!cleared) {
clear()
cleared = true
}
set( set(
it.results.mapNotNull { k -> repo.get(k) ?.let { k to it } } it.results.mapNotNull { k -> repo.get(k) ?.let { k to it } }
) )
@ -33,11 +41,15 @@ suspend inline fun <K, V> KVCache<K, V>.actualizeAll(
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>
) { ) {
clear() var cleared = false
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE
val initPagination = FirstPagePagination(count) val initPagination = FirstPagePagination(count)
doForAllWithNextPaging(initPagination) { doForAllWithNextPaging(initPagination) {
keys(it).also { keys(it).also {
if (!cleared) {
clear()
cleared = true
}
set( set(
it.results.associateWith { k -> repo.getAll(k) } it.results.associateWith { k -> repo.getAll(k) }
) )
@ -48,11 +60,15 @@ suspend inline fun <K, V> KVCache<K, List<V>>.actualizeAll(
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>
) { ) {
clear() var cleared = false
val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE val count = repo.count().takeIf { it < Int.MAX_VALUE } ?.toInt() ?: Int.MAX_VALUE
val initPagination = FirstPagePagination(count) val initPagination = FirstPagePagination(count)
doForAllWithNextPaging(initPagination) { doForAllWithNextPaging(initPagination) {
keys(it).also { keys(it).also {
if (!cleared) {
clear()
cleared = true
}
set( set(
it.results.mapNotNull { k -> repo.getById(k) ?.let { k to it } } it.results.mapNotNull { k -> repo.getById(k) ?.let { k to it } }
) )