Skip to content

Commit 05862f6

Browse files
committed
refactor Retrieve with RetrieveByKey
1 parent 42f360e commit 05862f6

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

storage/operation/reads.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,28 +154,33 @@ func KeyExists(r storage.Reader, key []byte) (bool, error) {
154154
return true, nil
155155
}
156156

157-
// Retrieve will retrieve the binary data under the given key from the database
157+
// Retrieve returns a functor that retrieves the binary data under the given key from the database
158+
func Retrieve(key []byte, entity interface{}) func(storage.Reader) error {
159+
return func(r storage.Reader) error {
160+
return RetrieveByKey(r, key, entity)
161+
}
162+
}
163+
164+
// RetrieveByKey will retrieve the binary data under the given key from the database
158165
// and decode it into the given entity. The provided entity needs to be a
159166
// pointer to an initialized entity of the correct type.
160167
// Error returns:
161168
// - storage.ErrNotFound if the key does not exist in the database
162169
// - generic error in case of unexpected failure from the database layer, or failure
163170
// to decode an existing database value
164-
func Retrieve(key []byte, entity interface{}) func(storage.Reader) error {
165-
return func(r storage.Reader) error {
166-
val, closer, err := r.Get(key)
167-
if err != nil {
168-
return err
169-
}
171+
func RetrieveByKey(r storage.Reader, key []byte, entity interface{}) error {
172+
val, closer, err := r.Get(key)
173+
if err != nil {
174+
return err
175+
}
170176

171-
defer closer.Close()
177+
defer closer.Close()
172178

173-
err = msgpack.Unmarshal(val, entity)
174-
if err != nil {
175-
return irrecoverable.NewExceptionf("could not decode entity: %w", err)
176-
}
177-
return nil
179+
err = msgpack.Unmarshal(val, entity)
180+
if err != nil {
181+
return irrecoverable.NewExceptionf("could not decode entity: %w", err)
178182
}
183+
return nil
179184
}
180185

181186
// FindHighestAtOrBelow is for database entries that are indexed by block height. It is suitable to search

0 commit comments

Comments
 (0)