Skip to content

Commit f69cbcc

Browse files
committed
update comments for Get method
1 parent dac89f2 commit f69cbcc

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

storage/operation/badgerimpl/reader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ func (noopCloser) Close() error { return nil }
2828
//
2929
// The caller should not modify the contents of the returned slice, but it is
3030
// safe to modify the contents of the argument after Get returns. The
31-
// returned slice will remain valid until the returned Closer is closed. On
32-
// success, the caller MUST call closer.Close() or a memory leak will occur.
31+
// returned slice will remain valid until the returned Closer is closed.
32+
// 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
3334
func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
3435
tx := b.db.NewTransaction(false)
3536
defer tx.Discard()

storage/operation/pebbleimpl/reader.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ func (noopCloser) Close() error { return nil }
3030
//
3131
// The caller should not modify the contents of the returned slice, but it is
3232
// safe to modify the contents of the argument after Get returns. The
33-
// returned slice will remain valid until the returned Closer is closed. On
34-
// success, the caller MUST call closer.Close() or a memory leak will occur.
33+
// returned slice will remain valid until the returned Closer is closed.
34+
// 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
3536
func (b dbReader) Get(key []byte) ([]byte, io.Closer, error) {
3637
value, closer, err := b.db.Get(key)
3738

storage/operation/reads.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,39 @@ func Traverse(prefix []byte, iterFunc IterationFunc, opt storage.IteratorOption)
121121
return Iterate(prefix, prefix, iterFunc, opt)
122122
}
123123

124-
// Exists returns true if a key exists in the database.
124+
// Exists takes a key and a pointer to an a boolean variable `keyExists` as inputs and returns an function.
125+
// When this returned function is executed (and only then), it will write into the `keyExists` whether
126+
// the key exists.
125127
// No errors are expected during normal operation.
126128
func Exists(key []byte, keyExists *bool) func(storage.Reader) error {
127129
return func(r storage.Reader) error {
128-
_, closer, err := r.Get(key)
130+
exists, err := KeyExists(r, key)
129131
if err != nil {
130-
// the key does not exist in the database
131-
if errors.Is(err, storage.ErrNotFound) {
132-
*keyExists = false
133-
return nil
134-
}
135-
// exception while checking for the key
136-
return irrecoverable.NewExceptionf("could not load data: %w", err)
132+
return err
137133
}
138-
defer closer.Close()
139-
140-
// the key does exist in the database
141-
*keyExists = true
134+
*keyExists = exists
142135
return nil
143136
}
144137
}
145138

139+
// KeyExists returns true if a key exists in the database.
140+
// No errors are expected during normal operation.
141+
func KeyExists(r storage.Reader, key []byte) (bool, error) {
142+
_, closer, err := r.Get(key)
143+
if err != nil {
144+
// the key does not exist in the database
145+
if errors.Is(err, storage.ErrNotFound) {
146+
return false, nil
147+
}
148+
// exception while checking for the key
149+
return false, irrecoverable.NewExceptionf("could not load data: %w", err)
150+
}
151+
defer closer.Close()
152+
153+
// the key does exist in the database
154+
return true, nil
155+
}
156+
146157
// Retrieve will retrieve the binary data under the given key from the database
147158
// and decode it into the given entity. The provided entity needs to be a
148159
// pointer to an initialized entity of the correct type.

storage/operations.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ type Reader interface {
7272
//
7373
// The caller should not modify the contents of the returned slice, but it is
7474
// safe to modify the contents of the `key` argument after Get returns. The
75-
// returned slice will remain valid until the returned Closer is closed. On
76-
// success, the caller MUST call closer.Close() or a memory leak will occur.
75+
// returned slice will remain valid until the returned Closer is closed.
76+
// 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
7778
Get(key []byte) (value []byte, closer io.Closer, err error)
7879

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

0 commit comments

Comments
 (0)