forked from tendermint/tm-db
-
Notifications
You must be signed in to change notification settings - Fork 3
/
boltdb_iterator.go
141 lines (118 loc) · 2.6 KB
/
boltdb_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
// +build boltdb
package db
import (
"bytes"
"go.etcd.io/bbolt"
)
// boltDBIterator allows you to iterate on range of keys/values given some
// start / end keys (nil & nil will result in doing full scan).
type boltDBIterator struct {
tx *bbolt.Tx
itr *bbolt.Cursor
start []byte
end []byte
currentKey []byte
currentValue []byte
isInvalid bool
isReverse bool
}
var _ Iterator = (*boltDBIterator)(nil)
// newBoltDBIterator creates a new boltDBIterator.
func newBoltDBIterator(tx *bbolt.Tx, start, end []byte, isReverse bool) *boltDBIterator {
itr := tx.Bucket(bucket).Cursor()
var ck, cv []byte
if isReverse {
switch {
case end == nil:
ck, cv = itr.Last()
default:
_, _ = itr.Seek(end) // after key
ck, cv = itr.Prev() // return to end key
}
} else {
switch {
case start == nil:
ck, cv = itr.First()
default:
ck, cv = itr.Seek(start)
}
}
return &boltDBIterator{
tx: tx,
itr: itr,
start: start,
end: end,
currentKey: ck,
currentValue: cv,
isReverse: isReverse,
isInvalid: false,
}
}
// Domain implements Iterator.
func (itr *boltDBIterator) Domain() ([]byte, []byte) {
return itr.start, itr.end
}
// Valid implements Iterator.
func (itr *boltDBIterator) Valid() bool {
if itr.isInvalid {
return false
}
if itr.Error() != nil {
itr.isInvalid = true
return false
}
// iterated to the end of the cursor
if itr.currentKey == nil {
itr.isInvalid = true
return false
}
if itr.isReverse {
if itr.start != nil && bytes.Compare(itr.currentKey, itr.start) < 0 {
itr.isInvalid = true
return false
}
} else {
if itr.end != nil && bytes.Compare(itr.end, itr.currentKey) <= 0 {
itr.isInvalid = true
return false
}
}
// Valid
return true
}
// Next implements Iterator.
func (itr *boltDBIterator) Next() {
itr.assertIsValid()
if itr.isReverse {
itr.currentKey, itr.currentValue = itr.itr.Prev()
} else {
itr.currentKey, itr.currentValue = itr.itr.Next()
}
}
// Key implements Iterator.
func (itr *boltDBIterator) Key() []byte {
itr.assertIsValid()
return append([]byte{}, itr.currentKey...)
}
// Value implements Iterator.
func (itr *boltDBIterator) Value() []byte {
itr.assertIsValid()
var value []byte
if itr.currentValue != nil {
value = append([]byte{}, itr.currentValue...)
}
return value
}
// Error implements Iterator.
func (itr *boltDBIterator) Error() error {
return nil
}
// Close implements Iterator.
func (itr *boltDBIterator) Close() error {
return itr.tx.Rollback()
}
func (itr *boltDBIterator) assertIsValid() {
if !itr.Valid() {
panic("iterator is invalid")
}
}