Skip to content

Commit 424ad26

Browse files
author
Oleksandr Dzhychko
committed
fix(model-datastructure): correctly calculate linear history between the same version
Previously, calculating the linear history between the same version resulted in linear history that contained more than only that one version. This in turn resulted in needles computations and deltas that were too big. Fixes: MODELIX-1025
1 parent 31d40c2 commit 424ad26

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

model-datastructure/src/commonMain/kotlin/org/modelix/model/LinearHistory.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class LinearHistory(private val baseVersionHash: String?) {
2424
private val byVersionDistanceFromGlobalRoot = mutableMapOf<CLVersion, Int>()
2525

2626
/**
27-
* Returns all versions between the [fromVersions] and a common version.
27+
* Returns all versions between the [fromVersions] (inclusive) and a common version (inclusive).
2828
* The common version may be identified by [baseVersionHash].
29-
* If no [baseVersionHash] is given, the common version wile be the first version
29+
* If no [baseVersionHash] is given, the common version will be the first version
3030
* aka the version without a [CLVersion.baseVersion].
3131
*
3232
* The order also ensures three properties:
@@ -35,14 +35,14 @@ class LinearHistory(private val baseVersionHash: String?) {
3535
* This means adding a version to the set of all versions will never change
3636
* the order of versions that were previously in the history.
3737
* For example, given versions 1, 2 and 3:
38-
* If 1 and 2 are ordered as (1, 2), ordering 1, 2 and 3 will never produce (2, 3, 1).
39-
* 3 can come anywhere (respecting the topological ordering), but 2 has to come after 1.
38+
* If 1 and 2 are ordered as (1, 2), ordering 1, 2 and x will never produce (2, x, 1).
39+
* x can come anywhere (respecting the topological ordering), but 2 has to come after 1.
4040
* 3. "Close versions are kept together"
41-
* Formally: A version that has only one child (ignoring) should always come before the child.
42-
* Example: 1 <- 2 <- 3 and 1 <- x, then [1, 2, 4, 3] is not allowed,
41+
* Formally: A version that has only one child should always come before the child.
42+
* Example: 1 <- 2 <- 3 and 1 <- x, then [1, 2, x, 3] is not allowed,
4343
* because 3 is the only child of 2.
44-
* Valid orders would be (1, x, 3, 4) and (1, x, 2, 3)
45-
* This is relevant for UnduOp and RedoOp.
44+
* Valid orders would be (1, 2, 3, x) and (1, x, 2, 3)
45+
* This is relevant for UndoOp and RedoOp.
4646
* See UndoTest.
4747
*/
4848
fun load(vararg fromVersions: CLVersion): List<CLVersion> {
@@ -128,6 +128,9 @@ class LinearHistory(private val baseVersionHash: String?) {
128128
}
129129

130130
private fun CLVersion.getParents(): List<CLVersion> {
131+
if (this.getContentHash() == baseVersionHash) {
132+
return emptyList()
133+
}
131134
val ancestors = if (isMerge()) {
132135
listOf(getMergedVersion1()!!, getMergedVersion2()!!)
133136
} else {

model-datastructure/src/commonTest/kotlin/LinearHistoryTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ import kotlin.test.assertTrue
2828
class LinearHistoryTest {
2929
private val initialTree = CLTree.builder(ObjectStoreCache(MapBasedStore())).repositoryId("LinearHistoryTest").build()
3030

31+
@Test
32+
fun baseVersionAndFromVersionAreIncluded() {
33+
val v1 = version(1, null)
34+
val v2 = version(2, v1)
35+
val v3 = version(3, v2)
36+
37+
assertHistory(v2, v3, listOf(v2, v3))
38+
}
39+
40+
@Test
41+
fun linearHistoryBetweenSameVersion() {
42+
val v1 = version(1, null)
43+
val v2 = version(2, v1)
44+
45+
assertHistory(v2, v2, listOf(v2))
46+
}
47+
48+
@Test
49+
fun linearHistoryBetweenAVersionAndItsBaseVersion() {
50+
val v1 = version(1, null)
51+
val v2 = version(2, v1)
52+
val v3 = version(3, v2)
53+
54+
assertHistory(v2, v3, listOf(v2, v3))
55+
}
56+
3157
@Test
3258
fun noCommonHistory() {
3359
val v20 = version(20, null)

0 commit comments

Comments
 (0)