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 000000000..8b54f4f0a --- /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 000000000..ceeaf6b44 --- /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 + } +}