-
Notifications
You must be signed in to change notification settings - Fork 351
/
iterator.go
88 lines (71 loc) · 1.56 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
package sstable
import (
"github.com/cockroachdb/pebble/sstable"
"github.com/treeverse/lakefs/pkg/graveler/committed"
)
// Iterator returns ordered iteration of the SSTable entries
type Iterator struct {
it sstable.Iterator
currKey *sstable.InternalKey
currValue []byte
postSeek bool
err error
derefer func() error
}
func NewIterator(it sstable.Iterator, derefer func() error) *Iterator {
iter := &Iterator{
it: it,
derefer: derefer,
}
return iter
}
func (iter *Iterator) SeekGE(lookup committed.Key) {
key, value := iter.it.SeekGE(lookup, sstable.SeekGEFlags(0))
val, _, err := value.Value(nil)
iter.currKey = key
iter.currValue = val
iter.err = err
iter.postSeek = true
}
func (iter *Iterator) Next() bool {
if !iter.postSeek {
key, value := iter.it.Next()
iter.currKey = key
val, _, err := value.Value(nil)
iter.err = err
iter.currValue = val
}
iter.postSeek = false
if iter.currKey == nil && iter.currValue == nil {
return false
}
return true
}
func (iter *Iterator) Value() *committed.Record {
if iter.currKey == nil || iter.err != nil || iter.postSeek {
return nil
}
return &committed.Record{
Key: iter.currKey.UserKey,
Value: iter.currValue,
}
}
func (iter *Iterator) Err() error {
return iter.err
}
func (iter *Iterator) Close() {
if iter.it == nil {
return
}
err := iter.it.Close()
iter.updateOnNilErr(err)
err = iter.derefer()
iter.updateOnNilErr(err)
iter.it = nil
}
func (iter *Iterator) updateOnNilErr(err error) {
if iter.err == nil {
// avoid overriding earlier errors
iter.err = err
}
}