github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitree/bdb/iterator.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bdb
    16  
    17  import (
    18  	"github.com/zuoyebang/bitalosdb/internal/base"
    19  )
    20  
    21  type BdbIterator struct {
    22  	iter      *Cursor
    23  	iterKey   []byte
    24  	iterValue []byte
    25  	ikey      *base.InternalKey
    26  	value     []byte
    27  	rTx       *ReadTx
    28  }
    29  
    30  func (i *BdbIterator) saveKV() {
    31  	if i.ikey == nil {
    32  		i.ikey = new(base.InternalKey)
    33  	}
    34  	*(i.ikey) = base.MakeInternalSetKey(i.iterKey)
    35  	i.value = i.iterValue
    36  }
    37  
    38  func (i *BdbIterator) First() (*base.InternalKey, []byte) {
    39  	i.iterKey, i.iterValue = i.iter.First()
    40  	if i.iterKey == nil {
    41  		return nil, nil
    42  	}
    43  
    44  	i.saveKV()
    45  	return i.ikey, i.value
    46  }
    47  
    48  func (i *BdbIterator) Last() (*base.InternalKey, []byte) {
    49  	i.iterKey, i.iterValue = i.iter.Last()
    50  	if i.iterKey == nil {
    51  		return nil, nil
    52  	}
    53  
    54  	i.saveKV()
    55  	return i.ikey, i.value
    56  }
    57  
    58  func (i *BdbIterator) Next() (*base.InternalKey, []byte) {
    59  	i.iterKey, i.iterValue = i.iter.Next()
    60  	if i.iterKey == nil {
    61  		return nil, nil
    62  	}
    63  
    64  	i.saveKV()
    65  	return i.ikey, i.value
    66  }
    67  
    68  func (i *BdbIterator) Prev() (*base.InternalKey, []byte) {
    69  	i.iterKey, i.iterValue = i.iter.Prev()
    70  	if i.iterKey == nil {
    71  		return nil, nil
    72  	}
    73  
    74  	i.saveKV()
    75  	return i.ikey, i.value
    76  }
    77  
    78  func (i *BdbIterator) SeekGE(key []byte) (*base.InternalKey, []byte) {
    79  	i.iterKey, i.iterValue = i.iter.Seek(key)
    80  	if i.iterKey == nil {
    81  		return nil, nil
    82  	}
    83  
    84  	i.saveKV()
    85  	return i.ikey, i.value
    86  }
    87  
    88  func (i *BdbIterator) SeekLT(key []byte) (*base.InternalKey, []byte) {
    89  	return nil, nil
    90  }
    91  
    92  func (i *BdbIterator) SeekPrefixGE(
    93  	prefix, key []byte, trySeekUsingNext bool,
    94  ) (ikey *base.InternalKey, value []byte) {
    95  	return nil, nil
    96  }
    97  
    98  func (i *BdbIterator) SetBounds(lower, upper []byte) {
    99  }
   100  
   101  func (i *BdbIterator) Error() error {
   102  	return nil
   103  }
   104  
   105  func (i *BdbIterator) Close() error {
   106  	return i.rTx.Unref(true)
   107  }
   108  
   109  func (i *BdbIterator) Ikey() *base.InternalKey {
   110  	return i.ikey
   111  }
   112  
   113  func (i *BdbIterator) Value() []byte {
   114  	return i.value
   115  }
   116  
   117  func (i *BdbIterator) String() string {
   118  	return "BdbIterator"
   119  }