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