Skip to content

Commit 1ff9a14

Browse files
author
Oleksandr Dzhychko
committed
fix(model-datastructure): implement PrefetchCache.getAll with prefetch support
Not implementing the method caused unsupported operations exceptions in existing projects.
1 parent 5458395 commit 1ff9a14

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

model-datastructure/src/commonMain/kotlin/org/modelix/model/lazy/PrefetchCache.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package org.modelix.model.lazy
1616
import org.modelix.kotlin.utils.ContextValue
1717
import org.modelix.model.IKeyValueStore
1818
import org.modelix.model.api.ITree
19+
import org.modelix.model.persistent.IKVValue
1920

2021
/**
2122
* There is no size limit. Entries are not evicted.
@@ -59,6 +60,15 @@ class PrefetchCache(private val store: IDeserializingKeyValueStore) : IDeseriali
5960
return hashes.map { entries[it] as T }
6061
}
6162

63+
override fun <T : IKVValue> getAll(regular: List<IKVEntryReference<T>>, prefetch: List<IKVEntryReference<T>>): Map<String, T?> {
64+
val missingRegular = regular.filterNot { entries.containsKey(it.getHash()) }
65+
val missingPrefetch = prefetch.filterNot { entries.containsKey(it.getHash()) }
66+
val missingEntries = store.getAll(missingRegular, missingPrefetch)
67+
entries.putAll(missingEntries)
68+
val regularAndPrefetch = regular.asSequence() + prefetch.asSequence()
69+
return regularAndPrefetch.associate { it.getHash() to entries[it.getHash()] as T? }
70+
}
71+
6272
override fun put(hash: String, deserialized: Any, serialized: String) {
6373
entries[hash] = deserialized
6474
store.put(hash, deserialized, serialized)

model-datastructure/src/commonTest/kotlin/org/modelix/model/lazy/PrefetchCacheTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import org.modelix.model.lazy.AccessTrackingStore
22
import org.modelix.model.lazy.NonCachingObjectStore
33
import org.modelix.model.lazy.PrefetchCache
4+
import org.modelix.model.lazy.WrittenEntry
5+
import org.modelix.model.persistent.CPNode
46
import org.modelix.model.persistent.MapBasedStore
57
import kotlin.test.Test
68
import kotlin.test.assertEquals
@@ -24,4 +26,28 @@ class PrefetchCacheTest {
2426
assertEquals(result, listOf("value"))
2527
assertTrue(accessTrackingKeyValueStore.accessedEntries.isEmpty())
2628
}
29+
30+
@Test
31+
fun entriesAreCachedAfterGettingMultipleEntriesAsMap() {
32+
val regularKey = "regularKey"
33+
val prefetchKey = "prefetchKey"
34+
val nodeForRegularKey = CPNode.create(
35+
2, null, 1, null, LongArray(0),
36+
emptyArray(), emptyArray(), emptyArray(), emptyArray(),
37+
)
38+
val nodeForPrefetchKey = CPNode.create(
39+
3, null, 1, null, LongArray(0),
40+
emptyArray(), emptyArray(), emptyArray(), emptyArray(),
41+
)
42+
keyValueStore.putAll(mapOf(regularKey to nodeForRegularKey.serialize(), prefetchKey to nodeForPrefetchKey.serialize()))
43+
val regularKeyReference = WrittenEntry(regularKey) { nodeForRegularKey }
44+
val prefetchKeyReference = WrittenEntry(prefetchKey) { nodeForPrefetchKey }
45+
prefetchCache.getAll(listOf(regularKeyReference), listOf(prefetchKeyReference))
46+
accessTrackingKeyValueStore.accessedEntries.clear()
47+
48+
val result = prefetchCache.getAll(listOf(regularKeyReference), listOf(prefetchKeyReference))
49+
50+
assertEquals(result, mapOf(regularKey to nodeForRegularKey, prefetchKey to nodeForPrefetchKey))
51+
assertTrue(accessTrackingKeyValueStore.accessedEntries.isEmpty())
52+
}
2753
}

0 commit comments

Comments
 (0)