/
iterator.go
94 lines (73 loc) · 1.81 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
package storage
import (
"io"
"github.com/dgraph-io/badger/v2"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
var ErrEndOfIterator = errors.New("end of iterator")
type Iterator interface {
Next(proto.Message) error
NextWithKey(key MonotonicallySerializable, value proto.Message) error
io.Closer
}
//BadgerIterator is a wrapper around *badger.Iterator that implements
//the Iterator interface
type BadgerIterator struct {
it *badger.Iterator
prefixLength int
}
func NewBadgerIterator(it *badger.Iterator, prefixLength int) *BadgerIterator {
return &BadgerIterator{
it: it,
prefixLength: prefixLength,
}
}
func (bi *BadgerIterator) NextWithKey(key MonotonicallySerializable, value proto.Message) error {
err := bi.currentKey(key)
if err != nil {
return err
}
err = bi.currentValue(value)
if err != nil {
return err
}
bi.it.Next()
return nil
}
func (bi *BadgerIterator) Next(value proto.Message) error {
err := bi.currentValue(value)
if err != nil {
return err
}
bi.it.Next()
return nil
}
func (bi *BadgerIterator) Close() error {
bi.it.Close()
return nil
}
func (bi *BadgerIterator) currentValue(value proto.Message) error {
if !bi.it.Valid() {
return ErrEndOfIterator
}
item := bi.it.Item() //important: this doesn't call Next()
err := item.Value(func(val []byte) error {
err := proto.Unmarshal(val, value)
return err
})
if err != nil {
return errors.Wrap(err, "couldn't unmarshal data")
}
return nil
}
func (bi *BadgerIterator) currentKey(key MonotonicallySerializable) error {
if !bi.it.Valid() {
return ErrEndOfIterator
}
item := bi.it.Item() //important: this doesn't call Next()
byteKey := item.Key()
strippedKey := byteKey[bi.prefixLength:] //TODO: maybe add function to do that
err := key.MonotonicUnmarshal(strippedKey)
return err
}