Skip to content

Commit 6d5560c

Browse files
authored
refactor: Remove discard return value (#71)
## Relevant issue(s) Resolves #70 ## Description Removes the discard return value, as no implementations ever return a value and it made calling `Discard` in a `defer` undesirable (requires ignoring potential errors, and adding a `//nolint`).
1 parent cd304e9 commit 6d5560c

5 files changed

Lines changed: 21 additions & 26 deletions

File tree

badger/badger.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,37 @@ func NewDatastoreFrom(db *badger.DB) *Datastore {
4444

4545
func (b *Datastore) Get(ctx context.Context, key []byte) ([]byte, error) {
4646
txn := b.newTxn(true)
47+
defer txn.Discard()
4748

48-
result, err := txn.Get(ctx, key)
49-
dErr := txn.Discard()
50-
51-
return result, errors.Join(err, dErr)
49+
return txn.Get(ctx, key)
5250
}
5351

5452
func (b *Datastore) Has(ctx context.Context, key []byte) (bool, error) {
5553
txn := b.newTxn(true)
54+
defer txn.Discard()
5655

57-
result, err := txn.Has(ctx, key)
58-
dErr := txn.Discard()
59-
60-
return result, errors.Join(err, dErr)
56+
return txn.Has(ctx, key)
6157
}
6258

6359
func (b *Datastore) Set(ctx context.Context, key []byte, value []byte) error {
6460
txn := b.newTxn(false)
61+
defer txn.Discard()
6562

6663
err := txn.Set(ctx, key, value)
6764
if err != nil {
68-
dErr := txn.Discard()
69-
return errors.Join(err, dErr)
65+
return err
7066
}
7167

7268
return txn.Commit()
7369
}
7470

7571
func (b *Datastore) Delete(ctx context.Context, key []byte) error {
7672
txn := b.newTxn(false)
73+
defer txn.Discard()
7774

7875
err := txn.Delete(ctx, key)
7976
if err != nil {
80-
dErr := txn.Discard()
81-
return errors.Join(err, dErr)
77+
return err
8278
}
8379

8480
return txn.Commit()
@@ -104,7 +100,8 @@ func (b *Datastore) Iterator(ctx context.Context, iterOpts corekv.IterOptions) (
104100
// so that the txn is discarded when the
105101
// iterator is closed
106102
it.withCloser(func() error {
107-
return txn.Discard()
103+
txn.Discard()
104+
return nil
108105
})
109106

110107
return it, nil
@@ -183,13 +180,13 @@ func (txn *bTxn) Commit() error {
183180
return badgerErrToKVErr(err)
184181
}
185182

186-
func (txn *bTxn) Discard() error {
183+
func (txn *bTxn) Discard() {
187184
txn.t.Discard()
188-
return nil
189185
}
190186

191187
func (t *bTxn) Close() error {
192-
return t.Discard()
188+
t.Discard()
189+
return nil
193190
}
194191

195192
var badgerErrToKVErrMap = map[error]error{

kv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,5 @@ type Txn interface {
167167

168168
// Discard discards all changes made via this object so far, returning
169169
// it to the state it was at at time of construction.
170-
Discard() error
170+
Discard()
171171
}

memory/txn.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,20 +321,19 @@ func (iter *txnIterator) Close() error {
321321
}
322322

323323
func (t *basicTxn) Close() error {
324-
return t.Discard()
324+
t.Discard()
325+
return nil
325326
}
326327

327328
// Discard removes all the operations added to the transaction.
328-
func (t *basicTxn) Discard() error {
329+
func (t *basicTxn) Discard() {
329330
if t.discarded {
330-
return nil
331+
return
331332
}
332333

333334
t.ops.Clear()
334335
t.clearInFlightTxn()
335336
t.discarded = true
336-
337-
return nil
338337
}
339338

340339
// Commit saves the operations to the underlying datastore.

namespace/namespace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ func (txn *Txn) Commit() error {
181181
return txn.txn.Commit()
182182
}
183183

184-
func (txn *Txn) Discard() error {
185-
return txn.txn.Discard()
184+
func (txn *Txn) Discard() {
185+
txn.txn.Discard()
186186
}
187187

188188
func cp(bz []byte) (ret []byte) {

test/action/txn.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ func DiscardI(id int) *DiscardTxn {
127127
func (a *DiscardTxn) Execute(s *state.State) {
128128
txn := s.Txns[a.ID]
129129

130-
err := txn.Discard()
131-
require.NoError(s.T, err)
130+
txn.Discard()
132131
}
133132

134133
// CommitTxn commits the given transaction when executed.

0 commit comments

Comments
 (0)