From 2162e83bce53738ea8997ed0866a9b85f16df731 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 29 Jan 2023 22:51:39 +0600 Subject: [PATCH] fixes in actualizeAll --- .../repos/cache/util/ActualizeAll.kt | 50 ++++--------------- 1 file changed, 10 insertions(+), 40 deletions(-) diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt index 7d90235adac..8eb15f14a96 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/util/ActualizeAll.kt @@ -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 KVCache.actualizeAll( getAll: () -> Map ) { - clear() set( getAll().also { clear() @@ -22,56 +23,25 @@ suspend inline fun KVCache.actualizeAll( suspend inline fun KVCache.actualizeAll( repo: ReadKeyValueRepo ) { - 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 KVCache>.actualizeAll( repo: ReadKeyValuesRepo ) { - 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 KVCache.actualizeAll( repo: ReadCRUDRepo ) { - 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() } }