LCOV - code coverage report
Current view: top level - pebble - range_keys.go (source / functions) Hit Total Coverage
Test: 2024-07-24 08:16Z 2752abb9 - meta test only.lcov Lines: 279 332 84.0 %
Date: 2024-07-24 08:16:56 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2021 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 pebble
       6             : 
       7             : import (
       8             :         "context"
       9             : 
      10             :         "github.com/cockroachdb/errors"
      11             :         "github.com/cockroachdb/pebble/internal/base"
      12             :         "github.com/cockroachdb/pebble/internal/invariants"
      13             :         "github.com/cockroachdb/pebble/internal/keyspan"
      14             :         "github.com/cockroachdb/pebble/internal/manifest"
      15             :         "github.com/cockroachdb/pebble/internal/treeprinter"
      16             :         "github.com/cockroachdb/pebble/sstable"
      17             : )
      18             : 
      19             : // constructRangeKeyIter constructs the range-key iterator stack, populating
      20             : // i.rangeKey.rangeKeyIter with the resulting iterator.
      21           1 : func (i *Iterator) constructRangeKeyIter() {
      22           1 :         i.rangeKey.rangeKeyIter = i.rangeKey.iterConfig.Init(
      23           1 :                 &i.comparer, i.seqNum, i.opts.LowerBound, i.opts.UpperBound,
      24           1 :                 &i.hasPrefix, &i.prefixOrFullSeekKey, false /* internalKeys */, &i.rangeKey.rangeKeyBuffers.internal)
      25           1 : 
      26           1 :         if i.opts.DebugRangeKeyStack {
      27           0 :                 // The default logger is preferable to i.opts.getLogger(), at least in the
      28           0 :                 // metamorphic test.
      29           0 :                 i.rangeKey.rangeKeyIter = keyspan.InjectLogging(i.rangeKey.rangeKeyIter, base.DefaultLogger)
      30           0 :         }
      31             : 
      32             :         // If there's an indexed batch with range keys, include it.
      33           1 :         if i.batch != nil {
      34           1 :                 if i.batch.index == nil {
      35           0 :                         // This isn't an indexed batch. We shouldn't have gotten this far.
      36           0 :                         panic(errors.AssertionFailedf("creating an iterator over an unindexed batch"))
      37           1 :                 } else {
      38           1 :                         // Only include the batch's range key iterator if it has any keys.
      39           1 :                         // NB: This can force reconstruction of the rangekey iterator stack
      40           1 :                         // in SetOptions if subsequently range keys are added. See
      41           1 :                         // SetOptions.
      42           1 :                         if i.batch.countRangeKeys > 0 {
      43           1 :                                 i.batch.initRangeKeyIter(&i.opts, &i.batchRangeKeyIter, i.batchSeqNum)
      44           1 :                                 i.rangeKey.iterConfig.AddLevel(&i.batchRangeKeyIter)
      45           1 :                         }
      46             :                 }
      47             :         }
      48             : 
      49           1 :         if !i.batchOnlyIter {
      50           1 :                 // Next are the flushables: memtables and large batches.
      51           1 :                 if i.readState != nil {
      52           1 :                         for j := len(i.readState.memtables) - 1; j >= 0; j-- {
      53           1 :                                 mem := i.readState.memtables[j]
      54           1 :                                 // We only need to read from memtables which contain sequence numbers older
      55           1 :                                 // than seqNum.
      56           1 :                                 if logSeqNum := mem.logSeqNum; logSeqNum >= i.seqNum {
      57           1 :                                         continue
      58             :                                 }
      59           1 :                                 if rki := mem.newRangeKeyIter(&i.opts); rki != nil {
      60           1 :                                         i.rangeKey.iterConfig.AddLevel(rki)
      61           1 :                                 }
      62             :                         }
      63             :                 }
      64             : 
      65           1 :                 current := i.version
      66           1 :                 if current == nil {
      67           1 :                         current = i.readState.current
      68           1 :                 }
      69             :                 // Next are the file levels: L0 sub-levels followed by lower levels.
      70             : 
      71             :                 // Add file-specific iterators for L0 files containing range keys. We
      72             :                 // maintain a separate manifest.LevelMetadata for each level containing only
      73             :                 // files that contain range keys, however we don't compute a separate
      74             :                 // L0Sublevels data structure too.
      75             :                 //
      76             :                 // We first use L0's LevelMetadata to peek and see whether L0 contains any
      77             :                 // range keys at all. If it does, we create a range key level iterator per
      78             :                 // level that contains range keys using the information from L0Sublevels.
      79             :                 // Some sublevels may not contain any range keys, and we need to iterate
      80             :                 // through the fileMetadata to determine that. Since L0's file count should
      81             :                 // not significantly exceed ~1000 files (see L0CompactionFileThreshold),
      82             :                 // this should be okay.
      83           1 :                 if !current.RangeKeyLevels[0].Empty() {
      84           1 :                         // L0 contains at least 1 file containing range keys.
      85           1 :                         // Add level iterators for the L0 sublevels, iterating from newest to
      86           1 :                         // oldest.
      87           1 :                         for j := len(current.L0SublevelFiles) - 1; j >= 0; j-- {
      88           1 :                                 iter := current.L0SublevelFiles[j].Iter()
      89           1 :                                 if !containsAnyRangeKeys(iter) {
      90           1 :                                         continue
      91             :                                 }
      92             : 
      93           1 :                                 li := i.rangeKey.iterConfig.NewLevelIter()
      94           1 :                                 li.Init(
      95           1 :                                         i.ctx,
      96           1 :                                         i.opts.SpanIterOptions(),
      97           1 :                                         i.cmp,
      98           1 :                                         i.newIterRangeKey,
      99           1 :                                         iter.Filter(manifest.KeyTypeRange),
     100           1 :                                         manifest.L0Sublevel(j),
     101           1 :                                         manifest.KeyTypeRange,
     102           1 :                                 )
     103           1 :                                 i.rangeKey.iterConfig.AddLevel(li)
     104             :                         }
     105             :                 }
     106             : 
     107             :                 // Add level iterators for the non-empty non-L0 levels.
     108           1 :                 for level := 1; level < len(current.RangeKeyLevels); level++ {
     109           1 :                         if current.RangeKeyLevels[level].Empty() {
     110           1 :                                 continue
     111             :                         }
     112           1 :                         li := i.rangeKey.iterConfig.NewLevelIter()
     113           1 :                         spanIterOpts := i.opts.SpanIterOptions()
     114           1 :                         li.Init(i.ctx, spanIterOpts, i.cmp, i.newIterRangeKey, current.RangeKeyLevels[level].Iter(),
     115           1 :                                 manifest.Level(level), manifest.KeyTypeRange)
     116           1 :                         i.rangeKey.iterConfig.AddLevel(li)
     117             :                 }
     118             :         }
     119             : }
     120             : 
     121           1 : func containsAnyRangeKeys(iter manifest.LevelIterator) bool {
     122           1 :         for f := iter.First(); f != nil; f = iter.Next() {
     123           1 :                 if f.HasRangeKeys {
     124           1 :                         return true
     125           1 :                 }
     126             :         }
     127           1 :         return false
     128             : }
     129             : 
     130             : // Range key masking
     131             : //
     132             : // Pebble iterators may be configured such that range keys with suffixes mask
     133             : // point keys with lower suffixes. The intended use is implementing a MVCC
     134             : // delete range operation using range keys, when suffixes are MVCC timestamps.
     135             : //
     136             : // To enable masking, the user populates the IterOptions's RangeKeyMasking
     137             : // field. The Suffix field configures which range keys act as masks. The
     138             : // intended use is to hold a MVCC read timestamp. When implementing a MVCC
     139             : // delete range operation, only range keys that are visible at the read
     140             : // timestamp should be visible. If a range key has a suffix ≤
     141             : // RangeKeyMasking.Suffix, it acts as a mask.
     142             : //
     143             : // Range key masking is facilitated by the keyspan.InterleavingIter. The
     144             : // interleaving iterator interleaves range keys and point keys during combined
     145             : // iteration. During user iteration, the interleaving iterator is configured
     146             : // with a keyspan.SpanMask, implemented by the rangeKeyMasking struct below.
     147             : // The SpanMask interface defines two methods: SpanChanged and SkipPoint.
     148             : //
     149             : // SpanChanged is used to keep the current mask up-to-date. Whenever the point
     150             : // iterator has stepped into or out of the bounds of a range key, the
     151             : // interleaving iterator invokes SpanChanged passing the current covering range
     152             : // key. The below rangeKeyMasking implementation scans the range keys looking
     153             : // for the range key with the largest suffix that's still ≤ the suffix supplied
     154             : // to IterOptions.RangeKeyMasking.Suffix (the "read timestamp"). If it finds a
     155             : // range key that meets the condition, the range key should act as a mask. The
     156             : // span and the relevant range key's suffix are saved.
     157             : //
     158             : // The above ensures that `rangeKeyMasking.maskActiveSuffix` always contains the
     159             : // current masking suffix such that any point keys with lower suffixes should be
     160             : // skipped.
     161             : //
     162             : // There are two ways in which masked point keys are skipped.
     163             : //
     164             : //   1. Interleaving iterator SkipPoint
     165             : //
     166             : // Whenever the interleaving iterator encounters a point key that falls within
     167             : // the bounds of a range key, it invokes SkipPoint. The interleaving iterator
     168             : // guarantees that the SpanChanged method described above has already been
     169             : // invoked with the covering range key. The below rangeKeyMasking implementation
     170             : // of SkipPoint splits the key into prefix and suffix, compares the suffix to
     171             : // the `maskActiveSuffix` updated by SpanChanged and returns true if
     172             : // suffix(point) < maskActiveSuffix.
     173             : //
     174             : // The SkipPoint logic is sufficient to ensure that the Pebble iterator filters
     175             : // out all masked point keys. However, it requires the iterator read each masked
     176             : // point key. For broad range keys that mask many points, this may be expensive.
     177             : //
     178             : //   2. Block property filter
     179             : //
     180             : // For more efficient handling of braad range keys that mask many points, the
     181             : // IterOptions.RangeKeyMasking field has an optional Filter option. This Filter
     182             : // field takes a superset of the block-property filter interface, adding a
     183             : // method to dynamically configure the filter's filtering criteria.
     184             : //
     185             : // To make use of the Filter option, the user is required to define and
     186             : // configure a block-property collector that collects a property containing at
     187             : // least the maximum suffix of a key within a block.
     188             : //
     189             : // When the SpanChanged method described above is invoked, rangeKeyMasking also
     190             : // reconfigures the user-provided filter. It invokes a SetSuffix method,
     191             : // providing the `maskActiveSuffix`, requesting that from now on the
     192             : // block-property filter return Intersects()=false for any properties indicating
     193             : // that a block contains exclusively keys with suffixes greater than the
     194             : // provided suffix.
     195             : //
     196             : // Note that unlike other block-property filters, the filter used for masking
     197             : // must not apply across the entire keyspace. It must only filter blocks that
     198             : // lie within the bounds of the range key that set the mask suffix. To
     199             : // accommodate this, rangeKeyMasking implements a special interface:
     200             : // sstable.BoundLimitedBlockPropertyFilter. This interface extends the block
     201             : // property filter interface with two new methods: KeyIsWithinLowerBound and
     202             : // KeyIsWithinUpperBound. The rangeKeyMasking type wraps the user-provided block
     203             : // property filter, implementing these two methods and overriding Intersects to
     204             : // always return true if there is no active mask.
     205             : //
     206             : // The logic to ensure that a mask block-property filter is only applied within
     207             : // the bounds of the masking range key is subtle. The interleaving iterator
     208             : // guarantees that it never invokes SpanChanged until the point iterator is
     209             : // positioned within the range key. During forward iteration, this guarantees
     210             : // that any block that a sstable reader might attempt to load contains only keys
     211             : // greater than or equal to the range key's lower bound. During backward
     212             : // iteration, it provides the analagous guarantee on the range key's upper
     213             : // bound.
     214             : //
     215             : // The above ensures that an sstable reader only needs to verify that a block
     216             : // that it skips meets the opposite bound. This is where the
     217             : // KeyIsWithinLowerBound and KeyIsWithinUpperBound methods are used. When an
     218             : // sstable iterator is configured with a BoundLimitedBlockPropertyFilter, it
     219             : // checks for intersection with the block-property filter before every block
     220             : // load, like ordinary block-property filters. However, if the bound-limited
     221             : // block property filter indicates that it does NOT intersect, the filter's
     222             : // relevant KeyIsWithin{Lower,Upper}Bound method is queried, using a block
     223             : // index separator as the bound. If the method indicates that the provided index
     224             : // separator does not fall within the range key bounds, the no-intersection
     225             : // result is ignored, and the block is read.
     226             : 
     227             : type rangeKeyMasking struct {
     228             :         cmp    base.Compare
     229             :         split  base.Split
     230             :         filter BlockPropertyFilterMask
     231             :         // maskActiveSuffix holds the suffix of a range key currently acting as a
     232             :         // mask, hiding point keys with suffixes greater than it. maskActiveSuffix
     233             :         // is only ever non-nil if IterOptions.RangeKeyMasking.Suffix is non-nil.
     234             :         // maskActiveSuffix is updated whenever the iterator passes over a new range
     235             :         // key. The maskActiveSuffix should only be used if maskSpan is non-nil.
     236             :         //
     237             :         // See SpanChanged.
     238             :         maskActiveSuffix []byte
     239             :         // maskSpan holds the span from which the active mask suffix was extracted.
     240             :         // The span is used for bounds comparisons, to ensure that a range-key mask
     241             :         // is not applied beyond the bounds of the range key.
     242             :         maskSpan *keyspan.Span
     243             :         parent   *Iterator
     244             : }
     245             : 
     246           1 : func (m *rangeKeyMasking) init(parent *Iterator, cmp base.Compare, split base.Split) {
     247           1 :         m.cmp = cmp
     248           1 :         m.split = split
     249           1 :         if parent.opts.RangeKeyMasking.Filter != nil {
     250           1 :                 m.filter = parent.opts.RangeKeyMasking.Filter()
     251           1 :         }
     252           1 :         m.parent = parent
     253             : }
     254             : 
     255             : // SpanChanged implements the keyspan.SpanMask interface, used during range key
     256             : // iteration.
     257           1 : func (m *rangeKeyMasking) SpanChanged(s *keyspan.Span) {
     258           1 :         if s == nil && m.maskSpan == nil {
     259           1 :                 return
     260           1 :         }
     261           1 :         m.maskSpan = nil
     262           1 :         m.maskActiveSuffix = m.maskActiveSuffix[:0]
     263           1 : 
     264           1 :         // Find the smallest suffix of a range key contained within the Span,
     265           1 :         // excluding suffixes less than m.opts.RangeKeyMasking.Suffix.
     266           1 :         if s != nil {
     267           1 :                 m.parent.rangeKey.stale = true
     268           1 :                 if m.parent.opts.RangeKeyMasking.Suffix != nil {
     269           1 :                         for j := range s.Keys {
     270           1 :                                 if s.Keys[j].Suffix == nil {
     271           1 :                                         continue
     272             :                                 }
     273           1 :                                 if m.cmp(s.Keys[j].Suffix, m.parent.opts.RangeKeyMasking.Suffix) < 0 {
     274           1 :                                         continue
     275             :                                 }
     276           1 :                                 if len(m.maskActiveSuffix) == 0 || m.cmp(m.maskActiveSuffix, s.Keys[j].Suffix) > 0 {
     277           1 :                                         m.maskSpan = s
     278           1 :                                         m.maskActiveSuffix = append(m.maskActiveSuffix[:0], s.Keys[j].Suffix...)
     279           1 :                                 }
     280             :                         }
     281             :                 }
     282             :         }
     283             : 
     284           1 :         if m.maskSpan != nil && m.parent.opts.RangeKeyMasking.Filter != nil {
     285           1 :                 // Update the  block-property filter to filter point keys with suffixes
     286           1 :                 // greater than m.maskActiveSuffix.
     287           1 :                 err := m.filter.SetSuffix(m.maskActiveSuffix)
     288           1 :                 if err != nil {
     289           0 :                         m.parent.err = err
     290           0 :                 }
     291             :         }
     292             :         // If no span is active, we leave the inner block-property filter configured
     293             :         // with its existing suffix. That's okay, because Intersects calls are first
     294             :         // evaluated by iteratorRangeKeyState.Intersects, which considers all blocks
     295             :         // as intersecting if there's no active mask.
     296             : }
     297             : 
     298             : // SkipPoint implements the keyspan.SpanMask interface, used during range key
     299             : // iteration. Whenever a point key is covered by a non-empty Span, the
     300             : // interleaving iterator invokes SkipPoint. This function is responsible for
     301             : // performing range key masking.
     302             : //
     303             : // If a non-nil IterOptions.RangeKeyMasking.Suffix is set, range key masking is
     304             : // enabled. Masking hides point keys, transparently skipping over the keys.
     305             : // Whether or not a point key is masked is determined by comparing the point
     306             : // key's suffix, the overlapping span's keys' suffixes, and the user-configured
     307             : // IterOption's RangeKeyMasking.Suffix. When configured with a masking threshold
     308             : // _t_, and there exists a span with suffix _r_ covering a point key with suffix
     309             : // _p_, and
     310             : //
     311             : //      _t_ ≤ _r_ < _p_
     312             : //
     313             : // then the point key is elided. Consider the following rendering, where using
     314             : // integer suffixes with higher integers sort before suffixes with lower
     315             : // integers, (for example @7 ≤ @6 < @5):
     316             : //
     317             : //           ^
     318             : //        @9 |        •―――――――――――――――○ [e,m)@9
     319             : //      s  8 |                      • l@8
     320             : //      u  7 |------------------------------------ @7 RangeKeyMasking.Suffix
     321             : //      f  6 |      [h,q)@6 •―――――――――――――――――○            (threshold)
     322             : //      f  5 |              • h@5
     323             : //      f  4 |                          • n@4
     324             : //      i  3 |          •―――――――――――○ [f,l)@3
     325             : //      x  2 |  • b@2
     326             : //         1 |
     327             : //         0 |___________________________________
     328             : //            a b c d e f g h i j k l m n o p q
     329             : //
     330             : // An iterator scanning the entire keyspace with the masking threshold set to @7
     331             : // will observe point keys b@2 and l@8. The span keys [h,q)@6 and [f,l)@3 serve
     332             : // as masks, because cmp(@6,@7) ≥ 0 and cmp(@3,@7) ≥ 0. The span key [e,m)@9
     333             : // does not serve as a mask, because cmp(@9,@7) < 0.
     334             : //
     335             : // Although point l@8 falls within the user key bounds of [e,m)@9, [e,m)@9 is
     336             : // non-masking due to its suffix. The point key l@8 also falls within the user
     337             : // key bounds of [h,q)@6, but since cmp(@6,@8) ≥ 0, l@8 is unmasked.
     338             : //
     339             : // Invariant: The userKey is within the user key bounds of the span most
     340             : // recently provided to `SpanChanged`.
     341           1 : func (m *rangeKeyMasking) SkipPoint(userKey []byte) bool {
     342           1 :         m.parent.stats.RangeKeyStats.ContainedPoints++
     343           1 :         if m.maskSpan == nil {
     344           1 :                 // No range key is currently acting as a mask, so don't skip.
     345           1 :                 return false
     346           1 :         }
     347             :         // Range key masking is enabled and the current span includes a range key
     348             :         // that is being used as a mask. (NB: SpanChanged already verified that the
     349             :         // range key's suffix is ≥ RangeKeyMasking.Suffix).
     350             :         //
     351             :         // This point key falls within the bounds of the range key (guaranteed by
     352             :         // the InterleavingIter). Skip the point key if the range key's suffix is
     353             :         // greater than the point key's suffix.
     354           1 :         pointSuffix := userKey[m.split(userKey):]
     355           1 :         if len(pointSuffix) > 0 && m.cmp(m.maskActiveSuffix, pointSuffix) < 0 {
     356           1 :                 m.parent.stats.RangeKeyStats.SkippedPoints++
     357           1 :                 return true
     358           1 :         }
     359           1 :         return false
     360             : }
     361             : 
     362             : // The iteratorRangeKeyState type implements the sstable package's
     363             : // BoundLimitedBlockPropertyFilter interface in order to use block property
     364             : // filters for range key masking. The iteratorRangeKeyState implementation wraps
     365             : // the block-property filter provided in Options.RangeKeyMasking.Filter.
     366             : //
     367             : // Using a block-property filter for range-key masking requires limiting the
     368             : // filter's effect to the bounds of the range key currently acting as a mask.
     369             : // Consider the range key [a,m)@10, and an iterator positioned just before the
     370             : // below block, bounded by index separators `c` and `z`:
     371             : //
     372             : //                c                          z
     373             : //         x      |  c@9 c@5 c@1 d@7 e@4 y@4 | ...
     374             : //      iter pos
     375             : //
     376             : // The next block cannot be skipped, despite the range key suffix @10 is greater
     377             : // than all the block's keys' suffixes, because it contains a key (y@4) outside
     378             : // the bounds of the range key.
     379             : //
     380             : // This extended BoundLimitedBlockPropertyFilter interface adds two new methods,
     381             : // KeyIsWithinLowerBound and KeyIsWithinUpperBound, for testing whether a
     382             : // particular block is within bounds.
     383             : //
     384             : // The iteratorRangeKeyState implements these new methods by first checking if
     385             : // the iterator is currently positioned within a range key. If not, the provided
     386             : // key is considered out-of-bounds. If the iterator is positioned within a range
     387             : // key, it compares the corresponding range key bound.
     388             : var _ sstable.BoundLimitedBlockPropertyFilter = (*rangeKeyMasking)(nil)
     389             : 
     390             : // Name implements the limitedBlockPropertyFilter interface defined in the
     391             : // sstable package by passing through to the user-defined block property filter.
     392           1 : func (m *rangeKeyMasking) Name() string {
     393           1 :         return m.filter.Name()
     394           1 : }
     395             : 
     396             : // Intersects implements the limitedBlockPropertyFilter interface defined in the
     397             : // sstable package by passing the intersection decision to the user-provided
     398             : // block property filter only if a range key is covering the current iterator
     399             : // position.
     400           1 : func (m *rangeKeyMasking) Intersects(prop []byte) (bool, error) {
     401           1 :         if m.maskSpan == nil {
     402           1 :                 // No span is actively masking.
     403           1 :                 return true, nil
     404           1 :         }
     405           1 :         return m.filter.Intersects(prop)
     406             : }
     407             : 
     408           1 : func (m *rangeKeyMasking) SyntheticSuffixIntersects(prop []byte, suffix []byte) (bool, error) {
     409           1 :         if m.maskSpan == nil {
     410           1 :                 // No span is actively masking.
     411           1 :                 return true, nil
     412           1 :         }
     413           1 :         return m.filter.SyntheticSuffixIntersects(prop, suffix)
     414             : }
     415             : 
     416             : // KeyIsWithinLowerBound implements the limitedBlockPropertyFilter interface
     417             : // defined in the sstable package. It's used to restrict the masking block
     418             : // property filter to only applying within the bounds of the active range key.
     419           1 : func (m *rangeKeyMasking) KeyIsWithinLowerBound(key []byte) bool {
     420           1 :         // Invariant: m.maskSpan != nil
     421           1 :         //
     422           1 :         // The provided `key` is an inclusive lower bound of the block we're
     423           1 :         // considering skipping.
     424           1 :         return m.cmp(m.maskSpan.Start, key) <= 0
     425           1 : }
     426             : 
     427             : // KeyIsWithinUpperBound implements the limitedBlockPropertyFilter interface
     428             : // defined in the sstable package. It's used to restrict the masking block
     429             : // property filter to only applying within the bounds of the active range key.
     430           1 : func (m *rangeKeyMasking) KeyIsWithinUpperBound(key []byte) bool {
     431           1 :         // Invariant: m.maskSpan != nil
     432           1 :         //
     433           1 :         // The provided `key` is an *inclusive* upper bound of the block we're
     434           1 :         // considering skipping, so the range key's end must be strictly greater
     435           1 :         // than the block bound for the block to be within bounds.
     436           1 :         return m.cmp(m.maskSpan.End, key) > 0
     437           1 : }
     438             : 
     439             : // lazyCombinedIter implements the internalIterator interface, wrapping a
     440             : // pointIter. It requires the pointIter's the levelIters be configured with
     441             : // pointers to its combinedIterState. When the levelIter observes a file
     442             : // containing a range key, the lazyCombinedIter constructs the combined
     443             : // range+point key iterator stack and switches to it.
     444             : type lazyCombinedIter struct {
     445             :         // parent holds a pointer to the root *pebble.Iterator containing this
     446             :         // iterator. It's used to mutate the internalIterator in use when switching
     447             :         // to combined iteration.
     448             :         parent            *Iterator
     449             :         pointIter         internalIterator
     450             :         combinedIterState combinedIterState
     451             : }
     452             : 
     453             : // combinedIterState encapsulates the current state of combined iteration.
     454             : // Various low-level iterators (mergingIter, leveliter) hold pointers to the
     455             : // *pebble.Iterator's combinedIterState. This allows them to check whether or
     456             : // not they must monitor for files containing range keys (!initialized), or not.
     457             : //
     458             : // When !initialized, low-level iterators watch for files containing range keys.
     459             : // When one is discovered, they set triggered=true and key to the smallest
     460             : // (forward direction) or largest (reverse direction) range key that's been
     461             : // observed.
     462             : type combinedIterState struct {
     463             :         // key holds the smallest (forward direction) or largest (backward
     464             :         // direction) user key from a range key bound discovered during the iterator
     465             :         // operation that triggered the switch to combined iteration.
     466             :         //
     467             :         // Slices stored here must be stable. This is possible because callers pass
     468             :         // a Smallest/Largest bound from a fileMetadata, which are immutable. A key
     469             :         // slice's bytes must not be overwritten.
     470             :         key         []byte
     471             :         triggered   bool
     472             :         initialized bool
     473             : }
     474             : 
     475             : // Assert that *lazyCombinedIter implements internalIterator.
     476             : var _ internalIterator = (*lazyCombinedIter)(nil)
     477             : 
     478             : // initCombinedIteration is invoked after a pointIter positioning operation
     479             : // resulted in i.combinedIterState.triggered=true.
     480             : //
     481             : // The `dir` parameter is `+1` or `-1` indicating forward iteration or backward
     482             : // iteration respectively.
     483             : //
     484             : // The `pointKey` and `pointValue` parameters provide the new point key-value
     485             : // pair that the iterator was just positioned to. The combined iterator should
     486             : // be seeded with this point key-value pair and return the smaller (forward
     487             : // iteration) or largest (backward iteration) of the two.
     488             : //
     489             : // The `seekKey` parameter is non-nil only if the iterator operation that
     490             : // triggered the switch to combined iteration was a SeekGE, SeekPrefixGE or
     491             : // SeekLT. It provides the seek key supplied and is used to seek the range-key
     492             : // iterator using the same key. This is necessary for SeekGE/SeekPrefixGE
     493             : // operations that land in the middle of a range key and must truncate to the
     494             : // user-provided seek key.
     495             : func (i *lazyCombinedIter) initCombinedIteration(
     496             :         dir int8, pointKV *base.InternalKV, seekKey []byte,
     497           1 : ) *base.InternalKV {
     498           1 :         // Invariant: i.parent.rangeKey is nil.
     499           1 :         // Invariant: !i.combinedIterState.initialized.
     500           1 :         if invariants.Enabled {
     501           1 :                 if i.combinedIterState.initialized {
     502           0 :                         panic("pebble: combined iterator already initialized")
     503             :                 }
     504           1 :                 if i.parent.rangeKey != nil {
     505           0 :                         panic("pebble: iterator already has a range-key iterator stack")
     506             :                 }
     507             :         }
     508             : 
     509             :         // We need to determine the key to seek the range key iterator to. If
     510             :         // seekKey is not nil, the user-initiated operation that triggered the
     511             :         // switch to combined iteration was itself a seek, and we can use that key.
     512             :         // Otherwise, a First/Last or relative positioning operation triggered the
     513             :         // switch to combined iteration.
     514             :         //
     515             :         // The levelIter that observed a file containing range keys populated
     516             :         // combinedIterState.key with the smallest (forward) or largest (backward)
     517             :         // range key it observed. If multiple levelIters observed files with range
     518             :         // keys during the same operation on the mergingIter, combinedIterState.key
     519             :         // is the smallest [during forward iteration; largest in reverse iteration]
     520             :         // such key.
     521           1 :         if seekKey == nil {
     522           1 :                 // Use the levelIter-populated key.
     523           1 :                 seekKey = i.combinedIterState.key
     524           1 : 
     525           1 :                 // We may need to adjust the levelIter-populated seek key to the
     526           1 :                 // surfaced point key. If the key observed is beyond [in the iteration
     527           1 :                 // direction] the current point key, there may still exist a range key
     528           1 :                 // at an earlier key. Consider the following example:
     529           1 :                 //
     530           1 :                 //   L5:  000003:[bar.DEL.5, foo.RANGEKEYSET.9]
     531           1 :                 //   L6:  000001:[bar.SET.2] 000002:[bax.RANGEKEYSET.8]
     532           1 :                 //
     533           1 :                 // A call to First() seeks the levels to files L5.000003 and L6.000001.
     534           1 :                 // The L5 levelIter observes that L5.000003 contains the range key with
     535           1 :                 // start key `foo`, and triggers a switch to combined iteration, setting
     536           1 :                 // `combinedIterState.key` = `foo`.
     537           1 :                 //
     538           1 :                 // The L6 levelIter did not observe the true first range key
     539           1 :                 // (bax.RANGEKEYSET.8), because it appears in a later sstable. When the
     540           1 :                 // combined iterator is initialized, the range key iterator must be
     541           1 :                 // seeked to a key that will find `bax`. To accomplish this, we seek the
     542           1 :                 // key instead to `bar`. It is guaranteed that no range key exists
     543           1 :                 // earlier than `bar`, otherwise a levelIter would've observed it and
     544           1 :                 // set `combinedIterState.key` to its start key.
     545           1 :                 if pointKV != nil {
     546           1 :                         if dir == +1 && i.parent.cmp(i.combinedIterState.key, pointKV.K.UserKey) > 0 {
     547           1 :                                 seekKey = pointKV.K.UserKey
     548           1 :                         } else if dir == -1 && i.parent.cmp(seekKey, pointKV.K.UserKey) < 0 {
     549           1 :                                 seekKey = pointKV.K.UserKey
     550           1 :                         }
     551             :                 }
     552             :         }
     553             : 
     554             :         // An operation on the point iterator observed a file containing range keys,
     555             :         // so we must switch to combined interleaving iteration. First, construct
     556             :         // the range key iterator stack. It must not exist, otherwise we'd already
     557             :         // be performing combined iteration.
     558           1 :         i.parent.rangeKey = iterRangeKeyStateAllocPool.Get().(*iteratorRangeKeyState)
     559           1 :         i.parent.rangeKey.init(i.parent.comparer.Compare, i.parent.comparer.Split, &i.parent.opts)
     560           1 :         i.parent.constructRangeKeyIter()
     561           1 : 
     562           1 :         // Initialize the Iterator's interleaving iterator.
     563           1 :         i.parent.rangeKey.iiter.Init(
     564           1 :                 &i.parent.comparer, i.parent.pointIter, i.parent.rangeKey.rangeKeyIter,
     565           1 :                 keyspan.InterleavingIterOpts{
     566           1 :                         Mask:       &i.parent.rangeKeyMasking,
     567           1 :                         LowerBound: i.parent.opts.LowerBound,
     568           1 :                         UpperBound: i.parent.opts.UpperBound,
     569           1 :                 })
     570           1 : 
     571           1 :         // Set the parent's primary iterator to point to the combined, interleaving
     572           1 :         // iterator that's now initialized with our current state.
     573           1 :         i.parent.iter = &i.parent.rangeKey.iiter
     574           1 :         i.combinedIterState.initialized = true
     575           1 :         i.combinedIterState.key = nil
     576           1 : 
     577           1 :         // All future iterator operations will go directly through the combined
     578           1 :         // iterator.
     579           1 :         //
     580           1 :         // Initialize the interleaving iterator. We pass the point key-value pair so
     581           1 :         // that the interleaving iterator knows where the point iterator is
     582           1 :         // positioned. Additionally, we pass the seek key to which the range-key
     583           1 :         // iterator should be seeked in order to initialize its position.
     584           1 :         //
     585           1 :         // In the forward direction (invert for backwards), the seek key is a key
     586           1 :         // guaranteed to find the smallest range key that's greater than the last
     587           1 :         // key the iterator returned. The range key may be less than pointKV, in
     588           1 :         // which case the range key will be interleaved next instead of the point
     589           1 :         // key.
     590           1 :         if dir == +1 {
     591           1 :                 var prefix []byte
     592           1 :                 if i.parent.hasPrefix {
     593           1 :                         prefix = i.parent.prefixOrFullSeekKey
     594           1 :                 }
     595           1 :                 return i.parent.rangeKey.iiter.InitSeekGE(prefix, seekKey, pointKV)
     596             :         }
     597           1 :         return i.parent.rangeKey.iiter.InitSeekLT(seekKey, pointKV)
     598             : }
     599             : 
     600           1 : func (i *lazyCombinedIter) SeekGE(key []byte, flags base.SeekGEFlags) *base.InternalKV {
     601           1 :         if i.combinedIterState.initialized {
     602           0 :                 return i.parent.rangeKey.iiter.SeekGE(key, flags)
     603           0 :         }
     604           1 :         kv := i.pointIter.SeekGE(key, flags)
     605           1 :         if i.combinedIterState.triggered {
     606           1 :                 return i.initCombinedIteration(+1, kv, key)
     607           1 :         }
     608           1 :         return kv
     609             : }
     610             : 
     611             : func (i *lazyCombinedIter) SeekPrefixGE(
     612             :         prefix, key []byte, flags base.SeekGEFlags,
     613           1 : ) *base.InternalKV {
     614           1 :         if i.combinedIterState.initialized {
     615           0 :                 return i.parent.rangeKey.iiter.SeekPrefixGE(prefix, key, flags)
     616           0 :         }
     617           1 :         kv := i.pointIter.SeekPrefixGE(prefix, key, flags)
     618           1 :         if i.combinedIterState.triggered {
     619           1 :                 return i.initCombinedIteration(+1, kv, key)
     620           1 :         }
     621           1 :         return kv
     622             : }
     623             : 
     624           1 : func (i *lazyCombinedIter) SeekLT(key []byte, flags base.SeekLTFlags) *base.InternalKV {
     625           1 :         if i.combinedIterState.initialized {
     626           0 :                 return i.parent.rangeKey.iiter.SeekLT(key, flags)
     627           0 :         }
     628           1 :         kv := i.pointIter.SeekLT(key, flags)
     629           1 :         if i.combinedIterState.triggered {
     630           1 :                 return i.initCombinedIteration(-1, kv, key)
     631           1 :         }
     632           1 :         return kv
     633             : }
     634             : 
     635           1 : func (i *lazyCombinedIter) First() *base.InternalKV {
     636           1 :         if i.combinedIterState.initialized {
     637           0 :                 return i.parent.rangeKey.iiter.First()
     638           0 :         }
     639           1 :         kv := i.pointIter.First()
     640           1 :         if i.combinedIterState.triggered {
     641           1 :                 return i.initCombinedIteration(+1, kv, nil)
     642           1 :         }
     643           1 :         return kv
     644             : }
     645             : 
     646           1 : func (i *lazyCombinedIter) Last() *base.InternalKV {
     647           1 :         if i.combinedIterState.initialized {
     648           0 :                 return i.parent.rangeKey.iiter.Last()
     649           0 :         }
     650           1 :         kv := i.pointIter.Last()
     651           1 :         if i.combinedIterState.triggered {
     652           1 :                 return i.initCombinedIteration(-1, kv, nil)
     653           1 :         }
     654           1 :         return kv
     655             : }
     656             : 
     657           1 : func (i *lazyCombinedIter) Next() *base.InternalKV {
     658           1 :         if i.combinedIterState.initialized {
     659           0 :                 return i.parent.rangeKey.iiter.Next()
     660           0 :         }
     661           1 :         kv := i.pointIter.Next()
     662           1 :         if i.combinedIterState.triggered {
     663           1 :                 return i.initCombinedIteration(+1, kv, nil)
     664           1 :         }
     665           1 :         return kv
     666             : }
     667             : 
     668           1 : func (i *lazyCombinedIter) NextPrefix(succKey []byte) *base.InternalKV {
     669           1 :         if i.combinedIterState.initialized {
     670           0 :                 return i.parent.rangeKey.iiter.NextPrefix(succKey)
     671           0 :         }
     672           1 :         kv := i.pointIter.NextPrefix(succKey)
     673           1 :         if i.combinedIterState.triggered {
     674           0 :                 return i.initCombinedIteration(+1, kv, nil)
     675           0 :         }
     676           1 :         return kv
     677             : }
     678             : 
     679           1 : func (i *lazyCombinedIter) Prev() *base.InternalKV {
     680           1 :         if i.combinedIterState.initialized {
     681           0 :                 return i.parent.rangeKey.iiter.Prev()
     682           0 :         }
     683           1 :         kv := i.pointIter.Prev()
     684           1 :         if i.combinedIterState.triggered {
     685           1 :                 return i.initCombinedIteration(-1, kv, nil)
     686           1 :         }
     687           1 :         return kv
     688             : }
     689             : 
     690           1 : func (i *lazyCombinedIter) Error() error {
     691           1 :         if i.combinedIterState.initialized {
     692           0 :                 return i.parent.rangeKey.iiter.Error()
     693           0 :         }
     694           1 :         return i.pointIter.Error()
     695             : }
     696             : 
     697           1 : func (i *lazyCombinedIter) Close() error {
     698           1 :         if i.combinedIterState.initialized {
     699           0 :                 return i.parent.rangeKey.iiter.Close()
     700           0 :         }
     701           1 :         return i.pointIter.Close()
     702             : }
     703             : 
     704           1 : func (i *lazyCombinedIter) SetBounds(lower, upper []byte) {
     705           1 :         if i.combinedIterState.initialized {
     706           0 :                 i.parent.rangeKey.iiter.SetBounds(lower, upper)
     707           0 :                 return
     708           0 :         }
     709           1 :         i.pointIter.SetBounds(lower, upper)
     710             : }
     711             : 
     712           0 : func (i *lazyCombinedIter) SetContext(ctx context.Context) {
     713           0 :         if i.combinedIterState.initialized {
     714           0 :                 i.parent.rangeKey.iiter.SetContext(ctx)
     715           0 :                 return
     716           0 :         }
     717           0 :         i.pointIter.SetContext(ctx)
     718             : }
     719             : 
     720             : // DebugTree is part of the InternalIterator interface.
     721           0 : func (i *lazyCombinedIter) DebugTree(tp treeprinter.Node) {
     722           0 :         n := tp.Childf("%T(%p)", i, i)
     723           0 :         if i.combinedIterState.initialized {
     724           0 :                 i.parent.rangeKey.iiter.DebugTree(n)
     725           0 :         } else {
     726           0 :                 i.pointIter.DebugTree(n)
     727           0 :         }
     728             : }
     729             : 
     730           0 : func (i *lazyCombinedIter) String() string {
     731           0 :         if i.combinedIterState.initialized {
     732           0 :                 return i.parent.rangeKey.iiter.String()
     733           0 :         }
     734           0 :         return i.pointIter.String()
     735             : }

Generated by: LCOV version 1.14