Skip to content

Commit 42f360e

Browse files
committed
making it optional to call closer when Get fails
1 parent f69cbcc commit 42f360e

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

storage/operation/badgerimpl/reader.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,21 @@ func (noopCloser) Close() error { return nil }
3030
// safe to modify the contents of the argument after Get returns. The
3131
// returned slice will remain valid until the returned Closer is closed.
3232
// when err == nil, the caller MUST call closer.Close() or a memory leak will occur.
33-
// when err != nil, then the caller io.Closer is nil, and should not be called
3433
func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
3534
tx := b.db.NewTransaction(false)
3635
defer tx.Discard()
3736

3837
item, err := tx.Get(key)
3938
if err != nil {
4039
if errors.Is(err, badger.ErrKeyNotFound) {
41-
return nil, nil, storage.ErrNotFound
40+
return nil, noopCloser{}, storage.ErrNotFound
4241
}
43-
return nil, nil, irrecoverable.NewExceptionf("could not load data: %w", err)
42+
return nil, noopCloser{}, irrecoverable.NewExceptionf("could not load data: %w", err)
4443
}
4544

4645
value, err := item.ValueCopy(nil)
4746
if err != nil {
48-
return nil, nil, irrecoverable.NewExceptionf("could not load value: %w", err)
47+
return nil, noopCloser{}, irrecoverable.NewExceptionf("could not load value: %w", err)
4948
}
5049

5150
return value, noopCloser{}, nil

storage/operation/pebbleimpl/reader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ func (noopCloser) Close() error { return nil }
3232
// safe to modify the contents of the argument after Get returns. The
3333
// returned slice will remain valid until the returned Closer is closed.
3434
// when err == nil, the caller MUST call closer.Close() or a memory leak will occur.
35-
// when err != nil, then the caller io.Closer is nil, and should not be called
3635
func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
3736
value, closer, err := b.db.Get(key)
3837

3938
if err != nil {
4039
if errors.Is(err, pebble.ErrNotFound) {
41-
return nil, nil, storage.ErrNotFound
40+
return nil, noopCloser{}, storage.ErrNotFound
4241
}
4342

4443
// exception while checking for the key
45-
return nil, nil, irrecoverable.NewExceptionf("could not load data: %w", err)
44+
return nil, noopCloser{}, irrecoverable.NewExceptionf("could not load data: %w", err)
4645
}
4746

4847
return value, closer, nil

storage/operations.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ type Reader interface {
7474
// safe to modify the contents of the `key` argument after Get returns. The
7575
// returned slice will remain valid until the returned Closer is closed.
7676
// when err == nil, the caller MUST call closer.Close() or a memory leak will occur.
77-
// when err != nil, then the caller io.Closer is nil, and should not be called
7877
Get(key []byte) (value []byte, closer io.Closer, err error)
7978

8079
// NewIter returns a new Iterator for the given key prefix range [startPrefix, endPrefix], both inclusive.

0 commit comments

Comments
 (0)