From a1cb48f80085f9adfa2cb43e0dbfa493fb492812 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 3 Dec 2020 14:29:33 +0300 Subject: [PATCH] [#231] metabase: Add container lister Signed-off-by: Alex Vanin --- .../metabase/v2/containers.go | 49 +++++++++++++++++++ .../metabase/v2/containers_test.go | 36 ++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 pkg/local_object_storage/metabase/v2/containers.go create mode 100644 pkg/local_object_storage/metabase/v2/containers_test.go diff --git a/pkg/local_object_storage/metabase/v2/containers.go b/pkg/local_object_storage/metabase/v2/containers.go new file mode 100644 index 00000000..8b54f4f0 --- /dev/null +++ b/pkg/local_object_storage/metabase/v2/containers.go @@ -0,0 +1,49 @@ +package meta + +import ( + "strings" + + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "go.etcd.io/bbolt" +) + +func (db *DB) Containers() (list []*container.ID, err error) { + err = db.boltDB.View(func(tx *bbolt.Tx) error { + list, err = db.containers(tx) + + return err + }) + + return list, err +} + +func (db *DB) containers(tx *bbolt.Tx) ([]*container.ID, error) { + result := make([]*container.ID, 0) + + err := tx.ForEach(func(name []byte, _ *bbolt.Bucket) error { + id, err := parseContainerID(name) + if err != nil { + return err + } + + if id != nil { + result = append(result, id) + } + + return nil + }) + + return result, err +} + +func parseContainerID(name []byte) (*container.ID, error) { + strName := string(name) + + if strings.Contains(strName, "_") { + return nil, nil + } + + id := container.NewID() + + return id, id.Parse(strName) +} diff --git a/pkg/local_object_storage/metabase/v2/containers_test.go b/pkg/local_object_storage/metabase/v2/containers_test.go new file mode 100644 index 00000000..ceeaf6b4 --- /dev/null +++ b/pkg/local_object_storage/metabase/v2/containers_test.go @@ -0,0 +1,36 @@ +package meta_test + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDB_Containers(t *testing.T) { + db := newDB(t) + defer releaseDB(db) + + const N = 10 + + cids := make(map[string]int, N) + + for i := 0; i < N; i++ { + obj := generateRawObject(t) + + cids[obj.ContainerID().String()] = 0 + + err := db.Put(obj.Object(), nil) + require.NoError(t, err) + } + + lst, err := db.Containers() + require.NoError(t, err) + + for _, cid := range lst { + i, ok := cids[cid.String()] + require.True(t, ok) + require.Equal(t, 0, i) + + cids[cid.String()] = 1 + } +}