github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/internal_test.go (about)

     1  // Copyright 2019 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package pebble
     6  
     7  import "github.com/cockroachdb/pebble/internal/base"
     8  
     9  // internalIterAdapter adapts the new internalIterator interface which returns
    10  // the key and value from positioning methods (Seek*, First, Last, Next, Prev)
    11  // to the old interface which returned a boolean corresponding to Valid. Only
    12  // used by test code.
    13  type internalIterAdapter struct {
    14  	internalIterator
    15  	key *InternalKey
    16  	val []byte
    17  	err error
    18  }
    19  
    20  func newInternalIterAdapter(iter internalIterator) *internalIterAdapter {
    21  	return &internalIterAdapter{
    22  		internalIterator: iter,
    23  	}
    24  }
    25  
    26  func (i *internalIterAdapter) update(key *InternalKey, val LazyValue) bool {
    27  	i.key = key
    28  	if v, _, err := val.Value(nil); err != nil {
    29  		i.key = nil
    30  		i.val = nil
    31  		i.err = err
    32  	} else {
    33  		i.val = v
    34  	}
    35  	return i.key != nil
    36  }
    37  
    38  func (i *internalIterAdapter) String() string {
    39  	return "internal-iter-adapter"
    40  }
    41  
    42  func (i *internalIterAdapter) SeekGE(key []byte, flags base.SeekGEFlags) bool {
    43  	return i.update(i.internalIterator.SeekGE(key, flags))
    44  }
    45  
    46  func (i *internalIterAdapter) SeekPrefixGE(prefix, key []byte, flags base.SeekGEFlags) bool {
    47  	return i.update(i.internalIterator.SeekPrefixGE(prefix, key, flags))
    48  }
    49  
    50  func (i *internalIterAdapter) SeekLT(key []byte, flags base.SeekLTFlags) bool {
    51  	return i.update(i.internalIterator.SeekLT(key, flags))
    52  }
    53  
    54  func (i *internalIterAdapter) First() bool {
    55  	return i.update(i.internalIterator.First())
    56  }
    57  
    58  func (i *internalIterAdapter) Last() bool {
    59  	return i.update(i.internalIterator.Last())
    60  }
    61  
    62  func (i *internalIterAdapter) Next() bool {
    63  	return i.update(i.internalIterator.Next())
    64  }
    65  
    66  func (i *internalIterAdapter) Prev() bool {
    67  	return i.update(i.internalIterator.Prev())
    68  }
    69  
    70  func (i *internalIterAdapter) Key() *InternalKey {
    71  	return i.key
    72  }
    73  
    74  func (i *internalIterAdapter) Value() []byte {
    75  	return i.val
    76  }
    77  
    78  func (i *internalIterAdapter) Valid() bool {
    79  	return i.key != nil
    80  }
    81  
    82  func (i *internalIterAdapter) Error() error {
    83  	err := i.internalIterator.Error()
    84  	if err != nil {
    85  		return err
    86  	}
    87  	return i.err
    88  }