2020-12-03 11:29:33 +00:00
|
|
|
package meta_test
|
|
|
|
|
|
|
|
import (
|
2021-01-22 11:07:08 +00:00
|
|
|
"math/rand"
|
2021-10-27 10:52:12 +00:00
|
|
|
"sort"
|
2020-12-03 11:29:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-01-22 11:07:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-17 08:07:51 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-12-03 11:29:33 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDB_Containers(t *testing.T) {
|
|
|
|
db := newDB(t)
|
|
|
|
|
|
|
|
const N = 10
|
|
|
|
|
|
|
|
cids := make(map[string]int, N)
|
|
|
|
|
|
|
|
for i := 0; i < N; i++ {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
2020-12-03 11:29:33 +00:00
|
|
|
|
|
|
|
cids[obj.ContainerID().String()] = 0
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, obj)
|
2020-12-03 11:29:33 +00:00
|
|
|
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
|
|
|
|
}
|
2020-12-17 08:07:51 +00:00
|
|
|
|
|
|
|
t.Run("Inhume", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
2020-12-17 08:07:51 +00:00
|
|
|
|
|
|
|
require.NoError(t, putBig(db, obj))
|
|
|
|
|
|
|
|
cnrs, err := db.Containers()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, cnrs, obj.ContainerID())
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
require.NoError(t, meta.Inhume(db, object.AddressOf(obj), generateAddress()))
|
2020-12-17 08:07:51 +00:00
|
|
|
|
|
|
|
cnrs, err = db.Containers()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, cnrs, obj.ContainerID())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ToMoveIt", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
2020-12-17 08:07:51 +00:00
|
|
|
|
|
|
|
require.NoError(t, putBig(db, obj))
|
|
|
|
|
|
|
|
cnrs, err := db.Containers()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, cnrs, obj.ContainerID())
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
require.NoError(t, meta.ToMoveIt(db, object.AddressOf(obj)))
|
2020-12-17 08:07:51 +00:00
|
|
|
|
|
|
|
cnrs, err = db.Containers()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, cnrs, obj.ContainerID())
|
|
|
|
})
|
2020-12-03 11:29:33 +00:00
|
|
|
}
|
2021-01-22 11:07:08 +00:00
|
|
|
|
2021-10-27 10:52:12 +00:00
|
|
|
func TestDB_ContainersCount(t *testing.T) {
|
|
|
|
db := newDB(t)
|
|
|
|
|
|
|
|
const R, T, SG = 10, 11, 12 // amount of object per type
|
|
|
|
|
|
|
|
uploadObjects := [...]struct {
|
|
|
|
amount int
|
|
|
|
typ objectSDK.Type
|
|
|
|
}{
|
|
|
|
{R, objectSDK.TypeRegular},
|
|
|
|
{T, objectSDK.TypeTombstone},
|
2021-10-27 10:56:30 +00:00
|
|
|
{SG, objectSDK.TypeStorageGroup},
|
2021-10-27 10:52:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := make([]*cid.ID, 0, R+T+SG)
|
|
|
|
|
|
|
|
for _, upload := range uploadObjects {
|
|
|
|
for i := 0; i < upload.amount; i++ {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
2021-10-27 10:52:12 +00:00
|
|
|
obj.SetType(upload.typ)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, obj)
|
2021-10-27 10:52:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected = append(expected, obj.ContainerID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(expected, func(i, j int) bool {
|
|
|
|
return expected[i].String() < expected[j].String()
|
|
|
|
})
|
|
|
|
|
|
|
|
got, err := db.Containers()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sort.Slice(got, func(i, j int) bool {
|
|
|
|
return got[i].String() < got[j].String()
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Equal(t, expected, got)
|
|
|
|
}
|
|
|
|
|
2021-01-22 11:07:08 +00:00
|
|
|
func TestDB_ContainerSize(t *testing.T) {
|
|
|
|
db := newDB(t)
|
|
|
|
|
|
|
|
const (
|
|
|
|
C = 3
|
|
|
|
N = 5
|
|
|
|
)
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
cids := make(map[*cid.ID]int, C)
|
2022-03-03 14:19:05 +00:00
|
|
|
objs := make(map[*cid.ID][]*objectSDK.Object, C*N)
|
2021-01-22 11:07:08 +00:00
|
|
|
|
|
|
|
for i := 0; i < C; i++ {
|
2021-12-01 13:56:48 +00:00
|
|
|
cid := cidtest.ID()
|
2021-01-22 11:07:08 +00:00
|
|
|
cids[cid] = 0
|
|
|
|
|
|
|
|
for j := 0; j < N; j++ {
|
|
|
|
size := rand.Intn(1024)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
parent := generateObjectWithCID(t, cid)
|
2021-01-22 11:07:08 +00:00
|
|
|
parent.SetPayloadSize(uint64(size / 2))
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObjectWithCID(t, cid)
|
2021-01-22 11:07:08 +00:00
|
|
|
obj.SetPayloadSize(uint64(size))
|
|
|
|
obj.SetParentID(parent.ID())
|
2022-03-03 14:19:05 +00:00
|
|
|
obj.SetParent(parent)
|
2021-01-22 11:07:08 +00:00
|
|
|
|
|
|
|
cids[cid] += size
|
|
|
|
objs[cid] = append(objs[cid], obj)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, obj)
|
2021-01-22 11:07:08 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for cid, volume := range cids {
|
|
|
|
n, err := db.ContainerSize(cid)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, volume, int(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Inhume", func(t *testing.T) {
|
|
|
|
for cid, list := range objs {
|
|
|
|
volume := cids[cid]
|
|
|
|
|
|
|
|
for _, obj := range list {
|
|
|
|
require.NoError(t, meta.Inhume(
|
|
|
|
db,
|
2022-03-03 14:19:05 +00:00
|
|
|
object.AddressOf(obj),
|
2021-01-22 11:07:08 +00:00
|
|
|
generateAddress(),
|
|
|
|
))
|
|
|
|
|
|
|
|
volume -= int(obj.PayloadSize())
|
|
|
|
|
|
|
|
n, err := db.ContainerSize(cid)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, volume, int(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|