forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvcache.go
178 lines (150 loc) · 3.66 KB
/
kvcache.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
package storage
import (
"bytes"
"sort"
)
type KVCache struct {
cache map[string]valueInfo
}
type valueInfo struct {
value []byte
deleted bool
}
// Creates an in-memory cache wrapping a map that stores the provided tombstone value for deleted keys
func NewKVCache() *KVCache {
return &KVCache{
cache: make(map[string]valueInfo),
}
}
func (kvc *KVCache) Info(key []byte) (value []byte, deleted bool) {
vi := kvc.cache[string(key)]
return vi.value, vi.deleted
}
func (kvc *KVCache) Get(key []byte) []byte {
return kvc.cache[string(key)].value
}
func (kvc *KVCache) Has(key []byte) bool {
vi, ok := kvc.cache[string(key)]
return ok && !vi.deleted
}
func (kvc *KVCache) Set(key, value []byte) {
skey := string(key)
vi := kvc.cache[skey]
vi.deleted = false
vi.value = value
kvc.cache[skey] = vi
}
func (kvc *KVCache) Delete(key []byte) {
skey := string(key)
vi := kvc.cache[skey]
vi.deleted = true
vi.value = nil
kvc.cache[skey] = vi
}
func (kvc *KVCache) Iterator(start, end []byte) KVIterator {
return kvc.newIterator(NormaliseDomain(start, end, false))
}
func (kvc *KVCache) ReverseIterator(start, end []byte) KVIterator {
return kvc.newIterator(NormaliseDomain(start, end, true))
}
func (kvc *KVCache) newIterator(start, end []byte) *KVCacheIterator {
kvi := &KVCacheIterator{
start: start,
end: end,
keys: kvc.SortedKeysInDomain(start, end),
cache: kvc.cache,
}
return kvi
}
// Writes contents of cache to backend without flushing the cache
func (kvi *KVCache) WriteTo(writer KVWriter) {
for k, vi := range kvi.cache {
kb := []byte(k)
if vi.deleted {
writer.Delete(kb)
} else {
writer.Set(kb, vi.value)
}
}
}
func (kvc *KVCache) Reset() {
kvc.cache = make(map[string]valueInfo)
}
type KVCacheIterator struct {
cache map[string]valueInfo
start []byte
end []byte
keys [][]byte
index int
}
func (kvi *KVCacheIterator) Domain() ([]byte, []byte) {
return kvi.start, kvi.end
}
func (kvi *KVCacheIterator) Info() (key, value []byte, deleted bool) {
key = kvi.keys[kvi.index]
vi := kvi.cache[string(key)]
return key, vi.value, vi.deleted
}
func (kvi *KVCacheIterator) Key() []byte {
return []byte(kvi.keys[kvi.index])
}
func (kvi *KVCacheIterator) Value() []byte {
return kvi.cache[string(kvi.keys[kvi.index])].value
}
func (kvi *KVCacheIterator) Next() {
if !kvi.Valid() {
panic("KVCacheIterator.Next() called on invalid iterator")
}
kvi.index++
}
func (kvi *KVCacheIterator) Valid() bool {
return kvi.index < len(kvi.keys)
}
func (kvi *KVCacheIterator) Close() {}
type byteSlices [][]byte
func (bss byteSlices) Len() int {
return len(bss)
}
func (bss byteSlices) Less(i, j int) bool {
return bytes.Compare(bss[i], bss[j]) == -1
}
func (bss byteSlices) Swap(i, j int) {
bss[i], bss[j] = bss[j], bss[i]
}
func (kvc *KVCache) SortedKeys(reverse bool) [][]byte {
keys := make(byteSlices, 0, len(kvc.cache))
for k := range kvc.cache {
keys = append(keys, []byte(k))
}
var sortable sort.Interface = keys
if reverse {
sortable = sort.Reverse(keys)
}
sort.Stable(sortable)
return keys
}
func (kvc *KVCache) SortedKeysInDomain(start, end []byte) [][]byte {
comp := CompareKeys(start, end)
if comp == 0 {
return [][]byte{}
}
// Sort keys depending on order of end points
sortedKeys := kvc.SortedKeys(comp == 1)
// Attempt to seek to the first key in the range
startIndex := len(sortedKeys)
for i, key := range sortedKeys {
if CompareKeys(key, start) != comp {
startIndex = i
break
}
}
// Reslice to beginning of range or end if not found
sortedKeys = sortedKeys[startIndex:]
for i, key := range sortedKeys {
if CompareKeys(key, end) != comp {
sortedKeys = sortedKeys[:i]
break
}
}
return sortedKeys
}