@@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.asFlow
18
18
import kotlinx.coroutines.flow.channelFlow
19
19
import kotlinx.coroutines.flow.emptyFlow
20
20
import kotlinx.coroutines.flow.map
21
+ import kotlinx.coroutines.runBlocking
21
22
import kotlinx.datetime.Clock
22
23
import org.apache.commons.collections4.map.LRUMap
23
24
import org.modelix.model.IKeyValueStore
@@ -30,7 +31,6 @@ import org.modelix.model.api.IdGeneratorDummy
30
31
import org.modelix.model.api.PBranch
31
32
import org.modelix.model.api.runSynchronized
32
33
import org.modelix.model.lazy.BranchReference
33
- import org.modelix.model.lazy.BulkQuery
34
34
import org.modelix.model.lazy.CLTree
35
35
import org.modelix.model.lazy.CLVersion
36
36
import org.modelix.model.lazy.IDeserializingKeyValueStore
@@ -46,6 +46,7 @@ import java.lang.ref.SoftReference
46
46
import java.util.UUID
47
47
48
48
class RepositoriesManager (val client : LocalModelClient ) {
49
+
49
50
init {
50
51
migrateLegacyRepositoriesList()
51
52
}
@@ -246,19 +247,23 @@ class RepositoriesManager(val client: LocalModelClient) {
246
247
// we have to do this to not emit objects more than once.
247
248
val seenHashes = mutableSetOf<String >()
248
249
fun emitObjects (entry : KVEntryReference <* >) {
250
+ if (seenHashes.contains(entry.getHash())) return
251
+ seenHashes.add(entry.getHash())
249
252
bulkQuery.get(entry).onSuccess {
250
- channel.trySend(entry.getHash() to it!! .serialize())
251
- for (referencedEntry in it.getReferencedEntries()) {
252
- val wasSeenBefore = ! seenHashes.add(referencedEntry.getHash())
253
- // Do not emit the object if we already emitted it.
254
- if (! wasSeenBefore) {
255
- emitObjects(referencedEntry)
256
- }
253
+ val value = checkNotNull(it) { " No value received for ${entry.getHash()} " }
254
+ // Use `send` instead of `trySend`,
255
+ // because `trySend` fails if the channel capacity is full.
256
+ // This might happen if the data is produced faster than consumed.
257
+ // A better solution would be to have bulk queries which itself are asynchronous
258
+ // but doing that needs more consideration.
259
+ runBlocking { channel.send(entry.getHash() to value.serialize()) }
260
+ for (referencedEntry in value.getReferencedEntries()) {
261
+ emitObjects(referencedEntry)
257
262
}
258
263
}
259
264
}
260
265
emitObjects(KVEntryReference (versionHash, CPVersion .DESERIALIZER ))
261
- ( bulkQuery as ? BulkQuery )? .process()
266
+ bulkQuery.process()
262
267
}
263
268
}
264
269
0 commit comments