Skip to content

Commit

Permalink
compaction: tolerate empty keys
Browse files Browse the repository at this point in the history
The compaction iterator will sometimes convert empty keys with nil.
Apparently, we want to allow empty keys (but not nil keys).

We make a small change to the compaction iterator to avoid this corner
case and we update `base.MinUserKey` to treat empty non-nil keys as
regular keys.
  • Loading branch information
RaduBerinde committed May 8, 2024
1 parent 98cadc9 commit 57caca1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,9 @@ func (d *DB) runCompaction(
if key != nil && firstKey == nil {
firstKey = key.UserKey
}
if invariants.Enabled && firstKey == nil {
panic("nil first key")
}
splitter := compact.NewOutputSplitter(c.cmp, firstKey, splitLimitFunc(firstKey), c.maxOutputFileSize, c.grandparents.Iter(), iter.Frontiers())
if err := newOutput(); err != nil {
return nil, pendingOutputs, stats, err
Expand Down
4 changes: 2 additions & 2 deletions internal/base/comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ func SharedPrefixLen(a, b []byte) int {
return i
}

// MinUserKey returns the smaller of two user keys. If one of the keys is empty,
// MinUserKey returns the smaller of two user keys. If one of the keys is nil,
// the other one is returned.
func MinUserKey(cmp Compare, a, b []byte) []byte {
if len(a) > 0 && (len(b) == 0 || cmp(a, b) < 0) {
if a != nil && (b == nil || cmp(a, b) < 0) {
return a
}
return b
Expand Down
3 changes: 3 additions & 0 deletions internal/compact/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ func NewIter(
i := &Iter{
cmp: cfg.Comparer.Compare,
cfg: cfg,
// We don't want a nil keyBuf because if the first key we encounter is
// empty, it would become nil.
keyBuf: make([]byte, 8),
}

iter := pointIter
Expand Down

0 comments on commit 57caca1

Please sign in to comment.