/
pebble.go
327 lines (249 loc) · 7.83 KB
/
pebble.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package pebble
import (
"errors"
"sync"
"github.com/cockroachdb/pebble"
"go.uber.org/atomic"
"github.com/iotaledger/hive.go/core/byteutils"
"github.com/iotaledger/hive.go/core/kvstore"
"github.com/iotaledger/hive.go/core/kvstore/utils"
"github.com/iotaledger/hive.go/core/types"
)
// pebbleStore implements the KVStore interface around a pebble instance.
type pebbleStore struct {
instance *pebble.DB
closed *atomic.Bool
dbPrefix []byte
}
// New creates a new KVStore with the underlying pebbleDB.
func New(db *pebble.DB) kvstore.KVStore {
return &pebbleStore{
instance: db,
closed: atomic.NewBool(false),
}
}
func (s *pebbleStore) WithRealm(realm kvstore.Realm) (kvstore.KVStore, error) {
if s.closed.Load() {
return nil, kvstore.ErrStoreClosed
}
return &pebbleStore{
instance: s.instance,
closed: s.closed,
dbPrefix: realm,
}, nil
}
func (s *pebbleStore) WithExtendedRealm(realm kvstore.Realm) (kvstore.KVStore, error) {
return s.WithRealm(byteutils.ConcatBytes(s.Realm(), realm))
}
func (s *pebbleStore) Realm() []byte {
return s.dbPrefix
}
// builds a key usable for the pebble instance using the realm and the given prefix.
func (s *pebbleStore) buildKeyPrefix(prefix kvstore.KeyPrefix) kvstore.KeyPrefix {
return byteutils.ConcatBytes(s.dbPrefix, prefix)
}
func (s *pebbleStore) getIterBounds(prefix []byte) ([]byte, []byte) {
start := s.buildKeyPrefix(prefix)
if len(start) == 0 {
// no bounds
return nil, nil
}
return start, utils.KeyPrefixUpperBound(start)
}
// getIterFuncs returns the function pointers for the iteration based on the given settings.
func (s *pebbleStore) getIterFuncs(it *pebble.Iterator, iterDirection ...kvstore.IterDirection) (start func() bool, valid func() bool, move func() bool) {
startFunc := it.First
validFunc := it.Valid
moveFunc := it.Next
if kvstore.GetIterDirection(iterDirection...) == kvstore.IterDirectionBackward {
startFunc = it.Last
moveFunc = it.Prev
}
return startFunc, validFunc, moveFunc
}
// Iterate iterates over all keys and values with the provided prefix. You can pass kvstore.EmptyPrefix to iterate over all keys and values.
// Optionally the direction for the iteration can be passed (default: IterDirectionForward).
func (s *pebbleStore) Iterate(prefix kvstore.KeyPrefix, consumerFunc kvstore.IteratorKeyValueConsumerFunc, iterDirection ...kvstore.IterDirection) error {
if s.closed.Load() {
return kvstore.ErrStoreClosed
}
start, end := s.getIterBounds(prefix)
it := s.instance.NewIter(&pebble.IterOptions{LowerBound: start, UpperBound: end})
defer it.Close()
startFunc, validFunc, moveFunc := s.getIterFuncs(it, iterDirection...)
for startFunc(); validFunc(); moveFunc() {
if !consumerFunc(utils.CopyBytes(it.Key())[len(s.dbPrefix):], utils.CopyBytes(it.Value())) {
break
}
}
return nil
}
// IterateKeys iterates over all keys with the provided prefix. You can pass kvstore.EmptyPrefix to iterate over all keys.
// Optionally the direction for the iteration can be passed (default: IterDirectionForward).
func (s *pebbleStore) IterateKeys(prefix kvstore.KeyPrefix, consumerFunc kvstore.IteratorKeyConsumerFunc, iterDirection ...kvstore.IterDirection) error {
if s.closed.Load() {
return kvstore.ErrStoreClosed
}
start, end := s.getIterBounds(prefix)
it := s.instance.NewIter(&pebble.IterOptions{LowerBound: start, UpperBound: end})
defer it.Close()
startFunc, validFunc, moveFunc := s.getIterFuncs(it, iterDirection...)
for startFunc(); validFunc(); moveFunc() {
if !consumerFunc(utils.CopyBytes(it.Key())[len(s.dbPrefix):]) {
break
}
}
return nil
}
func (s *pebbleStore) Clear() error {
if s.closed.Load() {
return kvstore.ErrStoreClosed
}
return s.DeletePrefix(kvstore.EmptyPrefix)
}
func (s *pebbleStore) Get(key kvstore.Key) (kvstore.Value, error) {
if s.closed.Load() {
return nil, kvstore.ErrStoreClosed
}
val, closer, err := s.instance.Get(byteutils.ConcatBytes(s.dbPrefix, key))
if err != nil {
if errors.Is(err, pebble.ErrNotFound) {
return nil, kvstore.ErrKeyNotFound
}
return nil, err
}
value := utils.CopyBytes(val)
if err := closer.Close(); err != nil {
return nil, err
}
return value, nil
}
func (s *pebbleStore) Set(key kvstore.Key, value kvstore.Value) error {
if s.closed.Load() {
return kvstore.ErrStoreClosed
}
return s.instance.Set(byteutils.ConcatBytes(s.dbPrefix, key), value, pebble.NoSync)
}
func (s *pebbleStore) Has(key kvstore.Key) (bool, error) {
if s.closed.Load() {
return false, kvstore.ErrStoreClosed
}
_, closer, err := s.instance.Get(byteutils.ConcatBytes(s.dbPrefix, key))
if errors.Is(err, pebble.ErrNotFound) {
return false, nil
}
if err != nil {
return false, err
}
if err := closer.Close(); err != nil {
return true, err
}
return true, nil
}
func (s *pebbleStore) Delete(key kvstore.Key) error {
if s.closed.Load() {
return kvstore.ErrStoreClosed
}
return s.instance.Delete(byteutils.ConcatBytes(s.dbPrefix, key), pebble.NoSync)
}
func (s *pebbleStore) DeletePrefix(prefix kvstore.KeyPrefix) error {
if s.closed.Load() {
return kvstore.ErrStoreClosed
}
start, end := s.getIterBounds(prefix)
if start == nil {
// DeleteRange does not work without range, so we have to iterate over all keys and delete them
it := s.instance.NewIter(&pebble.IterOptions{LowerBound: start, UpperBound: end})
defer it.Close()
b := s.instance.NewBatch()
for it.First(); it.Valid(); it.Next() {
if err := b.Delete(it.Key(), nil); err != nil {
b.Close()
return err
}
}
return b.Commit(pebble.NoSync)
}
return s.instance.DeleteRange(start, end, pebble.NoSync)
}
func (s *pebbleStore) Flush() error {
if s.closed.Load() {
return kvstore.ErrStoreClosed
}
return s.instance.Flush()
}
func (s *pebbleStore) Close() error {
if s.closed.Swap(true) {
// was already closed
return nil
}
return s.instance.Close()
}
func (s *pebbleStore) Batched() (kvstore.BatchedMutations, error) {
if s.closed.Load() {
return nil, kvstore.ErrStoreClosed
}
return &batchedMutations{
kvStore: s,
store: s.instance,
dbPrefix: s.dbPrefix,
setOperations: make(map[string]kvstore.Value),
deleteOperations: make(map[string]types.Empty),
closed: s.closed,
}, nil
}
// batchedMutations is a wrapper around a WriteBatch of a pebbleDB.
type batchedMutations struct {
kvStore *pebbleStore
store *pebble.DB
dbPrefix []byte
setOperations map[string]kvstore.Value
deleteOperations map[string]types.Empty
operationsMutex sync.Mutex
closed *atomic.Bool
}
func (b *batchedMutations) Set(key kvstore.Key, value kvstore.Value) error {
stringKey := byteutils.ConcatBytesToString(b.dbPrefix, key)
b.operationsMutex.Lock()
defer b.operationsMutex.Unlock()
delete(b.deleteOperations, stringKey)
b.setOperations[stringKey] = value
return nil
}
func (b *batchedMutations) Delete(key kvstore.Key) error {
stringKey := byteutils.ConcatBytesToString(b.dbPrefix, key)
b.operationsMutex.Lock()
defer b.operationsMutex.Unlock()
delete(b.setOperations, stringKey)
b.deleteOperations[stringKey] = types.Void
return nil
}
func (b *batchedMutations) Cancel() {
b.operationsMutex.Lock()
defer b.operationsMutex.Unlock()
b.setOperations = make(map[string]kvstore.Value)
b.deleteOperations = make(map[string]types.Empty)
}
func (b *batchedMutations) Commit() error {
if b.closed.Load() {
return kvstore.ErrStoreClosed
}
writeBatch := b.store.NewBatch()
b.operationsMutex.Lock()
defer b.operationsMutex.Unlock()
for key, value := range b.setOperations {
err := writeBatch.Set([]byte(key), value, nil)
if err != nil {
return err
}
}
for key := range b.deleteOperations {
err := writeBatch.Delete([]byte(key), nil)
if err != nil {
return err
}
}
return writeBatch.Commit(pebble.NoSync)
}
var _ kvstore.KVStore = &pebbleStore{}
var _ kvstore.BatchedMutations = &batchedMutations{}