From 36093b97417f4d886547e69697c7ad07a9276138 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 2 Aug 2024 23:36:08 +0600 Subject: [PATCH] fix of full caches initial actualization and realize MapKeyValuesRepo methods getAll --- .../repos/cache/full/FullCRUDCacheRepo.kt | 17 +++++++++++++-- .../repos/cache/full/FullKeyValueCacheRepo.kt | 17 +++++++++++++-- .../cache/full/FullKeyValuesCacheRepo.kt | 21 +++++++++++++++---- .../full/FullKeyValuesCacheRepoTests.kt | 1 + .../inmo/micro_utils/repos/MapKeyValueRepo.kt | 4 +++- .../micro_utils/repos/MapKeyValuesRepo.kt | 15 +++++++++++++ 6 files changed, 66 insertions(+), 9 deletions(-) diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt index 0afab87f949..d163b968f9d 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullCRUDCacheRepo.kt @@ -98,7 +98,7 @@ open class FullCRUDCacheRepo( kvCache: KeyValueRepo, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), skipStartInvalidate: Boolean = false, - locker: SmartRWLocker = SmartRWLocker(), + locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate), idGetter: (ObjectType) -> IdType ) : FullReadCRUDCacheRepo( parentRepo, @@ -116,10 +116,23 @@ open class FullCRUDCacheRepo( CRUDRepo { init { if (!skipStartInvalidate) { - scope.launchSafelyWithoutExceptions { invalidate() } + scope.launchSafelyWithoutExceptions { + if (locker.writeMutex.isLocked) { + initialInvalidate() + } else { + invalidate() + } + } } } + protected open suspend fun initialInvalidate() { + try { + kvCache.actualizeAll(parentRepo, locker = null) + } finally { + locker.unlockWrite() + } + } override suspend fun invalidate() { actualizeAll() } diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt index 0eeae1cba92..20b08e237b6 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValueCacheRepo.kt @@ -130,7 +130,7 @@ open class FullKeyValueCacheRepo( kvCache: KeyValueRepo, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), skipStartInvalidate: Boolean = false, - locker: SmartRWLocker = SmartRWLocker() + locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate), ) : FullWriteKeyValueCacheRepo(parentRepo, kvCache, scope), KeyValueRepo, ReadKeyValueRepo by FullReadKeyValueCacheRepo( @@ -140,12 +140,25 @@ open class FullKeyValueCacheRepo( ) { init { if (!skipStartInvalidate) { - scope.launchSafelyWithoutExceptions { invalidate() } + scope.launchSafelyWithoutExceptions { + if (locker.writeMutex.isLocked) { + initialInvalidate() + } else { + invalidate() + } + } } } override suspend fun unsetWithValues(toUnset: List) = parentRepo.unsetWithValues(toUnset) + protected open suspend fun initialInvalidate() { + try { + kvCache.actualizeAll(parentRepo, locker = null) + } finally { + locker.unlockWrite() + } + } override suspend fun invalidate() { kvCache.actualizeAll(parentRepo, locker) } diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt index aea3163ed0a..c68e238d27d 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/full/FullKeyValuesCacheRepo.kt @@ -10,6 +10,7 @@ import dev.inmo.micro_utils.pagination.utils.* import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.cache.util.ActualizeAllClearMode import dev.inmo.micro_utils.repos.cache.util.actualizeAll +import dev.inmo.micro_utils.repos.pagination.maxPagePagination import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.* @@ -203,14 +204,19 @@ open class FullKeyValuesCacheRepo( kvCache: KeyValueRepo>, scope: CoroutineScope = CoroutineScope(Dispatchers.Default), skipStartInvalidate: Boolean = false, - locker: SmartRWLocker = SmartRWLocker(), -) : //FullWriteKeyValuesCacheRepo(parentRepo, kvCache, scope, locker), - KeyValuesRepo, + locker: SmartRWLocker = SmartRWLocker(writeIsLocked = !skipStartInvalidate), +) : KeyValuesRepo, FullReadKeyValuesCacheRepo(parentRepo, kvCache, locker), WriteKeyValuesRepo by parentRepo { init { if (!skipStartInvalidate) { - scope.launchSafelyWithoutExceptions { invalidate() } + scope.launchSafelyWithoutExceptions { + if (locker.writeMutex.isLocked) { + initialInvalidate() + } else { + invalidate() + } + } } } @@ -222,6 +228,13 @@ open class FullKeyValuesCacheRepo( } } + protected open suspend fun initialInvalidate() { + try { + kvCache.actualizeAll(parentRepo, locker = null) + } finally { + locker.unlockWrite() + } + } override suspend fun invalidate() { kvCache.actualizeAll(parentRepo, locker = locker) } diff --git a/repos/cache/src/commonTest/kotlin/full/FullKeyValuesCacheRepoTests.kt b/repos/cache/src/commonTest/kotlin/full/FullKeyValuesCacheRepoTests.kt index c92a1a44881..4a2ffb34512 100644 --- a/repos/cache/src/commonTest/kotlin/full/FullKeyValuesCacheRepoTests.kt +++ b/repos/cache/src/commonTest/kotlin/full/FullKeyValuesCacheRepoTests.kt @@ -3,6 +3,7 @@ package full import com.benasher44.uuid.uuid4 import dev.inmo.micro_utils.repos.* import dev.inmo.micro_utils.repos.cache.full.FullKeyValuesCacheRepo +import dev.inmo.micro_utils.repos.pagination.maxPagePagination import korlibs.time.days import korlibs.time.years import kotlinx.coroutines.test.runTest diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt index 32b6815330f..460b3c9b794 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValueRepo.kt @@ -24,7 +24,9 @@ class ReadMapKeyValueRepo( ) : ReadKeyValueRepo { constructor(map: Map = emptyMap()) : this(map, SmartRWLocker()) - override suspend fun get(k: Key): Value? = locker.withReadAcquire { map[k] } + override suspend fun get(k: Key): Value? = locker.withReadAcquire { + map[k] + } override suspend fun values( pagination: Pagination, diff --git a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValuesRepo.kt b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValuesRepo.kt index 3a004120f92..a5e54cf0910 100644 --- a/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValuesRepo.kt +++ b/repos/inmemory/src/commonMain/kotlin/dev/inmo/micro_utils/repos/MapKeyValuesRepo.kt @@ -4,6 +4,7 @@ import dev.inmo.micro_utils.coroutines.SmartRWLocker import dev.inmo.micro_utils.coroutines.withReadAcquire import dev.inmo.micro_utils.coroutines.withWriteLock import dev.inmo.micro_utils.pagination.* +import dev.inmo.micro_utils.pagination.utils.optionallyReverse import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.reverse import kotlinx.coroutines.flow.* @@ -33,6 +34,20 @@ class MapReadKeyValuesRepo( ) } + override suspend fun getAll(k: Key, reversed: Boolean): List { + return locker.withReadAcquire { map[k] ?.optionallyReverse(reversed) ?: return emptyList() } + } + + override suspend fun getAll(reverseLists: Boolean): Map> { + return locker.withReadAcquire { + if (reverseLists) { + map.mapValues { it.value.reversed() } + } else { + map.toMap() + } + } + } + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult { val keys = locker.withReadAcquire { map.keys