github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/keyspan/truncate.go (about)

     1  // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package keyspan
     6  
     7  import "github.com/zuoyebang/bitalostable/internal/base"
     8  
     9  // Truncate creates a new iterator where every span in the supplied iterator is
    10  // truncated to be contained within the range [lower, upper). If start and end
    11  // are specified, filter out any spans that are completely outside those bounds.
    12  func Truncate(
    13  	cmp base.Compare, iter FragmentIterator, lower, upper []byte, start, end *base.InternalKey,
    14  ) FragmentIterator {
    15  	return Filter(iter, func(in *Span, out *Span) (keep bool) {
    16  		out.Start, out.End = in.Start, in.End
    17  		out.Keys = append(out.Keys[:0], in.Keys...)
    18  
    19  		// Ignore this span if it lies completely outside [start, end].
    20  		//
    21  		// The comparison between s.End and start is by user key only, as
    22  		// the span is exclusive at s.End, so comparing by user keys
    23  		// is sufficient.
    24  		if start != nil && cmp(in.End, start.UserKey) <= 0 {
    25  			return false
    26  		}
    27  		if end != nil {
    28  			v := cmp(in.Start, end.UserKey)
    29  			switch {
    30  			case v > 0:
    31  				// Wholly outside the end bound. Skip it.
    32  				return false
    33  			case v == 0:
    34  				// This span begins at the same user key as `end`. Whether or
    35  				// not any of the keys contained within the span are relevant is
    36  				// dependent on Trailers. Any keys contained within the span
    37  				// with trailers larger than end cover the small sliver of
    38  				// keyspace between [k#inf, k#<end-seqnum>]. Since keys are
    39  				// sorted descending by Trailer within the span, we need to find
    40  				// the prefix of keys with larger trailers.
    41  				for i := range in.Keys {
    42  					if in.Keys[i].Trailer < end.Trailer {
    43  						out.Keys = out.Keys[:i]
    44  						break
    45  					}
    46  				}
    47  			default:
    48  				// Wholly within the end bound. Keep it.
    49  			}
    50  		}
    51  		// Truncate the bounds to lower and upper.
    52  		if cmp(in.Start, lower) < 0 {
    53  			out.Start = lower
    54  		}
    55  		if cmp(in.End, upper) > 0 {
    56  			out.End = upper
    57  		}
    58  		return !out.Empty() && cmp(out.Start, out.End) < 0
    59  	})
    60  }