@@ -34,13 +34,18 @@ import kotlinx.coroutines.launch
34
34
import kotlinx.serialization.json.Json
35
35
import org.modelix.model.IVersion
36
36
import org.modelix.model.api.IIdGenerator
37
+ import org.modelix.model.api.INode
37
38
import org.modelix.model.api.IdGeneratorDummy
39
+ import org.modelix.model.api.TreePointer
40
+ import org.modelix.model.api.getRootNode
38
41
import org.modelix.model.client.IdGenerator
39
42
import org.modelix.model.lazy.BranchReference
43
+ import org.modelix.model.lazy.CLTree
40
44
import org.modelix.model.lazy.CLVersion
41
45
import org.modelix.model.lazy.ObjectStoreCache
42
46
import org.modelix.model.lazy.RepositoryId
43
47
import org.modelix.model.lazy.computeDelta
48
+ import org.modelix.model.operations.OTBranch
44
49
import org.modelix.model.persistent.HashUtil
45
50
import org.modelix.model.persistent.MapBasedStore
46
51
import org.modelix.model.server.api.v2.VersionDelta
@@ -307,3 +312,25 @@ private fun URLBuilder.appendPathSegmentsEncodingSlash(vararg components: String
307
312
}
308
313
309
314
fun VersionDelta.getAllObjects (): Map <String , String > = objectsMap + objects.associateBy { HashUtil .sha256(it) }
315
+
316
+ /* *
317
+ * Performs a write transaction on the root node of the given branch.
318
+ */
319
+ suspend fun <T > IModelClientV2.runWrite (branchRef : BranchReference , body : (INode ) -> T ): T {
320
+ val client = this
321
+ val baseVersion = client.pull(branchRef, null )
322
+ val branch = OTBranch (TreePointer (baseVersion.getTree(), client.getIdGenerator()), client.getIdGenerator(), (client as ModelClientV2 ).store)
323
+ val result = branch.computeWrite {
324
+ body(branch.getRootNode())
325
+ }
326
+ val (ops, newTree) = branch.getPendingChanges()
327
+ val newVersion = CLVersion .createRegularVersion(
328
+ id = client.getIdGenerator().generate(),
329
+ author = client.getUserId(),
330
+ tree = newTree as CLTree ,
331
+ baseVersion = baseVersion as CLVersion ? ,
332
+ operations = ops.map { it.getOriginalOp() }.toTypedArray(),
333
+ )
334
+ client.push(branchRef, newVersion, baseVersion)
335
+ return result
336
+ }
0 commit comments