forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvcache.go
223 lines (194 loc) · 4.84 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
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
package storage
import (
"bytes"
"sort"
"sync"
)
type KVCache struct {
sync.RWMutex
cache map[string]valueInfo
// Store a sortable slice of keys to avoid always hitting
keys byteSlices
}
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]
}
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) {
kvc.RLock()
defer kvc.RUnlock()
vi := kvc.cache[string(key)]
return vi.value, vi.deleted
}
func (kvc *KVCache) Get(key []byte) []byte {
kvc.RLock()
defer kvc.RUnlock()
return kvc.cache[string(key)].value
}
func (kvc *KVCache) Has(key []byte) bool {
kvc.RLock()
defer kvc.RUnlock()
vi, ok := kvc.cache[string(key)]
return ok && !vi.deleted
}
func (kvc *KVCache) Set(key, value []byte) {
kvc.Lock()
defer kvc.Unlock()
skey := string(key)
vi, ok := kvc.cache[skey]
if !ok {
// first Set/Delete
kvc.keys = append(kvc.keys, key)
// This slows down write quite a lot but does give faster repeated iterations
// kvc.keys = insertKey(kvc.keys, key)
}
vi.deleted = false
vi.value = value
kvc.cache[skey] = vi
}
func (kvc *KVCache) Delete(key []byte) {
kvc.Lock()
defer kvc.Unlock()
skey := string(key)
vi, ok := kvc.cache[skey]
if !ok {
// first Set/Delete
kvc.keys = append(kvc.keys, key)
// This slows down write quite a lot but does give faster repeated iterations
// kvc.keys = insertKey(kvc.keys, key)
}
vi.deleted = true
vi.value = nil
kvc.cache[skey] = vi
}
func (kvc *KVCache) Iterator(low, high []byte) KVIterator {
kvc.RLock()
defer kvc.RUnlock()
low, high = NormaliseDomain(low, high)
return kvc.newIterator(low, high, false)
}
func (kvc *KVCache) ReverseIterator(low, high []byte) KVIterator {
kvc.RLock()
defer kvc.RUnlock()
low, high = NormaliseDomain(low, high)
return kvc.newIterator(low, high, true)
}
// Writes contents of cache to backend without flushing the cache
func (kvc *KVCache) WriteTo(writer KVWriter) {
kvc.Lock()
defer kvc.Unlock()
for k, vi := range kvc.cache {
kb := []byte(k)
if vi.deleted {
writer.Delete(kb)
} else {
writer.Set(kb, vi.value)
}
}
}
func (kvc *KVCache) Reset() {
kvc.Lock()
defer kvc.Unlock()
kvc.cache = make(map[string]valueInfo)
}
func (kvc *KVCache) sortedKeysInDomain(low, high []byte) [][]byte {
// Sort keys (which may be partially sorted if we have iterated before)
sort.Sort(kvc.keys)
sortedKeys := kvc.keys
// Attempt to seek to the first key in the range
startIndex := len(kvc.keys)
for i, key := range sortedKeys {
// !(key < start) => key >= start then include (inclusive start)
if CompareKeys(key, low) != -1 {
startIndex = i
break
}
}
// Reslice to beginning of range or end if not found
sortedKeys = sortedKeys[startIndex:]
for i, key := range sortedKeys {
// !(key < end) => key >= end then exclude (exclusive end)
if CompareKeys(key, high) != -1 {
sortedKeys = sortedKeys[:i]
break
}
}
return sortedKeys
}
func (kvc *KVCache) newIterator(start, end []byte, reverse bool) *KVCacheIterator {
keys := kvc.sortedKeysInDomain(start, end)
kvi := &KVCacheIterator{
start: start,
end: end,
keys: keys,
cache: kvc.cache,
reverse: reverse,
}
return kvi
}
type KVCacheIterator struct {
cache map[string]valueInfo
start []byte
end []byte
keys [][]byte
keyIndex int
reverse bool
}
func (kvi *KVCacheIterator) Domain() ([]byte, []byte) {
return kvi.start, kvi.end
}
func (kvi *KVCacheIterator) Info() (key, value []byte, deleted bool) {
key = kvi.keys[kvi.sliceIndex()]
vi := kvi.cache[string(key)]
return key, vi.value, vi.deleted
}
func (kvi *KVCacheIterator) Key() []byte {
return []byte(kvi.keys[kvi.sliceIndex()])
}
func (kvi *KVCacheIterator) Value() []byte {
return kvi.cache[string(kvi.keys[kvi.sliceIndex()])].value
}
func (kvi *KVCacheIterator) Next() {
if !kvi.Valid() {
panic("KVCacheIterator.Next() called on invalid iterator")
}
kvi.keyIndex++
}
func (kvi *KVCacheIterator) Valid() bool {
return kvi.keyIndex < len(kvi.keys)
}
func (kvi *KVCacheIterator) Close() {}
func (kvi *KVCacheIterator) sliceIndex() int {
if kvi.reverse {
//reflect
return len(kvi.keys) - 1 - kvi.keyIndex
}
return kvi.keyIndex
}
func insertKey(sortedKeys [][]byte, key []byte) [][]byte {
i := sort.Search(len(sortedKeys), func(i int) bool {
// Smallest sortedKey such that key
return bytes.Compare(sortedKeys[i], key) > -1
})
// ensure space
sortedKeys = append(sortedKeys, nil)
copy(sortedKeys[i+1:], sortedKeys[i:])
sortedKeys[i] = key
return sortedKeys
}