|
14 | 14 | package org.modelix.model.server.handlers
|
15 | 15 |
|
16 | 16 | import kotlinx.datetime.Clock
|
| 17 | +import org.apache.commons.collections4.map.LRUMap |
17 | 18 | import org.modelix.model.VersionMerger
|
18 | 19 | import org.modelix.model.api.IBranch
|
19 | 20 | import org.modelix.model.api.IReadTransaction
|
20 | 21 | import org.modelix.model.api.ITree
|
21 | 22 | import org.modelix.model.api.IdGeneratorDummy
|
22 | 23 | import org.modelix.model.api.PBranch
|
| 24 | +import org.modelix.model.api.runSynchronized |
23 | 25 | import org.modelix.model.lazy.BranchReference
|
24 | 26 | import org.modelix.model.lazy.CLTree
|
25 | 27 | import org.modelix.model.lazy.CLVersion
|
@@ -147,10 +149,18 @@ class RepositoriesManager(val client: LocalModelClient) {
|
147 | 149 | ?: throw IllegalStateException("No version found for branch '${branch.branchName}' in repository '${branch.repositoryId}'")
|
148 | 150 | }
|
149 | 151 |
|
| 152 | + private val deltaCache = LRUMap<Pair<String, String?>, Lazy<Map<String, String>>>(10) |
150 | 153 | fun computeDelta(versionHash: String, baseVersionHash: String?): Map<String, String> {
|
151 |
| - val version = CLVersion(versionHash, client.storeCache) |
152 |
| - val baseVersion = baseVersionHash?.let { CLVersion(it, client.storeCache) } |
153 |
| - return version.computeDelta(baseVersion) |
| 154 | + return runSynchronized(deltaCache) { |
| 155 | + deltaCache.getOrPut(versionHash to baseVersionHash) { |
| 156 | + // lazy { ... } allows to run the computation without locking deltaCache |
| 157 | + lazy { |
| 158 | + val version = CLVersion(versionHash, client.storeCache) |
| 159 | + val baseVersion = baseVersionHash?.let { CLVersion(it, client.storeCache) } |
| 160 | + version.computeDelta(baseVersion) |
| 161 | + } |
| 162 | + } |
| 163 | + }.value |
154 | 164 | }
|
155 | 165 |
|
156 | 166 | private fun branchKey(branch: BranchReference): String {
|
|
0 commit comments