-
Notifications
You must be signed in to change notification settings - Fork 47
/
iterator.go
118 lines (97 loc) · 2.69 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package bolt
import (
"strings"
"go.ligato.io/cn-infra/v2/db/keyval"
)
// kvPair is used to represent a single K/V entry
type kvPair struct {
// Key is the name of the key.
Key string
// Value is the value for the key.
Value []byte
}
// bytesKeyIterator is an iterator returned by ListKeys call.
type bytesKeyIterator struct {
prefix string
index int
len int
keys []string
}
// bytesKeyValIterator is an iterator returned by ListValues call.
type bytesKeyValIterator struct {
prefix string
index int
len int
pairs []*kvPair
}
// bytesKeyVal represents a single key-value pair.
type bytesKeyVal struct {
key string
value []byte
prevValue []byte
revision int64
}
// GetNext returns the following key (+ revision) from the result set.
// When there are no more keys to get, <stop> is returned as *true*
// and <key> and <rev> are default values.
func (it *bytesKeyIterator) GetNext() (key string, rev int64, stop bool) {
if it.index >= it.len {
return "", 0, true
}
key = it.keys[it.index]
if it.prefix != "" {
key = strings.TrimPrefix(key, it.prefix)
}
it.index++
return key, 0, false
}
// Close does nothing since db cursors are not needed.
// The method is required by the code since it implements Iterator API.
func (it *bytesKeyIterator) Close() error {
return nil
}
// GetNext returns the following item from the result set.
// When there are no more items to get, <stop> is returned as *true* and <val>
// is simply *nil*.
func (it *bytesKeyValIterator) GetNext() (val keyval.BytesKeyVal, stop bool) {
if it.index >= it.len {
return nil, true
}
key := it.pairs[it.index].Key
if it.prefix != "" {
key = strings.TrimPrefix(key, it.prefix)
}
data := it.pairs[it.index].Value
var prevValue []byte
if len(it.pairs) > 0 && it.index > 0 {
prevValue = it.pairs[it.index-1].Value
}
it.index++
return &bytesKeyVal{key, data, prevValue, 0}, false
}
// Close does nothing since db cursors are not needed.
// The method is required by the code since it implements Iterator API.
func (it *bytesKeyValIterator) Close() error {
return nil
}
// Close does nothing since db cursors are not needed.
// The method is required by the code since it implements Iterator API.
func (kv *bytesKeyVal) Close() error {
return nil
}
// GetValue returns the value of the pair.
func (kv *bytesKeyVal) GetValue() []byte {
return kv.value
}
// GetPrevValue returns the previous value of the pair.
func (kv *bytesKeyVal) GetPrevValue() []byte {
return kv.prevValue
}
// GetKey returns the key of the pair.
func (kv *bytesKeyVal) GetKey() string {
return kv.key
}
// GetRevision returns the revision associated with the pair.
func (kv *bytesKeyVal) GetRevision() int64 {
return kv.revision
}