LCOV - code coverage report
Current view: top level - pebble - get_iter.go (source / functions) Hit Total Coverage
Test: 2023-12-19 08:16Z 5c5ad7ed - meta test only.lcov Lines: 128 162 79.0 %
Date: 2023-12-19 08:16:43 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2018 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/pebble/internal/base"
      12             :         "github.com/cockroachdb/pebble/internal/keyspan"
      13             :         "github.com/cockroachdb/pebble/internal/manifest"
      14             :         "github.com/cockroachdb/pebble/sstable"
      15             : )
      16             : 
      17             : // getIter is an internal iterator used to perform gets. It iterates through
      18             : // the values for a particular key, level by level. It is not a general purpose
      19             : // internalIterator, but specialized for Get operations so that it loads data
      20             : // lazily.
      21             : type getIter struct {
      22             :         logger       Logger
      23             :         comparer     *Comparer
      24             :         newIters     tableNewIters
      25             :         snapshot     uint64
      26             :         key          []byte
      27             :         iter         internalIterator
      28             :         rangeDelIter keyspan.FragmentIterator
      29             :         tombstone    *keyspan.Span
      30             :         levelIter    levelIter
      31             :         level        int
      32             :         batch        *Batch
      33             :         mem          flushableList
      34             :         l0           []manifest.LevelSlice
      35             :         version      *version
      36             :         iterKey      *InternalKey
      37             :         iterValue    base.LazyValue
      38             :         err          error
      39             : }
      40             : 
      41             : // TODO(sumeer): CockroachDB code doesn't use getIter, but, for completeness,
      42             : // make this implement InternalIteratorWithStats.
      43             : 
      44             : // getIter implements the base.InternalIterator interface.
      45             : var _ base.InternalIterator = (*getIter)(nil)
      46             : 
      47           0 : func (g *getIter) String() string {
      48           0 :         return fmt.Sprintf("len(l0)=%d, len(mem)=%d, level=%d", len(g.l0), len(g.mem), g.level)
      49           0 : }
      50             : 
      51           0 : func (g *getIter) SeekGE(key []byte, flags base.SeekGEFlags) (*InternalKey, base.LazyValue) {
      52           0 :         panic("pebble: SeekGE unimplemented")
      53             : }
      54             : 
      55             : func (g *getIter) SeekPrefixGE(
      56             :         prefix, key []byte, flags base.SeekGEFlags,
      57           0 : ) (*base.InternalKey, base.LazyValue) {
      58           0 :         panic("pebble: SeekPrefixGE unimplemented")
      59             : }
      60             : 
      61           0 : func (g *getIter) SeekLT(key []byte, flags base.SeekLTFlags) (*InternalKey, base.LazyValue) {
      62           0 :         panic("pebble: SeekLT unimplemented")
      63             : }
      64             : 
      65           1 : func (g *getIter) First() (*InternalKey, base.LazyValue) {
      66           1 :         return g.Next()
      67           1 : }
      68             : 
      69           0 : func (g *getIter) Last() (*InternalKey, base.LazyValue) {
      70           0 :         panic("pebble: Last unimplemented")
      71             : }
      72             : 
      73           1 : func (g *getIter) Next() (*InternalKey, base.LazyValue) {
      74           1 :         if g.iter != nil {
      75           1 :                 g.iterKey, g.iterValue = g.iter.Next()
      76           1 :         }
      77             : 
      78           1 :         for {
      79           1 :                 if g.iter != nil {
      80           1 :                         // We have to check rangeDelIter on each iteration because a single
      81           1 :                         // user-key can be spread across multiple tables in a level. A range
      82           1 :                         // tombstone will appear in the table corresponding to its start
      83           1 :                         // key. Every call to levelIter.Next() potentially switches to a new
      84           1 :                         // table and thus reinitializes rangeDelIter.
      85           1 :                         if g.rangeDelIter != nil {
      86           1 :                                 g.tombstone = keyspan.Get(g.comparer.Compare, g.rangeDelIter, g.key)
      87           1 :                                 if g.err = g.rangeDelIter.Close(); g.err != nil {
      88           0 :                                         return nil, base.LazyValue{}
      89           0 :                                 }
      90           1 :                                 g.rangeDelIter = nil
      91             :                         }
      92             : 
      93           1 :                         if g.iterKey != nil {
      94           1 :                                 key := g.iterKey
      95           1 :                                 if g.tombstone != nil && g.tombstone.CoversAt(g.snapshot, key.SeqNum()) {
      96           1 :                                         // We have a range tombstone covering this key. Rather than return a
      97           1 :                                         // point or range deletion here, we return false and close our
      98           1 :                                         // internal iterator which will make Valid() return false,
      99           1 :                                         // effectively stopping iteration.
     100           1 :                                         g.err = g.iter.Close()
     101           1 :                                         g.iter = nil
     102           1 :                                         return nil, base.LazyValue{}
     103           1 :                                 }
     104           1 :                                 if g.comparer.Equal(g.key, key.UserKey) {
     105           1 :                                         if !key.Visible(g.snapshot, base.InternalKeySeqNumMax) {
     106           1 :                                                 g.iterKey, g.iterValue = g.iter.Next()
     107           1 :                                                 continue
     108             :                                         }
     109           1 :                                         return g.iterKey, g.iterValue
     110             :                                 }
     111             :                         }
     112             :                         // We've advanced the iterator passed the desired key. Move on to the
     113             :                         // next memtable / level.
     114           1 :                         g.err = g.iter.Close()
     115           1 :                         g.iter = nil
     116           1 :                         if g.err != nil {
     117           0 :                                 return nil, base.LazyValue{}
     118           0 :                         }
     119             :                 }
     120             : 
     121             :                 // Create an iterator from the batch.
     122           1 :                 if g.batch != nil {
     123           1 :                         if g.batch.index == nil {
     124           0 :                                 g.err = ErrNotIndexed
     125           0 :                                 g.iterKey, g.iterValue = nil, base.LazyValue{}
     126           0 :                                 return nil, base.LazyValue{}
     127           0 :                         }
     128           1 :                         g.iter = g.batch.newInternalIter(nil)
     129           1 :                         g.rangeDelIter = g.batch.newRangeDelIter(
     130           1 :                                 nil,
     131           1 :                                 // Get always reads the entirety of the batch's history, so no
     132           1 :                                 // batch keys should be filtered.
     133           1 :                                 base.InternalKeySeqNumMax,
     134           1 :                         )
     135           1 :                         g.iterKey, g.iterValue = g.iter.SeekGE(g.key, base.SeekGEFlagsNone)
     136           1 :                         g.batch = nil
     137           1 :                         continue
     138             :                 }
     139             : 
     140             :                 // If we have a tombstone from a previous level it is guaranteed to delete
     141             :                 // keys in lower levels.
     142           1 :                 if g.tombstone != nil && g.tombstone.VisibleAt(g.snapshot) {
     143           1 :                         return nil, base.LazyValue{}
     144           1 :                 }
     145             : 
     146             :                 // Create iterators from memtables from newest to oldest.
     147           1 :                 if n := len(g.mem); n > 0 {
     148           1 :                         m := g.mem[n-1]
     149           1 :                         g.iter = m.newIter(nil)
     150           1 :                         g.rangeDelIter = m.newRangeDelIter(nil)
     151           1 :                         g.mem = g.mem[:n-1]
     152           1 :                         g.iterKey, g.iterValue = g.iter.SeekGE(g.key, base.SeekGEFlagsNone)
     153           1 :                         continue
     154             :                 }
     155             : 
     156           1 :                 if g.level == 0 {
     157           1 :                         // Create iterators from L0 from newest to oldest.
     158           1 :                         if n := len(g.l0); n > 0 {
     159           1 :                                 files := g.l0[n-1].Iter()
     160           1 :                                 g.l0 = g.l0[:n-1]
     161           1 :                                 iterOpts := IterOptions{
     162           1 :                                         // TODO(sumeer): replace with a parameter provided by the caller.
     163           1 :                                         CategoryAndQoS: sstable.CategoryAndQoS{
     164           1 :                                                 Category: "pebble-get",
     165           1 :                                                 QoSLevel: sstable.LatencySensitiveQoSLevel,
     166           1 :                                         },
     167           1 :                                         logger:                        g.logger,
     168           1 :                                         snapshotForHideObsoletePoints: g.snapshot}
     169           1 :                                 g.levelIter.init(context.Background(), iterOpts, g.comparer, g.newIters,
     170           1 :                                         files, manifest.L0Sublevel(n), internalIterOpts{})
     171           1 :                                 g.levelIter.initRangeDel(&g.rangeDelIter)
     172           1 :                                 bc := levelIterBoundaryContext{}
     173           1 :                                 g.levelIter.initBoundaryContext(&bc)
     174           1 :                                 g.iter = &g.levelIter
     175           1 : 
     176           1 :                                 // Compute the key prefix for bloom filtering if split function is
     177           1 :                                 // specified, or use the user key as default.
     178           1 :                                 prefix := g.key
     179           1 :                                 if g.comparer.Split != nil {
     180           1 :                                         prefix = g.key[:g.comparer.Split(g.key)]
     181           1 :                                 }
     182           1 :                                 g.iterKey, g.iterValue = g.iter.SeekPrefixGE(prefix, g.key, base.SeekGEFlagsNone)
     183           1 :                                 if bc.isSyntheticIterBoundsKey || bc.isIgnorableBoundaryKey {
     184           1 :                                         g.iterKey = nil
     185           1 :                                         g.iterValue = base.LazyValue{}
     186           1 :                                 }
     187           1 :                                 continue
     188             :                         }
     189           1 :                         g.level++
     190             :                 }
     191             : 
     192           1 :                 if g.level >= numLevels {
     193           1 :                         return nil, base.LazyValue{}
     194           1 :                 }
     195           1 :                 if g.version.Levels[g.level].Empty() {
     196           1 :                         g.level++
     197           1 :                         continue
     198             :                 }
     199             : 
     200           1 :                 iterOpts := IterOptions{
     201           1 :                         // TODO(sumeer): replace with a parameter provided by the caller.
     202           1 :                         CategoryAndQoS: sstable.CategoryAndQoS{
     203           1 :                                 Category: "pebble-get",
     204           1 :                                 QoSLevel: sstable.LatencySensitiveQoSLevel,
     205           1 :                         }, logger: g.logger, snapshotForHideObsoletePoints: g.snapshot}
     206           1 :                 g.levelIter.init(context.Background(), iterOpts, g.comparer, g.newIters,
     207           1 :                         g.version.Levels[g.level].Iter(), manifest.Level(g.level), internalIterOpts{})
     208           1 :                 g.levelIter.initRangeDel(&g.rangeDelIter)
     209           1 :                 bc := levelIterBoundaryContext{}
     210           1 :                 g.levelIter.initBoundaryContext(&bc)
     211           1 :                 g.level++
     212           1 :                 g.iter = &g.levelIter
     213           1 : 
     214           1 :                 // Compute the key prefix for bloom filtering if split function is
     215           1 :                 // specified, or use the user key as default.
     216           1 :                 prefix := g.key
     217           1 :                 if g.comparer.Split != nil {
     218           1 :                         prefix = g.key[:g.comparer.Split(g.key)]
     219           1 :                 }
     220           1 :                 g.iterKey, g.iterValue = g.iter.SeekPrefixGE(prefix, g.key, base.SeekGEFlagsNone)
     221           1 :                 if bc.isSyntheticIterBoundsKey || bc.isIgnorableBoundaryKey {
     222           1 :                         g.iterKey = nil
     223           1 :                         g.iterValue = base.LazyValue{}
     224           1 :                 }
     225             :         }
     226             : }
     227             : 
     228           0 : func (g *getIter) Prev() (*InternalKey, base.LazyValue) {
     229           0 :         panic("pebble: Prev unimplemented")
     230             : }
     231             : 
     232           0 : func (g *getIter) NextPrefix([]byte) (*InternalKey, base.LazyValue) {
     233           0 :         panic("pebble: NextPrefix unimplemented")
     234             : }
     235             : 
     236           0 : func (g *getIter) Valid() bool {
     237           0 :         return g.iterKey != nil && g.err == nil
     238           0 : }
     239             : 
     240           0 : func (g *getIter) Error() error {
     241           0 :         return g.err
     242           0 : }
     243             : 
     244           1 : func (g *getIter) Close() error {
     245           1 :         if g.iter != nil {
     246           1 :                 if err := g.iter.Close(); err != nil && g.err == nil {
     247           0 :                         g.err = err
     248           0 :                 }
     249           1 :                 g.iter = nil
     250             :         }
     251           1 :         return g.err
     252             : }
     253             : 
     254           0 : func (g *getIter) SetBounds(lower, upper []byte) {
     255           0 :         panic("pebble: SetBounds unimplemented")
     256             : }
     257             : 
     258           0 : func (g *getIter) SetContext(_ context.Context) {}

Generated by: LCOV version 1.14