Skip to content

Commit

Permalink
colblk: define behavior of KeyWriter.ComparePrev with no previous
Browse files Browse the repository at this point in the history
Define the behavior of KeyWriter.ComparePrev when called on a KeyWriter that
has no previously-written key.
  • Loading branch information
jbowens committed Sep 25, 2024
1 parent 01dcf57 commit 0595c1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 5 additions & 1 deletion sstable/colblk/cockroach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 7 additions & 4 deletions sstable/colblk/data_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
Expand Down

0 comments on commit 0595c1f

Please sign in to comment.