-
Notifications
You must be signed in to change notification settings - Fork 79
/
iter.go
98 lines (83 loc) · 1.94 KB
/
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
// Copyright 2022 ByteDance and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package badger
import (
"bytes"
"context"
"io"
"github.com/dgraph-io/badger"
"github.com/kubewharf/kubebrain/pkg/storage"
)
type iter struct {
txn *badger.Txn
iIter *badger.Iterator
reverse bool
err error
start []byte
end []byte
limit uint64
counter uint64
seeked bool
}
func newIter(db *badger.DB, start, end []byte, ts uint64, limit uint64) storage.Iter {
i := &iter{}
i.start = start
i.end = end
i.txn = db.NewTransaction(false)
i.reverse = bytes.Compare(start, end) > 0
i.limit = limit
i.iIter = i.txn.NewIterator(badger.IteratorOptions{Reverse: i.reverse})
return i
}
func (i *iter) inRange() bool {
if i.limit != 0 && i.counter >= i.limit {
return false
}
i.counter++
key := i.iIter.Item().Key()
cmp := bytes.Compare(key, i.end)
if i.reverse {
return cmp > 0
} else {
return cmp < 0
}
}
func (i *iter) Next(ctx context.Context) (err error) {
if i.err != nil {
return i.err
}
if i.seeked {
i.iIter.Next()
} else {
i.iIter.Seek(i.start)
i.seeked = true
}
if !i.iIter.Valid() || !i.inRange() {
i.err = io.EOF
return i.err
}
return
}
func (i *iter) Key() []byte {
return i.iIter.Item().KeyCopy(nil)
}
func (i *iter) Val() (v []byte) {
v, i.err = i.iIter.Item().ValueCopy(nil)
return v
}
func (i *iter) Close() error {
i.iIter.Close()
defer i.txn.Discard()
return i.txn.Commit()
}