Upgrade versions of vendored libs
This commit is contained in:
270
vendor/github.com/cockroachdb/pebble/internal/rangekey/coalesce.go
generated
vendored
270
vendor/github.com/cockroachdb/pebble/internal/rangekey/coalesce.go
generated
vendored
@@ -11,117 +11,162 @@ import (
|
||||
"github.com/cockroachdb/pebble/internal/base"
|
||||
"github.com/cockroachdb/pebble/internal/invariants"
|
||||
"github.com/cockroachdb/pebble/internal/keyspan"
|
||||
"github.com/cockroachdb/pebble/internal/manifest"
|
||||
)
|
||||
|
||||
// InitUserIteration initializes the range key iterator stack for user
|
||||
// iteration. The resulting fragment iterator applies range key semantics,
|
||||
// defragments spans according to their user-observable state and removes all
|
||||
// Keys other than RangeKeySets describing the current state of range keys.
|
||||
// UserIteratorConfig holds state for constructing the range key iterator stack
|
||||
// for user iteration.
|
||||
type UserIteratorConfig struct {
|
||||
snapshot uint64
|
||||
comparer *base.Comparer
|
||||
miter keyspan.MergingIter
|
||||
biter keyspan.BoundedIter
|
||||
diter keyspan.DefragmentingIter
|
||||
liters [manifest.NumLevels]keyspan.LevelIter
|
||||
litersUsed int
|
||||
sortBuf keysBySuffix
|
||||
}
|
||||
|
||||
// Init initializes the range key iterator stack for user iteration. The
|
||||
// resulting fragment iterator applies range key semantics, defragments spans
|
||||
// according to their user-observable state and removes all Keys other than
|
||||
// RangeKeySets describing the current state of range keys. The resulting spans
|
||||
// contain Keys sorted by Suffix.
|
||||
//
|
||||
// The snapshot sequence number parameter determines which keys are visible. Any
|
||||
// keys not visible at the provided snapshot are ignored.
|
||||
func InitUserIteration(
|
||||
cmp base.Compare,
|
||||
func (ui *UserIteratorConfig) Init(
|
||||
comparer *base.Comparer,
|
||||
snapshot uint64,
|
||||
miter *keyspan.MergingIter,
|
||||
diter *keyspan.DefragmentingIter,
|
||||
levelIters ...keyspan.FragmentIterator,
|
||||
lower, upper []byte,
|
||||
hasPrefix *bool,
|
||||
prefix *[]byte,
|
||||
iters ...keyspan.FragmentIterator,
|
||||
) keyspan.FragmentIterator {
|
||||
miter.Init(cmp, userIterationTransform(snapshot), levelIters...)
|
||||
diter.Init(cmp, miter, userIterationDefragmenter(), keyspan.StaticDefragmentReducer)
|
||||
return diter
|
||||
ui.snapshot = snapshot
|
||||
ui.comparer = comparer
|
||||
ui.miter.Init(comparer.Compare, ui, iters...)
|
||||
ui.biter.Init(comparer.Compare, comparer.Split, &ui.miter, lower, upper, hasPrefix, prefix)
|
||||
ui.diter.Init(comparer, &ui.biter, ui, keyspan.StaticDefragmentReducer)
|
||||
ui.litersUsed = 0
|
||||
return &ui.diter
|
||||
}
|
||||
|
||||
// userIterationTransform returns a keyspan.Transform for use with a
|
||||
// keyspan.MergingIter that transforms spans by resolving range keys at the
|
||||
// AddLevel adds a new level to the bottom of the iterator stack. AddLevel
|
||||
// must be called after Init and before any other method on the iterator.
|
||||
func (ui *UserIteratorConfig) AddLevel(iter keyspan.FragmentIterator) {
|
||||
ui.miter.AddLevel(iter)
|
||||
}
|
||||
|
||||
// NewLevelIter returns a pointer to a newly allocated or reused
|
||||
// keyspan.LevelIter. The caller is responsible for calling Init() on this
|
||||
// instance.
|
||||
func (ui *UserIteratorConfig) NewLevelIter() *keyspan.LevelIter {
|
||||
if ui.litersUsed >= len(ui.liters) {
|
||||
return &keyspan.LevelIter{}
|
||||
}
|
||||
ui.litersUsed++
|
||||
return &ui.liters[ui.litersUsed-1]
|
||||
}
|
||||
|
||||
// SetBounds propagates bounds to the iterator stack. The fragment iterator
|
||||
// interface ordinarily doesn't enforce bounds, so this is exposed as an
|
||||
// explicit method on the user iterator config.
|
||||
func (ui *UserIteratorConfig) SetBounds(lower, upper []byte) {
|
||||
ui.biter.SetBounds(lower, upper)
|
||||
}
|
||||
|
||||
// Transform implements the keyspan.Transformer interface for use with a
|
||||
// keyspan.MergingIter. It transforms spans by resolving range keys at the
|
||||
// provided snapshot sequence number. Shadowing of keys is resolved (eg, removal
|
||||
// of unset keys, removal of keys overwritten by a set at the same suffix, etc)
|
||||
// and then non-RangeKeySet keys are removed. The resulting transformed spans
|
||||
// only contain RangeKeySets describing the state visible at the provided
|
||||
// sequence number.
|
||||
func userIterationTransform(snapshot uint64) keyspan.Transform {
|
||||
return func(cmp base.Compare, s keyspan.Span, dst *keyspan.Span) error {
|
||||
// Apply shadowing of keys.
|
||||
if err := Coalesce(cmp, s.Visible(snapshot), dst); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// During user iteration over range keys, unsets and deletes don't
|
||||
// matter. Remove them. This step helps logical defragmentation during
|
||||
// iteration.
|
||||
keys := dst.Keys
|
||||
dst.Keys = dst.Keys[:0]
|
||||
for i := range keys {
|
||||
switch keys[i].Kind() {
|
||||
case base.InternalKeyKindRangeKeySet:
|
||||
dst.Keys = append(dst.Keys, keys[i])
|
||||
case base.InternalKeyKindRangeKeyUnset:
|
||||
// Skip.
|
||||
continue
|
||||
case base.InternalKeyKindRangeKeyDelete:
|
||||
// Skip.
|
||||
continue
|
||||
default:
|
||||
return base.CorruptionErrorf("pebble: unrecognized range key kind %s", keys[i].Kind())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
// sequence number, and hold their Keys sorted by Suffix.
|
||||
func (ui *UserIteratorConfig) Transform(cmp base.Compare, s keyspan.Span, dst *keyspan.Span) error {
|
||||
// Apply shadowing of keys.
|
||||
dst.Start = s.Start
|
||||
dst.End = s.End
|
||||
ui.sortBuf = keysBySuffix{
|
||||
cmp: cmp,
|
||||
keys: dst.Keys[:0],
|
||||
}
|
||||
if err := coalesce(&ui.sortBuf, s.Visible(ui.snapshot).Keys, &dst.Keys); err != nil {
|
||||
return err
|
||||
}
|
||||
// During user iteration over range keys, unsets and deletes don't
|
||||
// matter. Remove them. This step helps logical defragmentation during
|
||||
// iteration.
|
||||
keys := dst.Keys
|
||||
dst.Keys = dst.Keys[:0]
|
||||
for i := range keys {
|
||||
switch keys[i].Kind() {
|
||||
case base.InternalKeyKindRangeKeySet:
|
||||
if invariants.Enabled && len(dst.Keys) > 0 && cmp(dst.Keys[len(dst.Keys)-1].Suffix, keys[i].Suffix) > 0 {
|
||||
panic("pebble: keys unexpectedly not in ascending suffix order")
|
||||
}
|
||||
dst.Keys = append(dst.Keys, keys[i])
|
||||
case base.InternalKeyKindRangeKeyUnset:
|
||||
if invariants.Enabled && len(dst.Keys) > 0 && cmp(dst.Keys[len(dst.Keys)-1].Suffix, keys[i].Suffix) > 0 {
|
||||
panic("pebble: keys unexpectedly not in ascending suffix order")
|
||||
}
|
||||
// Skip.
|
||||
continue
|
||||
case base.InternalKeyKindRangeKeyDelete:
|
||||
// Skip.
|
||||
continue
|
||||
default:
|
||||
return base.CorruptionErrorf("pebble: unrecognized range key kind %s", keys[i].Kind())
|
||||
}
|
||||
}
|
||||
// coalesce results in dst.Keys being sorted by Suffix.
|
||||
dst.KeysOrder = keyspan.BySuffixAsc
|
||||
return nil
|
||||
}
|
||||
|
||||
// userIterationDefragmenter constructs a DefragmentMethod that configures a
|
||||
// ShouldDefragment implements the DefragmentMethod interface and configures a
|
||||
// DefragmentingIter to defragment spans of range keys if their user-visible
|
||||
// state is identical. This defragmenting method assumes the provided spans have
|
||||
// already been transformed through UserIterationTransform, so all RangeKeySets
|
||||
// are user-visible sets. This defragmenter checks for equality between set
|
||||
// suffixes and values (ignoring sequence numbers). It's intended for use during
|
||||
// user iteration, when the wrapped keyspan iterator is merging spans across all
|
||||
// levels of the LSM.
|
||||
//
|
||||
// The returned defragmenting method is stateful, and must not be used on
|
||||
// multiple DefragmentingIters concurrently.
|
||||
func userIterationDefragmenter() keyspan.DefragmentMethod {
|
||||
var bufA keysBySuffix
|
||||
var bufB keysBySuffix
|
||||
return func(cmp base.Compare, a, b keyspan.Span) bool {
|
||||
// UserIterationDefragmenter must only be used on spans that have
|
||||
// transformed by UserIterationTransform. The transform applies
|
||||
// shadowing and removes all keys besides the resulting Sets. Since
|
||||
// shadowing has been applied, each Set must set a unique suffix. If the
|
||||
// two spans are equivalent, they must have the same number of range key
|
||||
// sets.
|
||||
if len(a.Keys) != len(b.Keys) {
|
||||
return false
|
||||
}
|
||||
// already been transformed through (UserIterationConfig).Transform, so all
|
||||
// RangeKeySets are user-visible sets and are already in Suffix order. This
|
||||
// defragmenter checks for equality between set suffixes and values (ignoring
|
||||
// sequence numbers). It's intended for use during user iteration, when the
|
||||
// wrapped keyspan iterator is merging spans across all levels of the LSM.
|
||||
func (ui *UserIteratorConfig) ShouldDefragment(equal base.Equal, a, b *keyspan.Span) bool {
|
||||
// This implementation must only be used on spans that have transformed by
|
||||
// ui.Transform. The transform applies shadowing, removes all keys besides
|
||||
// the resulting Sets and sorts the keys by suffix. Since shadowing has been
|
||||
// applied, each Set must set a unique suffix. If the two spans are
|
||||
// equivalent, they must have the same number of range key sets.
|
||||
if len(a.Keys) != len(b.Keys) || len(a.Keys) == 0 {
|
||||
return false
|
||||
}
|
||||
if a.KeysOrder != keyspan.BySuffixAsc || b.KeysOrder != keyspan.BySuffixAsc {
|
||||
panic("pebble: range key span's keys unexpectedly not in ascending suffix order")
|
||||
}
|
||||
|
||||
// The keys in both spans must be all sets. In order to be equivalent
|
||||
// during user iteration, they must set identical suffix-value mappings.
|
||||
// They do not need to have the same sequence number.
|
||||
//
|
||||
// Currently, the Keys are sorted by Trailer (as per the keyspan.Span
|
||||
// and keyspan.FragmentIterator interfaces). Collect all the keys in
|
||||
// both spans into buffers and sort them by suffix for comparison.
|
||||
bufA.cmp = cmp
|
||||
bufA.keys = append(bufA.keys[:0], a.Keys...)
|
||||
sort.Sort(bufA)
|
||||
bufB.cmp = cmp
|
||||
bufB.keys = append(bufB.keys[:0], b.Keys...)
|
||||
sort.Sort(bufB)
|
||||
|
||||
for i := range bufA.keys {
|
||||
if bufA.keys[i].Kind() != base.InternalKeyKindRangeKeySet ||
|
||||
bufB.keys[i].Kind() != base.InternalKeyKindRangeKeySet {
|
||||
ret := true
|
||||
for i := range a.Keys {
|
||||
if invariants.Enabled {
|
||||
if a.Keys[i].Kind() != base.InternalKeyKindRangeKeySet ||
|
||||
b.Keys[i].Kind() != base.InternalKeyKindRangeKeySet {
|
||||
panic("pebble: unexpected non-RangeKeySet during defragmentation")
|
||||
}
|
||||
if cmp(bufA.keys[i].Suffix, bufB.keys[i].Suffix) != 0 {
|
||||
return false
|
||||
}
|
||||
if !bytes.Equal(bufA.keys[i].Value, bufB.keys[i].Value) {
|
||||
return false
|
||||
if i > 0 && (ui.comparer.Compare(a.Keys[i].Suffix, a.Keys[i-1].Suffix) < 0 ||
|
||||
ui.comparer.Compare(b.Keys[i].Suffix, b.Keys[i-1].Suffix) < 0) {
|
||||
panic("pebble: range keys not ordered by suffix during defragmentation")
|
||||
}
|
||||
}
|
||||
return true
|
||||
if !equal(a.Keys[i].Suffix, b.Keys[i].Suffix) {
|
||||
ret = false
|
||||
break
|
||||
}
|
||||
if !bytes.Equal(a.Keys[i].Value, b.Keys[i].Value) {
|
||||
ret = false
|
||||
break
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Coalesce imposes range key semantics and coalesces range keys with the same
|
||||
@@ -157,18 +202,28 @@ func userIterationDefragmenter() keyspan.DefragmentMethod {
|
||||
// keys do not affect one another. Ingested sstables are expected to be
|
||||
// consistent with respect to the set/unset suffixes: A given suffix should be
|
||||
// set or unset but not both.
|
||||
func Coalesce(cmp base.Compare, span keyspan.Span, dst *keyspan.Span) error {
|
||||
//
|
||||
// The resulting dst Keys slice is sorted by Trailer.
|
||||
func Coalesce(cmp base.Compare, keys []keyspan.Key, dst *[]keyspan.Key) error {
|
||||
// TODO(jackson): Currently, Coalesce doesn't actually perform the sequence
|
||||
// number promotion described in the comment above.
|
||||
|
||||
keysBySuffix := keysBySuffix{
|
||||
cmp: cmp,
|
||||
keys: dst.Keys[:0],
|
||||
keys: (*dst)[:0],
|
||||
}
|
||||
if err := coalesce(&keysBySuffix, keys, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
// coalesce left the keys in *dst sorted by suffix. Re-sort them by trailer.
|
||||
keyspan.SortKeysByTrailer(dst)
|
||||
return nil
|
||||
}
|
||||
|
||||
func coalesce(keysBySuffix *keysBySuffix, keys []keyspan.Key, dst *[]keyspan.Key) error {
|
||||
var deleted bool
|
||||
for i := 0; i < len(span.Keys) && !deleted; i++ {
|
||||
k := span.Keys[i]
|
||||
if invariants.Enabled && i > 0 && k.Trailer > span.Keys[i-1].Trailer {
|
||||
for i := 0; i < len(keys) && !deleted; i++ {
|
||||
k := keys[i]
|
||||
if invariants.Enabled && i > 0 && k.Trailer > keys[i-1].Trailer {
|
||||
panic("pebble: invariant violation: span keys unordered")
|
||||
}
|
||||
|
||||
@@ -210,26 +265,13 @@ func Coalesce(cmp base.Compare, span keyspan.Span, dst *keyspan.Span) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Update the span with the (potentially reduced) keys slice, and re-sort it
|
||||
// by Trailer.
|
||||
*dst = keyspan.Span{
|
||||
Start: span.Start,
|
||||
End: span.End,
|
||||
Keys: keysBySuffix.keys,
|
||||
}
|
||||
keyspan.SortKeys(dst.Keys)
|
||||
// Update the span with the (potentially reduced) keys slice.
|
||||
// NB: We don't re-sort by Trailer. The exported Coalesce function however
|
||||
// will.
|
||||
*dst = keysBySuffix.keys
|
||||
return nil
|
||||
}
|
||||
|
||||
// SortBySuffix sorts the provided keys by suffix.
|
||||
func SortBySuffix(cmp base.Compare, keys []keyspan.Key) {
|
||||
bySuffix := keysBySuffix{
|
||||
cmp: cmp,
|
||||
keys: keys,
|
||||
}
|
||||
sort.Sort(bySuffix)
|
||||
}
|
||||
|
||||
type keysBySuffix struct {
|
||||
cmp base.Compare
|
||||
keys []keyspan.Key
|
||||
@@ -238,7 +280,7 @@ type keysBySuffix struct {
|
||||
// get searches for suffix among the first n keys in keys. If the suffix is
|
||||
// found, it returns the index of the item with the suffix. If the suffix is not
|
||||
// found, it returns n.
|
||||
func (s keysBySuffix) get(n int, suffix []byte) (i int) {
|
||||
func (s *keysBySuffix) get(n int, suffix []byte) (i int) {
|
||||
// Binary search for the suffix to see if there's an existing key with the
|
||||
// suffix. Only binary search among the first n items. get is called while
|
||||
// appending new keys with suffixes that may sort before existing keys.
|
||||
@@ -254,6 +296,6 @@ func (s keysBySuffix) get(n int, suffix []byte) (i int) {
|
||||
return n
|
||||
}
|
||||
|
||||
func (s keysBySuffix) Len() int { return len(s.keys) }
|
||||
func (s keysBySuffix) Less(i, j int) bool { return s.cmp(s.keys[i].Suffix, s.keys[j].Suffix) < 0 }
|
||||
func (s keysBySuffix) Swap(i, j int) { s.keys[i], s.keys[j] = s.keys[j], s.keys[i] }
|
||||
func (s *keysBySuffix) Len() int { return len(s.keys) }
|
||||
func (s *keysBySuffix) Less(i, j int) bool { return s.cmp(s.keys[i].Suffix, s.keys[j].Suffix) < 0 }
|
||||
func (s *keysBySuffix) Swap(i, j int) { s.keys[i], s.keys[j] = s.keys[j], s.keys[i] }
|
||||
|
||||
Reference in New Issue
Block a user