forked from TrueCloudLab/frostfs-node
[#231] metabase: Add container lister
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
3a37eda410
commit
a1cb48f800
2 changed files with 85 additions and 0 deletions
49
pkg/local_object_storage/metabase/v2/containers.go
Normal file
49
pkg/local_object_storage/metabase/v2/containers.go
Normal file
|
@ -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)
|
||||
}
|
36
pkg/local_object_storage/metabase/v2/containers_test.go
Normal file
36
pkg/local_object_storage/metabase/v2/containers_test.go
Normal file
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue