github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/cache/lfucache/array_table_iter.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 lfucache
    16  
    17  import (
    18  	"bytes"
    19  	"sort"
    20  	"sync"
    21  
    22  	"github.com/zuoyebang/bitalosdb/internal/cache/lfucache/internal/base"
    23  )
    24  
    25  var _ base.InternalIterator = (*arrayTableFlushIterator)(nil)
    26  var _ base.InternalIterator = (*arrayTableIterator)(nil)
    27  
    28  type arrayTableIterator struct {
    29  	at        *arrayTable
    30  	indexPos  int
    31  	iterKey   *internalKey
    32  	iterValue []byte
    33  }
    34  
    35  var iterPool = sync.Pool{
    36  	New: func() interface{} {
    37  		return &arrayTableIterator{}
    38  	},
    39  }
    40  
    41  func (ai *arrayTableIterator) findItem() (*internalKey, []byte) {
    42  	key, value := ai.at.getKV(ai.indexPos)
    43  	if key == nil {
    44  		return nil, nil
    45  	}
    46  
    47  	*(ai.iterKey) = base.MakeMinKey(key)
    48  	ai.iterValue = value
    49  	return ai.iterKey, ai.iterValue
    50  }
    51  
    52  func (ai *arrayTableIterator) First() (*internalKey, []byte) {
    53  	ai.indexPos = 0
    54  	return ai.findItem()
    55  }
    56  
    57  func (ai *arrayTableIterator) Next() (*internalKey, []byte) {
    58  	ai.indexPos++
    59  	return ai.findItem()
    60  }
    61  
    62  func (ai *arrayTableIterator) SeekGE(key []byte) (*internalKey, []byte) {
    63  	ai.indexPos = sort.Search(ai.at.num, func(i int) bool {
    64  		return bytes.Compare(ai.at.getKey(i), key) != -1
    65  	})
    66  
    67  	return ai.findItem()
    68  }
    69  
    70  func (ai *arrayTableIterator) Prev() (*internalKey, []byte) {
    71  	panic("arrayTableIterator: Prev unimplemented")
    72  }
    73  
    74  func (ai *arrayTableIterator) Last() (*internalKey, []byte) {
    75  	panic("arrayTableIterator: Last unimplemented")
    76  }
    77  
    78  func (ai *arrayTableIterator) SeekLT(seek []byte) (*internalKey, []byte) {
    79  	panic("arrayTableIterator: SeekLT unimplemented")
    80  }
    81  
    82  func (ai *arrayTableIterator) SeekPrefixGE(
    83  	prefix, key []byte, trySeekUsingNext bool,
    84  ) (ikey *internalKey, value []byte) {
    85  	panic("arrayTableIterator: SeekPrefixGE unimplemented")
    86  }
    87  
    88  func (ai *arrayTableIterator) SetBounds(lower, upper []byte) {
    89  }
    90  
    91  func (ai *arrayTableIterator) Error() error {
    92  	return nil
    93  }
    94  
    95  func (ai *arrayTableIterator) Close() error {
    96  	iterPool.Put(ai)
    97  	return nil
    98  }
    99  
   100  func (ai *arrayTableIterator) String() string {
   101  	return "arrayTableIterator"
   102  }
   103  
   104  type arrayTableFlushIterator struct {
   105  	arrayTableIterator
   106  	bytesIterated *uint64
   107  }
   108  
   109  func (ai *arrayTableFlushIterator) First() (*internalKey, []byte) {
   110  	ai.indexPos = 0
   111  	return ai.findItem()
   112  }
   113  
   114  func (ai *arrayTableFlushIterator) Next() (*internalKey, []byte) {
   115  	ai.indexPos++
   116  	return ai.findItem()
   117  }
   118  
   119  func (ai *arrayTableFlushIterator) SeekGE(seek []byte) (*internalKey, []byte) {
   120  	panic("arrayTableFlushIterator: SeekGE unimplemented")
   121  }
   122  
   123  func (ai *arrayTableFlushIterator) Prev() (*internalKey, []byte) {
   124  	panic("arrayTableFlushIterator: Prev unimplemented")
   125  }
   126  
   127  func (ai *arrayTableFlushIterator) SeekLT(seek []byte) (*internalKey, []byte) {
   128  	panic("arrayTableFlushIterator: SeekLT unimplemented")
   129  }
   130  
   131  func (ai *arrayTableFlushIterator) SeekPrefixGE(
   132  	prefix, key []byte, trySeekUsingNext bool,
   133  ) (ikey *internalKey, value []byte) {
   134  	panic("arrayTableFlushIterator: SeekPrefixGE unimplemented")
   135  }
   136  
   137  func (ai *arrayTableFlushIterator) String() string {
   138  	return "arrayTableFlushIterator"
   139  }