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

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