@@ -18,6 +18,7 @@ import kotlinx.coroutines.flow.asFlow
1818import kotlinx.coroutines.flow.channelFlow
1919import kotlinx.coroutines.flow.emptyFlow
2020import kotlinx.coroutines.flow.map
21+ import kotlinx.coroutines.runBlocking
2122import kotlinx.datetime.Clock
2223import org.apache.commons.collections4.map.LRUMap
2324import org.modelix.model.IKeyValueStore
@@ -30,7 +31,6 @@ import org.modelix.model.api.IdGeneratorDummy
3031import org.modelix.model.api.PBranch
3132import org.modelix.model.api.runSynchronized
3233import org.modelix.model.lazy.BranchReference
33- import org.modelix.model.lazy.BulkQuery
3434import org.modelix.model.lazy.CLTree
3535import org.modelix.model.lazy.CLVersion
3636import org.modelix.model.lazy.IDeserializingKeyValueStore
@@ -46,6 +46,7 @@ import java.lang.ref.SoftReference
4646import java.util.UUID
4747
4848class RepositoriesManager (val client : LocalModelClient ) {
49+
4950 init {
5051 migrateLegacyRepositoriesList()
5152 }
@@ -246,19 +247,23 @@ class RepositoriesManager(val client: LocalModelClient) {
246247 // we have to do this to not emit objects more than once.
247248 val seenHashes = mutableSetOf<String >()
248249 fun emitObjects (entry : KVEntryReference <* >) {
250+ if (seenHashes.contains(entry.getHash())) return
251+ seenHashes.add(entry.getHash())
249252 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)
257262 }
258263 }
259264 }
260265 emitObjects(KVEntryReference (versionHash, CPVersion .DESERIALIZER ))
261- ( bulkQuery as ? BulkQuery )? .process()
266+ bulkQuery.process()
262267 }
263268 }
264269
0 commit comments