From 856e657f818e10e6b29cf507f1ca47842b5c3999 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 23 Jul 2023 13:47:20 +0600 Subject: [PATCH] fixes in KeyValueRepo.clear --- CHANGELOG.md | 3 +++ .../repos/cache/KeyValueCacheRepo.kt | 7 ++++++- .../repos/cache/cache/SimpleFullKVCache.kt | 6 ++++++ .../repos/cache/cache/SimpleKVCache.kt | 4 ++++ .../keyvalue/AutoRecacheKeyValueRepo.kt | 5 +++++ .../repos/cache/full/FullKeyValueCacheRepo.kt | 5 +++++ .../repos/mappers/KeyValueMappers.kt | 6 +++++- .../micro_utils/repos/FileKeyValueRepo.kt | 7 ++++++- .../repos/keyvalue/KeyValueStore.kt | 19 +++++++++++++++++++ .../keyvalue/AbstractExposedKeyValueRepo.kt | 14 ++++++++++++++ .../exposed/keyvalue/ExposedKeyValueRepo.kt | 15 +++++++++++++++ .../inmo/micro_utils/repos/MapKeyValueRepo.kt | 6 +++++- 12 files changed, 93 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c88e7bc2b44..01388928e36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.19.8 +* `Repos`: + * Fixes In `KeyValueRepo.clear()` of almost all inheritors of `KeyValueRepo` + ## 0.19.7 * `Versions`: diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt index 53203664e43..aba9a20c00e 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/KeyValueCacheRepo.kt @@ -38,7 +38,7 @@ fun ReadKeyValueRepo.cached( ) = ReadKeyValueCacheRepo(this, kvCache) open class KeyValueCacheRepo( - parentRepo: KeyValueRepo, + override val parentRepo: KeyValueRepo, kvCache: KVCache, scope: CoroutineScope = CoroutineScope(Dispatchers.Default) ) : ReadKeyValueCacheRepo(parentRepo, kvCache), KeyValueRepo, WriteKeyValueRepo by parentRepo, CommonCacheRepo { @@ -46,6 +46,11 @@ open class KeyValueCacheRepo( protected val onRemoveJob = parentRepo.onValueRemoved.onEach { kvCache.unset(it) }.launchIn(scope) override suspend fun invalidate() = kvCache.clear() + + override suspend fun clear() { + parentRepo.clear() + kvCache.clear() + } } fun KeyValueRepo.cached( diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleFullKVCache.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleFullKVCache.kt index a5ee4639b3d..2514b84068b 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleFullKVCache.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleFullKVCache.kt @@ -21,6 +21,12 @@ open class SimpleFullKVCache( kvParent.unset(toUnset) } } + + override suspend fun clear() { + syncMutex.withLock { + kvParent.clear() + } + } } inline fun FullKVCache( diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt index e3321a5fca1..75d91874641 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/cache/SimpleKVCache.kt @@ -37,6 +37,10 @@ open class SimpleKVCache( override suspend fun unset(toUnset: List) { syncMutex.withLock { makeUnset(toUnset) } } + + override suspend fun clear() { + syncMutex.withLock { makeUnset(cacheQueue) } + } } inline fun KVCache( diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheKeyValueRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheKeyValueRepo.kt index e1864829e3c..42f5d05e574 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheKeyValueRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/keyvalue/AutoRecacheKeyValueRepo.kt @@ -39,4 +39,9 @@ open class AutoRecacheKeyValueRepo( ).also { kvCache.unsetWithValues(toUnset) } + + override suspend fun clear() { + originalRepo.clear() + kvCache.clear() + } } 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 39e96edc1a8..943a822db19 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 @@ -115,6 +115,11 @@ open class FullKeyValueCacheRepo( override suspend fun invalidate() { kvCache.actualizeAll(parentRepo) } + + override suspend fun clear() { + parentRepo.clear() + kvCache.clear() + } } fun KeyValueRepo.fullyCached( diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt index 15b9fb1c078..da1530a9495 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/mappers/KeyValueMappers.kt @@ -127,7 +127,11 @@ open class MapperKeyValueRepo( ) : KeyValueRepo, MapperRepo by mapper, ReadKeyValueRepo by MapperReadKeyValueRepo(to, mapper), - WriteKeyValueRepo by MapperWriteKeyValueRepo(to, mapper) + WriteKeyValueRepo by MapperWriteKeyValueRepo(to, mapper) { + override suspend fun clear() { + to.clear() + } +} @Suppress("NOTHING_TO_INLINE") inline fun KeyValueRepo.withMapper( diff --git a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt index 6bc619365fe..6433f7146c2 100644 --- a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt +++ b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt @@ -202,9 +202,14 @@ class FileWriteKeyValueRepo( @Warning("Files watching will not correctly works on Android Platform with version of API lower than API 26") @Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE") class FileKeyValueRepo( - folder: File, + private val folder: File, filesChangedProcessingScope: CoroutineScope? = null ) : KeyValueRepo, WriteKeyValueRepo by FileWriteKeyValueRepo(folder, filesChangedProcessingScope), ReadKeyValueRepo by FileReadKeyValueRepo(folder) { + override suspend fun clear() { + withContext(Dispatchers.IO) { + folder.listFiles() ?.forEach { runCatching { it.deleteRecursively() } } + } + } } diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt index 6cd00e7f151..34cf52aa862 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/keyvalue/KeyValueStore.kt @@ -7,6 +7,7 @@ import dev.inmo.micro_utils.pagination.* import dev.inmo.micro_utils.pagination.utils.paginate import dev.inmo.micro_utils.pagination.utils.reverse import dev.inmo.micro_utils.repos.KeyValueRepo +import dev.inmo.micro_utils.repos.pagination.maxPagePagination import kotlinx.coroutines.flow.* private val cache = HashMap>() @@ -159,6 +160,24 @@ class KeyValueStore internal constructor ( } } + override suspend fun clear() { + val keys = mutableSetOf() + doWithPagination(maxPagePagination()) { + keys(it).also { + keys.addAll(it.results) + }.nextPageIfNotEmpty() + } + val success = sharedPreferences.edit().apply { + clear() + }.commit() + + if (success) { + keys.forEach { + _onValueRemovedFlow.emit(it) + } + } + } + companion object { operator fun invoke( context: Context, diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt index cb24c23f14e..591d1b6ce42 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt @@ -73,4 +73,18 @@ abstract class AbstractExposedKeyValueRepo( _onValueRemoved.emit(it) } } + + override suspend fun clear() { + transaction(database) { + val keys = selectAll().map { it.asKey } + + deleteAll() + + keys + }.also { + it.forEach { + _onValueRemoved.emit(it) + } + } + } } diff --git a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt index 80c57f7cd4b..be470089c71 100644 --- a/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt +++ b/repos/exposed/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/exposed/keyvalue/ExposedKeyValueRepo.kt @@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.* import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList +import org.jetbrains.exposed.sql.SqlExpressionBuilder.inSubQuery import org.jetbrains.exposed.sql.transactions.transaction open class ExposedKeyValueRepo( @@ -72,4 +73,18 @@ open class ExposedKeyValueRepo( _onValueRemoved.emit(it) } } + + override suspend fun clear() { + transaction(database) { + val keys = selectAll().map { it.asKey } + + deleteAll() + + keys + }.also { + it.forEach { + _onValueRemoved.emit(it) + } + } + } } 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 328dccee38c..d93bbcc1ca8 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 @@ -94,6 +94,10 @@ class MapKeyValueRepo( private val map: MutableMap = mutableMapOf() ) : KeyValueRepo, ReadKeyValueRepo by ReadMapKeyValueRepo(map), - WriteKeyValueRepo by WriteMapKeyValueRepo(map) + WriteKeyValueRepo by WriteMapKeyValueRepo(map) { + override suspend fun clear() { + map.clear() + } +} fun MutableMap.asKeyValueRepo(): KeyValueRepo = MapKeyValueRepo(this)