-
Notifications
You must be signed in to change notification settings - Fork 2
/
MapKeyValuesRepo.kt
198 lines (181 loc) · 7.19 KB
/
MapKeyValuesRepo.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package dev.inmo.micro_utils.repos
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.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import kotlinx.coroutines.flow.*
/**
* [Map]-based [ReadKeyValuesRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withReadAcquire])
*
* **Warning**: It is not recommended to use constructor with both [Map] and [SmartRWLocker]. Besides, in case
* you are using your own [Map] as a [map] you should be careful with operations on this [map]
*/
class MapReadKeyValuesRepo<Key, Value>(
private val map: Map<Key, List<Value>> = emptyMap(),
private val locker: SmartRWLocker = SmartRWLocker()
) : ReadKeyValuesRepo<Key, Value> {
override suspend fun get(k: Key, pagination: Pagination, reversed: Boolean): PaginationResult<Value> {
val list = locker.withReadAcquire {
map[k] ?: return emptyPaginationResult()
}
return list.paginate(
if (reversed) {
pagination.reverse(list.size)
} else {
pagination
}
)
}
override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val keys = locker.withReadAcquire {
map.keys
}
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
return keys.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
}
override suspend fun keys(v: Value, pagination: Pagination, reversed: Boolean): PaginationResult<Key> {
val keys = locker.withReadAcquire {
map.keys.filter { map[it] ?.contains(v) == true }
}
val actualPagination = if (reversed) pagination.reverse(keys.size) else pagination
return keys.paginate(actualPagination).let {
if (reversed) {
it.copy(results = it.results.reversed())
} else {
it
}
}
}
override suspend fun contains(k: Key): Boolean = locker.withReadAcquire { map.containsKey(k) }
override suspend fun contains(k: Key, v: Value): Boolean = locker.withReadAcquire { map[k] ?.contains(v) } == true
override suspend fun count(k: Key): Long = locker.withReadAcquire { map[k] ?.size } ?.toLong() ?: 0L
override suspend fun count(): Long = locker.withReadAcquire { map.size }.toLong()
}
/**
* [MutableMap]-based [WriteKeyValuesRepo]. All internal operations will be locked with [locker] (mostly with
* [SmartRWLocker.withWriteLock])
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
class MapWriteKeyValuesRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf(),
private val locker: SmartRWLocker = SmartRWLocker()
) : WriteKeyValuesRepo<Key, Value> {
private val _onNewValue: MutableSharedFlow<Pair<Key, Value>> = MapsReposDefaultMutableSharedFlow()
override val onNewValue: Flow<Pair<Key, Value>> by lazy {
_onNewValue.asSharedFlow()
}
private val _onValueRemoved: MutableSharedFlow<Pair<Key, Value>> = MapsReposDefaultMutableSharedFlow()
override val onValueRemoved: Flow<Pair<Key, Value>> by lazy {
_onValueRemoved.asSharedFlow()
}
private val _onDataCleared: MutableSharedFlow<Key> = MapsReposDefaultMutableSharedFlow()
override val onDataCleared: Flow<Key> by lazy {
_onDataCleared.asSharedFlow()
}
override suspend fun add(toAdd: Map<Key, List<Value>>) {
locker.withWriteLock {
toAdd.keys.mapNotNull { k ->
(k to toAdd[k]).takeIf {
map.getOrPut(k) { mutableListOf() }.addAll(toAdd[k] ?: return@mapNotNull null)
}
}
}.forEach { (k, vs) ->
vs ?.forEach { v ->
_onNewValue.emit(k to v)
}
}
}
override suspend fun remove(toRemove: Map<Key, List<Value>>) {
val removed = mutableListOf<Pair<Key, Value>>()
val cleared = mutableListOf<Key>()
locker.withWriteLock {
toRemove.keys.forEach { k ->
if (map[k]?.removeAll(toRemove[k] ?: return@forEach) == true) {
toRemove[k]?.forEach { v ->
removed.add(k to v)
}
}
if (map[k]?.isEmpty() == true) {
map.remove(k)
cleared.add(k)
}
}
}
removed.forEach {
_onValueRemoved.emit(it)
}
cleared.forEach {
_onDataCleared.emit(it)
}
}
override suspend fun removeWithValue(v: Value) {
locker.withWriteLock {
map.mapNotNull { (k, values) ->
if (values.remove(v)) {
k to v
} else {
null
}
}
}.forEach {
_onValueRemoved.emit(it)
}
}
override suspend fun clear(k: Key) {
locker.withWriteLock {
map.remove(k)
} ?.also { _onDataCleared.emit(k) }
}
override suspend fun clearWithValue(v: Value) {
locker.withWriteLock {
map.filter { (_, values) ->
values.contains(v)
}.mapNotNull {
if (map.remove(it.key) != null) {
it.toPair()
} else {
null
}
}
}.forEach {
it.second.onEach { v ->
_onValueRemoved.emit(it.first to v)
}.also { _ ->
_onDataCleared.emit(it.first)
}
}
}
}
/**
* [MutableMap]-based [KeyValuesRepo]. All internal operations will be locked with [locker]
*
* **Warning**: It is not recommended to use constructor with both [MutableMap] and [SmartRWLocker]. Besides, in case
* you are using your own [MutableMap] as a [map] you should be careful with operations on this [map]
*/
@Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE")
class MapKeyValuesRepo<Key, Value>(
private val map: MutableMap<Key, MutableList<Value>> = mutableMapOf(),
private val locker: SmartRWLocker = SmartRWLocker()
) : KeyValuesRepo<Key, Value>,
ReadKeyValuesRepo<Key, Value> by MapReadKeyValuesRepo(map, locker),
WriteKeyValuesRepo<Key, Value> by MapWriteKeyValuesRepo(map, locker)
/**
* [MutableMap]-based [KeyValuesRepo]. All internal operations will be locked with [locker]
*
* **Warning**: In case you are using your own [MutableMap] as [this] receiver you should be careful with operations on [this] map
*/
fun <K, V> MutableMap<K, List<V>>.asKeyValuesRepo(locker: SmartRWLocker = SmartRWLocker()): KeyValuesRepo<K, V> = MapKeyValuesRepo(
map { (k, v) -> k to v.toMutableList() }.toMap().toMutableMap(),
locker
)