github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitree/bitree_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 bitree 16 17 import ( 18 "encoding/binary" 19 20 "github.com/zuoyebang/bitalosdb/bitpage" 21 "github.com/zuoyebang/bitalosdb/bitree/bdb" 22 "github.com/zuoyebang/bitalosdb/internal/base" 23 "github.com/zuoyebang/bitalosdb/internal/options" 24 "github.com/zuoyebang/bitalosdb/internal/utils" 25 ) 26 27 type BitreeIterator struct { 28 btree *Bitree 29 ops *options.IterOptions 30 cmp base.Compare 31 compact bool 32 err error 33 iterKey *base.InternalKey 34 iterValue []byte 35 ikey *base.InternalKey 36 value []byte 37 putPools []func() 38 lower []byte 39 upper []byte 40 bdbIter *bdb.BdbIterator 41 bpageIter *bitpage.PageIterator 42 bpageIters map[bitpage.PageNum]*bitpage.PageIterator 43 } 44 45 func (i *BitreeIterator) getKV() (*base.InternalKey, []byte) { 46 if i.iterKey == nil { 47 return nil, nil 48 } 49 50 if i.compact { 51 return i.iterKey, i.iterValue 52 } 53 54 switch i.iterKey.Kind() { 55 case base.InternalKeyKindDelete, base.InternalKeyKindPrefixDelete: 56 return i.iterKey, i.iterValue 57 } 58 59 iv := base.DecodeInternalValue(i.iterValue) 60 if iv.Kind() == base.InternalKeyKindSetBithash { 61 if !base.CheckValueValidByKeySetBithash(iv.UserValue) { 62 return nil, nil 63 } 64 fn := binary.LittleEndian.Uint32(iv.UserValue) 65 value, putPool, err := i.btree.bithashGet(i.iterKey.UserKey, fn) 66 if err != nil { 67 return nil, nil 68 } 69 70 i.value = value 71 iv.SetKind(base.InternalKeyKindSet) 72 if putPool != nil { 73 i.putPools = append(i.putPools, putPool) 74 } 75 } else { 76 i.value = iv.UserValue 77 } 78 79 if i.ikey == nil { 80 i.ikey = new(base.InternalKey) 81 } 82 *(i.ikey) = base.MakeInternalKey2(i.iterKey.UserKey, iv.Header) 83 84 return i.ikey, i.value 85 } 86 87 func (i *BitreeIterator) setBitpageIter(v []byte) bool { 88 pn := bitpage.PageNum(utils.BytesToUint32(v)) 89 pageIter, ok := i.bpageIters[pn] 90 if !ok { 91 pageIter = i.btree.newPageIter(pn, i.ops) 92 if pageIter == nil { 93 return false 94 } 95 i.bpageIters[pn] = pageIter 96 } 97 98 i.bpageIter = pageIter 99 return true 100 } 101 102 func (i *BitreeIterator) findBdbFirst() bool { 103 bdbKey, bdbValue := i.bdbIter.First() 104 if bdbKey == nil { 105 return false 106 } 107 108 return i.setBitpageIter(bdbValue) 109 } 110 111 func (i *BitreeIterator) findBdbLast() bool { 112 bdbKey, bdbValue := i.bdbIter.Last() 113 if bdbKey == nil { 114 return false 115 } 116 117 return i.setBitpageIter(bdbValue) 118 } 119 120 func (i *BitreeIterator) findBdbNext() bool { 121 bdbKey, bdbValue := i.bdbIter.Next() 122 if bdbKey == nil { 123 return false 124 } 125 126 return i.setBitpageIter(bdbValue) 127 } 128 129 func (i *BitreeIterator) findBdbPrev() bool { 130 bdbKey, bdbValue := i.bdbIter.Prev() 131 if bdbKey == nil { 132 return false 133 } 134 135 return i.setBitpageIter(bdbValue) 136 } 137 138 func (i *BitreeIterator) findBdbSeekGE(key []byte) bool { 139 bdbKey, bdbValue := i.bdbIter.SeekGE(key) 140 if bdbKey == nil { 141 return false 142 } 143 144 return i.setBitpageIter(bdbValue) 145 } 146 147 func (i *BitreeIterator) First() (*base.InternalKey, []byte) { 148 if !i.findBdbFirst() { 149 return nil, nil 150 } 151 152 i.iterKey, i.iterValue = i.bpageIter.First() 153 for i.iterKey == nil { 154 if !i.findBdbNext() { 155 return nil, nil 156 } 157 i.iterKey, i.iterValue = i.bpageIter.First() 158 } 159 160 if i.upper != nil && i.cmp(i.upper, i.iterKey.UserKey) <= 0 { 161 return nil, nil 162 } 163 164 return i.getKV() 165 } 166 167 func (i *BitreeIterator) Last() (*base.InternalKey, []byte) { 168 if !i.findBdbLast() { 169 return nil, nil 170 } 171 172 i.iterKey, i.iterValue = i.bpageIter.Last() 173 for i.iterKey == nil { 174 if !i.findBdbPrev() { 175 return nil, nil 176 } 177 i.iterKey, i.iterValue = i.bpageIter.Last() 178 } 179 180 if i.lower != nil && i.cmp(i.lower, i.iterKey.UserKey) > 0 { 181 return nil, nil 182 } 183 184 return i.getKV() 185 } 186 187 func (i *BitreeIterator) Next() (*base.InternalKey, []byte) { 188 if i.iterKey == nil { 189 return nil, nil 190 } 191 192 i.iterKey, i.iterValue = i.bpageIter.Next() 193 for i.iterKey == nil { 194 if !i.findBdbNext() { 195 return nil, nil 196 } 197 i.iterKey, i.iterValue = i.bpageIter.First() 198 } 199 200 if i.upper != nil && i.cmp(i.upper, i.iterKey.UserKey) <= 0 { 201 return nil, nil 202 } 203 204 return i.getKV() 205 } 206 207 func (i *BitreeIterator) Prev() (*base.InternalKey, []byte) { 208 if i.iterKey == nil { 209 return nil, nil 210 } 211 212 i.iterKey, i.iterValue = i.bpageIter.Prev() 213 for i.iterKey == nil { 214 if !i.findBdbPrev() { 215 return nil, nil 216 } 217 i.iterKey, i.iterValue = i.bpageIter.Last() 218 } 219 220 if i.lower != nil && i.cmp(i.lower, i.iterKey.UserKey) > 0 { 221 return nil, nil 222 } 223 224 return i.getKV() 225 } 226 227 func (i *BitreeIterator) SeekGE(key []byte) (*base.InternalKey, []byte) { 228 if !i.findBdbSeekGE(key) { 229 return nil, nil 230 } 231 232 i.iterKey, i.iterValue = i.bpageIter.SeekGE(key) 233 for i.iterKey == nil { 234 if !i.findBdbNext() { 235 return nil, nil 236 } 237 i.iterKey, i.iterValue = i.bpageIter.SeekGE(key) 238 } 239 240 if i.upper != nil && i.cmp(i.upper, i.iterKey.UserKey) <= 0 { 241 return nil, nil 242 } 243 244 return i.getKV() 245 } 246 247 func (i *BitreeIterator) SeekLT(key []byte) (*base.InternalKey, []byte) { 248 if !i.findBdbSeekGE(key) { 249 return nil, nil 250 } 251 252 i.iterKey, i.iterValue = i.bpageIter.SeekLT(key) 253 for i.iterKey == nil { 254 if !i.findBdbPrev() { 255 return nil, nil 256 } 257 i.iterKey, i.iterValue = i.bpageIter.SeekLT(key) 258 } 259 260 if i.lower != nil && i.cmp(i.lower, i.iterKey.UserKey) > 0 { 261 return nil, nil 262 } 263 264 return i.getKV() 265 } 266 267 func (i *BitreeIterator) SeekPrefixGE( 268 prefix, key []byte, trySeekUsingNext bool, 269 ) (*base.InternalKey, []byte) { 270 return i.SeekGE(key) 271 } 272 273 func (i *BitreeIterator) Close() error { 274 if len(i.putPools) > 0 { 275 for _, f := range i.putPools { 276 f() 277 } 278 } 279 280 for _, pageIter := range i.bpageIters { 281 if err := pageIter.Close(); err != nil && i.err == nil { 282 i.err = err 283 } 284 } 285 286 if err := i.bdbIter.Close(); err != nil && i.err == nil { 287 i.err = err 288 } 289 290 return i.err 291 } 292 293 func (i *BitreeIterator) Error() error { 294 return nil 295 } 296 297 func (i *BitreeIterator) SetBounds(lower, upper []byte) { 298 i.lower = lower 299 i.upper = upper 300 } 301 302 func (i *BitreeIterator) SetCompact() { 303 i.compact = true 304 } 305 306 func (i *BitreeIterator) String() string { 307 return "BitreeIterator" 308 }