diff --git a/sstable/colblk/cockroach_test.go b/sstable/colblk/cockroach_test.go index d023d6b3e7..038a53f7fe 100644 --- a/sstable/colblk/cockroach_test.go +++ b/sstable/colblk/cockroach_test.go @@ -60,9 +60,13 @@ type cockroachKeyWriter struct { } func (kw *cockroachKeyWriter) ComparePrev(key []byte) KeyComparison { - lp := kw.prefixes.UnsafeGet(kw.prefixes.Rows() - 1) var cmpv KeyComparison cmpv.PrefixLen = int32(crdbtest.Split(key)) // TODO(jackson): Inline + if kw.prefixes.Rows() == 0 { + cmpv.UserKeyComparison = 1 + return cmpv + } + lp := kw.prefixes.UnsafeGet(kw.prefixes.Rows() - 1) cmpv.CommonPrefixLen = int32(crbytes.CommonPrefix(lp, key[:cmpv.PrefixLen])) if cmpv.CommonPrefixLen == cmpv.PrefixLen { cmpv.UserKeyComparison = int32(crdbtest.CompareSuffixes(key[cmpv.PrefixLen:], kw.prevSuffix)) diff --git a/sstable/colblk/data_block.go b/sstable/colblk/data_block.go index 8359e83fa2..9ce2a09997 100644 --- a/sstable/colblk/data_block.go +++ b/sstable/colblk/data_block.go @@ -48,6 +48,9 @@ type KeyWriter interface { // key. The returned KeyComparison's UserKeyComparison field is equivalent // to Compare(key, prevKey) where prevKey is the last key passed to // WriteKey. + // + // If no key has been written yet, ComparePrev returns a KeyComparison with + // PrefixLen set and UserKeyComparison=1. ComparePrev(key []byte) KeyComparison // WriteKey writes a user key into the KeyWriter's columns. The // keyPrefixLenSharedWithPrev parameter takes the number of bytes prefixing @@ -169,15 +172,15 @@ type defaultKeyWriter struct { } func (w *defaultKeyWriter) ComparePrev(key []byte) KeyComparison { - lp := w.prefixes.UnsafeGet(w.prefixes.nKeys - 1) - var cmpv KeyComparison cmpv.PrefixLen = int32(w.comparer.Split(key)) - cmpv.CommonPrefixLen = int32(crbytes.CommonPrefix(lp, key[:cmpv.PrefixLen])) - if len(lp) == 0 { + if w.prefixes.nKeys == 0 { // The first key has no previous key to compare to. + cmpv.UserKeyComparison = 1 return cmpv } + lp := w.prefixes.UnsafeGet(w.prefixes.nKeys - 1) + cmpv.CommonPrefixLen = int32(crbytes.CommonPrefix(lp, key[:cmpv.PrefixLen])) if invariants.Enabled && bytes.Compare(lp, key[:cmpv.PrefixLen]) > 0 { panic(errors.AssertionFailedf("keys are not in order: %q > %q", lp, key[:cmpv.PrefixLen]))