Skip to content

Commit

Permalink
core: return a common error on KeyValueReader.Get when not found
Browse files Browse the repository at this point in the history
This allow to distinguish between not found and an actual read error. Also allow for
fancy layering on top of a KeyValueStore.

Also, document KeyValueWriter.Delete as idempotent.
  • Loading branch information
MichaelMure committed Oct 21, 2021
1 parent def17fb commit 97db15f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
8 changes: 8 additions & 0 deletions ethdb/database.go
Expand Up @@ -22,12 +22,19 @@ import (
"io"
)

var (
// ErrKVNotFound is returned if a key is requested that is not found in
// the KeyValueStore.
ErrKVNotFound = errors.New("not found")
)

// KeyValueReader wraps the Has and Get method of a backing data store.
type KeyValueReader interface {
// Has retrieves if a key is present in the key-value data store.
Has(key []byte) (bool, error)

// Get retrieves the given key if it's present in the key-value data store.
// Returns ErrKVNotFound if the key is not found.
Get(key []byte) ([]byte, error)
}

Expand All @@ -37,6 +44,7 @@ type KeyValueWriter interface {
Put(key []byte, value []byte) error

// Delete removes the key from the key-value data store.
// If the key doesn't exist, this method returns no error.
Delete(key []byte) error
}

Expand Down
4 changes: 4 additions & 0 deletions ethdb/dbtest/testsuite.go
Expand Up @@ -216,6 +216,10 @@ func TestKeyValueStoreSuite(t *testing.T, New func() ethdb.KeyValueStore) {
t.Errorf("wrong value: %t", got)
}

if _, err := db.Get(key); err != ethdb.ErrKVNotFound {
t.Errorf("expected ethdb.ErrKVNotFound")
}

value := []byte("hello world")
if err := db.Put(key, value); err != nil {
t.Error(err)
Expand Down
3 changes: 3 additions & 0 deletions ethdb/leveldb/leveldb.go
Expand Up @@ -188,6 +188,9 @@ func (db *Database) Has(key []byte) (bool, error) {
// Get retrieves the given key if it's present in the key-value store.
func (db *Database) Get(key []byte) ([]byte, error) {
dat, err := db.db.Get(key, nil)
if err == leveldb.ErrNotFound {
return nil, ethdb.ErrKVNotFound
}
if err != nil {
return nil, err
}
Expand Down
8 changes: 2 additions & 6 deletions ethdb/memorydb/memorydb.go
Expand Up @@ -31,10 +31,6 @@ var (
// errMemorydbClosed is returned if a memory database was already closed at the
// invocation of a data access operation.
errMemorydbClosed = errors.New("database closed")

// errMemorydbNotFound is returned if a key is requested that is not found in
// the provided memory database.
errMemorydbNotFound = errors.New("not found")
)

// Database is an ephemeral key-value store. Apart from basic data storage
Expand Down Expand Up @@ -62,7 +58,7 @@ func NewWithCap(size int) *Database {
}

// Close deallocates the internal map and ensures any consecutive data access op
// failes with an error.
// fails with an error.
func (db *Database) Close() error {
db.lock.Lock()
defer db.lock.Unlock()
Expand Down Expand Up @@ -94,7 +90,7 @@ func (db *Database) Get(key []byte) ([]byte, error) {
if entry, ok := db.db[string(key)]; ok {
return common.CopyBytes(entry), nil
}
return nil, errMemorydbNotFound
return nil, ethdb.ErrKVNotFound
}

// Put inserts the given value into the key-value store.
Expand Down

0 comments on commit 97db15f

Please sign in to comment.