Skip to content

Commit 289226e

Browse files
committed
spanset: test mergeSpans
Epic: none Release note: none
1 parent f9e764e commit 289226e

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

pkg/kv/kvserver/spanset/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ go_test(
2929
size = "small",
3030
srcs = [
3131
"batch_test.go",
32+
"merge_test.go",
3233
"spanset_test.go",
3334
],
3435
embed = [":spanset"],
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package spanset
7+
8+
import (
9+
"strconv"
10+
"strings"
11+
"testing"
12+
13+
"github.com/cockroachdb/cockroach/pkg/roachpb"
14+
"github.com/cockroachdb/cockroach/pkg/util/hlc"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
func TestMergeSpans(t *testing.T) {
19+
for _, tc := range []struct {
20+
spans string
21+
expected string
22+
}{
23+
{"", ""},
24+
{"a", "a"},
25+
{"a,b", "a,b"},
26+
{"b,a", "a,b"},
27+
{"a,a", "a"},
28+
{"a-b", "a-b"},
29+
{"a-b,b-c", "a-c"},
30+
{"a-c,a-b", "a-c"},
31+
{"a,b-c", "a,b-c"},
32+
{"a,a-c", "a-c"},
33+
{"a-c,b", "a-c"},
34+
{"a-c,c", "a-c\x00"},
35+
{"a-c,b-bb", "a-c"},
36+
{"a-c,b-c", "a-c"},
37+
{"a-c,b-d", "a-d"},
38+
39+
{"a-b,b-b\x00", "a-b\x00"},
40+
{"b,b\x00", "b,b\x00"}, // TODO(pav-kv): should merge here too
41+
{"b,b\x00-c", "b,b\x00-c"}, // TODO(pav-kv): should merge here too
42+
43+
{"a@10", "a@10"},
44+
{"a@10,b@10", "a@10,b@10"},
45+
{"a@10,b@20", "a@10,b@20"},
46+
{"b@20,a@10", "a@10,b@20"},
47+
{"a@10,a-b@10", "a-b@10"},
48+
{"a@10,a-b@20", "a@10,a-b@20"},
49+
{"a-b@20,b@20", "a-b\x00@20"},
50+
{"a-b@20,b@30", "a-b\x00@20,b@30"}, // FIXME: this is a bug
51+
{"a-c@20,m-n@10,c-o@10,o-z@20", "a-c@20,c-o@10,o-z@20"},
52+
} {
53+
t.Run("", func(t *testing.T) {
54+
spans := mergeSpans(makeSpans(t, tc.spans))
55+
expected := makeSpans(t, tc.expected)
56+
require.Equal(t, expected, spans, tc.spans)
57+
})
58+
}
59+
}
60+
61+
func makeSpan(t *testing.T, s string) Span {
62+
var ts hlc.Timestamp
63+
if idx := strings.IndexByte(s, '@'); idx != -1 {
64+
i, err := strconv.ParseInt(s[idx+1:], 10, 63)
65+
require.NoError(t, err)
66+
ts = hlc.Timestamp{WallTime: i}
67+
s = s[:idx]
68+
}
69+
parts := strings.Split(s, "-")
70+
if len(parts) != 2 {
71+
return Span{Span: roachpb.Span{Key: roachpb.Key(s)}, Timestamp: ts}
72+
}
73+
return Span{
74+
Span: roachpb.Span{
75+
Key: roachpb.Key(parts[0]),
76+
EndKey: roachpb.Key(parts[1]),
77+
},
78+
Timestamp: ts,
79+
}
80+
}
81+
82+
func makeSpans(t *testing.T, s string) []Span {
83+
var spans []Span
84+
for _, p := range strings.Split(s, ",") {
85+
spans = append(spans, makeSpan(t, p))
86+
}
87+
return spans
88+
}

0 commit comments

Comments
 (0)