-
Notifications
You must be signed in to change notification settings - Fork 1
/
bucket_iter.go
145 lines (121 loc) · 2.64 KB
/
bucket_iter.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
package puredb
import (
"github.com/dgraph-io/badger"
"fmt"
)
type BucketIterOpts struct {
Prefix []byte
Reverse bool
}
type BucketIter struct {
bucket *Bucket
prefix []byte
txn *badger.Txn
it *badger.Iterator
bOpts *badger.IteratorOptions
Opts BucketIterOpts
Err error
}
func NewBucketIter(bucket *Bucket, opts BucketIterOpts) *BucketIter {
bOpts := badger.DefaultIteratorOptions
bOpts.PrefetchSize = 10
bOpts.Reverse = opts.Reverse
db := bucket.badgerDB
txn := db.NewTransaction(false) // read-only transaction (update set to false)
prefix := []byte(fmt.Sprintf("%s__", bucket.GetName()))
if len(opts.Prefix) > 0 {
prefix = append(prefix, opts.Prefix...)
}
if opts.Reverse {
prefix = append(prefix, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}...) // trick, see https://github.com/dgraph-io/badger/issues/436#issuecomment-400095559
}
it := BucketIter{
bucket: bucket,
prefix: prefix,
txn: txn,
it: txn.NewIterator(bOpts),
bOpts: &bOpts,
Opts: opts,
}
it.it.Seek(it.prefix)
return &it
}
func (it *BucketIter) Close() {
it.it.Close()
it.txn.Discard()
}
func (it *BucketIter) Rewind() {
it.it.Seek(it.prefix)
}
func (it *BucketIter) Valid() bool {
return it.it.ValidForPrefix(it.prefix)
}
func (it *BucketIter) EOF() bool {
return (! it.it.ValidForPrefix(it.prefix))
}
func (it *BucketIter) Next() {
it.it.Next()
}
func (it *BucketIter) Error() bool {
return it.Err != nil
}
func (it *BucketIter) Get(keyp *interface{}, valuep *interface{}) error {
item := it.it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
it.Err = err
return err
}
k_b := k_prefixed[len(it.prefix):]
err = it.bucket.UnmarshalKey(k_b, keyp)
if err != nil {
it.Err = err
return err
}
err = it.bucket.UnmarshalValue(v_b, valuep)
if err != nil {
it.Err = err
return err
}
return nil
}
func (it *BucketIter) Find(value interface{}, cmpFn BucketPredicate, keyp *interface{}) (bool, error) {
for ; it.Valid(); it.Next() {
item := it.it.Item()
k_prefixed := item.Key()
v_b, err := item.Value()
if err != nil {
it.Err = err
return false, err
}
k_b := k_prefixed[len(it.prefix):]
var k_i interface{}
var v_i interface{}
err = it.bucket.UnmarshalKey(k_b, &k_i)
if err != nil {
it.Err = err
return false, err
}
err = it.bucket.UnmarshalValue(v_b, &v_i)
if err != nil {
it.Err = err
return false, err
}
if cmpFn != nil {
found, err := cmpFn(it.bucket, k_i, v_i)
if err != nil {
it.Err = err
return false, err
}
if found {
*keyp = k_i
return true, nil
}
} else if v_i == value {
*keyp = k_i
return true, nil
}
}
return false, nil
}