Skip to content

Commit 72911cb

Browse files
refactor: remove unnecessary/repeated/pedantic code in BatchWithFlusher (backport #846) (#847)
Co-authored-by: Emmanuel T Odeke <[email protected]>
1 parent d5b3b1e commit 72911cb

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

batch.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type BatchWithFlusher struct {
1414
flushThreshold int // The threshold to flush the batch to disk.
1515
}
1616

17-
var _ dbm.Batch = &BatchWithFlusher{}
17+
var _ dbm.Batch = (*BatchWithFlusher)(nil)
1818

1919
// NewBatchWithFlusher returns new BatchWithFlusher wrapping the passed in batch
2020
func NewBatchWithFlusher(db dbm.DB, flushThreshold int) *BatchWithFlusher {
@@ -45,27 +45,21 @@ func (b *BatchWithFlusher) estimateSizeAfterSetting(key []byte, value []byte) (i
4545
// If the set causes the underlying batch size to exceed flushThreshold,
4646
// the batch is flushed to disk, cleared, and a new one is created with buffer pre-allocated to threshold.
4747
// The addition entry is then added to the batch.
48-
func (b *BatchWithFlusher) Set(key []byte, value []byte) error {
48+
func (b *BatchWithFlusher) Set(key, value []byte) error {
4949
batchSizeAfter, err := b.estimateSizeAfterSetting(key, value)
5050
if err != nil {
5151
return err
5252
}
5353
if batchSizeAfter > b.flushThreshold {
54-
err = b.batch.Write()
55-
if err != nil {
54+
if err := b.batch.Write(); err != nil {
5655
return err
5756
}
58-
err = b.batch.Close()
59-
if err != nil {
57+
if err := b.batch.Close(); err != nil {
6058
return err
6159
}
6260
b.batch = b.db.NewBatchWithSize(b.flushThreshold)
6361
}
64-
err = b.batch.Set(key, value)
65-
if err != nil {
66-
return err
67-
}
68-
return nil
62+
return b.batch.Set(key, value)
6963
}
7064

7165
// Delete delete value at the given key to the db.
@@ -78,21 +72,15 @@ func (b *BatchWithFlusher) Delete(key []byte) error {
7872
return err
7973
}
8074
if batchSizeAfter > b.flushThreshold {
81-
err = b.batch.Write()
82-
if err != nil {
75+
if err := b.batch.Write(); err != nil {
8376
return err
8477
}
85-
err = b.batch.Close()
86-
if err != nil {
78+
if err := b.batch.Close(); err != nil {
8779
return err
8880
}
8981
b.batch = b.db.NewBatchWithSize(b.flushThreshold)
9082
}
91-
err = b.batch.Delete(key)
92-
if err != nil {
93-
return err
94-
}
95-
return nil
83+
return b.batch.Delete(key)
9684
}
9785

9886
func (b *BatchWithFlusher) Write() error {

0 commit comments

Comments
 (0)