diff --git a/storage/operation/badgerimpl/dbstore.go b/storage/operation/badgerimpl/dbstore.go index b8460165e32..bbe5e274d4f 100644 --- a/storage/operation/badgerimpl/dbstore.go +++ b/storage/operation/badgerimpl/dbstore.go @@ -21,3 +21,7 @@ func (b *dbStore) Reader() storage.Reader { func (b *dbStore) WithReaderBatchWriter(fn func(storage.ReaderBatchWriter) error) error { return WithReaderBatchWriter(b.db, fn) } + +func (b *dbStore) NewBatch() storage.Batch { + return NewReaderBatchWriter(b.db) +} diff --git a/storage/operation/pebbleimpl/dbstore.go b/storage/operation/pebbleimpl/dbstore.go index fcc5b14a06a..c7362681f24 100644 --- a/storage/operation/pebbleimpl/dbstore.go +++ b/storage/operation/pebbleimpl/dbstore.go @@ -21,3 +21,7 @@ func (b *dbStore) Reader() storage.Reader { func (b *dbStore) WithReaderBatchWriter(fn func(storage.ReaderBatchWriter) error) error { return WithReaderBatchWriter(b.db, fn) } + +func (b *dbStore) NewBatch() storage.Batch { + return NewReaderBatchWriter(b.db) +} diff --git a/storage/operations.go b/storage/operations.go index 95e25fcfa9d..b36d230103e 100644 --- a/storage/operations.go +++ b/storage/operations.go @@ -104,6 +104,19 @@ type DB interface { // atomic batch updates to the database. // Any error returned are considered fatal and the batch is not committed. WithReaderBatchWriter(func(ReaderBatchWriter) error) error + + // NewBatch create a new batch for writing. + NewBatch() Batch +} + +// Batch is an interface for a batch of writes to a storage backend. +// The batch is pending until it is committed. +// Useful for dynamically adding writes to the batch +type Batch interface { + ReaderBatchWriter + + // Commit applies the batched updates to the database. + Commit() error } // OnlyWriter is an adapter to convert a function that takes a Writer