|
1 | 1 | package org.modelix.model |
2 | 2 |
|
3 | 3 | import org.modelix.model.lazy.CLVersion |
4 | | -import org.modelix.model.lazy.IDeserializingKeyValueStore |
5 | | -import org.modelix.model.lazy.KVEntryReference |
6 | | -import org.modelix.model.persistent.CPVersion |
7 | 4 |
|
8 | | -/** |
9 | | - * Was introduced in https://github.com/modelix/modelix/commit/19c74bed5921028af3ac3ee9d997fc1c4203ad44 |
10 | | - * together with the UndoOp. The idea is that an undo should only revert changes if there is no other change that relies |
11 | | - * on it. In that case the undo should do nothing, to not indirectly undo newer changes. |
12 | | - * For example, if you added a node and someone else started changing properties on the that node, your undo should not |
13 | | - * remove the node to not lose the property changes. |
14 | | - * This requires the versions to be ordered in a way that the undo appears later. |
15 | | - */ |
16 | 5 | class LinearHistory(val baseVersionHash: String?) { |
17 | 6 |
|
18 | | - val version2directDescendants: MutableMap<Long, Set<Long>> = HashMap() |
19 | | - val versions: MutableMap<Long, CLVersion> = LinkedHashMap() |
20 | | - |
21 | 7 | /** |
22 | | - * @param fromVersions it is assumed that the versions are sorted by the oldest version first. When merging a new |
23 | | - * version into an existing one the new version should appear after the existing one. The resulting order |
24 | | - * will prefer existing versions to new ones, meaning during the conflict resolution the existing changes |
25 | | - * have a higher probability of surviving. |
26 | | - * @returns oldest version first |
| 8 | + * Order all versions descending from any versions in [[fromVersions]] topologically. |
| 9 | + * This means that a version must come after all its descendants. |
| 10 | + * Returns the ordered versions starting with the earliest version. |
27 | 11 | */ |
28 | | - fun load(vararg fromVersions: CLVersion): List<CLVersion> { |
29 | | - for (fromVersion in fromVersions) { |
30 | | - collect(fromVersion) |
31 | | - } |
32 | | - |
33 | | - var result: List<Long> = emptyList() |
34 | | - |
35 | | - for (version in versions.values.filter { !it.isMerge() }.sortedBy { it.id }) { |
36 | | - val descendantIds = collectAllDescendants(version.id).filter { !versions[it]!!.isMerge() }.sorted().toSet() |
37 | | - val idsInResult = result.toHashSet() |
38 | | - if (idsInResult.contains(version.id)) { |
39 | | - result = |
40 | | - result + |
41 | | - descendantIds.filter { !idsInResult.contains(it) } |
42 | | - } else { |
43 | | - result = |
44 | | - result.filter { !descendantIds.contains(it) } + |
45 | | - version.id + |
46 | | - result.filter { descendantIds.contains(it) } + |
47 | | - descendantIds.filter { !idsInResult.contains(it) } |
| 12 | + fun loadLazy(vararg fromVersions: CLVersion) = sequence { |
| 13 | + // The algorithm sorts the versions topologically. |
| 14 | + // It performs a depth-first search. |
| 15 | + // It is implemented as an iterative algorithm with a stack. |
| 16 | + |
| 17 | + val stack = ArrayDeque<CLVersion>() |
| 18 | + val visited = mutableSetOf<CLVersion>() |
| 19 | + |
| 20 | + // Ensure deterministic merging, |
| 21 | + // by putting versions with lower id before versions with higher id. |
| 22 | + fromVersions.sortedBy { it.id }.forEach { fromVersion -> |
| 23 | + // Not putting fromVersions directly on the stack and checking visited.contains(fromVersion) , |
| 24 | + // ensures the algorithm terminates if one version in `fromVersion` |
| 25 | + // is a descendant of another version in `fromVersion` |
| 26 | + if (!visited.contains(fromVersion)) { |
| 27 | + stack.addLast(fromVersion) |
48 | 28 | } |
49 | | - } |
50 | | - return result.map { versions[it]!! } |
51 | | - } |
52 | | - |
53 | | - private fun collectAllDescendants(root: Long): Set<Long> { |
54 | | - val result = LinkedHashSet<Long>() |
55 | | - var previousSize = 0 |
56 | | - result += root |
57 | | - |
58 | | - while (previousSize != result.size) { |
59 | | - val nextElements = result.asSequence().drop(previousSize).toList() |
60 | | - previousSize = result.size |
61 | | - for (ancestor in nextElements) { |
62 | | - version2directDescendants[ancestor]?.let { result += it } |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - return result.drop(1).toSet() |
67 | | - } |
68 | | - |
69 | | - private fun collect(root: CLVersion) { |
70 | | - if (root.getContentHash() == baseVersionHash) return |
71 | | - |
72 | | - var previousSize = versions.size |
73 | | - versions[root.id] = root |
74 | | - |
75 | | - while (previousSize != versions.size) { |
76 | | - val nextElements = versions.asSequence().drop(previousSize).map { it.value }.toList() |
77 | | - previousSize = versions.size |
78 | | - |
79 | | - for (descendant in nextElements) { |
80 | | - val ancestors = if (descendant.isMerge()) { |
81 | | - sequenceOf( |
82 | | - getVersion(descendant.data!!.mergedVersion1!!, descendant.store), |
83 | | - getVersion(descendant.data!!.mergedVersion2!!, descendant.store), |
84 | | - ) |
| 29 | + while (stack.isNotEmpty()) { |
| 30 | + val version = stack.last() |
| 31 | + val versionWasVisited = !visited.add(version) |
| 32 | + if (versionWasVisited) { |
| 33 | + stack.removeLast() |
| 34 | + if (!version.isMerge()) { |
| 35 | + yield(version) |
| 36 | + } |
| 37 | + } |
| 38 | + val descendants = if (version.isMerge()) { |
| 39 | + // Put version 1 last, so that is processed first. |
| 40 | + // We are using a stack and the last version is viewed first. |
| 41 | + listOf(version.getMergedVersion2()!!, version.getMergedVersion1()!!) |
85 | 42 | } else { |
86 | | - sequenceOf(descendant.baseVersion) |
87 | | - }.filterNotNull().filter { it.getContentHash() != baseVersionHash }.toList() |
88 | | - for (ancestor in ancestors) { |
89 | | - versions[ancestor.id] = ancestor |
90 | | - version2directDescendants[ancestor.id] = (version2directDescendants[ancestor.id] ?: emptySet()) + setOf(descendant.id) |
| 43 | + listOfNotNull(version.baseVersion) |
91 | 44 | } |
| 45 | + // Ignore descendant, if it is a base version. |
| 46 | + val relevantDescendants = descendants.filter { it.getContentHash() != baseVersionHash } |
| 47 | + // Ignore already visited descendants. |
| 48 | + val nonCheckedDescendants = relevantDescendants.filterNot { visited.contains(it) } |
| 49 | + nonCheckedDescendants.forEach { stack.addLast(it) } |
92 | 50 | } |
93 | 51 | } |
94 | 52 | } |
95 | 53 |
|
96 | | - private fun getVersion(hash: KVEntryReference<CPVersion>, store: IDeserializingKeyValueStore): CLVersion { |
97 | | - return CLVersion(hash.getValue(store), store) |
| 54 | + /** |
| 55 | + * Same as [[loadLazy]], but returning as a list instead of a lazy sequence. |
| 56 | + */ |
| 57 | + fun load(vararg fromVersions: CLVersion): List<CLVersion> { |
| 58 | + return loadLazy(*fromVersions).toList() |
98 | 59 | } |
99 | 60 | } |
0 commit comments