Skip to content

Commit

Permalink
refactor: remove unnecessary/repeated/pedantic code in BatchWithFlush…
Browse files Browse the repository at this point in the history
…er (backport #846) (#847)

Co-authored-by: Emmanuel T Odeke <emmanuel@orijtech.com>
  • Loading branch information
mergify[bot] and odeke-em authored Oct 25, 2023
1 parent d5b3b1e commit 72911cb
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type BatchWithFlusher struct {
flushThreshold int // The threshold to flush the batch to disk.
}

var _ dbm.Batch = &BatchWithFlusher{}
var _ dbm.Batch = (*BatchWithFlusher)(nil)

// NewBatchWithFlusher returns new BatchWithFlusher wrapping the passed in batch
func NewBatchWithFlusher(db dbm.DB, flushThreshold int) *BatchWithFlusher {
Expand Down Expand Up @@ -45,27 +45,21 @@ func (b *BatchWithFlusher) estimateSizeAfterSetting(key []byte, value []byte) (i
// If the set causes the underlying batch size to exceed flushThreshold,
// the batch is flushed to disk, cleared, and a new one is created with buffer pre-allocated to threshold.
// The addition entry is then added to the batch.
func (b *BatchWithFlusher) Set(key []byte, value []byte) error {
func (b *BatchWithFlusher) Set(key, value []byte) error {
batchSizeAfter, err := b.estimateSizeAfterSetting(key, value)
if err != nil {
return err
}
if batchSizeAfter > b.flushThreshold {
err = b.batch.Write()
if err != nil {
if err := b.batch.Write(); err != nil {
return err
}
err = b.batch.Close()
if err != nil {
if err := b.batch.Close(); err != nil {
return err
}
b.batch = b.db.NewBatchWithSize(b.flushThreshold)
}
err = b.batch.Set(key, value)
if err != nil {
return err
}
return nil
return b.batch.Set(key, value)
}

// Delete delete value at the given key to the db.
Expand All @@ -78,21 +72,15 @@ func (b *BatchWithFlusher) Delete(key []byte) error {
return err
}
if batchSizeAfter > b.flushThreshold {
err = b.batch.Write()
if err != nil {
if err := b.batch.Write(); err != nil {
return err
}
err = b.batch.Close()
if err != nil {
if err := b.batch.Close(); err != nil {
return err
}
b.batch = b.db.NewBatchWithSize(b.flushThreshold)
}
err = b.batch.Delete(key)
if err != nil {
return err
}
return nil
return b.batch.Delete(key)
}

func (b *BatchWithFlusher) Write() error {
Expand Down

0 comments on commit 72911cb

Please sign in to comment.