|
| 1 | +package badgerimpl |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/dgraph-io/badger/v2" |
| 7 | + |
| 8 | + "github.com/onflow/flow-go/storage" |
| 9 | + "github.com/onflow/flow-go/storage/operation" |
| 10 | + op "github.com/onflow/flow-go/storage/operation" |
| 11 | +) |
| 12 | + |
| 13 | +type ReaderBatchWriter struct { |
| 14 | + globalReader storage.Reader |
| 15 | + batch *badger.WriteBatch |
| 16 | + |
| 17 | + callbacks op.Callbacks |
| 18 | +} |
| 19 | + |
| 20 | +var _ storage.ReaderBatchWriter = (*ReaderBatchWriter)(nil) |
| 21 | + |
| 22 | +// GlobalReader returns a database-backed reader which reads the latest committed global database state ("read-committed isolation"). |
| 23 | +// This reader will not read un-committed writes written to ReaderBatchWriter.Writer until the write batch is committed. |
| 24 | +// This reader may observe different values for the same key on subsequent reads. |
| 25 | +func (b *ReaderBatchWriter) GlobalReader() storage.Reader { |
| 26 | + return b.globalReader |
| 27 | +} |
| 28 | + |
| 29 | +// Writer returns a writer associated with a batch of writes. The batch is pending until it is committed. |
| 30 | +// When we `Write` into the batch, that write operation is added to the pending batch, but not committed. |
| 31 | +// The commit operation is atomic w.r.t. the batch; either all writes are applied to the database, or no writes are. |
| 32 | +// Note: |
| 33 | +// - The writer cannot be used concurrently for writing. |
| 34 | +func (b *ReaderBatchWriter) Writer() storage.Writer { |
| 35 | + return b |
| 36 | +} |
| 37 | + |
| 38 | +// BadgerWriteBatch returns the badger write batch |
| 39 | +func (b *ReaderBatchWriter) BadgerWriteBatch() *badger.WriteBatch { |
| 40 | + return b.batch |
| 41 | +} |
| 42 | + |
| 43 | +// AddCallback adds a callback to execute after the batch has been flush |
| 44 | +// regardless the batch update is succeeded or failed. |
| 45 | +// The error parameter is the error returned by the batch update. |
| 46 | +func (b *ReaderBatchWriter) AddCallback(callback func(error)) { |
| 47 | + b.callbacks.AddCallback(callback) |
| 48 | +} |
| 49 | + |
| 50 | +// Commit flushes the batch to the database. |
| 51 | +// No errors expected during normal operation |
| 52 | +func (b *ReaderBatchWriter) Commit() error { |
| 53 | + err := b.batch.Flush() |
| 54 | + |
| 55 | + b.callbacks.NotifyCallbacks(err) |
| 56 | + |
| 57 | + return err |
| 58 | +} |
| 59 | + |
| 60 | +func WithReaderBatchWriter(db *badger.DB, fn func(storage.ReaderBatchWriter) error) error { |
| 61 | + batch := NewReaderBatchWriter(db) |
| 62 | + |
| 63 | + err := fn(batch) |
| 64 | + if err != nil { |
| 65 | + // fn might use lock to ensure concurrent safety while reading and writing data |
| 66 | + // and the lock is usually released by a callback. |
| 67 | + // in other words, fn might hold a lock to be released by a callback, |
| 68 | + // we need to notify the callback for the locks to be released before |
| 69 | + // returning the error. |
| 70 | + batch.callbacks.NotifyCallbacks(err) |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + return batch.Commit() |
| 75 | +} |
| 76 | + |
| 77 | +func NewReaderBatchWriter(db *badger.DB) *ReaderBatchWriter { |
| 78 | + return &ReaderBatchWriter{ |
| 79 | + globalReader: ToReader(db), |
| 80 | + batch: db.NewWriteBatch(), |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +var _ storage.Writer = (*ReaderBatchWriter)(nil) |
| 85 | + |
| 86 | +// Set sets the value for the given key. It overwrites any previous value |
| 87 | +// for that key; a DB is not a multi-map. |
| 88 | +// |
| 89 | +// It is safe to modify the contents of the arguments after Set returns. |
| 90 | +// No errors expected during normal operation |
| 91 | +func (b *ReaderBatchWriter) Set(key, value []byte) error { |
| 92 | + return b.batch.Set(key, value) |
| 93 | +} |
| 94 | + |
| 95 | +// Delete deletes the value for the given key. Deletes are blind all will |
| 96 | +// succeed even if the given key does not exist. |
| 97 | +// |
| 98 | +// It is safe to modify the contents of the arguments after Delete returns. |
| 99 | +// No errors expected during normal operation |
| 100 | +func (b *ReaderBatchWriter) Delete(key []byte) error { |
| 101 | + return b.batch.Delete(key) |
| 102 | +} |
| 103 | + |
| 104 | +// DeleteByRange removes all keys with a prefix that falls within the |
| 105 | +// range [start, end], both inclusive. |
| 106 | +// It returns error if endPrefix < startPrefix |
| 107 | +// no other errors are expected during normal operation |
| 108 | +func (b *ReaderBatchWriter) DeleteByRange(globalReader storage.Reader, startPrefix, endPrefix []byte) error { |
| 109 | + err := operation.Iterate(startPrefix, endPrefix, func(key []byte) error { |
| 110 | + err := b.batch.Delete(key) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("could not add key to delete batch (%v): %w", key, err) |
| 113 | + } |
| 114 | + return nil |
| 115 | + })(globalReader) |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("could not find keys by range to be deleted: %w", err) |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
0 commit comments