Skip to content

Commit f886d2b

Browse files
committed
roachpb: simplify MergeSpans
Epic: none Release note: none
1 parent 1ed2fa1 commit f886d2b

File tree

2 files changed

+26
-46
lines changed

2 files changed

+26
-46
lines changed

pkg/roachpb/merge_spans.go

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,49 +38,27 @@ func MergeSpans(spans []Span) ([]Span, bool) {
3838

3939
for _, cur := range spans[1:] {
4040
prev := &r[len(r)-1]
41-
if len(cur.EndKey) == 0 && len(prev.EndKey) == 0 {
42-
if cur.Key.Compare(prev.Key) != 0 {
43-
// [a, nil] merge [b, nil]
44-
r = append(r, cur)
41+
if len(prev.EndKey) == 0 { // prev is a point key
42+
if !cur.Key.Equal(prev.Key) { // cur.Key > prev.Key
43+
r = append(r, cur) // [a] + [b,any) = [a], [b,any)
4544
} else {
46-
// [a, nil] merge [a, nil]
47-
distinct = false
45+
distinct = false // [a] + [a,any) = [a,any)
46+
prev.EndKey = cur.EndKey // cur.EndKey >= prev.EndKey
4847
}
49-
continue
50-
}
51-
if len(prev.EndKey) == 0 {
52-
if cur.Key.Compare(prev.Key) == 0 {
53-
// [a, nil] merge [a, b]
54-
prev.EndKey = cur.EndKey
55-
distinct = false
56-
} else {
57-
// [a, nil] merge [b, c]
58-
r = append(r, cur)
48+
} else if c := cur.Key.Compare(prev.EndKey); c > 0 {
49+
r = append(r, cur) // // [a,b) + [c,any) = [a,b), [c,any)
50+
} else if c == 0 {
51+
if len(cur.EndKey) == 0 { // cur is a point key
52+
prev.EndKey = cur.Key.Next() // [a,b) + [b] = [a,b.Next())
53+
} else if cur.EndKey.Compare(prev.EndKey) > 0 {
54+
prev.EndKey = cur.EndKey // [a,b) + [b,c) = [a,c)
5955
}
60-
continue
61-
}
62-
if c := prev.EndKey.Compare(cur.Key); c >= 0 {
63-
if cur.EndKey != nil {
64-
if prev.EndKey.Compare(cur.EndKey) < 0 {
65-
// [a, c] merge [b, d]
66-
prev.EndKey = cur.EndKey
67-
if c > 0 {
68-
distinct = false
69-
}
70-
} else {
71-
// [a, c] merge [b, c]
72-
distinct = false
73-
}
74-
} else if c == 0 {
75-
// [a, b] merge [b, nil]
76-
prev.EndKey = cur.Key.Next()
77-
} else {
78-
// [a, c] merge [b, nil]
79-
distinct = false
56+
} else {
57+
distinct = false // cur.Key is contained in prev
58+
if len(cur.EndKey) != 0 && cur.EndKey.Compare(prev.EndKey) > 0 {
59+
prev.EndKey = cur.EndKey // [a,c) + [b,d) = [a,d)
8060
}
81-
continue
8261
}
83-
r = append(r, cur)
8462
}
8563
return r, distinct
8664
}

pkg/roachpb/merge_spans_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func makeSpans(s string) Spans {
3434
}
3535

3636
func TestMergeSpans(t *testing.T) {
37-
testCases := []struct {
37+
for _, tc := range []struct {
3838
spans string
3939
expected string
4040
distinct bool
@@ -53,13 +53,15 @@ func TestMergeSpans(t *testing.T) {
5353
{"a-c,c", "a-c\x00", true},
5454
{"a-c,b-bb", "a-c", false},
5555
{"a-c,b-c", "a-c", false},
56-
}
57-
for _, c := range testCases {
58-
spans := makeSpans(c.spans)
59-
spans, distinct := MergeSpans(spans)
60-
expected := makeSpans(c.expected)
61-
require.Equal(t, expected, spans)
62-
require.Equal(t, c.distinct, distinct)
56+
{"a-c,b-d", "a-d", false},
57+
} {
58+
t.Run("", func(t *testing.T) {
59+
spans := makeSpans(tc.spans)
60+
spans, distinct := MergeSpans(spans)
61+
expected := makeSpans(tc.expected)
62+
require.Equal(t, expected, spans)
63+
require.Equal(t, tc.distinct, distinct)
64+
})
6365
}
6466
}
6567

0 commit comments

Comments
 (0)