From aa6c62d66e19f8578a6aec913c3a5e5b91ea525a Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 13 May 2024 21:25:13 +0600 Subject: [PATCH] fixes in CachedRatingsRepo --- ratings/gc/src/jvmMain/kotlin/Plugin.kt | 6 +++-- .../kotlin/repo/CachedRatingsRepo.kt | 22 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/ratings/gc/src/jvmMain/kotlin/Plugin.kt b/ratings/gc/src/jvmMain/kotlin/Plugin.kt index 755d5e7..0369eac 100644 --- a/ratings/gc/src/jvmMain/kotlin/Plugin.kt +++ b/ratings/gc/src/jvmMain/kotlin/Plugin.kt @@ -71,11 +71,13 @@ object Plugin : Plugin { autoClearLogger.i { "Start autoclear" } val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds autoClearLogger.i { "Posts drop created before: ${dropCreatedBefore.toStringDefault()}" } - val idsToDelete = ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.filter { + val idsToDelete = ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.also { + autoClearLogger.i { "Selected posts by rating: $it" } + }.filter { val postCreationDateTime = postsRepo.getPostCreationTime(it) ?: (dropCreatedBefore - 1.seconds) // do dropping if post creation time is not available postCreationDateTime < dropCreatedBefore } - autoClearLogger.i { "Posts to drop: $idsToDelete" } + autoClearLogger.i { "Filtered posts by datetime: $idsToDelete" } if (idsToDelete.isNotEmpty()) { runCatching { ratingsRepo.unset(idsToDelete) } autoClearLogger.i { "Ratings dropped" } diff --git a/ratings/src/commonMain/kotlin/repo/CachedRatingsRepo.kt b/ratings/src/commonMain/kotlin/repo/CachedRatingsRepo.kt index 8e3e0ec..98e484d 100644 --- a/ratings/src/commonMain/kotlin/repo/CachedRatingsRepo.kt +++ b/ratings/src/commonMain/kotlin/repo/CachedRatingsRepo.kt @@ -1,6 +1,8 @@ package dev.inmo.plaguposter.ratings.repo import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging +import dev.inmo.micro_utils.pagination.utils.optionallyReverse +import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.repos.KeyValueRepo import dev.inmo.micro_utils.repos.MapKeyValueRepo import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo @@ -19,20 +21,16 @@ class CachedRatingsRepo( count: Int?, exclude: List ): Map { - val result = mutableMapOf() - - doForAllWithNextPaging { - kvCache.keys(it).also { - it.results.forEach { - val rating = get(it) ?: return@forEach - if (it !in exclude && rating in range) { - result[it] = rating - } - } + return kvCache.getAll().filter { (it, rating) -> + it !in exclude && rating in range + }.let { + if (count == null) { + it + } else { + val keys = it.keys.optionallyReverse(reversed).take(count) + keys.associateWith { id -> it.getValue(id) } } } - - return result.toMap() } override suspend fun getPostsWithRatingGreaterEq(