Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added error to bucket Get #6

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions store/kv/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func (b *dpBucket) updateIndex() {

// Get implements kv.Bucket. It returns the value associated to the key, or nil
// if it does not exist.
func (b *dpBucket) Get(key []byte) []byte {
func (b *dpBucket) Get(key []byte) ([]byte, error) {
v, found := b.Kv[string(key)]
if found {
return v
return v, nil
}
return nil
return nil, xerrors.Errorf("failed to find key %v in bucket", string(key))
}

// Set implements kv.Bucket. It sets the provided key to the value.
Expand Down
15 changes: 10 additions & 5 deletions store/kv/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func TestDb_UpdateAndView(t *testing.T) {
bucket := txn.GetBucket([]byte("bucket"))
require.NotNil(t, bucket)

value := bucket.Get([]byte("ping"))
value, err := bucket.Get([]byte("ping"))
require.NoError(t, err)
require.Equal(t, []byte("pong"), value)

return nil
Expand Down Expand Up @@ -119,15 +120,18 @@ func TestDb_GetSetDelete(t *testing.T) {

require.NoError(t, b.Set([]byte("ping"), []byte("pong")))

value := b.Get([]byte("ping"))
value, err := b.Get([]byte("ping"))
require.NoError(t, err)
require.Equal(t, []byte("pong"), value)

value = b.Get([]byte("pong"))
value, err = b.Get([]byte("pong"))
require.Error(t, err)
require.Nil(t, value)

require.NoError(t, b.Delete([]byte("ping")))

value = b.Get([]byte("ping"))
value, err = b.Get([]byte("ping"))
require.Error(t, err)
require.Nil(t, value)

return nil
Expand Down Expand Up @@ -167,7 +171,8 @@ func TestDb_SetReopenGet(t *testing.T) {
b := txn.GetBucket([]byte("bucket"))
require.NotNil(t, b)

value := b.Get([]byte("ping"))
value, err := b.Get([]byte("ping"))
require.NoError(t, err)
require.Equal(t, []byte("pong"), value)

return nil
Expand Down
2 changes: 1 addition & 1 deletion store/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "go.dedis.ch/dela/core/store"
type Bucket interface {
// Get reads the key from the bucket and returns the value, or nil if the
// key does not exist.
Get(key []byte) []byte
Get(key []byte) ([]byte, error)

// Set assigns the value to the provided key.
Set(key, value []byte) error
Expand Down
15 changes: 10 additions & 5 deletions store/kv/purbdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func TestPurbDb_UpdateAndView(t *testing.T) {
bucket := txn.GetBucket([]byte("bucket"))
require.NotNil(t, bucket)

value := bucket.Get([]byte("ping"))
value, err := bucket.Get([]byte("ping"))
require.NoError(t, err)
require.Equal(t, []byte("pong"), value)

return nil
Expand Down Expand Up @@ -119,15 +120,18 @@ func TestPurbDb_GetSetDelete(t *testing.T) {

require.NoError(t, b.Set([]byte("ping"), []byte("pong")))

value := b.Get([]byte("ping"))
value, err := b.Get([]byte("ping"))
require.NoError(t, err)
require.Equal(t, []byte("pong"), value)

value = b.Get([]byte("pong"))
value, err = b.Get([]byte("pong"))
require.Error(t, err)
require.Nil(t, value)

require.NoError(t, b.Delete([]byte("ping")))

value = b.Get([]byte("ping"))
value, err = b.Get([]byte("ping"))
require.Error(t, err)
require.Nil(t, value)

return nil
Expand Down Expand Up @@ -167,7 +171,8 @@ func TestPurbDb_SetReopenGet(t *testing.T) {
b := txn.GetBucket([]byte("bucket"))
require.NotNil(t, b)

value := b.Get([]byte("ping"))
value, err := b.Get([]byte("ping"))
require.NoError(t, err)
require.Equal(t, []byte("pong"), value)

return nil
Expand Down
Loading