Skip to content

Commit 9b3327a

Browse files
committed
spanset: simplify mergeSpans
Epic: none Release note: none
1 parent f886d2b commit 9b3327a

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

pkg/kv/kvserver/spanset/merge.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,23 @@ func mergeSpans(latches []Span) []Span {
3535
// Can only merge spans at the same timestamp.
3636
if cur.Timestamp != prev.Timestamp {
3737
r = append(r, cur)
38-
continue
39-
}
40-
if len(cur.EndKey) == 0 && len(prev.EndKey) == 0 {
41-
if cur.Key.Compare(prev.Key) != 0 {
42-
// [a, nil] merge [b, nil]
43-
r = append(r, cur)
44-
}
45-
continue
46-
}
47-
if len(prev.EndKey) == 0 {
48-
if cur.Key.Compare(prev.Key) == 0 {
49-
// [a, nil] merge [a, b]
50-
prev.EndKey = cur.EndKey
51-
} else {
52-
// [a, nil] merge [b, c]
53-
r = append(r, cur)
38+
} else if len(prev.EndKey) == 0 { // prev is a point key
39+
if !cur.Key.Equal(prev.Key) { // cur.Key > prev.Key
40+
r = append(r, cur) // [a] + [b,any) = [a], [b,any)
41+
} else { // [a] + [a,any) = [a,any)
42+
prev.EndKey = cur.EndKey // cur.EndKey >= prev.EndKey
5443
}
55-
continue
56-
}
57-
58-
if c := prev.EndKey.Compare(cur.Key); c >= 0 {
59-
if cur.EndKey != nil {
60-
if prev.EndKey.Compare(cur.EndKey) < 0 {
61-
// [a, c] merge [b, d]
62-
prev.EndKey = cur.EndKey
63-
}
64-
} else if c == 0 {
65-
// [a, b] merge [b, nil]
66-
prev.EndKey = cur.Key.Next()
44+
} else if c := cur.Key.Compare(prev.EndKey); c > 0 {
45+
r = append(r, cur) // [a,b) + [c,any) = [a,b), [c,any)
46+
} else if c == 0 {
47+
if len(cur.EndKey) == 0 { // cur is a point key
48+
prev.EndKey = cur.Key.Next() // [a,b) + [b] = [a,b.Next())
49+
} else if cur.EndKey.Compare(prev.EndKey) > 0 {
50+
prev.EndKey = cur.EndKey // [a,b) + [b,c) = [a,c)
6751
}
68-
continue
52+
} else if len(cur.EndKey) != 0 && cur.EndKey.Compare(prev.EndKey) > 0 {
53+
prev.EndKey = cur.EndKey // [a,c) + [b,d) = [a,d)
6954
}
70-
r = append(r, cur)
7155
}
7256
return r
7357
}

0 commit comments

Comments
 (0)