forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
479 lines (426 loc) · 12.6 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2011 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"bytes"
"errors"
"fmt"
)
type iterPos int8
const (
iterPosCur iterPos = 0
iterPosNext iterPos = 1
iterPosPrev iterPos = -1
)
// Iterator iterates over a DB's key/value pairs in key order.
//
// An iterator must be closed after use, but it is not necessary to read an
// iterator until exhaustion.
//
// An iterator is not goroutine-safe, but it is safe to use multiple iterators
// concurrently, with each in a dedicated goroutine.
//
// It is also safe to use an iterator concurrently with modifying its
// underlying DB, if that DB permits modification. However, the resultant
// key/value pairs are not guaranteed to be a consistent snapshot of that DB
// at a particular point in time.
type Iterator struct {
opts IterOptions
cmp Compare
equal Equal
merge Merge
split Split
iter internalIterator
readState *readState
err error
key []byte
keyBuf []byte
value []byte
valueBuf []byte
valueBuf2 []byte
valid bool
iterKey *InternalKey
iterValue []byte
pos iterPos
alloc *iterAlloc
prefix []byte
}
func (i *Iterator) findNextEntry() bool {
i.valid = false
i.pos = iterPosCur
for i.iterKey != nil {
key := *i.iterKey
switch key.Kind() {
case InternalKeyKindDelete:
i.nextUserKey()
continue
case InternalKeyKindRangeDelete:
// Range deletions are treated as no-ops. See the comments in levelIter
// for more details.
i.iterKey, i.iterValue = i.iter.Next()
continue
case InternalKeyKindSet:
if i.prefix != nil && !bytes.HasPrefix(key.UserKey, i.prefix) {
return false
}
i.keyBuf = append(i.keyBuf[:0], key.UserKey...)
i.key = i.keyBuf
i.value = i.iterValue
i.valid = true
return true
case InternalKeyKindMerge:
if i.prefix != nil && !bytes.HasPrefix(key.UserKey, i.prefix) {
return false
}
return i.mergeNext(key)
default:
i.err = fmt.Errorf("invalid internal key kind: %d", key.Kind())
return false
}
}
return false
}
func (i *Iterator) nextUserKey() {
if i.iterKey == nil {
return
}
done := i.iterKey.SeqNum() == 0
if !i.valid {
i.keyBuf = append(i.keyBuf[:0], i.iterKey.UserKey...)
i.key = i.keyBuf
}
for {
i.iterKey, i.iterValue = i.iter.Next()
if done || i.iterKey == nil || !i.equal(i.key, i.iterKey.UserKey) {
break
}
done = i.iterKey.SeqNum() == 0
}
}
func (i *Iterator) findPrevEntry() bool {
i.valid = false
i.pos = iterPosCur
for i.iterKey != nil {
key := *i.iterKey
if i.valid {
if !i.equal(key.UserKey, i.key) {
// We've iterated to the previous user key.
i.pos = iterPosPrev
return true
}
}
switch key.Kind() {
case InternalKeyKindDelete:
i.value = nil
i.valid = false
i.iterKey, i.iterValue = i.iter.Prev()
continue
case InternalKeyKindRangeDelete:
// Range deletions are treated as no-ops. See the comments in levelIter
// for more details.
i.iterKey, i.iterValue = i.iter.Prev()
continue
case InternalKeyKindSet:
if i.prefix != nil && !bytes.HasPrefix(key.UserKey, i.prefix) {
return false
}
i.keyBuf = append(i.keyBuf[:0], key.UserKey...)
i.key = i.keyBuf
i.value = i.iterValue
i.valid = true
i.iterKey, i.iterValue = i.iter.Prev()
continue
case InternalKeyKindMerge:
if !i.valid {
if i.prefix != nil && !bytes.HasPrefix(key.UserKey, i.prefix) {
return false
}
i.keyBuf = append(i.keyBuf[:0], key.UserKey...)
i.key = i.keyBuf
i.value = i.iterValue
i.valid = true
} else {
// The existing value is either stored in valueBuf2 or the underlying
// iterators value. We append the new value to valueBuf in order to
// merge(valueBuf, valueBuf2). Then we swap valueBuf and valueBuf2 in
// order to maintain the invariant that the existing value points to
// valueBuf2 (in preparation for handling th next merge value).
i.valueBuf = append(i.valueBuf[:0], i.iterValue...)
i.valueBuf = i.merge(i.key, i.valueBuf, i.value, nil)
i.valueBuf, i.valueBuf2 = i.valueBuf2, i.valueBuf
i.value = i.valueBuf2
}
i.iterKey, i.iterValue = i.iter.Prev()
continue
default:
i.err = fmt.Errorf("invalid internal key kind: %d", key.Kind())
return false
}
}
if i.valid {
i.pos = iterPosPrev
return true
}
return false
}
func (i *Iterator) prevUserKey() {
if i.iterKey == nil {
return
}
if !i.valid {
// If we're going to compare against the prev key, we need to save the
// current key.
i.keyBuf = append(i.keyBuf[:0], i.iterKey.UserKey...)
i.key = i.keyBuf
}
for {
i.iterKey, i.iterValue = i.iter.Prev()
if i.iterKey == nil || !i.equal(i.key, i.iterKey.UserKey) {
break
}
}
}
func (i *Iterator) mergeNext(key InternalKey) bool {
// Save the current key and value.
i.keyBuf = append(i.keyBuf[:0], key.UserKey...)
i.valueBuf = append(i.valueBuf[:0], i.iterValue...)
i.key, i.value = i.keyBuf, i.valueBuf
i.valid = true
// Loop looking for older values for this key and merging them.
for {
i.iterKey, i.iterValue = i.iter.Next()
if i.iterKey == nil {
i.pos = iterPosNext
return true
}
key = *i.iterKey
if !i.equal(i.key, key.UserKey) {
// We've advanced to the next key.
i.pos = iterPosNext
return true
}
switch key.Kind() {
case InternalKeyKindDelete:
// We've hit a deletion tombstone. Return everything up to this
// point.
return true
case InternalKeyKindRangeDelete:
// Range deletions are treated as no-ops. See the comments in levelIter
// for more details.
continue
case InternalKeyKindSet:
// We've hit a Set value. Merge with the existing value and return.
i.value = i.merge(i.key, i.value, i.iterValue, nil)
return true
case InternalKeyKindMerge:
// We've hit another Merge value. Merge with the existing value and
// continue looping.
i.value = i.merge(i.key, i.value, i.iterValue, nil)
i.valueBuf = i.value[:0]
continue
default:
i.err = fmt.Errorf("invalid internal key kind: %d", key.Kind())
return false
}
}
}
// SeekGE moves the iterator to the first key/value pair whose key is greater
// than or equal to the given key. Returns true if the iterator is pointing at
// a valid entry and false otherwise.
func (i *Iterator) SeekGE(key []byte) bool {
if i.err != nil {
return false
}
i.prefix = nil
if lowerBound := i.opts.GetLowerBound(); lowerBound != nil && i.cmp(key, lowerBound) < 0 {
key = lowerBound
}
i.iterKey, i.iterValue = i.iter.SeekGE(key)
return i.findNextEntry()
}
// SeekPrefixGE moves the iterator to the first key/value pair whose key is
// greater than or equal to the given key and shares a common prefix with the
// given key. Returns true if the iterator is pointing at a valid entry and
// false otherwise. Note that a user-defined Split function must be supplied to
// the Comparer. Also note that the iterator will not observe keys not matching
// the prefix.
func (i *Iterator) SeekPrefixGE(key []byte) bool {
if i.err != nil {
return false
}
if i.split == nil {
panic("pebble: split must be provided for SeekPrefixGE")
}
// Make a copy of the prefix so that modifications to the key after
// SeekPrefixGE returns does not affect the stored prefix.
prefixLen := i.split(key)
i.prefix = make([]byte, prefixLen)
copy(i.prefix, key[:prefixLen])
if lowerBound := i.opts.GetLowerBound(); lowerBound != nil && i.cmp(key, lowerBound) < 0 {
if !bytes.HasPrefix(lowerBound, i.prefix) {
i.err = errors.New("pebble: SeekPrefixGE supplied with key outside of lower bound")
} else {
key = lowerBound
}
}
i.iterKey, i.iterValue = i.iter.SeekPrefixGE(i.prefix, key)
return i.findNextEntry()
}
// SeekLT moves the iterator to the last key/value pair whose key is less than
// the given key. Returns true if the iterator is pointing at a valid entry and
// false otherwise.
func (i *Iterator) SeekLT(key []byte) bool {
if i.err != nil {
return false
}
i.prefix = nil
if upperBound := i.opts.GetUpperBound(); upperBound != nil && i.cmp(key, upperBound) >= 0 {
key = upperBound
}
i.iterKey, i.iterValue = i.iter.SeekLT(key)
return i.findPrevEntry()
}
// First moves the iterator the the first key/value pair. Returns true if the
// iterator is pointing at a valid entry and false otherwise.
func (i *Iterator) First() bool {
if i.err != nil {
return false
}
i.prefix = nil
if lowerBound := i.opts.GetLowerBound(); lowerBound != nil {
i.iterKey, i.iterValue = i.iter.SeekGE(lowerBound)
} else {
i.iterKey, i.iterValue = i.iter.First()
}
return i.findNextEntry()
}
// Last moves the iterator the the last key/value pair. Returns true if the
// iterator is pointing at a valid entry and false otherwise.
func (i *Iterator) Last() bool {
if i.err != nil {
return false
}
i.prefix = nil
if upperBound := i.opts.GetUpperBound(); upperBound != nil {
i.iterKey, i.iterValue = i.iter.SeekLT(upperBound)
} else {
i.iterKey, i.iterValue = i.iter.Last()
}
return i.findPrevEntry()
}
// Next moves the iterator to the next key/value pair. Returns true if the
// iterator is pointing at a valid entry and false otherwise.
func (i *Iterator) Next() bool {
if i.err != nil {
return false
}
switch i.pos {
case iterPosCur:
i.nextUserKey()
case iterPosPrev:
// The underlying iterator is pointed to the previous key (this can only
// happen when switching iteration directions). We set i.valid to false
// here to force the calls to nextUserKey to save the current key i.iter is
// pointing at in order to determine when the next user-key is reached.
i.valid = false
if i.iterKey == nil {
// We're positioned before the first key. Need to reposition to point to
// the first key.
if lowerBound := i.opts.GetLowerBound(); lowerBound != nil {
i.iterKey, i.iterValue = i.iter.SeekGE(lowerBound)
} else {
i.iterKey, i.iterValue = i.iter.First()
}
} else {
i.nextUserKey()
}
i.nextUserKey()
case iterPosNext:
}
return i.findNextEntry()
}
// Prev moves the iterator to the previous key/value pair. Returns true if the
// iterator is pointing at a valid entry and false otherwise.
func (i *Iterator) Prev() bool {
if i.err != nil {
return false
}
switch i.pos {
case iterPosCur:
i.prevUserKey()
case iterPosNext:
// The underlying iterator is pointed to the next key (this can only happen
// when switching iteration directions). We set i.valid to false here to
// force the calls to prevUserKey to save the current key i.iter is
// pointing at in order to determine when the next user-key is reached.
i.valid = false
if i.iterKey == nil {
// We're positioned after the last key. Need to reposition to point to
// the last key.
if upperBound := i.opts.GetUpperBound(); upperBound != nil {
i.iterKey, i.iterValue = i.iter.SeekLT(upperBound)
} else {
i.iterKey, i.iterValue = i.iter.Last()
}
} else {
i.prevUserKey()
}
i.prevUserKey()
case iterPosPrev:
}
return i.findPrevEntry()
}
// Key returns the key of the current key/value pair, or nil if done. The
// caller should not modify the contents of the returned slice, and its
// contents may change on the next call to Next.
func (i *Iterator) Key() []byte {
return i.key
}
// Value returns the value of the current key/value pair, or nil if done. The
// caller should not modify the contents of the returned slice, and its
// contents may change on the next call to Next.
func (i *Iterator) Value() []byte {
return i.value
}
// Valid returns true if the iterator is positioned at a valid key/value pair
// and false otherwise.
func (i *Iterator) Valid() bool {
return i.valid
}
// Error returns any accumulated error.
func (i *Iterator) Error() error {
return i.err
}
// Close closes the iterator and returns any accumulated error. Exhausting
// all the key/value pairs in a table is not considered to be an error.
// It is valid to call Close multiple times. Other methods should not be
// called after the iterator has been closed.
func (i *Iterator) Close() error {
if i.readState != nil {
i.readState.unref()
i.readState = nil
}
if err := i.iter.Close(); err != nil && i.err != nil {
i.err = err
}
err := i.err
if alloc := i.alloc; alloc != nil {
*i = Iterator{}
iterAllocPool.Put(alloc)
}
return err
}
// SetBounds sets the lower and upper bounds for the iterator. Note that the
// iterator will always be invalidated and must be repositioned with a call to
// SeekGE, SeekPrefixGE, SeekLT, First, or Last.
func (i *Iterator) SetBounds(lower, upper []byte) {
i.prefix = nil
i.iterKey = nil
i.iterValue = nil
i.pos = iterPosCur
i.valid = false
i.opts.LowerBound = lower
i.opts.UpperBound = upper
i.iter.SetBounds(lower, upper)
}