forked from cometbft/cometbft-db
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pebble.go
401 lines (347 loc) · 7.53 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
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, false)
}
// PebbleDB is a PebbleDB backend.
type PebbleDB struct {
db *pebble.DB
}
var _ DB = (*PebbleDB)(nil)
func NewPebbleDB(name string, dir string) (DB, 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
}
bytes, err := db.Get(key)
if err != nil {
return false, err
}
return bytes != 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
}
err := db.db.Set(key, value, pebble.NoSync)
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
}
err := db.db.Delete(key, pebble.NoSync)
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
}
// 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 (db *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 := db.db.NewIter(&o)
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 := db.db.NewIter(&o)
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{
batch: db.db.NewBatch(),
}
}
// Set implements Batch.
func (b *pebbleDBBatch) Set(key, value []byte) error {
// fmt.Println("pebbleDBBatch.Set")
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
if b.batch == nil {
return errBatchClosed
}
b.batch.Set(key, value, nil)
return nil
}
// Delete implements Batch.
func (b *pebbleDBBatch) Delete(key []byte) error {
// fmt.Println("pebbleDBBatch.Delete")
if len(key) == 0 {
return errKeyEmpty
}
if b.batch == nil {
return errBatchClosed
}
b.batch.Delete(key, nil)
return nil
}
// Write implements Batch.
func (b *pebbleDBBatch) Write() error {
// fmt.Println("pebbleDBBatch.Write")
if b.batch == nil {
return errBatchClosed
}
err := b.batch.Commit(pebble.NoSync)
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 {
// fmt.Println("pebbleDBBatch.WriteSync")
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 {
// fmt.Println("pebbleDBBatch.Close")
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) {
// fmt.Println("pebbleDBIterator.Domain")
return itr.start, itr.end
}
// Valid implements Iterator.
func (itr *pebbleDBIterator) Valid() bool {
// fmt.Println("pebbleDBIterator.Valid")
// 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.
func (itr *pebbleDBIterator) Key() []byte {
// fmt.Println("pebbleDBIterator.Key")
itr.assertIsValid()
return cp(itr.source.Key())
}
// Value implements Iterator.
func (itr *pebbleDBIterator) Value() []byte {
// fmt.Println("pebbleDBIterator.Value")
itr.assertIsValid()
return cp(itr.source.Value())
}
// Next implements Iterator.
func (itr pebbleDBIterator) Next() {
// fmt.Println("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 {
// fmt.Println("pebbleDBIterator.Close")
err := itr.source.Close()
if err != nil {
return err
}
return nil
}
func (itr *pebbleDBIterator) assertIsValid() {
if !itr.Valid() {
panic("iterator is invalid")
}
}