Skip to content

Commit 1a70d38

Browse files
author
Oleksandr Dzhychko
authored
Merge pull request #624 from modelix/MODELIX-831
fix(model-datastructure): make `MapBasedStore` thread safe
2 parents 603946a + 86a90ad commit 1a70d38

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

kotlin-utils/src/commonMain/kotlin/org/modelix/kotlin/utils/DataStructures.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ package org.modelix.kotlin.utils
2929
* Therefore, the memory efficient maps are used sparingly for only the very big maps.
3030
*/
3131
expect fun <K, V> createMemoryEfficientMap(): MutableMap<K, V>
32+
33+
expect fun <K, V> MutableMap<K, V>.toSynchronizedMap(): MutableMap<K, V>

kotlin-utils/src/jsMain/kotlin/org/modelix/kotlin/utils/DataStructures.js.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@
1717
package org.modelix.kotlin.utils
1818

1919
actual fun <K, V> createMemoryEfficientMap(): MutableMap<K, V> = HashMap()
20+
21+
actual fun <K, V> MutableMap<K, V>.toSynchronizedMap(): MutableMap<K, V> {
22+
// Because JS is single-threaded, no extra measures are needed.
23+
return this
24+
}

kotlin-utils/src/jvmMain/kotlin/org/modelix/kotlin/utils/DataStructures.jvm.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
package org.modelix.kotlin.utils
1818

1919
import gnu.trove.map.hash.THashMap
20+
import java.util.Collections
2021

2122
actual fun <K, V> createMemoryEfficientMap(): MutableMap<K, V> = THashMap()
23+
24+
actual fun <K, V> MutableMap<K, V>.toSynchronizedMap(): MutableMap<K, V> {
25+
return Collections.synchronizedMap(this)
26+
}

model-datastructure/src/commonMain/kotlin/org/modelix/model/persistent/MapBasedStore.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.modelix.model.persistent
1717

1818
import org.modelix.kotlin.utils.createMemoryEfficientMap
19+
import org.modelix.kotlin.utils.toSynchronizedMap
1920
import org.modelix.model.IKeyListener
2021
import org.modelix.model.IKeyValueStore
2122
import org.modelix.model.lazy.IBulkQuery
@@ -26,7 +27,7 @@ import org.modelix.model.lazy.NonBulkQuery
2627
open class MapBaseStore : MapBasedStore()
2728

2829
open class MapBasedStore : IKeyValueStore {
29-
private val map: MutableMap<String?, String?> = createMemoryEfficientMap()
30+
private val map = createMemoryEfficientMap<String?, String?>().toSynchronizedMap()
3031
override fun get(key: String): String? {
3132
return map[key]
3233
}

0 commit comments

Comments
 (0)