forked from ligato/cn-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
83 lines (65 loc) · 1.5 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
package filedb
import (
"strings"
"go.ligato.io/cn-infra/v2/db/keyval/filedb/decoder"
"go.ligato.io/cn-infra/v2/db/keyval"
)
// File system DB BytesKeyValIterator implementation
type bytesKeyValIterator struct {
index int
len int
rev int
data []*decoder.FileDataEntry
}
// File system DB BytesKeyIterator implementation
type bytesKeyIterator struct {
index int
len int
keys []string
prefix string
}
// File system DB BytesKeyVal implementation
type bytesKeyVal struct {
key string
value []byte
prevValue []byte
}
func (it *bytesKeyValIterator) GetNext() (val keyval.BytesKeyVal, stop bool) {
if it.index >= it.len {
return nil, true
}
key := it.data[it.index].Key
data := it.data[it.index].Value
var prevValue []byte
if len(it.data) > 0 && it.index > 0 {
prevValue = it.data[it.index-1].Value
}
it.index++
return &bytesKeyVal{key, data, prevValue}, false
}
func (it *bytesKeyIterator) GetNext() (key string, rev int64, stop bool) {
if it.index >= it.len {
return "", 0, true
}
key = string(it.keys[it.index])
if !strings.HasPrefix(key, "/") && strings.HasPrefix(it.prefix, "/") {
key = "/" + key
}
if it.prefix != "" {
key = strings.TrimPrefix(key, it.prefix)
}
it.index++
return key, rev, false
}
func (kv *bytesKeyVal) GetValue() []byte {
return kv.value
}
func (kv *bytesKeyVal) GetPrevValue() []byte {
return kv.prevValue
}
func (kv *bytesKeyVal) GetKey() string {
return kv.key
}
func (kv *bytesKeyVal) GetRevision() (rev int64) {
return 0
}