github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitable/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 bitable
    16  
    17  import (
    18  	"encoding/binary"
    19  
    20  	"github.com/zuoyebang/bitalosdb/internal/base"
    21  	bt "github.com/zuoyebang/bitalostable"
    22  )
    23  
    24  type BitableIterator struct {
    25  	btable    *Bitable
    26  	iter      *bt.Iterator
    27  	iterKey   []byte
    28  	iterValue []byte
    29  	ikey      *base.InternalKey
    30  	value     []byte
    31  	putPools  []func()
    32  }
    33  
    34  func (i *BitableIterator) saveKV() {
    35  	i.iterKey = i.iter.Key()
    36  	i.iterValue = i.iter.Value()
    37  
    38  	iv := base.DecodeInternalValue(i.iterValue)
    39  	if iv.Kind() == base.InternalKeyKindSetBithash {
    40  		if !base.CheckValueValidByKeySetBithash(iv.UserValue) {
    41  			i.ikey = nil
    42  			i.value = nil
    43  			return
    44  		}
    45  
    46  		fn := binary.LittleEndian.Uint32(iv.UserValue)
    47  		value, putPool, err := i.btable.bithashGet(i.iterKey, fn)
    48  		if err != nil {
    49  			i.ikey = nil
    50  			i.value = nil
    51  			return
    52  		}
    53  
    54  		i.value = value
    55  		iv.SetKind(base.InternalKeyKindSet)
    56  		if putPool != nil {
    57  			i.putPools = append(i.putPools, putPool)
    58  		}
    59  	} else {
    60  		i.value = iv.UserValue
    61  	}
    62  
    63  	if i.ikey == nil {
    64  		i.ikey = new(base.InternalKey)
    65  	}
    66  
    67  	*(i.ikey) = base.MakeInternalKey2(i.iterKey, iv.Header)
    68  }
    69  
    70  func (i *BitableIterator) First() (*base.InternalKey, []byte) {
    71  	if !i.iter.First() {
    72  		return nil, nil
    73  	}
    74  
    75  	i.saveKV()
    76  	return i.ikey, i.value
    77  }
    78  
    79  func (i *BitableIterator) Last() (*base.InternalKey, []byte) {
    80  	if !i.iter.Last() {
    81  		return nil, nil
    82  	}
    83  
    84  	i.saveKV()
    85  	return i.ikey, i.value
    86  }
    87  
    88  func (i *BitableIterator) Next() (*base.InternalKey, []byte) {
    89  	if !i.iter.Next() {
    90  		return nil, nil
    91  	}
    92  
    93  	i.saveKV()
    94  	return i.ikey, i.value
    95  }
    96  
    97  func (i *BitableIterator) Prev() (*base.InternalKey, []byte) {
    98  	if !i.iter.Prev() {
    99  		return nil, nil
   100  	}
   101  
   102  	i.saveKV()
   103  	return i.ikey, i.value
   104  }
   105  
   106  func (i *BitableIterator) SeekGE(seek []byte) (*base.InternalKey, []byte) {
   107  	if !i.iter.SeekGE(seek) {
   108  		return nil, nil
   109  	}
   110  
   111  	i.saveKV()
   112  	return i.ikey, i.value
   113  }
   114  
   115  func (i *BitableIterator) SeekLT(seek []byte) (*base.InternalKey, []byte) {
   116  	if !i.iter.SeekLT(seek) {
   117  		return nil, nil
   118  	}
   119  
   120  	i.saveKV()
   121  	return i.ikey, i.value
   122  }
   123  
   124  func (i *BitableIterator) SeekPrefixGE(prefix, key []byte, trySeekUsingNext bool) (*base.InternalKey, []byte) {
   125  	return i.SeekGE(key)
   126  }
   127  
   128  func (i *BitableIterator) Error() error {
   129  	return i.iter.Error()
   130  }
   131  
   132  func (i *BitableIterator) Close() error {
   133  	if len(i.putPools) > 0 {
   134  		for _, f := range i.putPools {
   135  			f()
   136  		}
   137  	}
   138  	return i.iter.Close()
   139  }
   140  
   141  func (i *BitableIterator) Valid() bool {
   142  	return i.iter.Valid()
   143  }
   144  
   145  func (i *BitableIterator) SetBounds(lower, upper []byte) {
   146  }
   147  
   148  func (i *BitableIterator) String() string {
   149  	return "BitableIterator"
   150  }