@@ -30,30 +30,47 @@ class OTBranch(
30
30
private val idGenerator : IIdGenerator ,
31
31
private val store : IDeserializingKeyValueStore ,
32
32
) : IBranch {
33
- private var operations: MutableList <IAppliedOperation > = ArrayList ()
34
- private var treeForOperations: ITree = branch.computeRead { branch.transaction.tree }
35
- private val operationsLock = Any ()
33
+ private var currentOperations: MutableList <IAppliedOperation > = ArrayList ()
34
+ private val completedChanges: MutableList <Pair <List <IAppliedOperation >, ITree >> = ArrayList ()
36
35
private val id: String = branch.getId()
37
36
38
37
fun operationApplied (op : IAppliedOperation ) {
39
- runSynchronized(operationsLock) {
40
- operations.add(op)
41
- treeForOperations = transaction.tree
42
- }
38
+ check(canWrite()) { " Only allowed inside a write transaction" }
39
+ currentOperations.add(op)
43
40
}
44
41
45
42
override fun getId (): String {
46
43
return id
47
44
}
48
45
46
+ @Deprecated(" renamed to getPendingChanges()" , ReplaceWith (" getPendingChanges()" ))
49
47
val operationsAndTree: Pair <List <IAppliedOperation >, ITree >
50
48
get() {
51
- runSynchronized(operationsLock) {
52
- val newOperations: List <IAppliedOperation > = operations
53
- operations = ArrayList ()
54
- return Pair (newOperations, treeForOperations)
49
+ return runSynchronized(completedChanges) {
50
+ val result = when (completedChanges.size) {
51
+ 0 -> emptyList<IAppliedOperation >() to computeReadT { it.tree }
52
+ 1 -> completedChanges[0 ]
53
+ else -> completedChanges.flatMap { it.first } to completedChanges.last().second
54
+ }
55
+ completedChanges.clear()
56
+ result
57
+ }
58
+ }
59
+
60
+ /* *
61
+ * @return the operations applied to the branch since the last call of this function and the resulting ITree.
62
+ */
63
+ fun getPendingChanges (): Pair <List <IAppliedOperation >, ITree> {
64
+ return runSynchronized(completedChanges) {
65
+ val result = when (completedChanges.size) {
66
+ 0 -> emptyList<IAppliedOperation >() to computeReadT { it.tree }
67
+ 1 -> completedChanges[0 ]
68
+ else -> completedChanges.flatMap { it.first } to completedChanges.last().second
55
69
}
70
+ completedChanges.clear()
71
+ result
56
72
}
73
+ }
57
74
58
75
override fun addListener (l : IBranchListener ) {
59
76
branch.addListener(l)
@@ -78,7 +95,21 @@ class OTBranch(
78
95
79
96
override fun <T > computeWrite (computable : () -> T ): T {
80
97
checkNotEDT()
81
- return branch.computeWrite(computable)
98
+ return if (canWrite()) {
99
+ branch.computeWrite(computable)
100
+ } else {
101
+ branch.computeWriteT { t ->
102
+ try {
103
+ val result = computable()
104
+ runSynchronized(completedChanges) {
105
+ completedChanges + = currentOperations to t.tree
106
+ }
107
+ result
108
+ } finally {
109
+ currentOperations = ArrayList ()
110
+ }
111
+ }
112
+ }
82
113
}
83
114
84
115
override val readTransaction: IReadTransaction
@@ -96,8 +127,7 @@ class OTBranch(
96
127
}
97
128
98
129
override fun runWrite (runnable : () -> Unit ) {
99
- checkNotEDT()
100
- branch.runWrite(runnable)
130
+ computeWrite(runnable)
101
131
}
102
132
103
133
fun wrap (t : ITransaction ): ITransaction {
0 commit comments