@@ -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}
0 commit comments