forked from cometbft/cometbft-db
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pebble.go
442 lines (381 loc) · 8.48 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//go:build pebbledb
// +build pebbledb
package db
import (
"bytes"
"fmt"
"path/filepath"
"github.com/cockroachdb/pebble"
)
func init() {
dbCreator := func(name string, dir string) (DB, error) {
return NewPebbleDB(name, dir)
}
registerDBCreator(PebbleDBBackend, dbCreator)
}
// PebbleDB is a PebbleDB backend.
type PebbleDB struct {
db *pebble.DB
}
var _ DB = (*PebbleDB)(nil)
func NewPebbleDB(name string, dir string) (*PebbleDB, error) {
opts := &pebble.Options{}
opts.EnsureDefaults()
return NewPebbleDBWithOpts(name, dir, opts)
}
func NewPebbleDBWithOpts(name string, dir string, opts *pebble.Options) (*PebbleDB, error) {
dbPath := filepath.Join(dir, name+".db")
opts.EnsureDefaults()
p, err := pebble.Open(dbPath, opts)
if err != nil {
return nil, err
}
return &PebbleDB{
db: p,
}, err
}
// Get implements DB.
func (db *PebbleDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errKeyEmpty
}
res, closer, err := db.db.Get(key)
if err != nil {
if err == pebble.ErrNotFound {
return nil, nil
}
return nil, err
}
defer closer.Close()
return cp(res), nil
}
// Has implements DB.
func (db *PebbleDB) Has(key []byte) (bool, error) {
if len(key) == 0 {
return false, errKeyEmpty
}
bytesPeb, err := db.Get(key)
if err != nil {
return false, err
}
return bytesPeb != nil, nil
}
// Set implements DB.
func (db *PebbleDB) Set(key []byte, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
wopts := pebble.NoSync
err := db.db.Set(key, value, wopts)
if err != nil {
return err
}
return nil
}
// SetSync implements DB.
func (db *PebbleDB) SetSync(key []byte, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
err := db.db.Set(key, value, pebble.Sync)
if err != nil {
return err
}
return nil
}
// Delete implements DB.
func (db *PebbleDB) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
wopts := pebble.NoSync
err := db.db.Delete(key, wopts)
if err != nil {
return err
}
return nil
}
// DeleteSync implements DB.
func (db PebbleDB) DeleteSync(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
err := db.db.Delete(key, pebble.Sync)
if err != nil {
return nil
}
return nil
}
func (db *PebbleDB) DB() *pebble.DB {
return db.db
}
func (db *PebbleDB) Compact(start, end []byte) (err error) {
// Currently nil,nil is an invalid range in Pebble.
// This was taken from https://github.com/cockroachdb/pebble/issues/1474
// In case the start and end keys are the same
// pebbleDB will throw an error that it cannot compact.
if start != nil && end != nil {
return db.db.Compact(start, end, true)
}
iter, err := db.db.NewIter(nil)
if err != nil {
return err
}
defer func() {
err2 := iter.Close()
if err2 != nil {
err = err2
}
}()
if start == nil && iter.First() {
start = append(start, iter.Key()...)
}
if end == nil && iter.Last() {
end = append(end, iter.Key()...)
}
err = db.db.Compact(start, end, true)
return
}
// Close implements DB.
func (db PebbleDB) Close() error {
db.db.Close()
return nil
}
// Print implements DB.
func (db *PebbleDB) Print() error {
itr, err := db.Iterator(nil, nil)
if err != nil {
return err
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
key := itr.Key()
value := itr.Value()
fmt.Printf("[%X]:\t[%X]\n", key, value)
}
return nil
}
// Stats implements DB.
func (*PebbleDB) Stats() map[string]string {
return nil
}
// NewBatch implements DB.
func (db *PebbleDB) NewBatch() Batch {
return newPebbleDBBatch(db)
}
// Iterator implements DB.
func (db *PebbleDB) Iterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
o := pebble.IterOptions{
LowerBound: start,
UpperBound: end,
}
itr, err := db.db.NewIter(&o)
if err != nil {
return nil, err
}
itr.First()
return newPebbleDBIterator(itr, start, end, false), nil
}
// ReverseIterator implements DB.
func (db *PebbleDB) ReverseIterator(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
o := pebble.IterOptions{
LowerBound: start,
UpperBound: end,
}
itr, err := db.db.NewIter(&o)
if err != nil {
return nil, err
}
itr.Last()
return newPebbleDBIterator(itr, start, end, true), nil
}
var _ Batch = (*pebbleDBBatch)(nil)
type pebbleDBBatch struct {
db *PebbleDB
batch *pebble.Batch
}
var _ Batch = (*pebbleDBBatch)(nil)
func newPebbleDBBatch(db *PebbleDB) *pebbleDBBatch {
return &pebbleDBBatch{
// For regular batch operations batch.db is going to be set to db
// and it is not needed to initialize the DB here.
// This is set to enable general DB operations like compaction
// (e.x. a call do pebbleDBBatch.db.Compact() would throw a nil pointer exception)
db: db,
batch: db.db.NewBatch(),
}
}
// Set implements Batch.
func (b *pebbleDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
if b.batch == nil {
return errBatchClosed
}
return b.batch.Set(key, value, nil)
}
// Delete implements Batch.
func (b *pebbleDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return errKeyEmpty
}
if b.batch == nil {
return errBatchClosed
}
return b.batch.Delete(key, nil)
}
// Write implements Batch.
func (b *pebbleDBBatch) Write() error {
if b.batch == nil {
return errBatchClosed
}
wopts := pebble.NoSync
err := b.batch.Commit(wopts)
if err != nil {
return err
}
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
return b.Close()
}
// WriteSync implements Batch.
func (b *pebbleDBBatch) WriteSync() error {
if b.batch == nil {
return errBatchClosed
}
err := b.batch.Commit(pebble.Sync)
if err != nil {
return err
}
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
return b.Close()
}
// Close implements Batch.
func (b *pebbleDBBatch) Close() error {
if b.batch != nil {
err := b.batch.Close()
if err != nil {
return err
}
b.batch = nil
}
return nil
}
type pebbleDBIterator struct {
source *pebble.Iterator
start, end []byte
isReverse bool
isInvalid bool
}
var _ Iterator = (*pebbleDBIterator)(nil)
func newPebbleDBIterator(source *pebble.Iterator, start, end []byte, isReverse bool) *pebbleDBIterator {
if isReverse {
if end == nil {
source.Last()
}
} else {
if start == nil {
source.First()
}
}
return &pebbleDBIterator{
source: source,
start: start,
end: end,
isReverse: isReverse,
isInvalid: false,
}
}
// Domain implements Iterator.
func (itr *pebbleDBIterator) Domain() ([]byte, []byte) {
return itr.start, itr.end
}
// Valid implements Iterator.
func (itr *pebbleDBIterator) Valid() bool {
// Once invalid, forever invalid.
if itr.isInvalid {
return false
}
// If source has error, invalid.
if err := itr.source.Error(); err != nil {
itr.isInvalid = true
return false
}
// If source is invalid, invalid.
if !itr.source.Valid() {
itr.isInvalid = true
return false
}
// If key is end or past it, invalid.
start := itr.start
end := itr.end
key := itr.source.Key()
if itr.isReverse {
if start != nil && bytes.Compare(key, start) < 0 {
itr.isInvalid = true
return false
}
} else {
if end != nil && bytes.Compare(end, key) <= 0 {
itr.isInvalid = true
return false
}
}
// It's valid.
return true
}
// Key implements Iterator.
// The caller should not modify the contents of the returned slice.
// Instead, the caller should make a copy and work on the copy.
func (itr *pebbleDBIterator) Key() []byte {
itr.assertIsValid()
return itr.source.Key()
}
// Value implements Iterator.
// The caller should not modify the contents of the returned slice.
// Instead, the caller should make a copy and work on the copy.
func (itr *pebbleDBIterator) Value() []byte {
itr.assertIsValid()
return itr.source.Value()
}
// Next implements Iterator.
func (itr pebbleDBIterator) Next() {
itr.assertIsValid()
if itr.isReverse {
itr.source.Prev()
} else {
itr.source.Next()
}
}
// Error implements Iterator.
func (itr *pebbleDBIterator) Error() error {
return itr.source.Error()
}
// Close implements Iterator.
func (itr *pebbleDBIterator) Close() error {
err := itr.source.Close()
if err != nil {
return err
}
return nil
}
func (itr *pebbleDBIterator) assertIsValid() {
if !itr.Valid() {
panic("iterator is invalid")
}
}