improve default 'set' of KeyValuesRepo

This commit is contained in:
InsanusMokrassar 2024-02-15 15:36:32 +06:00
parent cf531c949d
commit 4b0f20dbd1
2 changed files with 13 additions and 0 deletions

View File

@ -2,6 +2,10 @@
## 0.20.34
* `Repos`:
* `Common`:
* Improve default `set` realization of `KeyValuesRepo`
## 0.20.33
* `Colors`

View File

@ -1,5 +1,6 @@
package dev.inmo.micro_utils.repos
import dev.inmo.micro_utils.common.diff
import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.pagination.utils.getAllWithNextPaging
@ -130,6 +131,14 @@ interface KeyValuesRepo<Key, Value> : ReadKeyValuesRepo<Key, Value>, WriteKeyVal
remove(toRemove)
}
override suspend fun set(toSet: Map<Key, List<Value>>) {
toSet.forEach { (k, v) ->
val diff = getAll(k).diff(v)
remove(k, diff.removed.map { it.value })
add(k, diff.added.map { it.value })
}
}
}
typealias OneToManyKeyValueRepo<Key,Value> = KeyValuesRepo<Key, Value>