From 0ee3507e5af31c3e15abb2de1119283c6356d3c2 Mon Sep 17 00:00:00 2001 From: Jean Date: Wed, 20 Mar 2024 03:58:10 +0100 Subject: [PATCH] added error to bucket Get --- store/kv/bucket.go | 6 +++--- store/kv/db_test.go | 15 ++++++++++----- store/kv/kv.go | 2 +- store/kv/purbdb_test.go | 15 ++++++++++----- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/store/kv/bucket.go b/store/kv/bucket.go index d179de2..1b228be 100644 --- a/store/kv/bucket.go +++ b/store/kv/bucket.go @@ -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. diff --git a/store/kv/db_test.go b/store/kv/db_test.go index d5f49ee..fa04d87 100644 --- a/store/kv/db_test.go +++ b/store/kv/db_test.go @@ -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 @@ -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 @@ -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 diff --git a/store/kv/kv.go b/store/kv/kv.go index 5df7db8..19a57ec 100644 --- a/store/kv/kv.go +++ b/store/kv/kv.go @@ -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 diff --git a/store/kv/purbdb_test.go b/store/kv/purbdb_test.go index 87576ca..d0c1b70 100644 --- a/store/kv/purbdb_test.go +++ b/store/kv/purbdb_test.go @@ -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 @@ -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 @@ -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