Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

fixing namespace seeding issue #241

Merged
merged 7 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 0 additions & 19 deletions pkg/service/credential/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,6 @@ func NewCredentialStorage(db storage.ServiceStorage) (*Storage, error) {
return nil, errors.New("bolt db reference is nil")
}

// TODO: (Neal) there is a current bug with our Bolt implementation where if we do a GET without anything in the db it will throw an error
// Doing initial writes and then deleting will "warm up" our database and when we do a GET after that it will not crash and return empty list
// https://github.com/TBD54566975/ssi-service/issues/176
if err := db.Write(credentialNamespace, fakeKey, nil); err != nil {
return nil, util.LoggingErrorMsg(err, "problem writing status initial write to db")

}
if err := db.Delete(credentialNamespace, fakeKey); err != nil {
return nil, util.LoggingErrorMsg(err, "problem with initial delete to db")
}

if err := db.Write(statusListCredentialNamespace, fakeKey, nil); err != nil {
return nil, util.LoggingErrorMsg(err, "problem writing status initial write to db")
}

if err := db.Delete(statusListCredentialNamespace, fakeKey); err != nil {
return nil, util.LoggingErrorMsg(err, "problem with initial delete to db")
}

Comment on lines -81 to -99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

randUniqueList := randomUniqueNum(bitStringLength)
uniqueNumBytes, err := json.Marshal(randUniqueList)
if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions pkg/storage/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/goccy/go-json"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/tbd54566975/ssi-service/internal/util"
bolt "go.etcd.io/bbolt"
)
Expand Down Expand Up @@ -90,7 +89,6 @@ func (b *BoltDB) Read(namespace, key string) ([]byte, error) {
err := b.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
logrus.Infof("namespace<%s> does not exist", namespace)
return nil
}
result = bucket.Get([]byte(key))
Expand All @@ -105,9 +103,7 @@ func (b *BoltDB) ReadPrefix(namespace, prefix string) (map[string][]byte, error)
err := b.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
errMsg := fmt.Sprintf("namespace<%s> does not exist", namespace)
logrus.Error(errMsg)
return errors.New(errMsg)
return nil
}
cursor := bucket.Cursor()
prefix := []byte(prefix)
Expand All @@ -124,7 +120,6 @@ func (b *BoltDB) ReadAll(namespace string) (map[string][]byte, error) {
err := b.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
logrus.Errorf("namespace<%s> does not exist", namespace)
return nil
}
cursor := bucket.Cursor()
Expand All @@ -141,7 +136,7 @@ func (b *BoltDB) ReadAllKeys(namespace string) ([]string, error) {
err := b.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(namespace))
if bucket == nil {
return util.LoggingNewErrorf("namespace<%s> does not exist", namespace)
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the error as a warn or info

}
cursor := bucket.Cursor()
for k, _ := cursor.First(); k != nil; k, _ = cursor.Next() {
Expand Down
25 changes: 25 additions & 0 deletions pkg/storage/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,31 @@ func TestDBPrefixAndKeys(t *testing.T) {
}
}

func TestDBEmptyNamespace(t *testing.T) {
for _, dbImpl := range getDBImplementations(t) {
db := dbImpl

namespace := "dne"
key := "doesnotexist"

prefixValues, err := db.ReadPrefix(namespace, key)
assert.NoError(t, err)
assert.Len(t, prefixValues, 0)

allKeys, err := db.ReadAllKeys(namespace)
assert.NoError(t, err)
assert.Len(t, allKeys, 0)

allValues, err := db.ReadAll(namespace)
assert.NoError(t, err)
assert.Len(t, allValues, 0)

value, err := db.Read(namespace, key)
assert.NoError(t, err)
assert.Nil(t, value)
}
}

type testStruct struct {
Status int `json:"status"`
Reason string `json:"reason"`
Expand Down