github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/arenaskl/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 arenaskl
    16  
    17  import (
    18  	"encoding/binary"
    19  	"sync"
    20  
    21  	"github.com/zuoyebang/bitalosdb/internal/base"
    22  )
    23  
    24  type splice struct {
    25  	prev *node
    26  	next *node
    27  }
    28  
    29  func (s *splice) init(prev, next *node) {
    30  	s.prev = prev
    31  	s.next = next
    32  }
    33  
    34  type Iterator struct {
    35  	list  *Skiplist
    36  	nd    *node
    37  	key   base.InternalKey
    38  	lower []byte
    39  	upper []byte
    40  }
    41  
    42  var _ base.InternalIterator = (*Iterator)(nil)
    43  
    44  var iterPool = sync.Pool{
    45  	New: func() interface{} {
    46  		return &Iterator{}
    47  	},
    48  }
    49  
    50  func (it *Iterator) Close() error {
    51  	it.list = nil
    52  	it.nd = nil
    53  	it.lower = nil
    54  	it.upper = nil
    55  	iterPool.Put(it)
    56  	return nil
    57  }
    58  
    59  func (it *Iterator) String() string {
    60  	return "memtable"
    61  }
    62  
    63  func (it *Iterator) Error() error {
    64  	return nil
    65  }
    66  
    67  func (it *Iterator) SeekGE(key []byte) (*base.InternalKey, []byte) {
    68  	_, it.nd, _ = it.list.seekForBaseSplice(key)
    69  	if it.nd == it.list.tail {
    70  		return nil, nil
    71  	}
    72  	it.decodeKey()
    73  	if it.upper != nil && it.list.cmp(it.upper, it.key.UserKey) <= 0 {
    74  		it.nd = it.list.tail
    75  		return nil, nil
    76  	}
    77  	return &it.key, it.value()
    78  }
    79  
    80  func (it *Iterator) SeekPrefixGE(
    81  	prefix, key []byte, trySeekUsingNext bool,
    82  ) (*base.InternalKey, []byte) {
    83  	if trySeekUsingNext {
    84  		if it.nd == it.list.tail {
    85  			return nil, nil
    86  		}
    87  		less := it.list.cmp(it.key.UserKey, key) < 0
    88  		const numNexts = 5
    89  		for i := 0; less && i < numNexts; i++ {
    90  			k, _ := it.Next()
    91  			if k == nil {
    92  				return nil, nil
    93  			}
    94  			less = it.list.cmp(it.key.UserKey, key) < 0
    95  		}
    96  		if !less {
    97  			return &it.key, it.value()
    98  		}
    99  	}
   100  	return it.SeekGE(key)
   101  }
   102  
   103  func (it *Iterator) SeekLT(key []byte) (*base.InternalKey, []byte) {
   104  	it.nd, _, _ = it.list.seekForBaseSplice(key)
   105  	if it.nd == it.list.head {
   106  		return nil, nil
   107  	}
   108  	it.decodeKey()
   109  	if it.lower != nil && it.list.cmp(it.lower, it.key.UserKey) > 0 {
   110  		it.nd = it.list.head
   111  		return nil, nil
   112  	}
   113  	return &it.key, it.value()
   114  }
   115  
   116  func (it *Iterator) First() (*base.InternalKey, []byte) {
   117  	it.nd = it.list.getNext(it.list.head, 0)
   118  	if it.nd == it.list.tail {
   119  		return nil, nil
   120  	}
   121  	it.decodeKey()
   122  	if it.upper != nil && it.list.cmp(it.upper, it.key.UserKey) <= 0 {
   123  		it.nd = it.list.tail
   124  		return nil, nil
   125  	}
   126  	return &it.key, it.value()
   127  }
   128  
   129  func (it *Iterator) Last() (*base.InternalKey, []byte) {
   130  	it.nd = it.list.getPrev(it.list.tail, 0)
   131  	if it.nd == it.list.head {
   132  		return nil, nil
   133  	}
   134  	it.decodeKey()
   135  	if it.lower != nil && it.list.cmp(it.lower, it.key.UserKey) > 0 {
   136  		it.nd = it.list.head
   137  		return nil, nil
   138  	}
   139  	return &it.key, it.value()
   140  }
   141  
   142  func (it *Iterator) Next() (*base.InternalKey, []byte) {
   143  	it.nd = it.list.getSkipNext(it.nd)
   144  	if it.nd == it.list.tail {
   145  		return nil, nil
   146  	}
   147  	it.decodeKey()
   148  	if it.upper != nil && it.list.cmp(it.upper, it.key.UserKey) <= 0 {
   149  		it.nd = it.list.tail
   150  		return nil, nil
   151  	}
   152  	return &it.key, it.value()
   153  }
   154  
   155  func (it *Iterator) Prev() (*base.InternalKey, []byte) {
   156  	it.nd = it.list.getSkipPrev(it.nd)
   157  	if it.nd == it.list.head {
   158  		return nil, nil
   159  	}
   160  	it.decodeKey()
   161  	if it.lower != nil && it.list.cmp(it.lower, it.key.UserKey) > 0 {
   162  		it.nd = it.list.head
   163  		return nil, nil
   164  	}
   165  	return &it.key, it.value()
   166  }
   167  
   168  func (it *Iterator) value() []byte {
   169  	return it.nd.getValue(it.list.arena)
   170  }
   171  
   172  func (it *Iterator) Head() bool {
   173  	return it.nd == it.list.head
   174  }
   175  
   176  func (it *Iterator) Tail() bool {
   177  	return it.nd == it.list.tail
   178  }
   179  
   180  func (it *Iterator) SetBounds(lower, upper []byte) {
   181  	it.lower = lower
   182  	it.upper = upper
   183  }
   184  
   185  func (it *Iterator) decodeKey() {
   186  	b := it.list.arena.getBytes(it.nd.keyOffset, it.nd.keySize)
   187  	l := len(b) - 8
   188  	if l >= 0 {
   189  		it.key.Trailer = binary.LittleEndian.Uint64(b[l:])
   190  		it.key.UserKey = b[:l:l]
   191  	} else {
   192  		it.key.Trailer = uint64(base.InternalKeyKindInvalid)
   193  		it.key.UserKey = nil
   194  	}
   195  }