LCOV - code coverage report
Current view: top level - pebble/sstable - block.go (source / functions) Hit Total Coverage
Test: 2023-12-30 08:15Z 1cce3d01 - meta test only.lcov Lines: 1062 1287 82.5 %
Date: 2023-12-30 08:16:10 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 sstable
       6             : 
       7             : import (
       8             :         "context"
       9             :         "encoding/binary"
      10             :         "unsafe"
      11             : 
      12             :         "github.com/cockroachdb/errors"
      13             :         "github.com/cockroachdb/pebble/internal/base"
      14             :         "github.com/cockroachdb/pebble/internal/invariants"
      15             :         "github.com/cockroachdb/pebble/internal/keyspan"
      16             :         "github.com/cockroachdb/pebble/internal/manual"
      17             :         "github.com/cockroachdb/pebble/internal/rangedel"
      18             :         "github.com/cockroachdb/pebble/internal/rangekey"
      19             : )
      20             : 
      21           1 : func uvarintLen(v uint32) int {
      22           1 :         i := 0
      23           1 :         for v >= 0x80 {
      24           0 :                 v >>= 7
      25           0 :                 i++
      26           0 :         }
      27           1 :         return i + 1
      28             : }
      29             : 
      30             : type blockWriter struct {
      31             :         restartInterval int
      32             :         nEntries        int
      33             :         nextRestart     int
      34             :         buf             []byte
      35             :         // For datablocks in TableFormatPebblev3, we steal the most significant bit
      36             :         // in restarts for encoding setHasSameKeyPrefixSinceLastRestart. This leaves
      37             :         // us with 31 bits, which is more than enough (no one needs > 2GB blocks).
      38             :         // Typically, restarts occur every 16 keys, and by storing this bit with the
      39             :         // restart, we can optimize for the case where a user wants to skip to the
      40             :         // next prefix which happens to be in the same data block, but is > 16 keys
      41             :         // away. We have seen production situations with 100+ versions per MVCC key
      42             :         // (which share the same prefix). Additionally, for such writers, the prefix
      43             :         // compression of the key, that shares the key with the preceding key, is
      44             :         // limited to the prefix part of the preceding key -- this ensures that when
      45             :         // doing NPrefix (see blockIter) we don't need to assemble the full key
      46             :         // for each step since by limiting the length of the shared key we are
      47             :         // ensuring that any of the keys with the same prefix can be used to
      48             :         // assemble the full key when the prefix does change.
      49             :         restarts []uint32
      50             :         // Do not read curKey directly from outside blockWriter since it can have
      51             :         // the InternalKeyKindSSTableInternalObsoleteBit set. Use getCurKey() or
      52             :         // getCurUserKey() instead.
      53             :         curKey []byte
      54             :         // curValue excludes the optional prefix provided to
      55             :         // storeWithOptionalValuePrefix.
      56             :         curValue []byte
      57             :         prevKey  []byte
      58             :         tmp      [4]byte
      59             :         // We don't know the state of the sets that were at the end of the previous
      60             :         // block, so this is initially 0. It may be true for the second and later
      61             :         // restarts in a block. Not having inter-block information is fine since we
      62             :         // will optimize by stepping through restarts only within the same block.
      63             :         // Note that the first restart is the first key in the block.
      64             :         setHasSameKeyPrefixSinceLastRestart bool
      65             : }
      66             : 
      67           1 : func (w *blockWriter) clear() {
      68           1 :         *w = blockWriter{
      69           1 :                 buf:      w.buf[:0],
      70           1 :                 restarts: w.restarts[:0],
      71           1 :                 curKey:   w.curKey[:0],
      72           1 :                 curValue: w.curValue[:0],
      73           1 :                 prevKey:  w.prevKey[:0],
      74           1 :         }
      75           1 : }
      76             : 
      77             : // MaximumBlockSize is an extremely generous maximum block size of 256MiB. We
      78             : // explicitly place this limit to reserve a few bits in the restart for
      79             : // internal use.
      80             : const MaximumBlockSize = 1 << 28
      81             : const setHasSameKeyPrefixRestartMask uint32 = 1 << 31
      82             : const restartMaskLittleEndianHighByteWithoutSetHasSamePrefix byte = 0b0111_1111
      83             : const restartMaskLittleEndianHighByteOnlySetHasSamePrefix byte = 0b1000_0000
      84             : 
      85           1 : func (w *blockWriter) getCurKey() InternalKey {
      86           1 :         k := base.DecodeInternalKey(w.curKey)
      87           1 :         k.Trailer = k.Trailer & trailerObsoleteMask
      88           1 :         return k
      89           1 : }
      90             : 
      91           1 : func (w *blockWriter) getCurUserKey() []byte {
      92           1 :         n := len(w.curKey) - base.InternalTrailerLen
      93           1 :         if n < 0 {
      94           0 :                 panic(errors.AssertionFailedf("corrupt key in blockWriter buffer"))
      95             :         }
      96           1 :         return w.curKey[:n:n]
      97             : }
      98             : 
      99             : // If !addValuePrefix, the valuePrefix is ignored.
     100             : func (w *blockWriter) storeWithOptionalValuePrefix(
     101             :         keySize int,
     102             :         value []byte,
     103             :         maxSharedKeyLen int,
     104             :         addValuePrefix bool,
     105             :         valuePrefix valuePrefix,
     106             :         setHasSameKeyPrefix bool,
     107           1 : ) {
     108           1 :         shared := 0
     109           1 :         if !setHasSameKeyPrefix {
     110           1 :                 w.setHasSameKeyPrefixSinceLastRestart = false
     111           1 :         }
     112           1 :         if w.nEntries == w.nextRestart {
     113           1 :                 w.nextRestart = w.nEntries + w.restartInterval
     114           1 :                 restart := uint32(len(w.buf))
     115           1 :                 if w.setHasSameKeyPrefixSinceLastRestart {
     116           1 :                         restart = restart | setHasSameKeyPrefixRestartMask
     117           1 :                 }
     118           1 :                 w.setHasSameKeyPrefixSinceLastRestart = true
     119           1 :                 w.restarts = append(w.restarts, restart)
     120           1 :         } else {
     121           1 :                 // TODO(peter): Manually inlined version of base.SharedPrefixLen(). This
     122           1 :                 // is 3% faster on BenchmarkWriter on go1.16. Remove if future versions
     123           1 :                 // show this to not be a performance win. For now, functions that use of
     124           1 :                 // unsafe cannot be inlined.
     125           1 :                 n := maxSharedKeyLen
     126           1 :                 if n > len(w.prevKey) {
     127           1 :                         n = len(w.prevKey)
     128           1 :                 }
     129           1 :                 asUint64 := func(b []byte, i int) uint64 {
     130           1 :                         return binary.LittleEndian.Uint64(b[i:])
     131           1 :                 }
     132           1 :                 for shared < n-7 && asUint64(w.curKey, shared) == asUint64(w.prevKey, shared) {
     133           1 :                         shared += 8
     134           1 :                 }
     135           1 :                 for shared < n && w.curKey[shared] == w.prevKey[shared] {
     136           1 :                         shared++
     137           1 :                 }
     138             :         }
     139             : 
     140           1 :         lenValuePlusOptionalPrefix := len(value)
     141           1 :         if addValuePrefix {
     142           1 :                 lenValuePlusOptionalPrefix++
     143           1 :         }
     144           1 :         needed := 3*binary.MaxVarintLen32 + len(w.curKey[shared:]) + lenValuePlusOptionalPrefix
     145           1 :         n := len(w.buf)
     146           1 :         if cap(w.buf) < n+needed {
     147           1 :                 newCap := 2 * cap(w.buf)
     148           1 :                 if newCap == 0 {
     149           1 :                         newCap = 1024
     150           1 :                 }
     151           1 :                 for newCap < n+needed {
     152           1 :                         newCap *= 2
     153           1 :                 }
     154           1 :                 newBuf := make([]byte, n, newCap)
     155           1 :                 copy(newBuf, w.buf)
     156           1 :                 w.buf = newBuf
     157             :         }
     158           1 :         w.buf = w.buf[:n+needed]
     159           1 : 
     160           1 :         // TODO(peter): Manually inlined versions of binary.PutUvarint(). This is 15%
     161           1 :         // faster on BenchmarkWriter on go1.13. Remove if go1.14 or future versions
     162           1 :         // show this to not be a performance win.
     163           1 :         {
     164           1 :                 x := uint32(shared)
     165           1 :                 for x >= 0x80 {
     166           0 :                         w.buf[n] = byte(x) | 0x80
     167           0 :                         x >>= 7
     168           0 :                         n++
     169           0 :                 }
     170           1 :                 w.buf[n] = byte(x)
     171           1 :                 n++
     172             :         }
     173             : 
     174           1 :         {
     175           1 :                 x := uint32(keySize - shared)
     176           1 :                 for x >= 0x80 {
     177           0 :                         w.buf[n] = byte(x) | 0x80
     178           0 :                         x >>= 7
     179           0 :                         n++
     180           0 :                 }
     181           1 :                 w.buf[n] = byte(x)
     182           1 :                 n++
     183             :         }
     184             : 
     185           1 :         {
     186           1 :                 x := uint32(lenValuePlusOptionalPrefix)
     187           1 :                 for x >= 0x80 {
     188           1 :                         w.buf[n] = byte(x) | 0x80
     189           1 :                         x >>= 7
     190           1 :                         n++
     191           1 :                 }
     192           1 :                 w.buf[n] = byte(x)
     193           1 :                 n++
     194             :         }
     195             : 
     196           1 :         n += copy(w.buf[n:], w.curKey[shared:])
     197           1 :         if addValuePrefix {
     198           1 :                 w.buf[n : n+1][0] = byte(valuePrefix)
     199           1 :                 n++
     200           1 :         }
     201           1 :         n += copy(w.buf[n:], value)
     202           1 :         w.buf = w.buf[:n]
     203           1 : 
     204           1 :         w.curValue = w.buf[n-len(value):]
     205           1 : 
     206           1 :         w.nEntries++
     207             : }
     208             : 
     209           1 : func (w *blockWriter) add(key InternalKey, value []byte) {
     210           1 :         w.addWithOptionalValuePrefix(
     211           1 :                 key, false, value, len(key.UserKey), false, 0, false)
     212           1 : }
     213             : 
     214             : // Callers that always set addValuePrefix to false should use add() instead.
     215             : //
     216             : // isObsolete indicates whether this key-value pair is obsolete in this
     217             : // sstable (only applicable when writing data blocks) -- see the comment in
     218             : // table.go and the longer one in format.go. addValuePrefix adds a 1 byte
     219             : // prefix to the value, specified in valuePrefix -- this is used for data
     220             : // blocks in TableFormatPebblev3 onwards for SETs (see the comment in
     221             : // format.go, with more details in value_block.go). setHasSameKeyPrefix is
     222             : // also used in TableFormatPebblev3 onwards for SETs.
     223             : func (w *blockWriter) addWithOptionalValuePrefix(
     224             :         key InternalKey,
     225             :         isObsolete bool,
     226             :         value []byte,
     227             :         maxSharedKeyLen int,
     228             :         addValuePrefix bool,
     229             :         valuePrefix valuePrefix,
     230             :         setHasSameKeyPrefix bool,
     231           1 : ) {
     232           1 :         w.curKey, w.prevKey = w.prevKey, w.curKey
     233           1 : 
     234           1 :         size := key.Size()
     235           1 :         if cap(w.curKey) < size {
     236           1 :                 w.curKey = make([]byte, 0, size*2)
     237           1 :         }
     238           1 :         w.curKey = w.curKey[:size]
     239           1 :         if isObsolete {
     240           1 :                 key.Trailer = key.Trailer | trailerObsoleteBit
     241           1 :         }
     242           1 :         key.Encode(w.curKey)
     243           1 : 
     244           1 :         w.storeWithOptionalValuePrefix(
     245           1 :                 size, value, maxSharedKeyLen, addValuePrefix, valuePrefix, setHasSameKeyPrefix)
     246             : }
     247             : 
     248           1 : func (w *blockWriter) finish() []byte {
     249           1 :         // Write the restart points to the buffer.
     250           1 :         if w.nEntries == 0 {
     251           1 :                 // Every block must have at least one restart point.
     252           1 :                 if cap(w.restarts) > 0 {
     253           1 :                         w.restarts = w.restarts[:1]
     254           1 :                         w.restarts[0] = 0
     255           1 :                 } else {
     256           1 :                         w.restarts = append(w.restarts, 0)
     257           1 :                 }
     258             :         }
     259           1 :         tmp4 := w.tmp[:4]
     260           1 :         for _, x := range w.restarts {
     261           1 :                 binary.LittleEndian.PutUint32(tmp4, x)
     262           1 :                 w.buf = append(w.buf, tmp4...)
     263           1 :         }
     264           1 :         binary.LittleEndian.PutUint32(tmp4, uint32(len(w.restarts)))
     265           1 :         w.buf = append(w.buf, tmp4...)
     266           1 :         result := w.buf
     267           1 : 
     268           1 :         // Reset the block state.
     269           1 :         w.nEntries = 0
     270           1 :         w.nextRestart = 0
     271           1 :         w.buf = w.buf[:0]
     272           1 :         w.restarts = w.restarts[:0]
     273           1 :         return result
     274             : }
     275             : 
     276             : // emptyBlockSize holds the size of an empty block. Every block ends
     277             : // in a uint32 trailer encoding the number of restart points within the
     278             : // block.
     279             : const emptyBlockSize = 4
     280             : 
     281           1 : func (w *blockWriter) estimatedSize() int {
     282           1 :         return len(w.buf) + 4*len(w.restarts) + emptyBlockSize
     283           1 : }
     284             : 
     285             : type blockEntry struct {
     286             :         offset   int32
     287             :         keyStart int32
     288             :         keyEnd   int32
     289             :         valStart int32
     290             :         valSize  int32
     291             : }
     292             : 
     293             : // blockIter is an iterator over a single block of data.
     294             : //
     295             : // A blockIter provides an additional guarantee around key stability when a
     296             : // block has a restart interval of 1 (i.e. when there is no prefix
     297             : // compression). Key stability refers to whether the InternalKey.UserKey bytes
     298             : // returned by a positioning call will remain stable after a subsequent
     299             : // positioning call. The normal case is that a positioning call will invalidate
     300             : // any previously returned InternalKey.UserKey. If a block has a restart
     301             : // interval of 1 (no prefix compression), blockIter guarantees that
     302             : // InternalKey.UserKey will point to the key as stored in the block itself
     303             : // which will remain valid until the blockIter is closed. The key stability
     304             : // guarantee is used by the range tombstone and range key code, which knows that
     305             : // the respective blocks are always encoded with a restart interval of 1. This
     306             : // per-block key stability guarantee is sufficient for range tombstones and
     307             : // range deletes as they are always encoded in a single block.
     308             : //
     309             : // A blockIter also provides a value stability guarantee for range deletions and
     310             : // range keys since there is only a single range deletion and range key block
     311             : // per sstable and the blockIter will not release the bytes for the block until
     312             : // it is closed.
     313             : //
     314             : // Note on why blockIter knows about lazyValueHandling:
     315             : //
     316             : // blockIter's positioning functions (that return a LazyValue), are too
     317             : // complex to inline even prior to lazyValueHandling. blockIter.Next and
     318             : // blockIter.First were by far the cheapest and had costs 195 and 180
     319             : // respectively, which exceeds the budget of 80. We initially tried to keep
     320             : // the lazyValueHandling logic out of blockIter by wrapping it with a
     321             : // lazyValueDataBlockIter. singleLevelIter and twoLevelIter would use this
     322             : // wrapped iter. The functions in lazyValueDataBlockIter were simple, in that
     323             : // they called the corresponding blockIter func and then decided whether the
     324             : // value was in fact in-place (so return immediately) or needed further
     325             : // handling. But these also turned out too costly for mid-stack inlining since
     326             : // simple calls like the following have a high cost that is barely under the
     327             : // budget of 80
     328             : //
     329             : //      k, v := i.data.SeekGE(key, flags)  // cost 74
     330             : //      k, v := i.data.Next()              // cost 72
     331             : //
     332             : // We have 2 options for minimizing performance regressions:
     333             : //   - Include the lazyValueHandling logic in the already non-inlineable
     334             : //     blockIter functions: Since most of the time is spent in data block iters,
     335             : //     it is acceptable to take the small hit of unnecessary branching (which
     336             : //     hopefully branch prediction will predict correctly) for other kinds of
     337             : //     blocks.
     338             : //   - Duplicate the logic of singleLevelIterator and twoLevelIterator for the
     339             : //     v3 sstable and only use the aforementioned lazyValueDataBlockIter for a
     340             : //     v3 sstable. We would want to manage these copies via code generation.
     341             : //
     342             : // We have picked the first option here.
     343             : type blockIter struct {
     344             :         cmp Compare
     345             :         // offset is the byte index that marks where the current key/value is
     346             :         // encoded in the block.
     347             :         offset int32
     348             :         // nextOffset is the byte index where the next key/value is encoded in the
     349             :         // block.
     350             :         nextOffset int32
     351             :         // A "restart point" in a block is a point where the full key is encoded,
     352             :         // instead of just having a suffix of the key encoded. See readEntry() for
     353             :         // how prefix compression of keys works. Keys in between two restart points
     354             :         // only have a suffix encoded in the block. When restart interval is 1, no
     355             :         // prefix compression of keys happens. This is the case with range tombstone
     356             :         // blocks.
     357             :         //
     358             :         // All restart offsets are listed in increasing order in
     359             :         // i.ptr[i.restarts:len(block)-4], while numRestarts is encoded in the last
     360             :         // 4 bytes of the block as a uint32 (i.ptr[len(block)-4:]). i.restarts can
     361             :         // therefore be seen as the point where data in the block ends, and a list
     362             :         // of offsets of all restart points begins.
     363             :         restarts int32
     364             :         // Number of restart points in this block. Encoded at the end of the block
     365             :         // as a uint32.
     366             :         numRestarts  int32
     367             :         globalSeqNum uint64
     368             :         ptr          unsafe.Pointer
     369             :         data         []byte
     370             :         // key contains the raw key the iterator is currently pointed at. This may
     371             :         // point directly to data stored in the block (for a key which has no prefix
     372             :         // compression), to fullKey (for a prefix compressed key), or to a slice of
     373             :         // data stored in cachedBuf (during reverse iteration).
     374             :         key []byte
     375             :         // fullKey is a buffer used for key prefix decompression.
     376             :         fullKey []byte
     377             :         // val contains the value the iterator is currently pointed at. If non-nil,
     378             :         // this points to a slice of the block data.
     379             :         val []byte
     380             :         // lazyValue is val turned into a LazyValue, whenever a positioning method
     381             :         // returns a non-nil key-value pair.
     382             :         lazyValue base.LazyValue
     383             :         // ikey contains the decoded InternalKey the iterator is currently pointed
     384             :         // at. Note that the memory backing ikey.UserKey is either data stored
     385             :         // directly in the block, fullKey, or cachedBuf. The key stability guarantee
     386             :         // for blocks built with a restart interval of 1 is achieved by having
     387             :         // ikey.UserKey always point to data stored directly in the block.
     388             :         ikey InternalKey
     389             :         // cached and cachedBuf are used during reverse iteration. They are needed
     390             :         // because we can't perform prefix decoding in reverse, only in the forward
     391             :         // direction. In order to iterate in reverse, we decode and cache the entries
     392             :         // between two restart points.
     393             :         //
     394             :         // Note that cached[len(cached)-1] contains the previous entry to the one the
     395             :         // blockIter is currently pointed at. As usual, nextOffset will contain the
     396             :         // offset of the next entry. During reverse iteration, nextOffset will be
     397             :         // updated to point to offset, and we'll set the blockIter to point at the
     398             :         // entry cached[len(cached)-1]. See Prev() for more details.
     399             :         //
     400             :         // For a block encoded with a restart interval of 1, cached and cachedBuf
     401             :         // will not be used as there are no prefix compressed entries between the
     402             :         // restart points.
     403             :         cached    []blockEntry
     404             :         cachedBuf []byte
     405             :         handle    bufferHandle
     406             :         // for block iteration for already loaded blocks.
     407             :         firstUserKey      []byte
     408             :         lazyValueHandling struct {
     409             :                 vbr            *valueBlockReader
     410             :                 hasValuePrefix bool
     411             :         }
     412             :         hideObsoletePoints bool
     413             : }
     414             : 
     415             : // blockIter implements the base.InternalIterator interface.
     416             : var _ base.InternalIterator = (*blockIter)(nil)
     417             : 
     418           1 : func newBlockIter(cmp Compare, block block) (*blockIter, error) {
     419           1 :         i := &blockIter{}
     420           1 :         return i, i.init(cmp, block, 0, false)
     421           1 : }
     422             : 
     423           0 : func (i *blockIter) String() string {
     424           0 :         return "block"
     425           0 : }
     426             : 
     427             : func (i *blockIter) init(
     428             :         cmp Compare, block block, globalSeqNum uint64, hideObsoletePoints bool,
     429           1 : ) error {
     430           1 :         numRestarts := int32(binary.LittleEndian.Uint32(block[len(block)-4:]))
     431           1 :         if numRestarts == 0 {
     432           0 :                 return base.CorruptionErrorf("pebble/table: invalid table (block has no restart points)")
     433           0 :         }
     434           1 :         i.cmp = cmp
     435           1 :         i.restarts = int32(len(block)) - 4*(1+numRestarts)
     436           1 :         i.numRestarts = numRestarts
     437           1 :         i.globalSeqNum = globalSeqNum
     438           1 :         i.ptr = unsafe.Pointer(&block[0])
     439           1 :         i.data = block
     440           1 :         i.fullKey = i.fullKey[:0]
     441           1 :         i.val = nil
     442           1 :         i.hideObsoletePoints = hideObsoletePoints
     443           1 :         i.clearCache()
     444           1 :         if i.restarts > 0 {
     445           1 :                 if err := i.readFirstKey(); err != nil {
     446           0 :                         return err
     447           0 :                 }
     448           1 :         } else {
     449           1 :                 // Block is empty.
     450           1 :                 i.firstUserKey = nil
     451           1 :         }
     452           1 :         return nil
     453             : }
     454             : 
     455             : // NB: two cases of hideObsoletePoints:
     456             : //   - Local sstable iteration: globalSeqNum will be set iff the sstable was
     457             : //     ingested.
     458             : //   - Foreign sstable iteration: globalSeqNum is always set.
     459             : func (i *blockIter) initHandle(
     460             :         cmp Compare, block bufferHandle, globalSeqNum uint64, hideObsoletePoints bool,
     461           1 : ) error {
     462           1 :         i.handle.Release()
     463           1 :         i.handle = block
     464           1 :         return i.init(cmp, block.Get(), globalSeqNum, hideObsoletePoints)
     465           1 : }
     466             : 
     467           1 : func (i *blockIter) invalidate() {
     468           1 :         i.clearCache()
     469           1 :         i.offset = 0
     470           1 :         i.nextOffset = 0
     471           1 :         i.restarts = 0
     472           1 :         i.numRestarts = 0
     473           1 :         i.data = nil
     474           1 : }
     475             : 
     476             : // isDataInvalidated returns true when the blockIter has been invalidated
     477             : // using an invalidate call. NB: this is different from blockIter.Valid
     478             : // which is part of the InternalIterator implementation.
     479           1 : func (i *blockIter) isDataInvalidated() bool {
     480           1 :         return i.data == nil
     481           1 : }
     482             : 
     483           1 : func (i *blockIter) resetForReuse() blockIter {
     484           1 :         return blockIter{
     485           1 :                 fullKey:   i.fullKey[:0],
     486           1 :                 cached:    i.cached[:0],
     487           1 :                 cachedBuf: i.cachedBuf[:0],
     488           1 :                 data:      nil,
     489           1 :         }
     490           1 : }
     491             : 
     492           1 : func (i *blockIter) readEntry() {
     493           1 :         ptr := unsafe.Pointer(uintptr(i.ptr) + uintptr(i.offset))
     494           1 : 
     495           1 :         // This is an ugly performance hack. Reading entries from blocks is one of
     496           1 :         // the inner-most routines and decoding the 3 varints per-entry takes
     497           1 :         // significant time. Neither go1.11 or go1.12 will inline decodeVarint for
     498           1 :         // us, so we do it manually. This provides a 10-15% performance improvement
     499           1 :         // on blockIter benchmarks on both go1.11 and go1.12.
     500           1 :         //
     501           1 :         // TODO(peter): remove this hack if go:inline is ever supported.
     502           1 : 
     503           1 :         var shared uint32
     504           1 :         if a := *((*uint8)(ptr)); a < 128 {
     505           1 :                 shared = uint32(a)
     506           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     507           1 :         } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
     508           0 :                 shared = uint32(b)<<7 | uint32(a)
     509           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     510           0 :         } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
     511           0 :                 shared = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     512           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     513           0 :         } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
     514           0 :                 shared = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     515           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     516           0 :         } else {
     517           0 :                 d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
     518           0 :                 shared = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     519           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     520           0 :         }
     521             : 
     522           1 :         var unshared uint32
     523           1 :         if a := *((*uint8)(ptr)); a < 128 {
     524           1 :                 unshared = uint32(a)
     525           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     526           1 :         } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
     527           0 :                 unshared = uint32(b)<<7 | uint32(a)
     528           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     529           0 :         } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
     530           0 :                 unshared = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     531           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     532           0 :         } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
     533           0 :                 unshared = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     534           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     535           0 :         } else {
     536           0 :                 d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
     537           0 :                 unshared = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     538           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     539           0 :         }
     540             : 
     541           1 :         var value uint32
     542           1 :         if a := *((*uint8)(ptr)); a < 128 {
     543           1 :                 value = uint32(a)
     544           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     545           1 :         } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
     546           1 :                 value = uint32(b)<<7 | uint32(a)
     547           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     548           1 :         } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
     549           0 :                 value = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     550           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     551           0 :         } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
     552           0 :                 value = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     553           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     554           0 :         } else {
     555           0 :                 d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
     556           0 :                 value = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     557           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     558           0 :         }
     559             : 
     560           1 :         unsharedKey := getBytes(ptr, int(unshared))
     561           1 :         // TODO(sumeer): move this into the else block below.
     562           1 :         i.fullKey = append(i.fullKey[:shared], unsharedKey...)
     563           1 :         if shared == 0 {
     564           1 :                 // Provide stability for the key across positioning calls if the key
     565           1 :                 // doesn't share a prefix with the previous key. This removes requiring the
     566           1 :                 // key to be copied if the caller knows the block has a restart interval of
     567           1 :                 // 1. An important example of this is range-del blocks.
     568           1 :                 i.key = unsharedKey
     569           1 :         } else {
     570           1 :                 i.key = i.fullKey
     571           1 :         }
     572           1 :         ptr = unsafe.Pointer(uintptr(ptr) + uintptr(unshared))
     573           1 :         i.val = getBytes(ptr, int(value))
     574           1 :         i.nextOffset = int32(uintptr(ptr)-uintptr(i.ptr)) + int32(value)
     575             : }
     576             : 
     577           1 : func (i *blockIter) readFirstKey() error {
     578           1 :         ptr := i.ptr
     579           1 : 
     580           1 :         // This is an ugly performance hack. Reading entries from blocks is one of
     581           1 :         // the inner-most routines and decoding the 3 varints per-entry takes
     582           1 :         // significant time. Neither go1.11 or go1.12 will inline decodeVarint for
     583           1 :         // us, so we do it manually. This provides a 10-15% performance improvement
     584           1 :         // on blockIter benchmarks on both go1.11 and go1.12.
     585           1 :         //
     586           1 :         // TODO(peter): remove this hack if go:inline is ever supported.
     587           1 : 
     588           1 :         if shared := *((*uint8)(ptr)); shared == 0 {
     589           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     590           1 :         } else {
     591           0 :                 // The shared length is != 0, which is invalid.
     592           0 :                 panic("first key in block must have zero shared length")
     593             :         }
     594             : 
     595           1 :         var unshared uint32
     596           1 :         if a := *((*uint8)(ptr)); a < 128 {
     597           1 :                 unshared = uint32(a)
     598           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     599           1 :         } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
     600           0 :                 unshared = uint32(b)<<7 | uint32(a)
     601           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     602           0 :         } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
     603           0 :                 unshared = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     604           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     605           0 :         } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
     606           0 :                 unshared = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     607           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     608           0 :         } else {
     609           0 :                 d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
     610           0 :                 unshared = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     611           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     612           0 :         }
     613             : 
     614             :         // Skip the value length.
     615           1 :         if a := *((*uint8)(ptr)); a < 128 {
     616           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     617           1 :         } else if a := *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); a < 128 {
     618           1 :                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     619           1 :         } else if a := *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); a < 128 {
     620           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     621           0 :         } else if a := *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); a < 128 {
     622           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     623           0 :         } else {
     624           0 :                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     625           0 :         }
     626             : 
     627           1 :         firstKey := getBytes(ptr, int(unshared))
     628           1 :         // Manually inlining base.DecodeInternalKey provides a 5-10% speedup on
     629           1 :         // BlockIter benchmarks.
     630           1 :         if n := len(firstKey) - 8; n >= 0 {
     631           1 :                 i.firstUserKey = firstKey[:n:n]
     632           1 :         } else {
     633           0 :                 i.firstUserKey = nil
     634           0 :                 return base.CorruptionErrorf("pebble/table: invalid firstKey in block")
     635           0 :         }
     636           1 :         return nil
     637             : }
     638             : 
     639             : // The sstable internal obsolete bit is set when writing a block and unset by
     640             : // blockIter, so no code outside block writing/reading code ever sees it.
     641             : const trailerObsoleteBit = uint64(base.InternalKeyKindSSTableInternalObsoleteBit)
     642             : const trailerObsoleteMask = (InternalKeySeqNumMax << 8) | uint64(base.InternalKeyKindSSTableInternalObsoleteMask)
     643             : 
     644           1 : func (i *blockIter) decodeInternalKey(key []byte) (hiddenPoint bool) {
     645           1 :         // Manually inlining base.DecodeInternalKey provides a 5-10% speedup on
     646           1 :         // BlockIter benchmarks.
     647           1 :         if n := len(key) - 8; n >= 0 {
     648           1 :                 trailer := binary.LittleEndian.Uint64(key[n:])
     649           1 :                 hiddenPoint = i.hideObsoletePoints &&
     650           1 :                         (trailer&trailerObsoleteBit != 0)
     651           1 :                 i.ikey.Trailer = trailer & trailerObsoleteMask
     652           1 :                 i.ikey.UserKey = key[:n:n]
     653           1 :                 if i.globalSeqNum != 0 {
     654           1 :                         i.ikey.SetSeqNum(i.globalSeqNum)
     655           1 :                 }
     656           1 :         } else {
     657           1 :                 i.ikey.Trailer = uint64(InternalKeyKindInvalid)
     658           1 :                 i.ikey.UserKey = nil
     659           1 :         }
     660           1 :         return hiddenPoint
     661             : }
     662             : 
     663           1 : func (i *blockIter) clearCache() {
     664           1 :         i.cached = i.cached[:0]
     665           1 :         i.cachedBuf = i.cachedBuf[:0]
     666           1 : }
     667             : 
     668           1 : func (i *blockIter) cacheEntry() {
     669           1 :         var valStart int32
     670           1 :         valSize := int32(len(i.val))
     671           1 :         if valSize > 0 {
     672           1 :                 valStart = int32(uintptr(unsafe.Pointer(&i.val[0])) - uintptr(i.ptr))
     673           1 :         }
     674             : 
     675           1 :         i.cached = append(i.cached, blockEntry{
     676           1 :                 offset:   i.offset,
     677           1 :                 keyStart: int32(len(i.cachedBuf)),
     678           1 :                 keyEnd:   int32(len(i.cachedBuf) + len(i.key)),
     679           1 :                 valStart: valStart,
     680           1 :                 valSize:  valSize,
     681           1 :         })
     682           1 :         i.cachedBuf = append(i.cachedBuf, i.key...)
     683             : }
     684             : 
     685           1 : func (i *blockIter) getFirstUserKey() []byte {
     686           1 :         return i.firstUserKey
     687           1 : }
     688             : 
     689             : // SeekGE implements internalIterator.SeekGE, as documented in the pebble
     690             : // package.
     691           1 : func (i *blockIter) SeekGE(key []byte, flags base.SeekGEFlags) (*InternalKey, base.LazyValue) {
     692           1 :         if invariants.Enabled && i.isDataInvalidated() {
     693           0 :                 panic(errors.AssertionFailedf("invalidated blockIter used"))
     694             :         }
     695             : 
     696           1 :         i.clearCache()
     697           1 :         // Find the index of the smallest restart point whose key is > the key
     698           1 :         // sought; index will be numRestarts if there is no such restart point.
     699           1 :         i.offset = 0
     700           1 :         var index int32
     701           1 : 
     702           1 :         {
     703           1 :                 // NB: manually inlined sort.Seach is ~5% faster.
     704           1 :                 //
     705           1 :                 // Define f(-1) == false and f(n) == true.
     706           1 :                 // Invariant: f(index-1) == false, f(upper) == true.
     707           1 :                 upper := i.numRestarts
     708           1 :                 for index < upper {
     709           1 :                         h := int32(uint(index+upper) >> 1) // avoid overflow when computing h
     710           1 :                         // index ≤ h < upper
     711           1 :                         offset := decodeRestart(i.data[i.restarts+4*h:])
     712           1 :                         // For a restart point, there are 0 bytes shared with the previous key.
     713           1 :                         // The varint encoding of 0 occupies 1 byte.
     714           1 :                         ptr := unsafe.Pointer(uintptr(i.ptr) + uintptr(offset+1))
     715           1 : 
     716           1 :                         // Decode the key at that restart point, and compare it to the key
     717           1 :                         // sought. See the comment in readEntry for why we manually inline the
     718           1 :                         // varint decoding.
     719           1 :                         var v1 uint32
     720           1 :                         if a := *((*uint8)(ptr)); a < 128 {
     721           1 :                                 v1 = uint32(a)
     722           1 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     723           1 :                         } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
     724           0 :                                 v1 = uint32(b)<<7 | uint32(a)
     725           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     726           0 :                         } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
     727           0 :                                 v1 = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     728           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     729           0 :                         } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
     730           0 :                                 v1 = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     731           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     732           0 :                         } else {
     733           0 :                                 d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
     734           0 :                                 v1 = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     735           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     736           0 :                         }
     737             : 
     738           1 :                         if *((*uint8)(ptr)) < 128 {
     739           1 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     740           1 :                         } else if *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))) < 128 {
     741           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     742           0 :                         } else if *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))) < 128 {
     743           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     744           0 :                         } else if *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))) < 128 {
     745           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     746           0 :                         } else {
     747           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     748           0 :                         }
     749             : 
     750             :                         // Manually inlining part of base.DecodeInternalKey provides a 5-10%
     751             :                         // speedup on BlockIter benchmarks.
     752           1 :                         s := getBytes(ptr, int(v1))
     753           1 :                         var k []byte
     754           1 :                         if n := len(s) - 8; n >= 0 {
     755           1 :                                 k = s[:n:n]
     756           1 :                         }
     757             :                         // Else k is invalid, and left as nil
     758             : 
     759           1 :                         if i.cmp(key, k) > 0 {
     760           1 :                                 // The search key is greater than the user key at this restart point.
     761           1 :                                 // Search beyond this restart point, since we are trying to find the
     762           1 :                                 // first restart point with a user key >= the search key.
     763           1 :                                 index = h + 1 // preserves f(i-1) == false
     764           1 :                         } else {
     765           1 :                                 // k >= search key, so prune everything after index (since index
     766           1 :                                 // satisfies the property we are looking for).
     767           1 :                                 upper = h // preserves f(j) == true
     768           1 :                         }
     769             :                 }
     770             :                 // index == upper, f(index-1) == false, and f(upper) (= f(index)) == true
     771             :                 // => answer is index.
     772             :         }
     773             : 
     774             :         // index is the first restart point with key >= search key. Define the keys
     775             :         // between a restart point and the next restart point as belonging to that
     776             :         // restart point.
     777             :         //
     778             :         // Since keys are strictly increasing, if index > 0 then the restart point
     779             :         // at index-1 will be the first one that has some keys belonging to it that
     780             :         // could be equal to the search key.  If index == 0, then all keys in this
     781             :         // block are larger than the key sought, and offset remains at zero.
     782           1 :         if index > 0 {
     783           1 :                 i.offset = decodeRestart(i.data[i.restarts+4*(index-1):])
     784           1 :         }
     785           1 :         i.readEntry()
     786           1 :         hiddenPoint := i.decodeInternalKey(i.key)
     787           1 : 
     788           1 :         // Iterate from that restart point to somewhere >= the key sought.
     789           1 :         if !i.valid() {
     790           0 :                 return nil, base.LazyValue{}
     791           0 :         }
     792           1 :         if !hiddenPoint && i.cmp(i.ikey.UserKey, key) >= 0 {
     793           1 :                 // Initialize i.lazyValue
     794           1 :                 if !i.lazyValueHandling.hasValuePrefix ||
     795           1 :                         base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
     796           1 :                         i.lazyValue = base.MakeInPlaceValue(i.val)
     797           1 :                 } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
     798           1 :                         i.lazyValue = base.MakeInPlaceValue(i.val[1:])
     799           1 :                 } else {
     800           1 :                         i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
     801           1 :                 }
     802           1 :                 return &i.ikey, i.lazyValue
     803             :         }
     804           1 :         for i.Next(); i.valid(); i.Next() {
     805           1 :                 if i.cmp(i.ikey.UserKey, key) >= 0 {
     806           1 :                         // i.Next() has already initialized i.lazyValue.
     807           1 :                         return &i.ikey, i.lazyValue
     808           1 :                 }
     809             :         }
     810           1 :         return nil, base.LazyValue{}
     811             : }
     812             : 
     813             : // SeekPrefixGE implements internalIterator.SeekPrefixGE, as documented in the
     814             : // pebble package.
     815             : func (i *blockIter) SeekPrefixGE(
     816             :         prefix, key []byte, flags base.SeekGEFlags,
     817           0 : ) (*base.InternalKey, base.LazyValue) {
     818           0 :         // This should never be called as prefix iteration is handled by sstable.Iterator.
     819           0 :         panic("pebble: SeekPrefixGE unimplemented")
     820             : }
     821             : 
     822             : // SeekLT implements internalIterator.SeekLT, as documented in the pebble
     823             : // package.
     824           1 : func (i *blockIter) SeekLT(key []byte, flags base.SeekLTFlags) (*InternalKey, base.LazyValue) {
     825           1 :         if invariants.Enabled && i.isDataInvalidated() {
     826           0 :                 panic(errors.AssertionFailedf("invalidated blockIter used"))
     827             :         }
     828             : 
     829           1 :         i.clearCache()
     830           1 :         // Find the index of the smallest restart point whose key is >= the key
     831           1 :         // sought; index will be numRestarts if there is no such restart point.
     832           1 :         i.offset = 0
     833           1 :         var index int32
     834           1 : 
     835           1 :         {
     836           1 :                 // NB: manually inlined sort.Search is ~5% faster.
     837           1 :                 //
     838           1 :                 // Define f(-1) == false and f(n) == true.
     839           1 :                 // Invariant: f(index-1) == false, f(upper) == true.
     840           1 :                 upper := i.numRestarts
     841           1 :                 for index < upper {
     842           1 :                         h := int32(uint(index+upper) >> 1) // avoid overflow when computing h
     843           1 :                         // index ≤ h < upper
     844           1 :                         offset := decodeRestart(i.data[i.restarts+4*h:])
     845           1 :                         // For a restart point, there are 0 bytes shared with the previous key.
     846           1 :                         // The varint encoding of 0 occupies 1 byte.
     847           1 :                         ptr := unsafe.Pointer(uintptr(i.ptr) + uintptr(offset+1))
     848           1 : 
     849           1 :                         // Decode the key at that restart point, and compare it to the key
     850           1 :                         // sought. See the comment in readEntry for why we manually inline the
     851           1 :                         // varint decoding.
     852           1 :                         var v1 uint32
     853           1 :                         if a := *((*uint8)(ptr)); a < 128 {
     854           1 :                                 v1 = uint32(a)
     855           1 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     856           1 :                         } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
     857           0 :                                 v1 = uint32(b)<<7 | uint32(a)
     858           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     859           0 :                         } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
     860           0 :                                 v1 = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     861           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     862           0 :                         } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
     863           0 :                                 v1 = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     864           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     865           0 :                         } else {
     866           0 :                                 d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
     867           0 :                                 v1 = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
     868           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     869           0 :                         }
     870             : 
     871           1 :                         if *((*uint8)(ptr)) < 128 {
     872           1 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 1)
     873           1 :                         } else if *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))) < 128 {
     874           1 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 2)
     875           1 :                         } else if *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))) < 128 {
     876           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 3)
     877           0 :                         } else if *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))) < 128 {
     878           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 4)
     879           0 :                         } else {
     880           0 :                                 ptr = unsafe.Pointer(uintptr(ptr) + 5)
     881           0 :                         }
     882             : 
     883             :                         // Manually inlining part of base.DecodeInternalKey provides a 5-10%
     884             :                         // speedup on BlockIter benchmarks.
     885           1 :                         s := getBytes(ptr, int(v1))
     886           1 :                         var k []byte
     887           1 :                         if n := len(s) - 8; n >= 0 {
     888           1 :                                 k = s[:n:n]
     889           1 :                         }
     890             :                         // Else k is invalid, and left as nil
     891             : 
     892           1 :                         if i.cmp(key, k) > 0 {
     893           1 :                                 // The search key is greater than the user key at this restart point.
     894           1 :                                 // Search beyond this restart point, since we are trying to find the
     895           1 :                                 // first restart point with a user key >= the search key.
     896           1 :                                 index = h + 1 // preserves f(i-1) == false
     897           1 :                         } else {
     898           1 :                                 // k >= search key, so prune everything after index (since index
     899           1 :                                 // satisfies the property we are looking for).
     900           1 :                                 upper = h // preserves f(j) == true
     901           1 :                         }
     902             :                 }
     903             :                 // index == upper, f(index-1) == false, and f(upper) (= f(index)) == true
     904             :                 // => answer is index.
     905             :         }
     906             : 
     907             :         // index is the first restart point with key >= search key. Define the keys
     908             :         // between a restart point and the next restart point as belonging to that
     909             :         // restart point. Note that index could be equal to i.numRestarts, i.e., we
     910             :         // are past the last restart.
     911             :         //
     912             :         // Since keys are strictly increasing, if index > 0 then the restart point
     913             :         // at index-1 will be the first one that has some keys belonging to it that
     914             :         // are less than the search key.  If index == 0, then all keys in this block
     915             :         // are larger than the search key, so there is no match.
     916           1 :         targetOffset := i.restarts
     917           1 :         if index > 0 {
     918           1 :                 i.offset = decodeRestart(i.data[i.restarts+4*(index-1):])
     919           1 :                 if index < i.numRestarts {
     920           1 :                         targetOffset = decodeRestart(i.data[i.restarts+4*(index):])
     921           1 :                 }
     922           1 :         } else if index == 0 {
     923           1 :                 // If index == 0 then all keys in this block are larger than the key
     924           1 :                 // sought.
     925           1 :                 i.offset = -1
     926           1 :                 i.nextOffset = 0
     927           1 :                 return nil, base.LazyValue{}
     928           1 :         }
     929             : 
     930             :         // Iterate from that restart point to somewhere >= the key sought, then back
     931             :         // up to the previous entry. The expectation is that we'll be performing
     932             :         // reverse iteration, so we cache the entries as we advance forward.
     933           1 :         i.nextOffset = i.offset
     934           1 : 
     935           1 :         for {
     936           1 :                 i.offset = i.nextOffset
     937           1 :                 i.readEntry()
     938           1 :                 // When hidden keys are common, there is additional optimization possible
     939           1 :                 // by not caching entries that are hidden (note that some calls to
     940           1 :                 // cacheEntry don't decode the internal key before caching, but checking
     941           1 :                 // whether a key is hidden does not require full decoding). However, we do
     942           1 :                 // need to use the blockEntry.offset in the cache for the first entry at
     943           1 :                 // the reset point to do the binary search when the cache is empty -- so
     944           1 :                 // we would need to cache that first entry (though not the key) even if
     945           1 :                 // was hidden. Our current assumption is that if there are large numbers
     946           1 :                 // of hidden keys we will be able to skip whole blocks (using block
     947           1 :                 // property filters) so we don't bother optimizing.
     948           1 :                 hiddenPoint := i.decodeInternalKey(i.key)
     949           1 : 
     950           1 :                 // NB: we don't use the hiddenPoint return value of decodeInternalKey
     951           1 :                 // since we want to stop as soon as we reach a key >= ikey.UserKey, so
     952           1 :                 // that we can reverse.
     953           1 :                 if i.cmp(i.ikey.UserKey, key) >= 0 {
     954           1 :                         // The current key is greater than or equal to our search key. Back up to
     955           1 :                         // the previous key which was less than our search key. Note that this for
     956           1 :                         // loop will execute at least once with this if-block not being true, so
     957           1 :                         // the key we are backing up to is the last one this loop cached.
     958           1 :                         return i.Prev()
     959           1 :                 }
     960             : 
     961           1 :                 if i.nextOffset >= targetOffset {
     962           1 :                         // We've reached the end of the current restart block. Return the
     963           1 :                         // current key if not hidden, else call Prev().
     964           1 :                         //
     965           1 :                         // When the restart interval is 1, the first iteration of the for loop
     966           1 :                         // will bring us here. In that case ikey is backed by the block so we
     967           1 :                         // get the desired key stability guarantee for the lifetime of the
     968           1 :                         // blockIter. That is, we never cache anything and therefore never
     969           1 :                         // return a key backed by cachedBuf.
     970           1 :                         if hiddenPoint {
     971           1 :                                 return i.Prev()
     972           1 :                         }
     973           1 :                         break
     974             :                 }
     975             : 
     976           1 :                 i.cacheEntry()
     977             :         }
     978             : 
     979           1 :         if !i.valid() {
     980           1 :                 return nil, base.LazyValue{}
     981           1 :         }
     982           1 :         if !i.lazyValueHandling.hasValuePrefix ||
     983           1 :                 base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
     984           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val)
     985           1 :         } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
     986           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val[1:])
     987           1 :         } else {
     988           1 :                 i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
     989           1 :         }
     990           1 :         return &i.ikey, i.lazyValue
     991             : }
     992             : 
     993             : // First implements internalIterator.First, as documented in the pebble
     994             : // package.
     995           1 : func (i *blockIter) First() (*InternalKey, base.LazyValue) {
     996           1 :         if invariants.Enabled && i.isDataInvalidated() {
     997           0 :                 panic(errors.AssertionFailedf("invalidated blockIter used"))
     998             :         }
     999             : 
    1000           1 :         i.offset = 0
    1001           1 :         if !i.valid() {
    1002           1 :                 return nil, base.LazyValue{}
    1003           1 :         }
    1004           1 :         i.clearCache()
    1005           1 :         i.readEntry()
    1006           1 :         hiddenPoint := i.decodeInternalKey(i.key)
    1007           1 :         if hiddenPoint {
    1008           1 :                 return i.Next()
    1009           1 :         }
    1010           1 :         if !i.lazyValueHandling.hasValuePrefix ||
    1011           1 :                 base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
    1012           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val)
    1013           1 :         } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
    1014           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val[1:])
    1015           1 :         } else {
    1016           1 :                 i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
    1017           1 :         }
    1018           1 :         return &i.ikey, i.lazyValue
    1019             : }
    1020             : 
    1021           1 : func decodeRestart(b []byte) int32 {
    1022           1 :         _ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
    1023           1 :         return int32(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 |
    1024           1 :                 uint32(b[3]&restartMaskLittleEndianHighByteWithoutSetHasSamePrefix)<<24)
    1025           1 : }
    1026             : 
    1027             : // Last implements internalIterator.Last, as documented in the pebble package.
    1028           1 : func (i *blockIter) Last() (*InternalKey, base.LazyValue) {
    1029           1 :         if invariants.Enabled && i.isDataInvalidated() {
    1030           0 :                 panic(errors.AssertionFailedf("invalidated blockIter used"))
    1031             :         }
    1032             : 
    1033             :         // Seek forward from the last restart point.
    1034           1 :         i.offset = decodeRestart(i.data[i.restarts+4*(i.numRestarts-1):])
    1035           1 :         if !i.valid() {
    1036           1 :                 return nil, base.LazyValue{}
    1037           1 :         }
    1038             : 
    1039           1 :         i.readEntry()
    1040           1 :         i.clearCache()
    1041           1 : 
    1042           1 :         for i.nextOffset < i.restarts {
    1043           1 :                 i.cacheEntry()
    1044           1 :                 i.offset = i.nextOffset
    1045           1 :                 i.readEntry()
    1046           1 :         }
    1047             : 
    1048           1 :         hiddenPoint := i.decodeInternalKey(i.key)
    1049           1 :         if hiddenPoint {
    1050           1 :                 return i.Prev()
    1051           1 :         }
    1052           1 :         if !i.lazyValueHandling.hasValuePrefix ||
    1053           1 :                 base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
    1054           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val)
    1055           1 :         } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
    1056           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val[1:])
    1057           1 :         } else {
    1058           1 :                 i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
    1059           1 :         }
    1060           1 :         return &i.ikey, i.lazyValue
    1061             : }
    1062             : 
    1063             : // Next implements internalIterator.Next, as documented in the pebble
    1064             : // package.
    1065           1 : func (i *blockIter) Next() (*InternalKey, base.LazyValue) {
    1066           1 :         if len(i.cachedBuf) > 0 {
    1067           1 :                 // We're switching from reverse iteration to forward iteration. We need to
    1068           1 :                 // populate i.fullKey with the current key we're positioned at so that
    1069           1 :                 // readEntry() can use i.fullKey for key prefix decompression. Note that we
    1070           1 :                 // don't know whether i.key is backed by i.cachedBuf or i.fullKey (if
    1071           1 :                 // SeekLT was the previous call, i.key may be backed by i.fullKey), but
    1072           1 :                 // copying into i.fullKey works for both cases.
    1073           1 :                 //
    1074           1 :                 // TODO(peter): Rather than clearing the cache, we could instead use the
    1075           1 :                 // cache until it is exhausted. This would likely be faster than falling
    1076           1 :                 // through to the normal forward iteration code below.
    1077           1 :                 i.fullKey = append(i.fullKey[:0], i.key...)
    1078           1 :                 i.clearCache()
    1079           1 :         }
    1080             : 
    1081             : start:
    1082           1 :         i.offset = i.nextOffset
    1083           1 :         if !i.valid() {
    1084           1 :                 return nil, base.LazyValue{}
    1085           1 :         }
    1086           1 :         i.readEntry()
    1087           1 :         // Manually inlined version of i.decodeInternalKey(i.key).
    1088           1 :         if n := len(i.key) - 8; n >= 0 {
    1089           1 :                 trailer := binary.LittleEndian.Uint64(i.key[n:])
    1090           1 :                 hiddenPoint := i.hideObsoletePoints &&
    1091           1 :                         (trailer&trailerObsoleteBit != 0)
    1092           1 :                 i.ikey.Trailer = trailer & trailerObsoleteMask
    1093           1 :                 i.ikey.UserKey = i.key[:n:n]
    1094           1 :                 if i.globalSeqNum != 0 {
    1095           1 :                         i.ikey.SetSeqNum(i.globalSeqNum)
    1096           1 :                 }
    1097           1 :                 if hiddenPoint {
    1098           1 :                         goto start
    1099             :                 }
    1100           0 :         } else {
    1101           0 :                 i.ikey.Trailer = uint64(InternalKeyKindInvalid)
    1102           0 :                 i.ikey.UserKey = nil
    1103           0 :         }
    1104           1 :         if !i.lazyValueHandling.hasValuePrefix ||
    1105           1 :                 base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
    1106           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val)
    1107           1 :         } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
    1108           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val[1:])
    1109           1 :         } else {
    1110           1 :                 i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
    1111           1 :         }
    1112           1 :         return &i.ikey, i.lazyValue
    1113             : }
    1114             : 
    1115             : // NextPrefix implements (base.InternalIterator).NextPrefix.
    1116           1 : func (i *blockIter) NextPrefix(succKey []byte) (*InternalKey, base.LazyValue) {
    1117           1 :         if i.lazyValueHandling.hasValuePrefix {
    1118           1 :                 return i.nextPrefixV3(succKey)
    1119           1 :         }
    1120           1 :         const nextsBeforeSeek = 3
    1121           1 :         k, v := i.Next()
    1122           1 :         for j := 1; k != nil && i.cmp(k.UserKey, succKey) < 0; j++ {
    1123           1 :                 if j >= nextsBeforeSeek {
    1124           1 :                         return i.SeekGE(succKey, base.SeekGEFlagsNone)
    1125           1 :                 }
    1126           1 :                 k, v = i.Next()
    1127             :         }
    1128           1 :         return k, v
    1129             : }
    1130             : 
    1131           1 : func (i *blockIter) nextPrefixV3(succKey []byte) (*InternalKey, base.LazyValue) {
    1132           1 :         // Doing nexts that involve a key comparison can be expensive (and the cost
    1133           1 :         // depends on the key length), so we use the same threshold of 3 that we use
    1134           1 :         // for TableFormatPebblev2 in blockIter.nextPrefix above. The next fast path
    1135           1 :         // that looks at setHasSamePrefix takes ~5ns per key, which is ~150x faster
    1136           1 :         // than doing a SeekGE within the block, so we do this 16 times
    1137           1 :         // (~5ns*16=80ns), and then switch to looking at restarts. Doing the binary
    1138           1 :         // search for the restart consumes > 100ns. If the number of versions is >
    1139           1 :         // 17, we will increment nextFastCount to 17, then do a binary search, and
    1140           1 :         // on average need to find a key between two restarts, so another 8 steps
    1141           1 :         // corresponding to nextFastCount, for a mean total of 17 + 8 = 25 such
    1142           1 :         // steps.
    1143           1 :         //
    1144           1 :         // TODO(sumeer): use the configured restartInterval for the sstable when it
    1145           1 :         // was written (which we don't currently store) instead of the default value
    1146           1 :         // of 16.
    1147           1 :         const nextCmpThresholdBeforeSeek = 3
    1148           1 :         const nextFastThresholdBeforeRestarts = 16
    1149           1 :         nextCmpCount := 0
    1150           1 :         nextFastCount := 0
    1151           1 :         usedRestarts := false
    1152           1 :         // INVARIANT: blockIter is valid.
    1153           1 :         if invariants.Enabled && !i.valid() {
    1154           0 :                 panic(errors.AssertionFailedf("nextPrefixV3 called on invalid blockIter"))
    1155             :         }
    1156           1 :         prevKeyIsSet := i.ikey.Kind() == InternalKeyKindSet
    1157           1 :         for {
    1158           1 :                 i.offset = i.nextOffset
    1159           1 :                 if !i.valid() {
    1160           1 :                         return nil, base.LazyValue{}
    1161           1 :                 }
    1162             :                 // Need to decode the length integers, so we can compute nextOffset.
    1163           1 :                 ptr := unsafe.Pointer(uintptr(i.ptr) + uintptr(i.offset))
    1164           1 :                 // This is an ugly performance hack. Reading entries from blocks is one of
    1165           1 :                 // the inner-most routines and decoding the 3 varints per-entry takes
    1166           1 :                 // significant time. Neither go1.11 or go1.12 will inline decodeVarint for
    1167           1 :                 // us, so we do it manually. This provides a 10-15% performance improvement
    1168           1 :                 // on blockIter benchmarks on both go1.11 and go1.12.
    1169           1 :                 //
    1170           1 :                 // TODO(peter): remove this hack if go:inline is ever supported.
    1171           1 : 
    1172           1 :                 // Decode the shared key length integer.
    1173           1 :                 var shared uint32
    1174           1 :                 if a := *((*uint8)(ptr)); a < 128 {
    1175           1 :                         shared = uint32(a)
    1176           1 :                         ptr = unsafe.Pointer(uintptr(ptr) + 1)
    1177           1 :                 } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
    1178           0 :                         shared = uint32(b)<<7 | uint32(a)
    1179           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 2)
    1180           0 :                 } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
    1181           0 :                         shared = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1182           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 3)
    1183           0 :                 } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
    1184           0 :                         shared = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1185           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 4)
    1186           0 :                 } else {
    1187           0 :                         d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
    1188           0 :                         shared = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1189           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 5)
    1190           0 :                 }
    1191             :                 // Decode the unshared key length integer.
    1192           1 :                 var unshared uint32
    1193           1 :                 if a := *((*uint8)(ptr)); a < 128 {
    1194           1 :                         unshared = uint32(a)
    1195           1 :                         ptr = unsafe.Pointer(uintptr(ptr) + 1)
    1196           1 :                 } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
    1197           0 :                         unshared = uint32(b)<<7 | uint32(a)
    1198           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 2)
    1199           0 :                 } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
    1200           0 :                         unshared = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1201           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 3)
    1202           0 :                 } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
    1203           0 :                         unshared = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1204           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 4)
    1205           0 :                 } else {
    1206           0 :                         d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
    1207           0 :                         unshared = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1208           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 5)
    1209           0 :                 }
    1210             :                 // Decode the value length integer.
    1211           1 :                 var value uint32
    1212           1 :                 if a := *((*uint8)(ptr)); a < 128 {
    1213           1 :                         value = uint32(a)
    1214           1 :                         ptr = unsafe.Pointer(uintptr(ptr) + 1)
    1215           1 :                 } else if a, b := a&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 1))); b < 128 {
    1216           0 :                         value = uint32(b)<<7 | uint32(a)
    1217           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 2)
    1218           0 :                 } else if b, c := b&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 2))); c < 128 {
    1219           0 :                         value = uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1220           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 3)
    1221           0 :                 } else if c, d := c&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 3))); d < 128 {
    1222           0 :                         value = uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1223           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 4)
    1224           0 :                 } else {
    1225           0 :                         d, e := d&0x7f, *((*uint8)(unsafe.Pointer(uintptr(ptr) + 4)))
    1226           0 :                         value = uint32(e)<<28 | uint32(d)<<21 | uint32(c)<<14 | uint32(b)<<7 | uint32(a)
    1227           0 :                         ptr = unsafe.Pointer(uintptr(ptr) + 5)
    1228           0 :                 }
    1229             :                 // The starting position of the value.
    1230           1 :                 valuePtr := unsafe.Pointer(uintptr(ptr) + uintptr(unshared))
    1231           1 :                 i.nextOffset = int32(uintptr(valuePtr)-uintptr(i.ptr)) + int32(value)
    1232           1 :                 if invariants.Enabled && unshared < 8 {
    1233           0 :                         // This should not happen since only the key prefix is shared, so even
    1234           0 :                         // if the prefix length is the same as the user key length, the unshared
    1235           0 :                         // will include the trailer.
    1236           0 :                         panic(errors.AssertionFailedf("unshared %d is too small", unshared))
    1237             :                 }
    1238             :                 // The trailer is written in little endian, so the key kind is the first
    1239             :                 // byte in the trailer that is encoded in the slice [unshared-8:unshared].
    1240           1 :                 keyKind := InternalKeyKind((*[manual.MaxArrayLen]byte)(ptr)[unshared-8])
    1241           1 :                 keyKind = keyKind & base.InternalKeyKindSSTableInternalObsoleteMask
    1242           1 :                 prefixChanged := false
    1243           1 :                 if keyKind == InternalKeyKindSet {
    1244           1 :                         if invariants.Enabled && value == 0 {
    1245           0 :                                 panic(errors.AssertionFailedf("value is of length 0, but we expect a valuePrefix"))
    1246             :                         }
    1247           1 :                         valPrefix := *((*valuePrefix)(valuePtr))
    1248           1 :                         if setHasSamePrefix(valPrefix) {
    1249           1 :                                 // Fast-path. No need to assemble i.fullKey, or update i.key. We know
    1250           1 :                                 // that subsequent keys will not have a shared length that is greater
    1251           1 :                                 // than the prefix of the current key, which is also the prefix of
    1252           1 :                                 // i.key. Since we are continuing to iterate, we don't need to
    1253           1 :                                 // initialize i.ikey and i.lazyValue (these are initialized before
    1254           1 :                                 // returning).
    1255           1 :                                 nextFastCount++
    1256           1 :                                 if nextFastCount > nextFastThresholdBeforeRestarts {
    1257           1 :                                         if usedRestarts {
    1258           1 :                                                 // Exhausted iteration budget. This will never happen unless
    1259           1 :                                                 // someone is using a restart interval > 16. It is just to guard
    1260           1 :                                                 // against long restart intervals causing too much iteration.
    1261           1 :                                                 break
    1262             :                                         }
    1263             :                                         // Haven't used restarts yet, so find the first restart at or beyond
    1264             :                                         // the current offset.
    1265           1 :                                         targetOffset := i.offset
    1266           1 :                                         var index int32
    1267           1 :                                         {
    1268           1 :                                                 // NB: manually inlined sort.Sort is ~5% faster.
    1269           1 :                                                 //
    1270           1 :                                                 // f defined for a restart point is true iff the offset >=
    1271           1 :                                                 // targetOffset.
    1272           1 :                                                 // Define f(-1) == false and f(i.numRestarts) == true.
    1273           1 :                                                 // Invariant: f(index-1) == false, f(upper) == true.
    1274           1 :                                                 upper := i.numRestarts
    1275           1 :                                                 for index < upper {
    1276           1 :                                                         h := int32(uint(index+upper) >> 1) // avoid overflow when computing h
    1277           1 :                                                         // index ≤ h < upper
    1278           1 :                                                         offset := decodeRestart(i.data[i.restarts+4*h:])
    1279           1 :                                                         if offset < targetOffset {
    1280           1 :                                                                 index = h + 1 // preserves f(index-1) == false
    1281           1 :                                                         } else {
    1282           1 :                                                                 upper = h // preserves f(upper) == true
    1283           1 :                                                         }
    1284             :                                                 }
    1285             :                                                 // index == upper, f(index-1) == false, and f(upper) (= f(index)) == true
    1286             :                                                 // => answer is index.
    1287             :                                         }
    1288           1 :                                         usedRestarts = true
    1289           1 :                                         nextFastCount = 0
    1290           1 :                                         if index == i.numRestarts {
    1291           1 :                                                 // Already past the last real restart, so iterate a bit more until
    1292           1 :                                                 // we are done with the block.
    1293           1 :                                                 continue
    1294             :                                         }
    1295             :                                         // Have some real restarts after index. NB: index is the first
    1296             :                                         // restart at or beyond the current offset.
    1297           1 :                                         startingIndex := index
    1298           1 :                                         for index != i.numRestarts &&
    1299           1 :                                                 // The restart at index is 4 bytes written in little endian format
    1300           1 :                                                 // starting at i.restart+4*index. The 0th byte is the least
    1301           1 :                                                 // significant and the 3rd byte is the most significant. Since the
    1302           1 :                                                 // most significant bit of the 3rd byte is what we use for
    1303           1 :                                                 // encoding the set-has-same-prefix information, the indexing
    1304           1 :                                                 // below has +3.
    1305           1 :                                                 i.data[i.restarts+4*index+3]&restartMaskLittleEndianHighByteOnlySetHasSamePrefix != 0 {
    1306           1 :                                                 // We still have the same prefix, so move to the next restart.
    1307           1 :                                                 index++
    1308           1 :                                         }
    1309             :                                         // index is the first restart that did not have the same prefix.
    1310           1 :                                         if index != startingIndex {
    1311           1 :                                                 // Managed to skip past at least one restart. Resume iteration
    1312           1 :                                                 // from index-1. Since nextFastCount has been reset to 0, we
    1313           1 :                                                 // should be able to iterate to the next prefix.
    1314           1 :                                                 i.offset = decodeRestart(i.data[i.restarts+4*(index-1):])
    1315           1 :                                                 i.readEntry()
    1316           1 :                                         }
    1317             :                                         // Else, unable to skip past any restart. Resume iteration. Since
    1318             :                                         // nextFastCount has been reset to 0, we should be able to iterate
    1319             :                                         // to the next prefix.
    1320           1 :                                         continue
    1321             :                                 }
    1322           1 :                                 continue
    1323           1 :                         } else if prevKeyIsSet {
    1324           1 :                                 prefixChanged = true
    1325           1 :                         }
    1326           1 :                 } else {
    1327           1 :                         prevKeyIsSet = false
    1328           1 :                 }
    1329             :                 // Slow-path cases:
    1330             :                 // - (Likely) The prefix has changed.
    1331             :                 // - (Unlikely) The prefix has not changed.
    1332             :                 // We assemble the key etc. under the assumption that it is the likely
    1333             :                 // case.
    1334           1 :                 unsharedKey := getBytes(ptr, int(unshared))
    1335           1 :                 // TODO(sumeer): move this into the else block below. This is a bit tricky
    1336           1 :                 // since the current logic assumes we have always copied the latest key
    1337           1 :                 // into fullKey, which is why when we get to the next key we can (a)
    1338           1 :                 // access i.fullKey[:shared], (b) append only the unsharedKey to
    1339           1 :                 // i.fullKey. For (a), we can access i.key[:shared] since that memory is
    1340           1 :                 // valid (even if unshared). For (b), we will need to remember whether
    1341           1 :                 // i.key refers to i.fullKey or not, and can append the unsharedKey only
    1342           1 :                 // in the former case and for the latter case need to copy the shared part
    1343           1 :                 // too. This same comment applies to the other place where we can do this
    1344           1 :                 // optimization, in readEntry().
    1345           1 :                 i.fullKey = append(i.fullKey[:shared], unsharedKey...)
    1346           1 :                 i.val = getBytes(valuePtr, int(value))
    1347           1 :                 if shared == 0 {
    1348           1 :                         // Provide stability for the key across positioning calls if the key
    1349           1 :                         // doesn't share a prefix with the previous key. This removes requiring the
    1350           1 :                         // key to be copied if the caller knows the block has a restart interval of
    1351           1 :                         // 1. An important example of this is range-del blocks.
    1352           1 :                         i.key = unsharedKey
    1353           1 :                 } else {
    1354           1 :                         i.key = i.fullKey
    1355           1 :                 }
    1356             :                 // Manually inlined version of i.decodeInternalKey(i.key).
    1357           1 :                 hiddenPoint := false
    1358           1 :                 if n := len(i.key) - 8; n >= 0 {
    1359           1 :                         trailer := binary.LittleEndian.Uint64(i.key[n:])
    1360           1 :                         hiddenPoint = i.hideObsoletePoints &&
    1361           1 :                                 (trailer&trailerObsoleteBit != 0)
    1362           1 :                         i.ikey.Trailer = trailer & trailerObsoleteMask
    1363           1 :                         i.ikey.UserKey = i.key[:n:n]
    1364           1 :                         if i.globalSeqNum != 0 {
    1365           1 :                                 i.ikey.SetSeqNum(i.globalSeqNum)
    1366           1 :                         }
    1367           0 :                 } else {
    1368           0 :                         i.ikey.Trailer = uint64(InternalKeyKindInvalid)
    1369           0 :                         i.ikey.UserKey = nil
    1370           0 :                 }
    1371           1 :                 nextCmpCount++
    1372           1 :                 if invariants.Enabled && prefixChanged && i.cmp(i.ikey.UserKey, succKey) < 0 {
    1373           0 :                         panic(errors.AssertionFailedf("prefix should have changed but %x < %x",
    1374           0 :                                 i.ikey.UserKey, succKey))
    1375             :                 }
    1376           1 :                 if prefixChanged || i.cmp(i.ikey.UserKey, succKey) >= 0 {
    1377           1 :                         // Prefix has changed.
    1378           1 :                         if hiddenPoint {
    1379           1 :                                 return i.Next()
    1380           1 :                         }
    1381           1 :                         if invariants.Enabled && !i.lazyValueHandling.hasValuePrefix {
    1382           0 :                                 panic(errors.AssertionFailedf("nextPrefixV3 being run for non-v3 sstable"))
    1383             :                         }
    1384           1 :                         if base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
    1385           1 :                                 i.lazyValue = base.MakeInPlaceValue(i.val)
    1386           1 :                         } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
    1387           1 :                                 i.lazyValue = base.MakeInPlaceValue(i.val[1:])
    1388           1 :                         } else {
    1389           0 :                                 i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
    1390           0 :                         }
    1391           1 :                         return &i.ikey, i.lazyValue
    1392             :                 }
    1393             :                 // Else prefix has not changed.
    1394             : 
    1395           1 :                 if nextCmpCount >= nextCmpThresholdBeforeSeek {
    1396           0 :                         break
    1397             :                 }
    1398             :         }
    1399           1 :         return i.SeekGE(succKey, base.SeekGEFlagsNone)
    1400             : }
    1401             : 
    1402             : // Prev implements internalIterator.Prev, as documented in the pebble
    1403             : // package.
    1404           1 : func (i *blockIter) Prev() (*InternalKey, base.LazyValue) {
    1405           1 : start:
    1406           1 :         for n := len(i.cached) - 1; n >= 0; n-- {
    1407           1 :                 i.nextOffset = i.offset
    1408           1 :                 e := &i.cached[n]
    1409           1 :                 i.offset = e.offset
    1410           1 :                 i.val = getBytes(unsafe.Pointer(uintptr(i.ptr)+uintptr(e.valStart)), int(e.valSize))
    1411           1 :                 // Manually inlined version of i.decodeInternalKey(i.key).
    1412           1 :                 i.key = i.cachedBuf[e.keyStart:e.keyEnd]
    1413           1 :                 if n := len(i.key) - 8; n >= 0 {
    1414           1 :                         trailer := binary.LittleEndian.Uint64(i.key[n:])
    1415           1 :                         hiddenPoint := i.hideObsoletePoints &&
    1416           1 :                                 (trailer&trailerObsoleteBit != 0)
    1417           1 :                         if hiddenPoint {
    1418           1 :                                 continue
    1419             :                         }
    1420           1 :                         i.ikey.Trailer = trailer & trailerObsoleteMask
    1421           1 :                         i.ikey.UserKey = i.key[:n:n]
    1422           1 :                         if i.globalSeqNum != 0 {
    1423           1 :                                 i.ikey.SetSeqNum(i.globalSeqNum)
    1424           1 :                         }
    1425           0 :                 } else {
    1426           0 :                         i.ikey.Trailer = uint64(InternalKeyKindInvalid)
    1427           0 :                         i.ikey.UserKey = nil
    1428           0 :                 }
    1429           1 :                 i.cached = i.cached[:n]
    1430           1 :                 if !i.lazyValueHandling.hasValuePrefix ||
    1431           1 :                         base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
    1432           1 :                         i.lazyValue = base.MakeInPlaceValue(i.val)
    1433           1 :                 } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
    1434           1 :                         i.lazyValue = base.MakeInPlaceValue(i.val[1:])
    1435           1 :                 } else {
    1436           1 :                         i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
    1437           1 :                 }
    1438           1 :                 return &i.ikey, i.lazyValue
    1439             :         }
    1440             : 
    1441           1 :         i.clearCache()
    1442           1 :         if i.offset <= 0 {
    1443           1 :                 i.offset = -1
    1444           1 :                 i.nextOffset = 0
    1445           1 :                 return nil, base.LazyValue{}
    1446           1 :         }
    1447             : 
    1448           1 :         targetOffset := i.offset
    1449           1 :         var index int32
    1450           1 : 
    1451           1 :         {
    1452           1 :                 // NB: manually inlined sort.Sort is ~5% faster.
    1453           1 :                 //
    1454           1 :                 // Define f(-1) == false and f(n) == true.
    1455           1 :                 // Invariant: f(index-1) == false, f(upper) == true.
    1456           1 :                 upper := i.numRestarts
    1457           1 :                 for index < upper {
    1458           1 :                         h := int32(uint(index+upper) >> 1) // avoid overflow when computing h
    1459           1 :                         // index ≤ h < upper
    1460           1 :                         offset := decodeRestart(i.data[i.restarts+4*h:])
    1461           1 :                         if offset < targetOffset {
    1462           1 :                                 // Looking for the first restart that has offset >= targetOffset, so
    1463           1 :                                 // ignore h and earlier.
    1464           1 :                                 index = h + 1 // preserves f(i-1) == false
    1465           1 :                         } else {
    1466           1 :                                 upper = h // preserves f(j) == true
    1467           1 :                         }
    1468             :                 }
    1469             :                 // index == upper, f(index-1) == false, and f(upper) (= f(index)) == true
    1470             :                 // => answer is index.
    1471             :         }
    1472             : 
    1473             :         // index is first restart with offset >= targetOffset. Note that
    1474             :         // targetOffset may not be at a restart point since one can call Prev()
    1475             :         // after Next() (so the cache was not populated) and targetOffset refers to
    1476             :         // the current entry. index-1 must have an offset < targetOffset (it can't
    1477             :         // be equal to targetOffset since the binary search would have selected that
    1478             :         // as the index).
    1479           1 :         i.offset = 0
    1480           1 :         if index > 0 {
    1481           1 :                 i.offset = decodeRestart(i.data[i.restarts+4*(index-1):])
    1482           1 :         }
    1483             :         // TODO(sumeer): why is the else case not an error given targetOffset is a
    1484             :         // valid offset.
    1485             : 
    1486           1 :         i.readEntry()
    1487           1 : 
    1488           1 :         // We stop when i.nextOffset == targetOffset since the targetOffset is the
    1489           1 :         // entry we are stepping back from, and we don't need to cache the entry
    1490           1 :         // before it, since it is the candidate to return.
    1491           1 :         for i.nextOffset < targetOffset {
    1492           1 :                 i.cacheEntry()
    1493           1 :                 i.offset = i.nextOffset
    1494           1 :                 i.readEntry()
    1495           1 :         }
    1496             : 
    1497           1 :         hiddenPoint := i.decodeInternalKey(i.key)
    1498           1 :         if hiddenPoint {
    1499           1 :                 // Use the cache.
    1500           1 :                 goto start
    1501             :         }
    1502           1 :         if !i.lazyValueHandling.hasValuePrefix ||
    1503           1 :                 base.TrailerKind(i.ikey.Trailer) != InternalKeyKindSet {
    1504           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val)
    1505           1 :         } else if i.lazyValueHandling.vbr == nil || !isValueHandle(valuePrefix(i.val[0])) {
    1506           1 :                 i.lazyValue = base.MakeInPlaceValue(i.val[1:])
    1507           1 :         } else {
    1508           1 :                 i.lazyValue = i.lazyValueHandling.vbr.getLazyValueForPrefixAndValueHandle(i.val)
    1509           1 :         }
    1510           1 :         return &i.ikey, i.lazyValue
    1511             : }
    1512             : 
    1513             : // Key implements internalIterator.Key, as documented in the pebble package.
    1514           1 : func (i *blockIter) Key() *InternalKey {
    1515           1 :         return &i.ikey
    1516           1 : }
    1517             : 
    1518           1 : func (i *blockIter) value() base.LazyValue {
    1519           1 :         return i.lazyValue
    1520           1 : }
    1521             : 
    1522             : // Error implements internalIterator.Error, as documented in the pebble
    1523             : // package.
    1524           1 : func (i *blockIter) Error() error {
    1525           1 :         return nil // infallible
    1526           1 : }
    1527             : 
    1528             : // Close implements internalIterator.Close, as documented in the pebble
    1529             : // package.
    1530           1 : func (i *blockIter) Close() error {
    1531           1 :         i.handle.Release()
    1532           1 :         i.handle = bufferHandle{}
    1533           1 :         i.val = nil
    1534           1 :         i.lazyValue = base.LazyValue{}
    1535           1 :         i.lazyValueHandling.vbr = nil
    1536           1 :         return nil
    1537           1 : }
    1538             : 
    1539           0 : func (i *blockIter) SetBounds(lower, upper []byte) {
    1540           0 :         // This should never be called as bounds are handled by sstable.Iterator.
    1541           0 :         panic("pebble: SetBounds unimplemented")
    1542             : }
    1543             : 
    1544           0 : func (i *blockIter) SetContext(_ context.Context) {}
    1545             : 
    1546           1 : func (i *blockIter) valid() bool {
    1547           1 :         return i.offset >= 0 && i.offset < i.restarts
    1548           1 : }
    1549             : 
    1550             : // fragmentBlockIter wraps a blockIter, implementing the
    1551             : // keyspan.FragmentIterator interface. It's used for reading range deletion and
    1552             : // range key blocks.
    1553             : //
    1554             : // Range deletions and range keys are fragmented before they're persisted to the
    1555             : // block. Overlapping fragments have identical bounds.  The fragmentBlockIter
    1556             : // gathers all the fragments with identical bounds within a block and returns a
    1557             : // single keyspan.Span describing all the keys defined over the span.
    1558             : //
    1559             : // # Memory lifetime
    1560             : //
    1561             : // A Span returned by fragmentBlockIter is only guaranteed to be stable until
    1562             : // the next fragmentBlockIter iteration positioning method. A Span's Keys slice
    1563             : // may be reused, so the user must not assume it's stable.
    1564             : //
    1565             : // Blocks holding range deletions and range keys are configured to use a restart
    1566             : // interval of 1. This provides key stability. The caller may treat the various
    1567             : // byte slices (start, end, suffix, value) as stable for the lifetime of the
    1568             : // iterator.
    1569             : type fragmentBlockIter struct {
    1570             :         blockIter blockIter
    1571             :         keyBuf    [2]keyspan.Key
    1572             :         span      keyspan.Span
    1573             :         err       error
    1574             :         dir       int8
    1575             :         closeHook func(i keyspan.FragmentIterator) error
    1576             : 
    1577             :         // elideSameSeqnum, if true, returns only the first-occurring (in forward
    1578             :         // order) Key for each sequence number.
    1579             :         elideSameSeqnum bool
    1580             : }
    1581             : 
    1582           1 : func (i *fragmentBlockIter) resetForReuse() fragmentBlockIter {
    1583           1 :         return fragmentBlockIter{blockIter: i.blockIter.resetForReuse()}
    1584           1 : }
    1585             : 
    1586           1 : func (i *fragmentBlockIter) decodeSpanKeys(k *InternalKey, internalValue []byte) {
    1587           1 :         // TODO(jackson): The use of i.span.Keys to accumulate keys across multiple
    1588           1 :         // calls to Decode is too confusing and subtle. Refactor to make it
    1589           1 :         // explicit.
    1590           1 : 
    1591           1 :         // decode the contents of the fragment's value. This always includes at
    1592           1 :         // least the end key: RANGEDELs store the end key directly as the value,
    1593           1 :         // whereas the various range key kinds store are more complicated.  The
    1594           1 :         // details of the range key internal value format are documented within the
    1595           1 :         // internal/rangekey package.
    1596           1 :         switch k.Kind() {
    1597           1 :         case base.InternalKeyKindRangeDelete:
    1598           1 :                 i.span = rangedel.Decode(*k, internalValue, i.span.Keys)
    1599           1 :                 i.err = nil
    1600           1 :         case base.InternalKeyKindRangeKeySet, base.InternalKeyKindRangeKeyUnset, base.InternalKeyKindRangeKeyDelete:
    1601           1 :                 i.span, i.err = rangekey.Decode(*k, internalValue, i.span.Keys)
    1602           0 :         default:
    1603           0 :                 i.span = keyspan.Span{}
    1604           0 :                 i.err = base.CorruptionErrorf("pebble: corrupt keyspan fragment of kind %d", k.Kind())
    1605             :         }
    1606             : }
    1607             : 
    1608           1 : func (i *fragmentBlockIter) elideKeysOfSameSeqNum() {
    1609           1 :         if invariants.Enabled {
    1610           1 :                 if !i.elideSameSeqnum || len(i.span.Keys) == 0 {
    1611           0 :                         panic("elideKeysOfSameSeqNum called when it should not be")
    1612             :                 }
    1613             :         }
    1614           1 :         lastSeqNum := i.span.Keys[0].SeqNum()
    1615           1 :         k := 1
    1616           1 :         for j := 1; j < len(i.span.Keys); j++ {
    1617           1 :                 if lastSeqNum != i.span.Keys[j].SeqNum() {
    1618           1 :                         lastSeqNum = i.span.Keys[j].SeqNum()
    1619           1 :                         i.span.Keys[k] = i.span.Keys[j]
    1620           1 :                         k++
    1621           1 :                 }
    1622             :         }
    1623           1 :         i.span.Keys = i.span.Keys[:k]
    1624             : }
    1625             : 
    1626             : // gatherForward gathers internal keys with identical bounds. Keys defined over
    1627             : // spans of the keyspace are fragmented such that any overlapping key spans have
    1628             : // identical bounds. When these spans are persisted to a range deletion or range
    1629             : // key block, they may be persisted as multiple internal keys in order to encode
    1630             : // multiple sequence numbers or key kinds.
    1631             : //
    1632             : // gatherForward iterates forward, re-combining the fragmented internal keys to
    1633             : // reconstruct a keyspan.Span that holds all the keys defined over the span.
    1634           1 : func (i *fragmentBlockIter) gatherForward(k *InternalKey, lazyValue base.LazyValue) *keyspan.Span {
    1635           1 :         i.span = keyspan.Span{}
    1636           1 :         if k == nil || !i.blockIter.valid() {
    1637           1 :                 return nil
    1638           1 :         }
    1639           1 :         i.err = nil
    1640           1 :         // Use the i.keyBuf array to back the Keys slice to prevent an allocation
    1641           1 :         // when a span contains few keys.
    1642           1 :         i.span.Keys = i.keyBuf[:0]
    1643           1 : 
    1644           1 :         // Decode the span's end key and individual keys from the value.
    1645           1 :         internalValue := lazyValue.InPlaceValue()
    1646           1 :         i.decodeSpanKeys(k, internalValue)
    1647           1 :         if i.err != nil {
    1648           0 :                 return nil
    1649           0 :         }
    1650           1 :         prevEnd := i.span.End
    1651           1 : 
    1652           1 :         // There might exist additional internal keys with identical bounds encoded
    1653           1 :         // within the block. Iterate forward, accumulating all the keys with
    1654           1 :         // identical bounds to s.
    1655           1 :         k, lazyValue = i.blockIter.Next()
    1656           1 :         internalValue = lazyValue.InPlaceValue()
    1657           1 :         for k != nil && i.blockIter.cmp(k.UserKey, i.span.Start) == 0 {
    1658           1 :                 i.decodeSpanKeys(k, internalValue)
    1659           1 :                 if i.err != nil {
    1660           0 :                         return nil
    1661           0 :                 }
    1662             : 
    1663             :                 // Since k indicates an equal start key, the encoded end key must
    1664             :                 // exactly equal the original end key from the first internal key.
    1665             :                 // Overlapping fragments are required to have exactly equal start and
    1666             :                 // end bounds.
    1667           1 :                 if i.blockIter.cmp(prevEnd, i.span.End) != 0 {
    1668           0 :                         i.err = base.CorruptionErrorf("pebble: corrupt keyspan fragmentation")
    1669           0 :                         i.span = keyspan.Span{}
    1670           0 :                         return nil
    1671           0 :                 }
    1672           1 :                 k, lazyValue = i.blockIter.Next()
    1673           1 :                 internalValue = lazyValue.InPlaceValue()
    1674             :         }
    1675           1 :         if i.elideSameSeqnum && len(i.span.Keys) > 0 {
    1676           1 :                 i.elideKeysOfSameSeqNum()
    1677           1 :         }
    1678             :         // i.blockIter is positioned over the first internal key for the next span.
    1679           1 :         return &i.span
    1680             : }
    1681             : 
    1682             : // gatherBackward gathers internal keys with identical bounds. Keys defined over
    1683             : // spans of the keyspace are fragmented such that any overlapping key spans have
    1684             : // identical bounds. When these spans are persisted to a range deletion or range
    1685             : // key block, they may be persisted as multiple internal keys in order to encode
    1686             : // multiple sequence numbers or key kinds.
    1687             : //
    1688             : // gatherBackward iterates backwards, re-combining the fragmented internal keys
    1689             : // to reconstruct a keyspan.Span that holds all the keys defined over the span.
    1690           1 : func (i *fragmentBlockIter) gatherBackward(k *InternalKey, lazyValue base.LazyValue) *keyspan.Span {
    1691           1 :         i.span = keyspan.Span{}
    1692           1 :         if k == nil || !i.blockIter.valid() {
    1693           1 :                 return nil
    1694           1 :         }
    1695           1 :         i.err = nil
    1696           1 :         // Use the i.keyBuf array to back the Keys slice to prevent an allocation
    1697           1 :         // when a span contains few keys.
    1698           1 :         i.span.Keys = i.keyBuf[:0]
    1699           1 : 
    1700           1 :         // Decode the span's end key and individual keys from the value.
    1701           1 :         internalValue := lazyValue.InPlaceValue()
    1702           1 :         i.decodeSpanKeys(k, internalValue)
    1703           1 :         if i.err != nil {
    1704           0 :                 return nil
    1705           0 :         }
    1706           1 :         prevEnd := i.span.End
    1707           1 : 
    1708           1 :         // There might exist additional internal keys with identical bounds encoded
    1709           1 :         // within the block. Iterate backward, accumulating all the keys with
    1710           1 :         // identical bounds to s.
    1711           1 :         k, lazyValue = i.blockIter.Prev()
    1712           1 :         internalValue = lazyValue.InPlaceValue()
    1713           1 :         for k != nil && i.blockIter.cmp(k.UserKey, i.span.Start) == 0 {
    1714           1 :                 i.decodeSpanKeys(k, internalValue)
    1715           1 :                 if i.err != nil {
    1716           0 :                         return nil
    1717           0 :                 }
    1718             : 
    1719             :                 // Since k indicates an equal start key, the encoded end key must
    1720             :                 // exactly equal the original end key from the first internal key.
    1721             :                 // Overlapping fragments are required to have exactly equal start and
    1722             :                 // end bounds.
    1723           1 :                 if i.blockIter.cmp(prevEnd, i.span.End) != 0 {
    1724           0 :                         i.err = base.CorruptionErrorf("pebble: corrupt keyspan fragmentation")
    1725           0 :                         i.span = keyspan.Span{}
    1726           0 :                         return nil
    1727           0 :                 }
    1728           1 :                 k, lazyValue = i.blockIter.Prev()
    1729           1 :                 internalValue = lazyValue.InPlaceValue()
    1730             :         }
    1731             :         // i.blockIter is positioned over the last internal key for the previous
    1732             :         // span.
    1733             : 
    1734             :         // Backwards iteration encounters internal keys in the wrong order.
    1735           1 :         keyspan.SortKeysByTrailer(&i.span.Keys)
    1736           1 : 
    1737           1 :         if i.elideSameSeqnum && len(i.span.Keys) > 0 {
    1738           1 :                 i.elideKeysOfSameSeqNum()
    1739           1 :         }
    1740           1 :         return &i.span
    1741             : }
    1742             : 
    1743             : // Error implements (keyspan.FragmentIterator).Error.
    1744           1 : func (i *fragmentBlockIter) Error() error {
    1745           1 :         return i.err
    1746           1 : }
    1747             : 
    1748             : // Close implements (keyspan.FragmentIterator).Close.
    1749           1 : func (i *fragmentBlockIter) Close() error {
    1750           1 :         var err error
    1751           1 :         if i.closeHook != nil {
    1752           0 :                 err = i.closeHook(i)
    1753           0 :         }
    1754           1 :         err = firstError(err, i.blockIter.Close())
    1755           1 :         return err
    1756             : }
    1757             : 
    1758             : // First implements (keyspan.FragmentIterator).First
    1759           1 : func (i *fragmentBlockIter) First() *keyspan.Span {
    1760           1 :         i.dir = +1
    1761           1 :         return i.gatherForward(i.blockIter.First())
    1762           1 : }
    1763             : 
    1764             : // Last implements (keyspan.FragmentIterator).Last.
    1765           1 : func (i *fragmentBlockIter) Last() *keyspan.Span {
    1766           1 :         i.dir = -1
    1767           1 :         return i.gatherBackward(i.blockIter.Last())
    1768           1 : }
    1769             : 
    1770             : // Next implements (keyspan.FragmentIterator).Next.
    1771           1 : func (i *fragmentBlockIter) Next() *keyspan.Span {
    1772           1 :         switch {
    1773           1 :         case i.dir == -1 && !i.span.Valid():
    1774           1 :                 // Switching directions.
    1775           1 :                 //
    1776           1 :                 // i.blockIter is exhausted, before the first key. Move onto the first.
    1777           1 :                 i.blockIter.First()
    1778           1 :                 i.dir = +1
    1779           1 :         case i.dir == -1 && i.span.Valid():
    1780           1 :                 // Switching directions.
    1781           1 :                 //
    1782           1 :                 // i.blockIter is currently positioned over the last internal key for
    1783           1 :                 // the previous span. Next it once to move to the first internal key
    1784           1 :                 // that makes up the current span, and gatherForwaad to land on the
    1785           1 :                 // first internal key making up the next span.
    1786           1 :                 //
    1787           1 :                 // In the diagram below, if the last span returned to the user during
    1788           1 :                 // reverse iteration was [b,c), i.blockIter is currently positioned at
    1789           1 :                 // [a,b). The block iter must be positioned over [d,e) to gather the
    1790           1 :                 // next span's fragments.
    1791           1 :                 //
    1792           1 :                 //    ... [a,b) [b,c) [b,c) [b,c) [d,e) ...
    1793           1 :                 //          ^                       ^
    1794           1 :                 //     i.blockIter                 want
    1795           1 :                 if x := i.gatherForward(i.blockIter.Next()); invariants.Enabled && !x.Valid() {
    1796           0 :                         panic("pebble: invariant violation: next entry unexpectedly invalid")
    1797             :                 }
    1798           1 :                 i.dir = +1
    1799             :         }
    1800             :         // We know that this blockIter has in-place values.
    1801           1 :         return i.gatherForward(&i.blockIter.ikey, base.MakeInPlaceValue(i.blockIter.val))
    1802             : }
    1803             : 
    1804             : // Prev implements (keyspan.FragmentIterator).Prev.
    1805           1 : func (i *fragmentBlockIter) Prev() *keyspan.Span {
    1806           1 :         switch {
    1807           1 :         case i.dir == +1 && !i.span.Valid():
    1808           1 :                 // Switching directions.
    1809           1 :                 //
    1810           1 :                 // i.blockIter is exhausted, after the last key. Move onto the last.
    1811           1 :                 i.blockIter.Last()
    1812           1 :                 i.dir = -1
    1813           1 :         case i.dir == +1 && i.span.Valid():
    1814           1 :                 // Switching directions.
    1815           1 :                 //
    1816           1 :                 // i.blockIter is currently positioned over the first internal key for
    1817           1 :                 // the next span. Prev it once to move to the last internal key that
    1818           1 :                 // makes up the current span, and gatherBackward to land on the last
    1819           1 :                 // internal key making up the previous span.
    1820           1 :                 //
    1821           1 :                 // In the diagram below, if the last span returned to the user during
    1822           1 :                 // forward iteration was [b,c), i.blockIter is currently positioned at
    1823           1 :                 // [d,e). The block iter must be positioned over [a,b) to gather the
    1824           1 :                 // previous span's fragments.
    1825           1 :                 //
    1826           1 :                 //    ... [a,b) [b,c) [b,c) [b,c) [d,e) ...
    1827           1 :                 //          ^                       ^
    1828           1 :                 //        want                  i.blockIter
    1829           1 :                 if x := i.gatherBackward(i.blockIter.Prev()); invariants.Enabled && !x.Valid() {
    1830           0 :                         panic("pebble: invariant violation: previous entry unexpectedly invalid")
    1831             :                 }
    1832           1 :                 i.dir = -1
    1833             :         }
    1834             :         // We know that this blockIter has in-place values.
    1835           1 :         return i.gatherBackward(&i.blockIter.ikey, base.MakeInPlaceValue(i.blockIter.val))
    1836             : }
    1837             : 
    1838             : // SeekGE implements (keyspan.FragmentIterator).SeekGE.
    1839           1 : func (i *fragmentBlockIter) SeekGE(k []byte) *keyspan.Span {
    1840           1 :         if s := i.SeekLT(k); s != nil && i.blockIter.cmp(k, s.End) < 0 {
    1841           1 :                 return s
    1842           1 :         }
    1843             :         // TODO(jackson): If the above i.SeekLT(k) discovers a span but the span
    1844             :         // doesn't meet the k < s.End comparison, then there's no need for the
    1845             :         // SeekLT to gatherBackward.
    1846           1 :         return i.Next()
    1847             : }
    1848             : 
    1849             : // SeekLT implements (keyspan.FragmentIterator).SeekLT.
    1850           1 : func (i *fragmentBlockIter) SeekLT(k []byte) *keyspan.Span {
    1851           1 :         i.dir = -1
    1852           1 :         return i.gatherBackward(i.blockIter.SeekLT(k, base.SeekLTFlagsNone))
    1853           1 : }
    1854             : 
    1855             : // String implements fmt.Stringer.
    1856           0 : func (i *fragmentBlockIter) String() string {
    1857           0 :         return "fragment-block-iter"
    1858           0 : }
    1859             : 
    1860             : // SetCloseHook implements sstable.FragmentIterator.
    1861           0 : func (i *fragmentBlockIter) SetCloseHook(fn func(i keyspan.FragmentIterator) error) {
    1862           0 :         i.closeHook = fn
    1863           0 : }

Generated by: LCOV version 1.14