diff --git a/compaction.go b/compaction.go index 94d990d5d1..350c731874 100644 --- a/compaction.go +++ b/compaction.go @@ -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 diff --git a/internal/base/comparer.go b/internal/base/comparer.go index a8f3eeb349..828b432feb 100644 --- a/internal/base/comparer.go +++ b/internal/base/comparer.go @@ -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 diff --git a/internal/compact/iterator.go b/internal/compact/iterator.go index 7837571bbd..6f3d7d1d33 100644 --- a/internal/compact/iterator.go +++ b/internal/compact/iterator.go @@ -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