LCOV - code coverage report
Current view: top level - pebble - scan_internal.go (source / functions) Hit Total Coverage
Test: 2023-11-21 08:16Z 407f8606 - tests only.lcov Lines: 539 626 86.1 %
Date: 2023-11-21 08:16:42 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2023 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             :         "fmt"
      10             : 
      11             :         "github.com/cockroachdb/errors"
      12             :         "github.com/cockroachdb/pebble/internal/base"
      13             :         "github.com/cockroachdb/pebble/internal/invariants"
      14             :         "github.com/cockroachdb/pebble/internal/keyspan"
      15             :         "github.com/cockroachdb/pebble/internal/manifest"
      16             :         "github.com/cockroachdb/pebble/objstorage"
      17             :         "github.com/cockroachdb/pebble/objstorage/remote"
      18             :         "github.com/cockroachdb/pebble/sstable"
      19             : )
      20             : 
      21             : const (
      22             :         // In skip-shared iteration mode, keys in levels sharedLevelsStart and greater
      23             :         // (i.e. lower in the LSM) are skipped.
      24             :         sharedLevelsStart = remote.SharedLevelsStart
      25             : )
      26             : 
      27             : // ErrInvalidSkipSharedIteration is returned by ScanInternal if it was called
      28             : // with a shared file visitor function, and a file in a shareable level (i.e.
      29             : // level >= sharedLevelsStart) was found to not be in shared storage according
      30             : // to objstorage.Provider, or not shareable for another reason such as for
      31             : // containing keys newer than the snapshot sequence number.
      32             : var ErrInvalidSkipSharedIteration = errors.New("pebble: cannot use skip-shared iteration due to non-shareable files in lower levels")
      33             : 
      34             : // SharedSSTMeta represents an sstable on shared storage that can be ingested
      35             : // by another pebble instance. This struct must contain all fields that are
      36             : // required for a Pebble instance to ingest a foreign sstable on shared storage,
      37             : // including constructing any relevant objstorage.Provider / remoteobjcat.Catalog
      38             : // data structures, as well as creating virtual FileMetadatas.
      39             : //
      40             : // Note that the Pebble instance creating and returning a SharedSSTMeta might
      41             : // not be the one that created the underlying sstable on shared storage to begin
      42             : // with; it's possible for a Pebble instance to reshare an sstable that was
      43             : // shared to it.
      44             : type SharedSSTMeta struct {
      45             :         // Backing is the shared object underlying this SST. Can be attached to an
      46             :         // objstorage.Provider.
      47             :         Backing objstorage.RemoteObjectBackingHandle
      48             : 
      49             :         // Smallest and Largest internal keys for the overall bounds. The kind and
      50             :         // SeqNum of these will reflect what is physically present on the source Pebble
      51             :         // instance's view of the sstable; it's up to the ingesting instance to set the
      52             :         // sequence number in the trailer to match the read-time sequence numbers
      53             :         // reserved for the level this SST is being ingested into. The Kind is expected
      54             :         // to remain unchanged by the ingesting instance.
      55             :         //
      56             :         // Note that these bounds could be narrower than the bounds of the underlying
      57             :         // sstable; ScanInternal is expected to truncate sstable bounds to the user key
      58             :         // bounds passed into that method.
      59             :         Smallest, Largest InternalKey
      60             : 
      61             :         // SmallestRangeKey and LargestRangeKey are internal keys that denote the
      62             :         // range key bounds of this sstable. Must lie within [Smallest, Largest].
      63             :         SmallestRangeKey, LargestRangeKey InternalKey
      64             : 
      65             :         // SmallestPointKey and LargestPointKey are internal keys that denote the
      66             :         // point key bounds of this sstable. Must lie within [Smallest, Largest].
      67             :         SmallestPointKey, LargestPointKey InternalKey
      68             : 
      69             :         // Level denotes the level at which this file was present at read time.
      70             :         // For files visited by ScanInternal, this value will only be 5 or 6.
      71             :         Level uint8
      72             : 
      73             :         // Size contains an estimate of the size of this sstable.
      74             :         Size uint64
      75             : 
      76             :         // fileNum at time of creation in the creator instance. Only used for
      77             :         // debugging/tests.
      78             :         fileNum base.FileNum
      79             : }
      80             : 
      81           1 : func (s *SharedSSTMeta) cloneFromFileMeta(f *fileMetadata) {
      82           1 :         *s = SharedSSTMeta{
      83           1 :                 Smallest:         f.Smallest.Clone(),
      84           1 :                 Largest:          f.Largest.Clone(),
      85           1 :                 SmallestRangeKey: f.SmallestRangeKey.Clone(),
      86           1 :                 LargestRangeKey:  f.LargestRangeKey.Clone(),
      87           1 :                 SmallestPointKey: f.SmallestPointKey.Clone(),
      88           1 :                 LargestPointKey:  f.LargestPointKey.Clone(),
      89           1 :                 Size:             f.Size,
      90           1 :                 fileNum:          f.FileNum,
      91           1 :         }
      92           1 : }
      93             : 
      94             : type sharedByLevel []SharedSSTMeta
      95             : 
      96           1 : func (s sharedByLevel) Len() int           { return len(s) }
      97           0 : func (s sharedByLevel) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
      98           1 : func (s sharedByLevel) Less(i, j int) bool { return s[i].Level < s[j].Level }
      99             : 
     100             : type pcIterPos int
     101             : 
     102             : const (
     103             :         pcIterPosCur pcIterPos = iota
     104             :         pcIterPosNext
     105             : )
     106             : 
     107             : // pointCollapsingIterator is an internalIterator that collapses point keys and
     108             : // returns at most one point internal key for each user key. Merges and
     109             : // SingleDels are not supported and result in a panic if encountered. Point keys
     110             : // deleted by rangedels are considered shadowed and not exposed.
     111             : //
     112             : // Only used in ScanInternal to return at most one internal key per user key.
     113             : type pointCollapsingIterator struct {
     114             :         iter     keyspan.InterleavingIter
     115             :         pos      pcIterPos
     116             :         comparer *base.Comparer
     117             :         merge    base.Merge
     118             :         err      error
     119             :         seqNum   uint64
     120             :         // The current position of `iter`. Always owned by the underlying iter.
     121             :         iterKey *InternalKey
     122             :         // The last saved key. findNextEntry and similar methods are expected to save
     123             :         // the current value of iterKey to savedKey if they're iterating away from the
     124             :         // current key but still need to retain it. See comments in findNextEntry on
     125             :         // how this field is used.
     126             :         //
     127             :         // At the end of a positioning call:
     128             :         //  - if pos == pcIterPosNext, iterKey is pointing to the next user key owned
     129             :         //    by `iter` while savedKey is holding a copy to our current key.
     130             :         //  - If pos == pcIterPosCur, iterKey is pointing to an `iter`-owned current
     131             :         //    key, and savedKey is either undefined or pointing to a version of the
     132             :         //    current key owned by this iterator (i.e. backed by savedKeyBuf).
     133             :         savedKey    InternalKey
     134             :         savedKeyBuf []byte
     135             :         // Value at the current iterator position, at iterKey.
     136             :         iterValue base.LazyValue
     137             :         // If fixedSeqNum is non-zero, all emitted points are verified to have this
     138             :         // fixed sequence number.
     139             :         fixedSeqNum uint64
     140             : }
     141             : 
     142           1 : func (p *pointCollapsingIterator) Span() *keyspan.Span {
     143           1 :         return p.iter.Span()
     144           1 : }
     145             : 
     146             : // SeekPrefixGE implements the InternalIterator interface.
     147             : func (p *pointCollapsingIterator) SeekPrefixGE(
     148             :         prefix, key []byte, flags base.SeekGEFlags,
     149           0 : ) (*base.InternalKey, base.LazyValue) {
     150           0 :         p.resetKey()
     151           0 :         p.iterKey, p.iterValue = p.iter.SeekPrefixGE(prefix, key, flags)
     152           0 :         p.pos = pcIterPosCur
     153           0 :         if p.iterKey == nil {
     154           0 :                 return nil, base.LazyValue{}
     155           0 :         }
     156           0 :         return p.findNextEntry()
     157             : }
     158             : 
     159             : // SeekGE implements the InternalIterator interface.
     160             : func (p *pointCollapsingIterator) SeekGE(
     161             :         key []byte, flags base.SeekGEFlags,
     162           1 : ) (*base.InternalKey, base.LazyValue) {
     163           1 :         p.resetKey()
     164           1 :         p.iterKey, p.iterValue = p.iter.SeekGE(key, flags)
     165           1 :         p.pos = pcIterPosCur
     166           1 :         if p.iterKey == nil {
     167           1 :                 return nil, base.LazyValue{}
     168           1 :         }
     169           1 :         return p.findNextEntry()
     170             : }
     171             : 
     172             : // SeekLT implements the InternalIterator interface.
     173             : func (p *pointCollapsingIterator) SeekLT(
     174             :         key []byte, flags base.SeekLTFlags,
     175           0 : ) (*base.InternalKey, base.LazyValue) {
     176           0 :         panic("unimplemented")
     177             : }
     178             : 
     179           1 : func (p *pointCollapsingIterator) resetKey() {
     180           1 :         p.savedKey.UserKey = p.savedKeyBuf[:0]
     181           1 :         p.savedKey.Trailer = 0
     182           1 :         p.iterKey = nil
     183           1 :         p.pos = pcIterPosCur
     184           1 : }
     185             : 
     186           1 : func (p *pointCollapsingIterator) verifySeqNum(key *base.InternalKey) *base.InternalKey {
     187           1 :         if !invariants.Enabled {
     188           0 :                 return key
     189           0 :         }
     190           1 :         if p.fixedSeqNum == 0 || key == nil || key.Kind() == InternalKeyKindRangeDelete {
     191           1 :                 return key
     192           1 :         }
     193           0 :         if key.SeqNum() != p.fixedSeqNum {
     194           0 :                 panic(fmt.Sprintf("expected foreign point key to have seqnum %d, got %d", p.fixedSeqNum, key.SeqNum()))
     195             :         }
     196           0 :         return key
     197             : }
     198             : 
     199             : // findNextEntry is called to return the next key. p.iter must be positioned at the
     200             : // start of the first user key we are interested in.
     201           1 : func (p *pointCollapsingIterator) findNextEntry() (*base.InternalKey, base.LazyValue) {
     202           1 :         p.saveKey()
     203           1 :         // Saves a comparison in the fast path
     204           1 :         firstIteration := true
     205           1 :         for p.iterKey != nil {
     206           1 :                 // NB: p.savedKey is either the current key (iff p.iterKey == firstKey),
     207           1 :                 // or the previous key.
     208           1 :                 if !firstIteration && !p.comparer.Equal(p.iterKey.UserKey, p.savedKey.UserKey) {
     209           1 :                         p.saveKey()
     210           1 :                         continue
     211             :                 }
     212           1 :                 firstIteration = false
     213           1 :                 if s := p.iter.Span(); s != nil && s.CoversAt(p.seqNum, p.iterKey.SeqNum()) {
     214           1 :                         // All future keys for this user key must be deleted.
     215           1 :                         if p.savedKey.Kind() == InternalKeyKindSingleDelete {
     216           0 :                                 panic("cannot process singledel key in point collapsing iterator")
     217             :                         }
     218             :                         // Fast forward to the next user key.
     219           1 :                         p.saveKey()
     220           1 :                         p.iterKey, p.iterValue = p.iter.Next()
     221           1 :                         for p.iterKey != nil && p.savedKey.SeqNum() >= p.iterKey.SeqNum() && p.comparer.Equal(p.iterKey.UserKey, p.savedKey.UserKey) {
     222           1 :                                 p.iterKey, p.iterValue = p.iter.Next()
     223           1 :                         }
     224           1 :                         continue
     225             :                 }
     226           1 :                 switch p.savedKey.Kind() {
     227           1 :                 case InternalKeyKindSet, InternalKeyKindDelete, InternalKeyKindSetWithDelete, InternalKeyKindDeleteSized:
     228           1 :                         // Note that we return SETs directly, even if they would otherwise get
     229           1 :                         // compacted into a Del to turn into a SetWithDelete. This is a fast
     230           1 :                         // path optimization that can break SINGLEDEL determinism. To lead to
     231           1 :                         // consistent SINGLEDEL behaviour, this iterator should *not* be used for
     232           1 :                         // a keyspace where SINGLEDELs could be in use. If this iterator observes
     233           1 :                         // a SINGLEDEL as the first internal key for a user key, it will panic.
     234           1 :                         //
     235           1 :                         // As p.value is a lazy value owned by the child iterator, we can thread
     236           1 :                         // it through without loading it into p.valueBuf.
     237           1 :                         //
     238           1 :                         // TODO(bilal): We can even avoid saving the key in this fast path if
     239           1 :                         // we are in a block where setHasSamePrefix = false in a v3 sstable,
     240           1 :                         // guaranteeing that there's only one internal key for each user key.
     241           1 :                         // Thread this logic through the sstable iterators and/or consider
     242           1 :                         // collapsing (ha) this logic into the sstable iterators that are aware
     243           1 :                         // of blocks and can determine user key changes without doing key saves
     244           1 :                         // or comparisons.
     245           1 :                         p.pos = pcIterPosCur
     246           1 :                         return p.verifySeqNum(p.iterKey), p.iterValue
     247           0 :                 case InternalKeyKindSingleDelete:
     248           0 :                         // Panic, as this iterator is not expected to observe single deletes.
     249           0 :                         panic("cannot process singledel key in point collapsing iterator")
     250           0 :                 case InternalKeyKindMerge:
     251           0 :                         // Panic, as this iterator is not expected to observe merges.
     252           0 :                         panic("cannot process merge key in point collapsing iterator")
     253           1 :                 case InternalKeyKindRangeDelete:
     254           1 :                         // These are interleaved by the interleaving iterator ahead of all points.
     255           1 :                         // We should pass them as-is, but also account for any points ahead of
     256           1 :                         // them.
     257           1 :                         p.pos = pcIterPosCur
     258           1 :                         return p.verifySeqNum(p.iterKey), p.iterValue
     259           0 :                 default:
     260           0 :                         panic(fmt.Sprintf("unexpected kind: %d", p.iterKey.Kind()))
     261             :                 }
     262             :         }
     263           0 :         p.resetKey()
     264           0 :         return nil, base.LazyValue{}
     265             : }
     266             : 
     267             : // First implements the InternalIterator interface.
     268           1 : func (p *pointCollapsingIterator) First() (*base.InternalKey, base.LazyValue) {
     269           1 :         p.resetKey()
     270           1 :         p.iterKey, p.iterValue = p.iter.First()
     271           1 :         p.pos = pcIterPosCur
     272           1 :         if p.iterKey == nil {
     273           0 :                 return nil, base.LazyValue{}
     274           0 :         }
     275           1 :         return p.findNextEntry()
     276             : }
     277             : 
     278             : // Last implements the InternalIterator interface.
     279           0 : func (p *pointCollapsingIterator) Last() (*base.InternalKey, base.LazyValue) {
     280           0 :         panic("unimplemented")
     281             : }
     282             : 
     283           1 : func (p *pointCollapsingIterator) saveKey() {
     284           1 :         if p.iterKey == nil {
     285           1 :                 p.savedKey = InternalKey{UserKey: p.savedKeyBuf[:0]}
     286           1 :                 return
     287           1 :         }
     288           1 :         p.savedKeyBuf = append(p.savedKeyBuf[:0], p.iterKey.UserKey...)
     289           1 :         p.savedKey = InternalKey{UserKey: p.savedKeyBuf, Trailer: p.iterKey.Trailer}
     290             : }
     291             : 
     292             : // Next implements the InternalIterator interface.
     293           1 : func (p *pointCollapsingIterator) Next() (*base.InternalKey, base.LazyValue) {
     294           1 :         switch p.pos {
     295           1 :         case pcIterPosCur:
     296           1 :                 p.saveKey()
     297           1 :                 if p.iterKey != nil && p.iterKey.Kind() == InternalKeyKindRangeDelete {
     298           1 :                         // Step over the interleaved range delete and process the very next
     299           1 :                         // internal key, even if it's at the same user key. This is because a
     300           1 :                         // point for that user key has not been returned yet.
     301           1 :                         p.iterKey, p.iterValue = p.iter.Next()
     302           1 :                         break
     303             :                 }
     304             :                 // Fast forward to the next user key.
     305           1 :                 key, val := p.iter.Next()
     306           1 :                 // p.iterKey.SeqNum() >= key.SeqNum() is an optimization that allows us to
     307           1 :                 // use p.iterKey.SeqNum() < key.SeqNum() as a sign that the user key has
     308           1 :                 // changed, without needing to do the full key comparison.
     309           1 :                 for key != nil && p.savedKey.SeqNum() >= key.SeqNum() &&
     310           1 :                         p.comparer.Equal(p.savedKey.UserKey, key.UserKey) {
     311           1 :                         key, val = p.iter.Next()
     312           1 :                 }
     313           1 :                 if key == nil {
     314           1 :                         // There are no keys to return.
     315           1 :                         p.resetKey()
     316           1 :                         return nil, base.LazyValue{}
     317           1 :                 }
     318           1 :                 p.iterKey, p.iterValue = key, val
     319           0 :         case pcIterPosNext:
     320           0 :                 p.pos = pcIterPosCur
     321             :         }
     322           1 :         if p.iterKey == nil {
     323           1 :                 p.resetKey()
     324           1 :                 return nil, base.LazyValue{}
     325           1 :         }
     326           1 :         return p.findNextEntry()
     327             : }
     328             : 
     329             : // NextPrefix implements the InternalIterator interface.
     330           0 : func (p *pointCollapsingIterator) NextPrefix(succKey []byte) (*base.InternalKey, base.LazyValue) {
     331           0 :         panic("unimplemented")
     332             : }
     333             : 
     334             : // Prev implements the InternalIterator interface.
     335           0 : func (p *pointCollapsingIterator) Prev() (*base.InternalKey, base.LazyValue) {
     336           0 :         panic("unimplemented")
     337             : }
     338             : 
     339             : // Error implements the InternalIterator interface.
     340           1 : func (p *pointCollapsingIterator) Error() error {
     341           1 :         if p.err != nil {
     342           0 :                 return p.err
     343           0 :         }
     344           1 :         return p.iter.Error()
     345             : }
     346             : 
     347             : // Close implements the InternalIterator interface.
     348           1 : func (p *pointCollapsingIterator) Close() error {
     349           1 :         return p.iter.Close()
     350           1 : }
     351             : 
     352             : // SetBounds implements the InternalIterator interface.
     353           0 : func (p *pointCollapsingIterator) SetBounds(lower, upper []byte) {
     354           0 :         p.resetKey()
     355           0 :         p.iter.SetBounds(lower, upper)
     356           0 : }
     357             : 
     358           0 : func (p *pointCollapsingIterator) SetContext(ctx context.Context) {
     359           0 :         p.iter.SetContext(ctx)
     360           0 : }
     361             : 
     362             : // String implements the InternalIterator interface.
     363           0 : func (p *pointCollapsingIterator) String() string {
     364           0 :         return p.iter.String()
     365           0 : }
     366             : 
     367             : var _ internalIterator = &pointCollapsingIterator{}
     368             : 
     369             : // IteratorLevelKind is used to denote whether the current ScanInternal iterator
     370             : // is unknown, belongs to a flushable, or belongs to an LSM level type.
     371             : type IteratorLevelKind int8
     372             : 
     373             : const (
     374             :         // IteratorLevelUnknown indicates an unknown LSM level.
     375             :         IteratorLevelUnknown IteratorLevelKind = iota
     376             :         // IteratorLevelLSM indicates an LSM level.
     377             :         IteratorLevelLSM
     378             :         // IteratorLevelFlushable indicates a flushable (i.e. memtable).
     379             :         IteratorLevelFlushable
     380             : )
     381             : 
     382             : // IteratorLevel is used with scanInternalIterator to surface additional iterator-specific info where possible.
     383             : // Note: this is struct is only provided for point keys.
     384             : type IteratorLevel struct {
     385             :         Kind IteratorLevelKind
     386             :         // FlushableIndex indicates the position within the flushable queue of this level.
     387             :         // Only valid if kind == IteratorLevelFlushable.
     388             :         FlushableIndex int
     389             :         // The level within the LSM. Only valid if Kind == IteratorLevelLSM.
     390             :         Level int
     391             :         // Sublevel is only valid if Kind == IteratorLevelLSM and Level == 0.
     392             :         Sublevel int
     393             : }
     394             : 
     395             : // scanInternalIterator is an iterator that returns all internal keys, including
     396             : // tombstones. For instance, an InternalKeyKindDelete would be returned as an
     397             : // InternalKeyKindDelete instead of the iterator skipping over to the next key.
     398             : // Internal keys within a user key are collapsed, eg. if there are two SETs, the
     399             : // one with the higher sequence is returned. Useful if an external user of Pebble
     400             : // needs to observe and rebuild Pebble's history of internal keys, such as in
     401             : // node-to-node replication. For use with {db,snapshot}.ScanInternal().
     402             : //
     403             : // scanInternalIterator is expected to ignore point keys deleted by range
     404             : // deletions, and range keys shadowed by a range key unset or delete, however it
     405             : // *must* return the range delete as well as the range key unset/delete that did
     406             : // the shadowing.
     407             : type scanInternalIterator struct {
     408             :         ctx             context.Context
     409             :         db              *DB
     410             :         opts            scanInternalOptions
     411             :         comparer        *base.Comparer
     412             :         merge           Merge
     413             :         iter            internalIterator
     414             :         readState       *readState
     415             :         version         *version
     416             :         rangeKey        *iteratorRangeKeyState
     417             :         pointKeyIter    internalIterator
     418             :         iterKey         *InternalKey
     419             :         iterValue       LazyValue
     420             :         alloc           *iterAlloc
     421             :         newIters        tableNewIters
     422             :         newIterRangeKey keyspan.TableNewSpanIter
     423             :         seqNum          uint64
     424             :         iterLevels      []IteratorLevel
     425             :         mergingIter     *mergingIter
     426             : 
     427             :         // boundsBuf holds two buffers used to store the lower and upper bounds.
     428             :         // Whenever the InternalIterator's bounds change, the new bounds are copied
     429             :         // into boundsBuf[boundsBufIdx]. The two bounds share a slice to reduce
     430             :         // allocations. opts.LowerBound and opts.UpperBound point into this slice.
     431             :         boundsBuf    [2][]byte
     432             :         boundsBufIdx int
     433             : }
     434             : 
     435             : // truncateSharedFile truncates a shared file's [Smallest, Largest] fields to
     436             : // [lower, upper), potentially opening iterators on the file to find keys within
     437             : // the requested bounds. A SharedSSTMeta is produced that is suitable for
     438             : // external consumption by other Pebble instances. If shouldSkip is true, this
     439             : // file does not contain any keys in [lower, upper) and can be skipped.
     440             : //
     441             : // TODO(bilal): If opening iterators and doing reads in this method is too
     442             : // inefficient, consider producing non-tight file bounds instead.
     443             : func (d *DB) truncateSharedFile(
     444             :         ctx context.Context,
     445             :         lower, upper []byte,
     446             :         level int,
     447             :         file *fileMetadata,
     448             :         objMeta objstorage.ObjectMetadata,
     449           1 : ) (sst *SharedSSTMeta, shouldSkip bool, err error) {
     450           1 :         cmp := d.cmp
     451           1 :         sst = &SharedSSTMeta{}
     452           1 :         sst.cloneFromFileMeta(file)
     453           1 :         sst.Level = uint8(level)
     454           1 :         sst.Backing, err = d.objProvider.RemoteObjectBacking(&objMeta)
     455           1 :         if err != nil {
     456           0 :                 return nil, false, err
     457           0 :         }
     458           1 :         needsLowerTruncate := cmp(lower, file.Smallest.UserKey) > 0
     459           1 :         needsUpperTruncate := cmp(upper, file.Largest.UserKey) < 0 || (cmp(upper, file.Largest.UserKey) == 0 && !file.Largest.IsExclusiveSentinel())
     460           1 :         // Fast path: file is entirely within [lower, upper).
     461           1 :         if !needsLowerTruncate && !needsUpperTruncate {
     462           1 :                 return sst, false, nil
     463           1 :         }
     464             : 
     465             :         // We will need to truncate file bounds in at least one direction. Open all
     466             :         // relevant iterators.
     467           1 :         iter, rangeDelIter, err := d.newIters(ctx, file, &IterOptions{
     468           1 :                 LowerBound: lower,
     469           1 :                 UpperBound: upper,
     470           1 :                 level:      manifest.Level(level),
     471           1 :         }, internalIterOpts{})
     472           1 :         if err != nil {
     473           0 :                 return nil, false, err
     474           0 :         }
     475           1 :         defer iter.Close()
     476           1 :         if rangeDelIter != nil {
     477           1 :                 rangeDelIter = keyspan.Truncate(
     478           1 :                         cmp, rangeDelIter, lower, upper, nil, nil,
     479           1 :                         false, /* panicOnUpperTruncate */
     480           1 :                 )
     481           1 :                 defer rangeDelIter.Close()
     482           1 :         }
     483           1 :         rangeKeyIter, err := d.tableNewRangeKeyIter(file, keyspan.SpanIterOptions{})
     484           1 :         if err != nil {
     485           0 :                 return nil, false, err
     486           0 :         }
     487           1 :         if rangeKeyIter != nil {
     488           1 :                 rangeKeyIter = keyspan.Truncate(
     489           1 :                         cmp, rangeKeyIter, lower, upper, nil, nil,
     490           1 :                         false, /* panicOnUpperTruncate */
     491           1 :                 )
     492           1 :                 defer rangeKeyIter.Close()
     493           1 :         }
     494             :         // Check if we need to truncate on the left side. This means finding a new
     495             :         // LargestPointKey and LargestRangeKey that is >= lower.
     496           1 :         if needsLowerTruncate {
     497           1 :                 sst.SmallestPointKey.UserKey = sst.SmallestPointKey.UserKey[:0]
     498           1 :                 sst.SmallestPointKey.Trailer = 0
     499           1 :                 key, _ := iter.SeekGE(lower, base.SeekGEFlagsNone)
     500           1 :                 foundPointKey := key != nil
     501           1 :                 if key != nil {
     502           1 :                         sst.SmallestPointKey.CopyFrom(*key)
     503           1 :                 }
     504           1 :                 if rangeDelIter != nil {
     505           1 :                         span := rangeDelIter.SeekGE(lower)
     506           1 :                         if span != nil && (len(sst.SmallestPointKey.UserKey) == 0 || base.InternalCompare(cmp, span.SmallestKey(), sst.SmallestPointKey) < 0) {
     507           1 :                                 sst.SmallestPointKey.CopyFrom(span.SmallestKey())
     508           1 :                                 foundPointKey = true
     509           1 :                         }
     510             :                 }
     511           1 :                 if !foundPointKey {
     512           1 :                         // There are no point keys in the span we're interested in.
     513           1 :                         sst.SmallestPointKey = InternalKey{}
     514           1 :                         sst.LargestPointKey = InternalKey{}
     515           1 :                 }
     516           1 :                 sst.SmallestRangeKey.UserKey = sst.SmallestRangeKey.UserKey[:0]
     517           1 :                 sst.SmallestRangeKey.Trailer = 0
     518           1 :                 if rangeKeyIter != nil {
     519           1 :                         span := rangeKeyIter.SeekGE(lower)
     520           1 :                         if span != nil {
     521           1 :                                 sst.SmallestRangeKey.CopyFrom(span.SmallestKey())
     522           1 :                         } else {
     523           1 :                                 // There are no range keys in the span we're interested in.
     524           1 :                                 sst.SmallestRangeKey = InternalKey{}
     525           1 :                                 sst.LargestRangeKey = InternalKey{}
     526           1 :                         }
     527             :                 }
     528             :         }
     529             :         // Check if we need to truncate on the right side. This means finding a new
     530             :         // LargestPointKey and LargestRangeKey that is < upper.
     531           1 :         if needsUpperTruncate {
     532           1 :                 sst.LargestPointKey.UserKey = sst.LargestPointKey.UserKey[:0]
     533           1 :                 sst.LargestPointKey.Trailer = 0
     534           1 :                 key, _ := iter.SeekLT(upper, base.SeekLTFlagsNone)
     535           1 :                 foundPointKey := key != nil
     536           1 :                 if key != nil {
     537           1 :                         sst.LargestPointKey.CopyFrom(*key)
     538           1 :                 }
     539           1 :                 if rangeDelIter != nil {
     540           1 :                         span := rangeDelIter.SeekLT(upper)
     541           1 :                         if span != nil && (len(sst.LargestPointKey.UserKey) == 0 || base.InternalCompare(cmp, span.LargestKey(), sst.LargestPointKey) > 0) {
     542           1 :                                 sst.LargestPointKey.CopyFrom(span.LargestKey())
     543           1 :                                 foundPointKey = true
     544           1 :                         }
     545             :                 }
     546           1 :                 if !foundPointKey {
     547           1 :                         // There are no point keys in the span we're interested in.
     548           1 :                         sst.SmallestPointKey = InternalKey{}
     549           1 :                         sst.LargestPointKey = InternalKey{}
     550           1 :                 }
     551           1 :                 sst.LargestRangeKey.UserKey = sst.LargestRangeKey.UserKey[:0]
     552           1 :                 sst.LargestRangeKey.Trailer = 0
     553           1 :                 if rangeKeyIter != nil {
     554           1 :                         span := rangeKeyIter.SeekLT(upper)
     555           1 :                         if span != nil {
     556           1 :                                 sst.LargestRangeKey.CopyFrom(span.LargestKey())
     557           1 :                         } else {
     558           1 :                                 // There are no range keys in the span we're interested in.
     559           1 :                                 sst.SmallestRangeKey = InternalKey{}
     560           1 :                                 sst.LargestRangeKey = InternalKey{}
     561           1 :                         }
     562             :                 }
     563             :         }
     564             :         // Set overall bounds based on {Smallest,Largest}{Point,Range}Key.
     565           1 :         switch {
     566           1 :         case len(sst.SmallestRangeKey.UserKey) == 0:
     567           1 :                 sst.Smallest = sst.SmallestPointKey
     568           1 :         case len(sst.SmallestPointKey.UserKey) == 0:
     569           1 :                 sst.Smallest = sst.SmallestRangeKey
     570           1 :         default:
     571           1 :                 sst.Smallest = sst.SmallestPointKey
     572           1 :                 if base.InternalCompare(cmp, sst.SmallestRangeKey, sst.SmallestPointKey) < 0 {
     573           1 :                         sst.Smallest = sst.SmallestRangeKey
     574           1 :                 }
     575             :         }
     576           1 :         switch {
     577           1 :         case len(sst.LargestRangeKey.UserKey) == 0:
     578           1 :                 sst.Largest = sst.LargestPointKey
     579           1 :         case len(sst.LargestPointKey.UserKey) == 0:
     580           1 :                 sst.Largest = sst.LargestRangeKey
     581           1 :         default:
     582           1 :                 sst.Largest = sst.LargestPointKey
     583           1 :                 if base.InternalCompare(cmp, sst.LargestRangeKey, sst.LargestPointKey) > 0 {
     584           1 :                         sst.Largest = sst.LargestRangeKey
     585           1 :                 }
     586             :         }
     587             :         // On rare occasion, a file might overlap with [lower, upper) but not actually
     588             :         // have any keys within those bounds. Skip such files.
     589           1 :         if len(sst.Smallest.UserKey) == 0 {
     590           1 :                 return nil, true, nil
     591           1 :         }
     592           1 :         sst.Size, err = d.tableCache.estimateSize(file, sst.Smallest.UserKey, sst.Largest.UserKey)
     593           1 :         if err != nil {
     594           0 :                 return nil, false, err
     595           0 :         }
     596             :         // On occasion, estimateSize gives us a low estimate, i.e. a 0 file size. This
     597             :         // can cause panics in places where we divide by file sizes. Correct for it
     598             :         // here.
     599           1 :         if sst.Size == 0 {
     600           1 :                 sst.Size = 1
     601           1 :         }
     602           1 :         return sst, false, nil
     603             : }
     604             : 
     605             : func scanInternalImpl(
     606             :         ctx context.Context, lower, upper []byte, iter *scanInternalIterator, opts *scanInternalOptions,
     607           1 : ) error {
     608           1 :         if opts.visitSharedFile != nil && (lower == nil || upper == nil) {
     609           0 :                 panic("lower and upper bounds must be specified in skip-shared iteration mode")
     610             :         }
     611             :         // Before starting iteration, check if any files in levels sharedLevelsStart
     612             :         // and below are *not* shared. Error out if that is the case, as skip-shared
     613             :         // iteration will not produce a consistent point-in-time view of this range
     614             :         // of keys. For files that are shared, call visitSharedFile with a truncated
     615             :         // version of that file.
     616           1 :         cmp := iter.comparer.Compare
     617           1 :         provider := iter.db.ObjProvider()
     618           1 :         seqNum := iter.seqNum
     619           1 :         current := iter.version
     620           1 :         if current == nil {
     621           1 :                 current = iter.readState.current
     622           1 :         }
     623           1 :         if opts.visitSharedFile != nil {
     624           1 :                 if provider == nil {
     625           0 :                         panic("expected non-nil Provider in skip-shared iteration mode")
     626             :                 }
     627           1 :                 for level := sharedLevelsStart; level < numLevels; level++ {
     628           1 :                         files := current.Levels[level].Iter()
     629           1 :                         for f := files.SeekGE(cmp, lower); f != nil && cmp(f.Smallest.UserKey, upper) < 0; f = files.Next() {
     630           1 :                                 var objMeta objstorage.ObjectMetadata
     631           1 :                                 var err error
     632           1 :                                 objMeta, err = provider.Lookup(fileTypeTable, f.FileBacking.DiskFileNum)
     633           1 :                                 if err != nil {
     634           0 :                                         return err
     635           0 :                                 }
     636           1 :                                 if !objMeta.IsShared() {
     637           0 :                                         return errors.Wrapf(ErrInvalidSkipSharedIteration, "file %s is not shared", objMeta.DiskFileNum)
     638           0 :                                 }
     639           1 :                                 if !base.Visible(f.LargestSeqNum, seqNum, base.InternalKeySeqNumMax) {
     640           1 :                                         return errors.Wrapf(ErrInvalidSkipSharedIteration, "file %s contains keys newer than snapshot", objMeta.DiskFileNum)
     641           1 :                                 }
     642           1 :                                 var sst *SharedSSTMeta
     643           1 :                                 var skip bool
     644           1 :                                 sst, skip, err = iter.db.truncateSharedFile(ctx, lower, upper, level, f, objMeta)
     645           1 :                                 if err != nil {
     646           0 :                                         return err
     647           0 :                                 }
     648           1 :                                 if skip {
     649           1 :                                         continue
     650             :                                 }
     651           1 :                                 if err = opts.visitSharedFile(sst); err != nil {
     652           0 :                                         return err
     653           0 :                                 }
     654             :                         }
     655             :                 }
     656             :         }
     657             : 
     658           1 :         for valid := iter.seekGE(lower); valid && iter.error() == nil; valid = iter.next() {
     659           1 :                 key := iter.unsafeKey()
     660           1 : 
     661           1 :                 if opts.rateLimitFunc != nil {
     662           0 :                         if err := opts.rateLimitFunc(key, iter.lazyValue()); err != nil {
     663           0 :                                 return err
     664           0 :                         }
     665             :                 }
     666             : 
     667           1 :                 switch key.Kind() {
     668           1 :                 case InternalKeyKindRangeKeyDelete, InternalKeyKindRangeKeyUnset, InternalKeyKindRangeKeySet:
     669           1 :                         if opts.visitRangeKey != nil {
     670           1 :                                 span := iter.unsafeSpan()
     671           1 :                                 // NB: The caller isn't interested in the sequence numbers of these
     672           1 :                                 // range keys. Rather, the caller wants them to be in trailer order
     673           1 :                                 // _after_ zeroing of sequence numbers. Copy span.Keys, sort it, and then
     674           1 :                                 // call visitRangeKey.
     675           1 :                                 keysCopy := make([]keyspan.Key, len(span.Keys))
     676           1 :                                 for i := range span.Keys {
     677           1 :                                         keysCopy[i] = span.Keys[i]
     678           1 :                                         keysCopy[i].Trailer = base.MakeTrailer(0, span.Keys[i].Kind())
     679           1 :                                 }
     680           1 :                                 keyspan.SortKeysByTrailer(&keysCopy)
     681           1 :                                 if err := opts.visitRangeKey(span.Start, span.End, keysCopy); err != nil {
     682           0 :                                         return err
     683           0 :                                 }
     684             :                         }
     685           1 :                 case InternalKeyKindRangeDelete:
     686           1 :                         if opts.visitRangeDel != nil {
     687           1 :                                 rangeDel := iter.unsafeRangeDel()
     688           1 :                                 if err := opts.visitRangeDel(rangeDel.Start, rangeDel.End, rangeDel.LargestSeqNum()); err != nil {
     689           0 :                                         return err
     690           0 :                                 }
     691             :                         }
     692           1 :                 default:
     693           1 :                         if opts.visitPointKey != nil {
     694           1 :                                 var info IteratorLevel
     695           1 :                                 if len(iter.mergingIter.heap.items) > 0 {
     696           1 :                                         mergingIterIdx := iter.mergingIter.heap.items[0].index
     697           1 :                                         info = iter.iterLevels[mergingIterIdx]
     698           1 :                                 } else {
     699           0 :                                         info = IteratorLevel{Kind: IteratorLevelUnknown}
     700           0 :                                 }
     701           1 :                                 val := iter.lazyValue()
     702           1 :                                 if err := opts.visitPointKey(key, val, info); err != nil {
     703           0 :                                         return err
     704           0 :                                 }
     705             :                         }
     706             :                 }
     707             :         }
     708             : 
     709           1 :         return nil
     710             : }
     711             : 
     712             : // constructPointIter constructs a merging iterator and sets i.iter to it.
     713             : func (i *scanInternalIterator) constructPointIter(
     714             :         categoryAndQoS sstable.CategoryAndQoS, memtables flushableList, buf *iterAlloc,
     715           1 : ) {
     716           1 :         // Merging levels and levels from iterAlloc.
     717           1 :         mlevels := buf.mlevels[:0]
     718           1 :         levels := buf.levels[:0]
     719           1 : 
     720           1 :         // We compute the number of levels needed ahead of time and reallocate a slice if
     721           1 :         // the array from the iterAlloc isn't large enough. Doing this allocation once
     722           1 :         // should improve the performance.
     723           1 :         numMergingLevels := len(memtables)
     724           1 :         numLevelIters := 0
     725           1 : 
     726           1 :         current := i.version
     727           1 :         if current == nil {
     728           1 :                 current = i.readState.current
     729           1 :         }
     730           1 :         numMergingLevels += len(current.L0SublevelFiles)
     731           1 :         numLevelIters += len(current.L0SublevelFiles)
     732           1 : 
     733           1 :         for level := 1; level < len(current.Levels); level++ {
     734           1 :                 if current.Levels[level].Empty() {
     735           1 :                         continue
     736             :                 }
     737           1 :                 if i.opts.skipSharedLevels && level >= sharedLevelsStart {
     738           1 :                         continue
     739             :                 }
     740           1 :                 numMergingLevels++
     741           1 :                 numLevelIters++
     742             :         }
     743             : 
     744           1 :         if numMergingLevels > cap(mlevels) {
     745           0 :                 mlevels = make([]mergingIterLevel, 0, numMergingLevels)
     746           0 :         }
     747           1 :         if numLevelIters > cap(levels) {
     748           0 :                 levels = make([]levelIter, 0, numLevelIters)
     749           0 :         }
     750             :         // TODO(bilal): Push these into the iterAlloc buf.
     751           1 :         var rangeDelMiter keyspan.MergingIter
     752           1 :         rangeDelIters := make([]keyspan.FragmentIterator, 0, numMergingLevels)
     753           1 :         rangeDelLevels := make([]keyspan.LevelIter, 0, numLevelIters)
     754           1 : 
     755           1 :         i.iterLevels = make([]IteratorLevel, numMergingLevels)
     756           1 :         mlevelsIndex := 0
     757           1 : 
     758           1 :         // Next are the memtables.
     759           1 :         for j := len(memtables) - 1; j >= 0; j-- {
     760           1 :                 mem := memtables[j]
     761           1 :                 mlevels = append(mlevels, mergingIterLevel{
     762           1 :                         iter: mem.newIter(&i.opts.IterOptions),
     763           1 :                 })
     764           1 :                 i.iterLevels[mlevelsIndex] = IteratorLevel{
     765           1 :                         Kind:           IteratorLevelFlushable,
     766           1 :                         FlushableIndex: j,
     767           1 :                 }
     768           1 :                 mlevelsIndex++
     769           1 :                 if rdi := mem.newRangeDelIter(&i.opts.IterOptions); rdi != nil {
     770           1 :                         rangeDelIters = append(rangeDelIters, rdi)
     771           1 :                 }
     772             :         }
     773             : 
     774             :         // Next are the file levels: L0 sub-levels followed by lower levels.
     775           1 :         levelsIndex := len(levels)
     776           1 :         mlevels = mlevels[:numMergingLevels]
     777           1 :         levels = levels[:numLevelIters]
     778           1 :         rangeDelLevels = rangeDelLevels[:numLevelIters]
     779           1 :         i.opts.IterOptions.snapshotForHideObsoletePoints = i.seqNum
     780           1 :         i.opts.IterOptions.CategoryAndQoS = categoryAndQoS
     781           1 :         addLevelIterForFiles := func(files manifest.LevelIterator, level manifest.Level) {
     782           1 :                 li := &levels[levelsIndex]
     783           1 :                 rli := &rangeDelLevels[levelsIndex]
     784           1 : 
     785           1 :                 li.init(
     786           1 :                         i.ctx, i.opts.IterOptions, i.comparer, i.newIters, files, level,
     787           1 :                         internalIterOpts{})
     788           1 :                 li.initBoundaryContext(&mlevels[mlevelsIndex].levelIterBoundaryContext)
     789           1 :                 mlevels[mlevelsIndex].iter = li
     790           1 :                 rli.Init(keyspan.SpanIterOptions{RangeKeyFilters: i.opts.RangeKeyFilters},
     791           1 :                         i.comparer.Compare, tableNewRangeDelIter(i.ctx, i.newIters), files, level,
     792           1 :                         manifest.KeyTypePoint)
     793           1 :                 rangeDelIters = append(rangeDelIters, rli)
     794           1 : 
     795           1 :                 levelsIndex++
     796           1 :                 mlevelsIndex++
     797           1 :         }
     798             : 
     799           1 :         for j := len(current.L0SublevelFiles) - 1; j >= 0; j-- {
     800           1 :                 i.iterLevels[mlevelsIndex] = IteratorLevel{
     801           1 :                         Kind:     IteratorLevelLSM,
     802           1 :                         Level:    0,
     803           1 :                         Sublevel: j,
     804           1 :                 }
     805           1 :                 addLevelIterForFiles(current.L0SublevelFiles[j].Iter(), manifest.L0Sublevel(j))
     806           1 :         }
     807             :         // Add level iterators for the non-empty non-L0 levels.
     808           1 :         for level := 1; level < numLevels; level++ {
     809           1 :                 if current.Levels[level].Empty() {
     810           1 :                         continue
     811             :                 }
     812           1 :                 if i.opts.skipSharedLevels && level >= sharedLevelsStart {
     813           1 :                         continue
     814             :                 }
     815           1 :                 i.iterLevels[mlevelsIndex] = IteratorLevel{Kind: IteratorLevelLSM, Level: level}
     816           1 :                 addLevelIterForFiles(current.Levels[level].Iter(), manifest.Level(level))
     817             :         }
     818             : 
     819           1 :         buf.merging.init(&i.opts.IterOptions, &InternalIteratorStats{}, i.comparer.Compare, i.comparer.Split, mlevels...)
     820           1 :         buf.merging.snapshot = i.seqNum
     821           1 :         rangeDelMiter.Init(i.comparer.Compare, keyspan.VisibleTransform(i.seqNum), new(keyspan.MergingBuffers), rangeDelIters...)
     822           1 : 
     823           1 :         if i.opts.includeObsoleteKeys {
     824           1 :                 iiter := &keyspan.InterleavingIter{}
     825           1 :                 iiter.Init(i.comparer, &buf.merging, &rangeDelMiter,
     826           1 :                         keyspan.InterleavingIterOpts{
     827           1 :                                 LowerBound: i.opts.LowerBound,
     828           1 :                                 UpperBound: i.opts.UpperBound,
     829           1 :                         })
     830           1 :                 i.pointKeyIter = iiter
     831           1 :         } else {
     832           1 :                 pcIter := &pointCollapsingIterator{
     833           1 :                         comparer: i.comparer,
     834           1 :                         merge:    i.merge,
     835           1 :                         seqNum:   i.seqNum,
     836           1 :                 }
     837           1 :                 pcIter.iter.Init(i.comparer, &buf.merging, &rangeDelMiter, keyspan.InterleavingIterOpts{
     838           1 :                         LowerBound: i.opts.LowerBound,
     839           1 :                         UpperBound: i.opts.UpperBound,
     840           1 :                 })
     841           1 :                 i.pointKeyIter = pcIter
     842           1 :         }
     843           1 :         i.iter = i.pointKeyIter
     844             : }
     845             : 
     846             : // constructRangeKeyIter constructs the range-key iterator stack, populating
     847             : // i.rangeKey.rangeKeyIter with the resulting iterator. This is similar to
     848             : // Iterator.constructRangeKeyIter, except it doesn't handle batches and ensures
     849             : // iterConfig does *not* elide unsets/deletes.
     850           1 : func (i *scanInternalIterator) constructRangeKeyIter() {
     851           1 :         // We want the bounded iter from iterConfig, but not the collapsing of
     852           1 :         // RangeKeyUnsets and RangeKeyDels.
     853           1 :         i.rangeKey.rangeKeyIter = i.rangeKey.iterConfig.Init(
     854           1 :                 i.comparer, i.seqNum, i.opts.LowerBound, i.opts.UpperBound,
     855           1 :                 nil /* hasPrefix */, nil /* prefix */, false, /* onlySets */
     856           1 :                 &i.rangeKey.rangeKeyBuffers.internal)
     857           1 : 
     858           1 :         // Next are the flushables: memtables and large batches.
     859           1 :         if i.readState != nil {
     860           1 :                 for j := len(i.readState.memtables) - 1; j >= 0; j-- {
     861           1 :                         mem := i.readState.memtables[j]
     862           1 :                         // We only need to read from memtables which contain sequence numbers older
     863           1 :                         // than seqNum.
     864           1 :                         if logSeqNum := mem.logSeqNum; logSeqNum >= i.seqNum {
     865           1 :                                 continue
     866             :                         }
     867           1 :                         if rki := mem.newRangeKeyIter(&i.opts.IterOptions); rki != nil {
     868           1 :                                 i.rangeKey.iterConfig.AddLevel(rki)
     869           1 :                         }
     870             :                 }
     871             :         }
     872             : 
     873           1 :         current := i.version
     874           1 :         if current == nil {
     875           1 :                 current = i.readState.current
     876           1 :         }
     877             :         // Next are the file levels: L0 sub-levels followed by lower levels.
     878             :         //
     879             :         // Add file-specific iterators for L0 files containing range keys. This is less
     880             :         // efficient than using levelIters for sublevels of L0 files containing
     881             :         // range keys, but range keys are expected to be sparse anyway, reducing the
     882             :         // cost benefit of maintaining a separate L0Sublevels instance for range key
     883             :         // files and then using it here.
     884             :         //
     885             :         // NB: We iterate L0's files in reverse order. They're sorted by
     886             :         // LargestSeqNum ascending, and we need to add them to the merging iterator
     887             :         // in LargestSeqNum descending to preserve the merging iterator's invariants
     888             :         // around Key Trailer order.
     889           1 :         iter := current.RangeKeyLevels[0].Iter()
     890           1 :         for f := iter.Last(); f != nil; f = iter.Prev() {
     891           1 :                 spanIter, err := i.newIterRangeKey(f, i.opts.SpanIterOptions())
     892           1 :                 if err != nil {
     893           0 :                         i.rangeKey.iterConfig.AddLevel(&errorKeyspanIter{err: err})
     894           0 :                         continue
     895             :                 }
     896           1 :                 i.rangeKey.iterConfig.AddLevel(spanIter)
     897             :         }
     898             : 
     899             :         // Add level iterators for the non-empty non-L0 levels.
     900           1 :         for level := 1; level < len(current.RangeKeyLevels); level++ {
     901           1 :                 if current.RangeKeyLevels[level].Empty() {
     902           1 :                         continue
     903             :                 }
     904           1 :                 if i.opts.skipSharedLevels && level >= sharedLevelsStart {
     905           1 :                         continue
     906             :                 }
     907           1 :                 li := i.rangeKey.iterConfig.NewLevelIter()
     908           1 :                 spanIterOpts := i.opts.SpanIterOptions()
     909           1 :                 li.Init(spanIterOpts, i.comparer.Compare, i.newIterRangeKey, current.RangeKeyLevels[level].Iter(),
     910           1 :                         manifest.Level(level), manifest.KeyTypeRange)
     911           1 :                 i.rangeKey.iterConfig.AddLevel(li)
     912             :         }
     913             : }
     914             : 
     915             : // seekGE seeks this iterator to the first key that's greater than or equal
     916             : // to the specified user key.
     917           1 : func (i *scanInternalIterator) seekGE(key []byte) bool {
     918           1 :         i.iterKey, i.iterValue = i.iter.SeekGE(key, base.SeekGEFlagsNone)
     919           1 :         return i.iterKey != nil
     920           1 : }
     921             : 
     922             : // unsafeKey returns the unsafe InternalKey at the current position. The value
     923             : // is nil if the iterator is invalid or exhausted.
     924           1 : func (i *scanInternalIterator) unsafeKey() *InternalKey {
     925           1 :         return i.iterKey
     926           1 : }
     927             : 
     928             : // lazyValue returns a value pointer to the value at the current iterator
     929             : // position. Behaviour undefined if unsafeKey() returns a Range key or Rangedel
     930             : // kind key.
     931           1 : func (i *scanInternalIterator) lazyValue() LazyValue {
     932           1 :         return i.iterValue
     933           1 : }
     934             : 
     935             : // unsafeRangeDel returns a range key span. Behaviour undefined if UnsafeKey returns
     936             : // a non-rangedel kind.
     937           1 : func (i *scanInternalIterator) unsafeRangeDel() *keyspan.Span {
     938           1 :         type spanInternalIterator interface {
     939           1 :                 Span() *keyspan.Span
     940           1 :         }
     941           1 :         return i.pointKeyIter.(spanInternalIterator).Span()
     942           1 : }
     943             : 
     944             : // unsafeSpan returns a range key span. Behaviour undefined if UnsafeKey returns
     945             : // a non-rangekey type.
     946           1 : func (i *scanInternalIterator) unsafeSpan() *keyspan.Span {
     947           1 :         return i.rangeKey.iiter.Span()
     948           1 : }
     949             : 
     950             : // next advances the iterator in the forward direction, and returns the
     951             : // iterator's new validity state.
     952           1 : func (i *scanInternalIterator) next() bool {
     953           1 :         i.iterKey, i.iterValue = i.iter.Next()
     954           1 :         return i.iterKey != nil
     955           1 : }
     956             : 
     957             : // error returns an error from the internal iterator, if there's any.
     958           1 : func (i *scanInternalIterator) error() error {
     959           1 :         return i.iter.Error()
     960           1 : }
     961             : 
     962             : // close closes this iterator, and releases any pooled objects.
     963           1 : func (i *scanInternalIterator) close() error {
     964           1 :         if err := i.iter.Close(); err != nil {
     965           0 :                 return err
     966           0 :         }
     967           1 :         if i.readState != nil {
     968           1 :                 i.readState.unref()
     969           1 :         }
     970           1 :         if i.version != nil {
     971           1 :                 i.version.Unref()
     972           1 :         }
     973           1 :         if i.rangeKey != nil {
     974           1 :                 i.rangeKey.PrepareForReuse()
     975           1 :                 *i.rangeKey = iteratorRangeKeyState{
     976           1 :                         rangeKeyBuffers: i.rangeKey.rangeKeyBuffers,
     977           1 :                 }
     978           1 :                 iterRangeKeyStateAllocPool.Put(i.rangeKey)
     979           1 :                 i.rangeKey = nil
     980           1 :         }
     981           1 :         if alloc := i.alloc; alloc != nil {
     982           1 :                 for j := range i.boundsBuf {
     983           1 :                         if cap(i.boundsBuf[j]) >= maxKeyBufCacheSize {
     984           0 :                                 alloc.boundsBuf[j] = nil
     985           1 :                         } else {
     986           1 :                                 alloc.boundsBuf[j] = i.boundsBuf[j]
     987           1 :                         }
     988             :                 }
     989           1 :                 *alloc = iterAlloc{
     990           1 :                         keyBuf:              alloc.keyBuf[:0],
     991           1 :                         boundsBuf:           alloc.boundsBuf,
     992           1 :                         prefixOrFullSeekKey: alloc.prefixOrFullSeekKey[:0],
     993           1 :                 }
     994           1 :                 iterAllocPool.Put(alloc)
     995           1 :                 i.alloc = nil
     996             :         }
     997           1 :         return nil
     998             : }
     999             : 
    1000           1 : func (i *scanInternalIterator) initializeBoundBufs(lower, upper []byte) {
    1001           1 :         buf := i.boundsBuf[i.boundsBufIdx][:0]
    1002           1 :         if lower != nil {
    1003           1 :                 buf = append(buf, lower...)
    1004           1 :                 i.opts.LowerBound = buf
    1005           1 :         } else {
    1006           1 :                 i.opts.LowerBound = nil
    1007           1 :         }
    1008           1 :         if upper != nil {
    1009           1 :                 buf = append(buf, upper...)
    1010           1 :                 i.opts.UpperBound = buf[len(buf)-len(upper):]
    1011           1 :         } else {
    1012           1 :                 i.opts.UpperBound = nil
    1013           1 :         }
    1014           1 :         i.boundsBuf[i.boundsBufIdx] = buf
    1015           1 :         i.boundsBufIdx = 1 - i.boundsBufIdx
    1016             : }

Generated by: LCOV version 1.14