Skip to content

Commit ddc5eba

Browse files
committed
db: use stdlib slices package in level checker
Clean up and simplify some code paths in the level checker through using the slices package introduced in Go 1.21. This code predates Go 1.21 and can be simplified.
1 parent 23ae0dd commit ddc5eba

File tree

1 file changed

+16
-55
lines changed

1 file changed

+16
-55
lines changed

level_checker.go

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package pebble
66

77
import (
8+
stdcmp "cmp"
89
"context"
910
"fmt"
1011
"io"
1112
"iter"
13+
"slices"
1214
"sort"
1315

1416
"github.com/cockroachdb/errors"
@@ -298,9 +300,9 @@ func (m *simpleMergingIter) handleVisiblePoint(
298300
// - Collect the start and end user keys from all these tombstones
299301
// (collectAllUserKey()) and use them to fragment all the tombstones
300302
// (fragmentUsingUserKey()).
301-
// - Sort tombstones by start key and decreasing seqnum
302-
// (tombstonesByStartKeyAndSeqnum) - all tombstones that have the same start
303-
// key will have the same end key because they have been fragmented.
303+
// - Sort tombstones by start key and decreasing seqnum (all tombstones that
304+
// have the same start key will have the same end key because they have been
305+
// fragmented)
304306
// - Iterate and check (iterateAndCheckTombstones()).
305307
//
306308
// Note that this simple approach requires holding all the tombstones across all
@@ -316,33 +318,15 @@ type tombstoneWithLevel struct {
316318
fileNum base.FileNum
317319
}
318320

319-
// For sorting tombstoneWithLevels in increasing order of start UserKey and
320-
// for the same start UserKey in decreasing order of seqnum.
321-
type tombstonesByStartKeyAndSeqnum struct {
322-
cmp Compare
323-
buf []tombstoneWithLevel
324-
}
325-
326-
func (v *tombstonesByStartKeyAndSeqnum) Len() int { return len(v.buf) }
327-
func (v *tombstonesByStartKeyAndSeqnum) Less(i, j int) bool {
328-
less := v.cmp(v.buf[i].Start, v.buf[j].Start)
329-
if less == 0 {
330-
return v.buf[i].LargestSeqNum() > v.buf[j].LargestSeqNum()
331-
}
332-
return less < 0
333-
}
334-
func (v *tombstonesByStartKeyAndSeqnum) Swap(i, j int) {
335-
v.buf[i], v.buf[j] = v.buf[j], v.buf[i]
336-
}
337-
338321
func iterateAndCheckTombstones(
339322
cmp Compare, formatKey base.FormatKey, tombstones []tombstoneWithLevel,
340323
) error {
341-
sortBuf := tombstonesByStartKeyAndSeqnum{
342-
cmp: cmp,
343-
buf: tombstones,
344-
}
345-
sort.Sort(&sortBuf)
324+
slices.SortFunc(tombstones, func(a, b tombstoneWithLevel) int {
325+
if v := cmp(a.Start, b.Start); v != 0 {
326+
return v
327+
}
328+
return stdcmp.Compare(b.LargestSeqNum(), a.LargestSeqNum())
329+
})
346330

347331
// For a sequence of tombstones that share the same start UserKey, we will
348332
// encounter them in non-increasing seqnum order and so should encounter them
@@ -490,38 +474,15 @@ func addTombstonesFromIter(
490474
return tombstones, nil
491475
}
492476

493-
type userKeysSort struct {
494-
cmp Compare
495-
buf [][]byte
496-
}
497-
498-
func (v *userKeysSort) Len() int { return len(v.buf) }
499-
func (v *userKeysSort) Less(i, j int) bool {
500-
return v.cmp(v.buf[i], v.buf[j]) < 0
501-
}
502-
func (v *userKeysSort) Swap(i, j int) {
503-
v.buf[i], v.buf[j] = v.buf[j], v.buf[i]
504-
}
505477
func collectAllUserKeys(cmp Compare, tombstones []tombstoneWithLevel) [][]byte {
506478
keys := make([][]byte, 0, len(tombstones)*2)
507479
for _, t := range tombstones {
508-
keys = append(keys, t.Start)
509-
keys = append(keys, t.End)
510-
}
511-
sorter := userKeysSort{
512-
cmp: cmp,
513-
buf: keys,
514-
}
515-
sort.Sort(&sorter)
516-
var last, curr int
517-
for last, curr = -1, 0; curr < len(keys); curr++ {
518-
if last < 0 || cmp(keys[last], keys[curr]) != 0 {
519-
last++
520-
keys[last] = keys[curr]
521-
}
480+
keys = append(keys, t.Start, t.End)
522481
}
523-
keys = keys[:last+1]
524-
return keys
482+
slices.SortFunc(keys, cmp)
483+
return slices.CompactFunc(keys, func(a, b []byte) bool {
484+
return cmp(a, b) == 0
485+
})
525486
}
526487

527488
func fragmentUsingUserKeys(

0 commit comments

Comments
 (0)