Skip to content

Commit 774bcc3

Browse files
committed
refactor(streams): moved getBlocking/getSuspending to internal interface
This ensures that they are not accidentally misused. An IStreamExecutor should always be provided.
1 parent 42fe0e1 commit 774bcc3

File tree

22 files changed

+296
-329
lines changed

22 files changed

+296
-329
lines changed

datastructures/src/commonMain/kotlin/org/modelix/datastructures/btree/BTree.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package org.modelix.datastructures.btree
22

33
import org.modelix.datastructures.objects.IObjectGraph
44
import org.modelix.streams.IStream
5+
import org.modelix.streams.IStreamExecutorProvider
56
import org.modelix.streams.getBlocking
67

7-
data class BTree<K, V>(val root: BTreeNode<K, V>) {
8+
data class BTree<K, V>(val root: BTreeNode<K, V>) : IStreamExecutorProvider by root.config.graph {
89
constructor(config: BTreeConfig<K, V>) : this(BTreeNodeLeaf(config, emptyList()))
910

1011
val graph: IObjectGraph get() = root.config.graph

datastructures/src/commonTest/kotlin/org/modelix/datastructures/BTreeTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.modelix.datastructures
33
import org.modelix.datastructures.btree.BTree
44
import org.modelix.datastructures.btree.BTreeConfig
55
import org.modelix.datastructures.objects.IObjectGraph
6+
import org.modelix.streams.getBlocking
67
import kotlin.random.Random
78
import kotlin.test.Test
89
import kotlin.test.assertEquals
@@ -71,7 +72,7 @@ class BTreeTest {
7172

7273
assertEquals(
7374
(100L..200L).map { it to it * 2 },
74-
tree.getAll((100L..200L)).toList().getSynchronous(),
75+
tree.getAll((100L..200L)).toList().getBlocking(tree),
7576
)
7677
}
7778
}

datastructures/src/commonTest/kotlin/org/modelix/datastructures/HamtCollisionTest.kt

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.modelix.datastructures.objects.IDataTypeConfiguration
77
import org.modelix.datastructures.objects.IObjectGraph
88
import org.modelix.datastructures.objects.LongDataTypeConfiguration
99
import org.modelix.datastructures.objects.StringDataTypeConfiguration
10+
import org.modelix.streams.getBlocking
1011
import kotlin.test.Test
1112
import kotlin.test.assertEquals
1213

@@ -26,19 +27,19 @@ class HamtCollisionTest {
2627
)
2728
var tree: HamtTree<Long, String> = HamtTree(HamtInternalNode.createEmpty(config))
2829

29-
tree = tree.put(0b000000000, "a").getSynchronous()
30-
tree = tree.put(0b000000001, "b").getSynchronous()
31-
tree = tree.put(0b000000011, "c").getSynchronous()
32-
tree = tree.put(0b100000000, "d").getSynchronous()
33-
tree = tree.put(0b100000001, "e").getSynchronous()
34-
tree = tree.put(0b100000011, "f").getSynchronous()
35-
36-
assertEquals("a", tree.get(0b000000000).getSynchronous())
37-
assertEquals("b", tree.get(0b000000001).getSynchronous())
38-
assertEquals("c", tree.get(0b000000011).getSynchronous())
39-
assertEquals("d", tree.get(0b100000000).getSynchronous())
40-
assertEquals("e", tree.get(0b100000001).getSynchronous())
41-
assertEquals("f", tree.get(0b100000011).getSynchronous())
30+
tree = tree.put(0b000000000, "a").getBlocking(tree)
31+
tree = tree.put(0b000000001, "b").getBlocking(tree)
32+
tree = tree.put(0b000000011, "c").getBlocking(tree)
33+
tree = tree.put(0b100000000, "d").getBlocking(tree)
34+
tree = tree.put(0b100000001, "e").getBlocking(tree)
35+
tree = tree.put(0b100000011, "f").getBlocking(tree)
36+
37+
assertEquals("a", tree.get(0b000000000).getBlocking(tree))
38+
assertEquals("b", tree.get(0b000000001).getBlocking(tree))
39+
assertEquals("c", tree.get(0b000000011).getBlocking(tree))
40+
assertEquals("d", tree.get(0b100000000).getBlocking(tree))
41+
assertEquals("e", tree.get(0b100000001).getBlocking(tree))
42+
assertEquals("f", tree.get(0b100000011).getBlocking(tree))
4243
}
4344

4445
/**
@@ -59,22 +60,22 @@ class HamtCollisionTest {
5960
)
6061
var tree: HamtTree<Long, String> = HamtTree(HamtInternalNode.createEmpty(config))
6162

62-
tree = tree.put(0b00, "a").getSynchronous()
63-
tree = tree.put(0b01, "b").getSynchronous()
64-
tree = tree.put(0b10, "c").getSynchronous()
65-
tree = tree.put(0b11, "d").getSynchronous()
63+
tree = tree.put(0b00, "a").getBlocking(tree)
64+
tree = tree.put(0b01, "b").getBlocking(tree)
65+
tree = tree.put(0b10, "c").getBlocking(tree)
66+
tree = tree.put(0b11, "d").getBlocking(tree)
6667

67-
assertEquals("a", tree.get(0b00).getSynchronous())
68-
assertEquals("b", tree.get(0b01).getSynchronous())
69-
assertEquals("c", tree.get(0b10).getSynchronous())
70-
assertEquals("d", tree.get(0b11).getSynchronous())
68+
assertEquals("a", tree.get(0b00).getBlocking(tree))
69+
assertEquals("b", tree.get(0b01).getBlocking(tree))
70+
assertEquals("c", tree.get(0b10).getBlocking(tree))
71+
assertEquals("d", tree.get(0b11).getBlocking(tree))
7172

72-
tree = tree.put(0b01, "changed").getSynchronous()
73+
tree = tree.put(0b01, "changed").getBlocking(tree)
7374

74-
assertEquals("a", tree.get(0b00).getSynchronous())
75-
assertEquals("changed", tree.get(0b01).getSynchronous())
76-
assertEquals("c", tree.get(0b10).getSynchronous())
77-
assertEquals("d", tree.get(0b11).getSynchronous())
75+
assertEquals("a", tree.get(0b00).getBlocking(tree))
76+
assertEquals("changed", tree.get(0b01).getBlocking(tree))
77+
assertEquals("c", tree.get(0b10).getBlocking(tree))
78+
assertEquals("d", tree.get(0b11).getBlocking(tree))
7879
}
7980
}
8081

datastructures/src/commonTest/kotlin/org/modelix/datastructures/HamtTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.modelix.datastructures.hamt.HamtNode
55
import org.modelix.datastructures.hamt.HamtTree
66
import org.modelix.datastructures.objects.IObjectGraph
77
import org.modelix.datastructures.objects.StringDataTypeConfiguration
8+
import org.modelix.streams.getBlocking
89
import kotlin.random.Random
910
import kotlin.test.Test
1011
import kotlin.test.assertEquals
@@ -32,21 +33,21 @@ class HamtTest {
3233
if (expected.isNotEmpty()) {
3334
val key = expected.keys.random(rand)
3435
// println("remove $key")
35-
tree = tree.remove(key).getSynchronous()
36+
tree = tree.remove(key).getBlocking(tree)
3637
expected.remove(key)
3738
}
3839
}
3940
else -> {
4041
val key = "k" + rand.nextInt(keyRange).toString()
4142
val value = "v" + rand.nextInt(valueRange).toString()
4243
// println("insert $key -> $value")
43-
tree = tree.put(key, value).getSynchronous()
44+
tree = tree.put(key, value).getBlocking(tree)
4445
expected[key] = value
4546
}
4647
}
4748

4849
for (entry in expected.entries) {
49-
assertEquals(entry.value, tree.get(entry.key).getSynchronous(), "for key ${entry.key}")
50+
assertEquals(entry.value, tree.get(entry.key).getBlocking(tree), "for key ${entry.key}")
5051
}
5152
}
5253
}

datastructures/src/commonTest/kotlin/org/modelix/datastructures/PatriciaTrieTest.kt

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.modelix.datastructures
22

33
import org.modelix.datastructures.objects.IObjectGraph
44
import org.modelix.datastructures.patricia.PatriciaTrie
5+
import org.modelix.streams.getBlocking
56
import kotlin.random.Random
67
import kotlin.test.Test
78
import kotlin.test.assertEquals
@@ -24,10 +25,10 @@ class PatriciaTrieTest {
2425

2526
fun assertTree() {
2627
val expectedEntries = addedEntries.associateWith { values[it]!! }
27-
val actualEntries = tree.getAll().toList().getSynchronous().toMap()
28+
val actualEntries = tree.getAll().toList().getBlocking(tree).toMap()
2829
assertEquals(expectedEntries, actualEntries)
2930

30-
val expectedTree = addedEntries.fold(initialTree) { acc, it -> acc.put(it, values[it]!!).getSynchronous() }
31+
val expectedTree = addedEntries.fold(initialTree) { acc, it -> acc.put(it, values[it]!!).getBlocking(tree) }
3132
assertEquals(expectedTree, tree)
3233

3334
assertEquals(expectedTree.asObject().getHash(), tree.asObject().getHash())
@@ -38,13 +39,13 @@ class PatriciaTrieTest {
3839
val key = removedEntries.random()
3940
removedEntries.remove(key)
4041
addedEntries.add(key)
41-
tree = tree.put(key, values[key]!!).getSynchronous()
42+
tree = tree.put(key, values[key]!!).getBlocking(tree)
4243
assertTree()
4344
} else {
4445
val key = addedEntries.random()
4546
removedEntries.add(key)
4647
addedEntries.remove(key)
47-
tree = tree.remove(key).getSynchronous()
48+
tree = tree.remove(key).getBlocking(tree)
4849
assertTree()
4950
}
5051
}
@@ -55,55 +56,55 @@ class PatriciaTrieTest {
5556
@Test
5657
fun `slice with shorter prefix and single entry`() {
5758
var tree: PatriciaTrie<String, String> = PatriciaTrie.withStrings(IObjectGraph.FREE_FLOATING)
58-
tree = tree.put("abcdef", "1").getSynchronous()
59+
tree = tree.put("abcdef", "1").getBlocking(tree)
5960

60-
assertEquals("1", tree.slice("abc").flatMapZeroOrOne { it.get("abcdef") }.getSynchronous())
61+
assertEquals("1", tree.slice("abc").flatMapZeroOrOne { it.get("abcdef") }.getBlocking(tree))
6162
}
6263

6364
@Test
6465
fun `slice with shorter prefix and two entries`() {
6566
var tree: PatriciaTrie<String, String> = PatriciaTrie.withStrings(IObjectGraph.FREE_FLOATING)
66-
tree = tree.put("abcdef", "1").getSynchronous()
67-
tree = tree.put("abcdeg", "2").getSynchronous()
67+
tree = tree.put("abcdef", "1").getBlocking(tree)
68+
tree = tree.put("abcdeg", "2").getBlocking(tree)
6869

69-
assertEquals("1", tree.slice("abc").flatMapZeroOrOne { it.get("abcdef") }.getSynchronous())
70-
assertEquals("2", tree.slice("abc").flatMapZeroOrOne { it.get("abcdeg") }.getSynchronous())
70+
assertEquals("1", tree.slice("abc").flatMapZeroOrOne { it.get("abcdef") }.getBlocking(tree))
71+
assertEquals("2", tree.slice("abc").flatMapZeroOrOne { it.get("abcdeg") }.getBlocking(tree))
7172
}
7273

7374
@Test
7475
fun `slice with prefix between two entries`() {
7576
var tree: PatriciaTrie<String, String> = PatriciaTrie.withStrings(IObjectGraph.FREE_FLOATING)
76-
tree = tree.put("ab", "1").getSynchronous()
77-
tree = tree.put("abcdef", "2").getSynchronous()
77+
tree = tree.put("ab", "1").getBlocking(tree)
78+
tree = tree.put("abcdef", "2").getBlocking(tree)
7879

79-
assertEquals(null, tree.slice("abcd").flatMapZeroOrOne { it.get("ab") }.getSynchronous())
80-
assertEquals("2", tree.slice("abcd").flatMapZeroOrOne { it.get("abcdef") }.getSynchronous())
80+
assertEquals(null, tree.slice("abcd").flatMapZeroOrOne { it.get("ab") }.getBlocking(tree))
81+
assertEquals("2", tree.slice("abcd").flatMapZeroOrOne { it.get("abcdef") }.getBlocking(tree))
8182
}
8283

8384
@Test
8485
fun `slice with one before and two after`() {
8586
var tree: PatriciaTrie<String, String> = PatriciaTrie.withStrings(IObjectGraph.FREE_FLOATING)
86-
tree = tree.put("ab", "1").getSynchronous()
87-
tree = tree.put("abcdef", "2").getSynchronous()
88-
tree = tree.put("abcdeg", "3").getSynchronous()
87+
tree = tree.put("ab", "1").getBlocking(tree)
88+
tree = tree.put("abcdef", "2").getBlocking(tree)
89+
tree = tree.put("abcdeg", "3").getBlocking(tree)
8990

90-
assertEquals(null, tree.slice("abcd").flatMapZeroOrOne { it.get("ab") }.getSynchronous())
91-
assertEquals("2", tree.slice("abcd").flatMapZeroOrOne { it.get("abcdef") }.getSynchronous())
92-
assertEquals("3", tree.slice("abcd").flatMapZeroOrOne { it.get("abcdeg") }.getSynchronous())
91+
assertEquals(null, tree.slice("abcd").flatMapZeroOrOne { it.get("ab") }.getBlocking(tree))
92+
assertEquals("2", tree.slice("abcd").flatMapZeroOrOne { it.get("abcdef") }.getBlocking(tree))
93+
assertEquals("3", tree.slice("abcd").flatMapZeroOrOne { it.get("abcdeg") }.getBlocking(tree))
9394
}
9495

9596
@Test
9697
fun `slice with two before and two after at existing split`() {
9798
var tree: PatriciaTrie<String, String> = PatriciaTrie.withStrings(IObjectGraph.FREE_FLOATING)
98-
tree = tree.put("a", "0").getSynchronous()
99-
tree = tree.put("ab", "1").getSynchronous()
100-
tree = tree.put("abcdef", "2").getSynchronous()
101-
tree = tree.put("abcdeg", "3").getSynchronous()
102-
103-
assertEquals(null, tree.slice("ab").flatMapZeroOrOne { it.get("a") }.getSynchronous())
104-
assertEquals("1", tree.slice("ab").flatMapZeroOrOne { it.get("ab") }.getSynchronous())
105-
assertEquals("2", tree.slice("ab").flatMapZeroOrOne { it.get("abcdef") }.getSynchronous())
106-
assertEquals("3", tree.slice("ab").flatMapZeroOrOne { it.get("abcdeg") }.getSynchronous())
99+
tree = tree.put("a", "0").getBlocking(tree)
100+
tree = tree.put("ab", "1").getBlocking(tree)
101+
tree = tree.put("abcdef", "2").getBlocking(tree)
102+
tree = tree.put("abcdeg", "3").getBlocking(tree)
103+
104+
assertEquals(null, tree.slice("ab").flatMapZeroOrOne { it.get("a") }.getBlocking(tree))
105+
assertEquals("1", tree.slice("ab").flatMapZeroOrOne { it.get("ab") }.getBlocking(tree))
106+
assertEquals("2", tree.slice("ab").flatMapZeroOrOne { it.get("abcdef") }.getBlocking(tree))
107+
assertEquals("3", tree.slice("ab").flatMapZeroOrOne { it.get("abcdeg") }.getBlocking(tree))
107108
}
108109

109110
@Test
@@ -126,16 +127,16 @@ class PatriciaTrieTest {
126127
"abcdC",
127128
)
128129
for (key in initialKeys) {
129-
tree = tree.put(key, "value of $key").getSynchronous()
130+
tree = tree.put(key, "value of $key").getBlocking(tree)
130131
}
131132

132133
assertEquals(
133134
initialKeys.associateWith { "value of $it" },
134-
tree.getAll().toList().getSynchronous().toMap(),
135+
tree.getAll().toList().getBlocking(tree).toMap(),
135136
)
136137

137138
for (key in initialKeys) {
138-
assertEquals("value of $key", tree.get(key).getSynchronous())
139+
assertEquals("value of $key", tree.get(key).getBlocking(tree))
139140
}
140141

141142
var replacementTree = PatriciaTrie.withStrings(IObjectGraph.FREE_FLOATING)
@@ -150,16 +151,16 @@ class PatriciaTrieTest {
150151
"abcdB93",
151152
)
152153
for (key in replacementKeys) {
153-
replacementTree = replacementTree.put(key, "new value of $key").getSynchronous()
154+
replacementTree = replacementTree.put(key, "new value of $key").getBlocking(tree)
154155
}
155-
tree = tree.replaceSlice("abcdB", replacementTree).getSynchronous()
156+
tree = tree.replaceSlice("abcdB", replacementTree).getBlocking(tree)
156157

157158
assertEquals(
158159
initialKeys.filterNot { it.startsWith("abcdB") }.map { it to "value of $it" }
159160
.plus(replacementKeys.map { it to "new value of $it" })
160161
.sortedBy { it.first }
161162
.joinToString("\n") { "${it.first} -> ${it.second}" },
162-
tree.getAll().toList().getSynchronous().sortedBy { it.first }.joinToString("\n") { "${it.first} -> ${it.second}" },
163+
tree.getAll().toList().getBlocking(tree).sortedBy { it.first }.joinToString("\n") { "${it.first} -> ${it.second}" },
163164
)
164165
}
165166
}

model-datastructure/src/commonTest/kotlin/HamtTest.kt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.modelix.model.lazy.createObjectStoreCache
1010
import org.modelix.model.persistent.CPNode
1111
import org.modelix.model.persistent.MapBaseStore
1212
import org.modelix.streams.IStream
13+
import org.modelix.streams.getBlocking
1314
import kotlin.random.Random
1415
import kotlin.test.Test
1516
import kotlin.test.assertEquals
@@ -34,25 +35,25 @@ class HamtTest {
3435
// add entry
3536
val key = rand.nextInt(1000).toLong()
3637
val value = rand.nextLong()
37-
hamt = hamt!!.put(key, createEntry(value, graph)).getSynchronous()
38+
hamt = hamt.put(key, createEntry(value, graph)).getBlocking(hamt)
3839
expectedMap[key] = value
3940
} else {
4041
val keys: List<Long> = ArrayList(expectedMap.keys)
4142
val key = keys[rand.nextInt(keys.size)]
4243
if (rand.nextBoolean()) {
4344
// remove entry
44-
hamt = hamt!!.remove(key).getSynchronous()
45+
hamt = hamt.remove(key).getBlocking(hamt)
4546
expectedMap.remove(key)
4647
} else {
4748
// replace entry
4849
val value = rand.nextLong()
49-
hamt = hamt!!.put(key, createEntry(value, graph)).getSynchronous()
50+
hamt = hamt.put(key, createEntry(value, graph)).getBlocking(hamt)
5051
expectedMap[key] = value
5152
}
5253
}
5354
storeCache.clearCache()
5455
for ((key, value) in expectedMap) {
55-
assertEquals(value, hamt!!.get(key).flatMapZeroOrOne { it.resolve() }.getSynchronous()!!.data.id)
56+
assertEquals(value, hamt.get(key).flatMapZeroOrOne { it.resolve() }.getBlocking(hamt)!!.data.id)
5657
}
5758
}
5859
}
@@ -82,21 +83,21 @@ class HamtTest {
8283
valueConfig = ObjectReferenceDataTypeConfiguration(graph, CPNode),
8384
)
8485
var hamt = HamtTree(HamtInternalNode.createEmpty(config))
85-
var getId = { e: IStream.ZeroOrOne<ObjectReference<CPNode>> -> e.flatMapZeroOrOne { it.resolve() }.getSynchronous()!!.data.id }
86+
var getId = { e: IStream.ZeroOrOne<ObjectReference<CPNode>> -> e.flatMapZeroOrOne { it.resolve() }.getBlocking(hamt)!!.data.id }
8687

87-
hamt = hamt!!.put(965L, createEntry(-6579471327666419615, graph)).getSynchronous()
88-
hamt = hamt!!.put(949L, createEntry(4912341421267007347, graph)).getSynchronous()
89-
assertEquals(4912341421267007347, getId(hamt!!.get(949L)))
90-
hamt = hamt!!.put(260L, createEntry(4166750678024106842, graph)).getSynchronous()
91-
assertEquals(4166750678024106842, getId(hamt!!.get(260L)))
92-
hamt = hamt!!.put(794L, createEntry(5492533034562136353, graph)).getSynchronous()
93-
hamt = hamt!!.put(104L, createEntry(-6505928823483070382, graph)).getSynchronous()
94-
hamt = hamt!!.put(47L, createEntry(3122507882718949737, graph)).getSynchronous()
95-
hamt = hamt!!.put(693L, createEntry(-2086105010854963537, graph)).getSynchronous()
88+
hamt = hamt.put(965L, createEntry(-6579471327666419615, graph)).getBlocking(hamt)
89+
hamt = hamt.put(949L, createEntry(4912341421267007347, graph)).getBlocking(hamt)
90+
assertEquals(4912341421267007347, getId(hamt.get(949L)))
91+
hamt = hamt.put(260L, createEntry(4166750678024106842, graph)).getBlocking(hamt)
92+
assertEquals(4166750678024106842, getId(hamt.get(260L)))
93+
hamt = hamt.put(794L, createEntry(5492533034562136353, graph)).getBlocking(hamt)
94+
hamt = hamt.put(104L, createEntry(-6505928823483070382, graph)).getBlocking(hamt)
95+
hamt = hamt.put(47L, createEntry(3122507882718949737, graph)).getBlocking(hamt)
96+
hamt = hamt.put(693L, createEntry(-2086105010854963537, graph)).getBlocking(hamt)
9697
storeCache.clearCache()
9798
// assertEquals(69239088, (hamt!!.getData() as CPHamtInternal).bitmap)
9899
// assertEquals(6, (hamt!!.getData() as CPHamtInternal).children.count())
99-
assertEquals(-2086105010854963537, getId(hamt!!.get(693L)))
100+
assertEquals(-2086105010854963537, getId(hamt.get(693L)))
100101
}
101102

102103
/**
@@ -129,8 +130,8 @@ class HamtTest {
129130

130131
for (i in 1..10) {
131132
var map = emptyMap
132-
entries.entries.shuffled(rand).forEach { map = map.put(it.key, it.value).getSynchronous()!! }
133-
keysToRemove.forEach { map = map.remove(it).getSynchronous()!! }
133+
entries.entries.shuffled(rand).forEach { map = map.put(it.key, it.value).getBlocking(map) }
134+
keysToRemove.forEach { map = map.remove(it).getBlocking(map) }
134135
val hash = map.asObject().getHashString()
135136
if (i == 1) {
136137
expectedHash = hash

0 commit comments

Comments
 (0)