forked from hyperledger-archives/burrow
/
prefix.go
228 lines (191 loc) · 4.66 KB
/
prefix.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package storage
import (
"bytes"
dbm "github.com/tendermint/tendermint/libs/db"
hex "github.com/tmthrgd/go-hex"
)
type Prefix []byte
func NewPrefix(bs []byte) Prefix {
p := make(Prefix, len(bs))
copy(p, bs)
return p
}
func (p Prefix) Key(key []byte) []byte {
// Avoid any unintended memory sharing between keys
return append(p[:len(p):len(p)], key...)
}
func (p Prefix) Suffix(key []byte) []byte {
bs := make([]byte, len(key)-len(p))
copy(bs, key[len(p):])
return bs
}
// Get the lexicographical sibling above this prefix (i.e. the fixed length integer plus one)
func (p Prefix) Above() []byte {
for i := len(p) - 1; i >= 0; i-- {
c := p[i]
if c < 0xff {
inc := make([]byte, i+1)
copy(inc, p)
inc[i]++
return inc
}
}
return nil
}
// Get the lexicographical sibling below this prefix (i.e. the fixed length integer minus one)
func (p Prefix) Below() []byte {
for i := len(p) - 1; i >= 0; i-- {
c := p[i]
if c > 0x00 {
inc := make([]byte, i+1)
copy(inc, p)
inc[i]--
return inc
}
}
return nil
}
func (p Prefix) CallbackIterable(source KVCallbackIterable) *prefixCallbackIterable {
return &prefixCallbackIterable{
prefix: p,
source: source,
}
}
type prefixCallbackIterable struct {
prefix Prefix
source KVCallbackIterable
}
func (pi *prefixCallbackIterable) Iterate(start, end []byte, ascending bool, fn func(key []byte, value []byte) error) error {
var pstart, pend []byte = pi.prefix.Key(start), nil
if start == nil {
// We may iterate on a key that does not start with prefix
pstart = pi.prefix.Below()
} else {
pstart = pi.prefix.Key(start)
}
if end == nil {
// Source is exclusive on end so we won't iterate over it
pend = pi.prefix.Above()
} else {
pend = pi.prefix.Key(end)
}
return pi.source.Iterate(pstart, pend, ascending, func(key []byte, value []byte) error {
if bytes.HasPrefix(key, pi.prefix) {
return fn(pi.prefix.Suffix(key), value)
}
return nil
})
}
func (p Prefix) Iterator(iteratorFn func(start, end []byte) dbm.Iterator, start, end []byte) KVIterator {
var pstart, pend []byte = p.Key(start), nil
if end == nil {
pend = p.Above()
} else {
pend = p.Key(end)
}
return &prefixIterator{
start: start,
end: end,
prefix: p,
source: iteratorFn(pstart, pend),
}
}
func (p Prefix) Iterable(source KVIterable) KVIterable {
return &prefixIterable{
prefix: p,
source: source,
}
}
type prefixIterable struct {
prefix Prefix
source KVIterable
}
func (pi *prefixIterable) Iterator(low, high []byte) KVIterator {
return pi.prefix.Iterator(pi.source.Iterator, low, high)
}
func (pi *prefixIterable) ReverseIterator(low, high []byte) KVIterator {
return pi.prefix.Iterator(pi.source.ReverseIterator, low, high)
}
func (p Prefix) Store(source KVStore) KVStore {
return &prefixKVStore{
prefix: p,
source: source,
}
}
func (p Prefix) Length() int {
return len(p)
}
func (p Prefix) String() string {
return string(p)
}
func (p Prefix) HexString() string {
return hex.EncodeUpperToString(p)
}
type prefixIterator struct {
prefix Prefix
source dbm.Iterator
start []byte
end []byte
invalid bool
}
func (pi *prefixIterator) Domain() ([]byte, []byte) {
return pi.start, pi.end
}
func (pi *prefixIterator) Valid() bool {
pi.validate()
return !pi.invalid && pi.source.Valid()
}
func (pi *prefixIterator) Next() {
if pi.invalid {
panic("prefixIterator.Next() called on invalid iterator")
}
pi.source.Next()
pi.validate()
}
func (pi *prefixIterator) Key() []byte {
if pi.invalid {
panic("prefixIterator.Key() called on invalid iterator")
}
return pi.prefix.Suffix(pi.source.Key())
}
func (pi *prefixIterator) Value() []byte {
if pi.invalid {
panic("prefixIterator.Value() called on invalid iterator")
}
return pi.source.Value()
}
func (pi *prefixIterator) Close() {
pi.source.Close()
}
func (pi *prefixIterator) validate() {
if pi.invalid {
return
}
sourceValid := pi.source.Valid()
pi.invalid = !sourceValid || !bytes.HasPrefix(pi.source.Key(), pi.prefix)
if pi.invalid {
pi.Close()
}
}
type prefixKVStore struct {
prefix Prefix
source KVStore
}
func (ps *prefixKVStore) Get(key []byte) []byte {
return ps.source.Get(ps.prefix.Key(key))
}
func (ps *prefixKVStore) Has(key []byte) bool {
return ps.source.Has(ps.prefix.Key(key))
}
func (ps *prefixKVStore) Set(key, value []byte) {
ps.source.Set(ps.prefix.Key(key), value)
}
func (ps *prefixKVStore) Delete(key []byte) {
ps.source.Delete(ps.prefix.Key(key))
}
func (ps *prefixKVStore) Iterator(low, high []byte) KVIterator {
return ps.prefix.Iterator(ps.source.Iterator, low, high)
}
func (ps *prefixKVStore) ReverseIterator(low, high []byte) KVIterator {
return ps.prefix.Iterator(ps.source.ReverseIterator, low, high)
}