fixes in KeyValueRepo.clear

This commit is contained in:
InsanusMokrassar 2023-07-23 13:47:20 +06:00
parent 3a609e5b66
commit 856e657f81
12 changed files with 93 additions and 4 deletions

View File

@ -2,6 +2,9 @@
## 0.19.8
* `Repos`:
* Fixes In `KeyValueRepo.clear()` of almost all inheritors of `KeyValueRepo`
## 0.19.7
* `Versions`:

View File

@ -38,7 +38,7 @@ fun <Key, Value> ReadKeyValueRepo<Key, Value>.cached(
) = ReadKeyValueCacheRepo(this, kvCache)
open class KeyValueCacheRepo<Key,Value>(
parentRepo: KeyValueRepo<Key, Value>,
override val parentRepo: KeyValueRepo<Key, Value>,
kvCache: KVCache<Key, Value>,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
) : ReadKeyValueCacheRepo<Key,Value>(parentRepo, kvCache), KeyValueRepo<Key,Value>, WriteKeyValueRepo<Key, Value> by parentRepo, CommonCacheRepo {
@ -46,6 +46,11 @@ open class KeyValueCacheRepo<Key,Value>(
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 <Key, Value> KeyValueRepo<Key, Value>.cached(

View File

@ -21,6 +21,12 @@ open class SimpleFullKVCache<K, V>(
kvParent.unset(toUnset)
}
}
override suspend fun clear() {
syncMutex.withLock {
kvParent.clear()
}
}
}
inline fun <K, V> FullKVCache(

View File

@ -37,6 +37,10 @@ open class SimpleKVCache<K, V>(
override suspend fun unset(toUnset: List<K>) {
syncMutex.withLock { makeUnset(toUnset) }
}
override suspend fun clear() {
syncMutex.withLock { makeUnset(cacheQueue) }
}
}
inline fun <K, V> KVCache(

View File

@ -39,4 +39,9 @@ open class AutoRecacheKeyValueRepo<Id, RegisteredObject>(
).also {
kvCache.unsetWithValues(toUnset)
}
override suspend fun clear() {
originalRepo.clear()
kvCache.clear()
}
}

View File

@ -115,6 +115,11 @@ open class FullKeyValueCacheRepo<Key,Value>(
override suspend fun invalidate() {
kvCache.actualizeAll(parentRepo)
}
override suspend fun clear() {
parentRepo.clear()
kvCache.clear()
}
}
fun <Key, Value> KeyValueRepo<Key, Value>.fullyCached(

View File

@ -127,7 +127,11 @@ open class MapperKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
) : KeyValueRepo<FromKey, FromValue>,
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
ReadKeyValueRepo<FromKey, FromValue> by MapperReadKeyValueRepo(to, mapper),
WriteKeyValueRepo<FromKey, FromValue> by MapperWriteKeyValueRepo(to, mapper)
WriteKeyValueRepo<FromKey, FromValue> by MapperWriteKeyValueRepo(to, mapper) {
override suspend fun clear() {
to.clear()
}
}
@Suppress("NOTHING_TO_INLINE")
inline fun <FromKey, FromValue, ToKey, ToValue> KeyValueRepo<ToKey, ToValue>.withMapper(

View File

@ -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<String, File>,
WriteKeyValueRepo<String, File> by FileWriteKeyValueRepo(folder, filesChangedProcessingScope),
ReadKeyValueRepo<String, File> by FileReadKeyValueRepo(folder) {
override suspend fun clear() {
withContext(Dispatchers.IO) {
folder.listFiles() ?.forEach { runCatching { it.deleteRecursively() } }
}
}
}

View File

@ -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<String, KeyValueStore<*>>()
@ -159,6 +160,24 @@ class KeyValueStore<T : Any> internal constructor (
}
}
override suspend fun clear() {
val keys = mutableSetOf<String>()
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 <T : Any> invoke(
context: Context,

View File

@ -73,4 +73,18 @@ abstract class AbstractExposedKeyValueRepo<Key, Value>(
_onValueRemoved.emit(it)
}
}
override suspend fun clear() {
transaction(database) {
val keys = selectAll().map { it.asKey }
deleteAll()
keys
}.also {
it.forEach {
_onValueRemoved.emit(it)
}
}
}
}

View File

@ -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<Key, Value>(
@ -72,4 +73,18 @@ open class ExposedKeyValueRepo<Key, Value>(
_onValueRemoved.emit(it)
}
}
override suspend fun clear() {
transaction(database) {
val keys = selectAll().map { it.asKey }
deleteAll()
keys
}.also {
it.forEach {
_onValueRemoved.emit(it)
}
}
}
}

View File

@ -94,6 +94,10 @@ class MapKeyValueRepo<Key, Value>(
private val map: MutableMap<Key, Value> = mutableMapOf()
) : KeyValueRepo<Key, Value>,
ReadKeyValueRepo<Key, Value> by ReadMapKeyValueRepo(map),
WriteKeyValueRepo<Key, Value> by WriteMapKeyValueRepo(map)
WriteKeyValueRepo<Key, Value> by WriteMapKeyValueRepo(map) {
override suspend fun clear() {
map.clear()
}
}
fun <K, V> MutableMap<K, V>.asKeyValueRepo(): KeyValueRepo<K, V> = MapKeyValueRepo(this)