Initial commit
This commit is contained in:
60
vendor/github.com/cockroachdb/pebble/internal/keyspan/truncate.go
generated
vendored
Normal file
60
vendor/github.com/cockroachdb/pebble/internal/keyspan/truncate.go
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
|
||||
// of this source code is governed by a BSD-style license that can be found in
|
||||
// the LICENSE file.
|
||||
|
||||
package keyspan
|
||||
|
||||
import "github.com/cockroachdb/pebble/internal/base"
|
||||
|
||||
// Truncate creates a new iterator where every span in the supplied iterator is
|
||||
// truncated to be contained within the range [lower, upper). If start and end
|
||||
// are specified, filter out any spans that are completely outside those bounds.
|
||||
func Truncate(
|
||||
cmp base.Compare, iter FragmentIterator, lower, upper []byte, start, end *base.InternalKey,
|
||||
) FragmentIterator {
|
||||
return Filter(iter, func(in *Span, out *Span) (keep bool) {
|
||||
out.Start, out.End = in.Start, in.End
|
||||
out.Keys = append(out.Keys[:0], in.Keys...)
|
||||
|
||||
// Ignore this span if it lies completely outside [start, end].
|
||||
//
|
||||
// The comparison between s.End and start is by user key only, as
|
||||
// the span is exclusive at s.End, so comparing by user keys
|
||||
// is sufficient.
|
||||
if start != nil && cmp(in.End, start.UserKey) <= 0 {
|
||||
return false
|
||||
}
|
||||
if end != nil {
|
||||
v := cmp(in.Start, end.UserKey)
|
||||
switch {
|
||||
case v > 0:
|
||||
// Wholly outside the end bound. Skip it.
|
||||
return false
|
||||
case v == 0:
|
||||
// This span begins at the same user key as `end`. Whether or
|
||||
// not any of the keys contained within the span are relevant is
|
||||
// dependent on Trailers. Any keys contained within the span
|
||||
// with trailers larger than end cover the small sliver of
|
||||
// keyspace between [k#inf, k#<end-seqnum>]. Since keys are
|
||||
// sorted descending by Trailer within the span, we need to find
|
||||
// the prefix of keys with larger trailers.
|
||||
for i := range in.Keys {
|
||||
if in.Keys[i].Trailer < end.Trailer {
|
||||
out.Keys = out.Keys[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Wholly within the end bound. Keep it.
|
||||
}
|
||||
}
|
||||
// Truncate the bounds to lower and upper.
|
||||
if cmp(in.Start, lower) < 0 {
|
||||
out.Start = lower
|
||||
}
|
||||
if cmp(in.End, upper) > 0 {
|
||||
out.End = upper
|
||||
}
|
||||
return !out.Empty() && cmp(out.Start, out.End) < 0
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user