2022-09-05 11:00:23 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
2023-04-13 12:36:20 +00:00
|
|
|
"context"
|
2022-09-05 11:00:23 +00:00
|
|
|
"errors"
|
2023-07-13 08:51:19 +00:00
|
|
|
"path"
|
2024-04-02 07:41:38 +00:00
|
|
|
"path/filepath"
|
2024-04-10 11:15:59 +00:00
|
|
|
"slices"
|
2022-09-05 11:00:23 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
2022-09-05 11:00:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetSubTree(t *testing.T) {
|
|
|
|
d := pilorama.CIDDescriptor{CID: cidtest.ID(), Size: 1}
|
|
|
|
treeID := "sometree"
|
|
|
|
p := pilorama.NewMemoryForest()
|
|
|
|
|
|
|
|
tree := []struct {
|
|
|
|
path []string
|
|
|
|
id uint64
|
|
|
|
}{
|
|
|
|
{path: []string{"dir1"}},
|
|
|
|
{path: []string{"dir2"}},
|
|
|
|
{path: []string{"dir1", "sub1"}},
|
|
|
|
{path: []string{"dir2", "sub1"}},
|
|
|
|
{path: []string{"dir2", "sub2"}},
|
|
|
|
{path: []string{"dir2", "sub1", "subsub1"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tree {
|
|
|
|
path := tree[i].path
|
|
|
|
meta := []pilorama.KeyValue{
|
2023-10-31 11:56:55 +00:00
|
|
|
{Key: pilorama.AttributeFilename, Value: []byte(path[len(path)-1])},
|
|
|
|
}
|
2022-09-05 11:00:23 +00:00
|
|
|
|
2023-04-13 12:36:20 +00:00
|
|
|
lm, err := p.TreeAddByPath(context.Background(), d, treeID, pilorama.AttributeFilename, path[:len(path)-1], meta)
|
2022-09-05 11:00:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(lm))
|
|
|
|
|
|
|
|
tree[i].id = lm[0].Child
|
|
|
|
}
|
|
|
|
|
|
|
|
testGetSubTree := func(t *testing.T, rootID uint64, depth uint32, errIndex int) []uint64 {
|
|
|
|
acc := subTreeAcc{errIndex: errIndex}
|
2023-04-13 12:36:20 +00:00
|
|
|
err := getSubTree(context.Background(), &acc, d.CID, &GetSubTreeRequest_Body{
|
2022-09-05 11:00:23 +00:00
|
|
|
TreeId: treeID,
|
2024-07-10 06:30:01 +00:00
|
|
|
RootId: []uint64{rootID},
|
2022-09-05 11:00:23 +00:00
|
|
|
Depth: depth,
|
|
|
|
}, p)
|
|
|
|
if errIndex == -1 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.ErrorIs(t, err, errSubTreeSend)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubTree must return child only after is has returned the parent.
|
2024-07-10 06:30:01 +00:00
|
|
|
require.Equal(t, rootID, acc.seen[0].Body.NodeId[0])
|
2022-09-05 11:00:23 +00:00
|
|
|
loop:
|
|
|
|
for i := 1; i < len(acc.seen); i++ {
|
|
|
|
parent := acc.seen[i].Body.ParentId
|
|
|
|
for j := 0; j < i; j++ {
|
2024-07-10 06:30:01 +00:00
|
|
|
if acc.seen[j].Body.NodeId[0] == parent[0] {
|
2022-09-05 11:00:23 +00:00
|
|
|
continue loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Fail(t, "node has parent %d, but it hasn't been seen", parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSubTree must return valid meta.
|
|
|
|
for i := range acc.seen {
|
|
|
|
b := acc.seen[i].Body
|
2024-07-10 06:30:01 +00:00
|
|
|
meta, node, err := p.TreeGetMeta(context.Background(), d.CID, treeID, b.NodeId[0])
|
2022-09-05 11:00:23 +00:00
|
|
|
require.NoError(t, err)
|
2024-07-10 06:30:01 +00:00
|
|
|
require.Equal(t, node, b.ParentId[0])
|
|
|
|
require.Equal(t, meta.Time, b.Timestamp[0])
|
2022-09-05 11:00:23 +00:00
|
|
|
require.Equal(t, metaToProto(meta.Items), b.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered := make([]uint64, len(acc.seen))
|
|
|
|
for i := range acc.seen {
|
2024-07-10 06:30:01 +00:00
|
|
|
ordered[i] = acc.seen[i].Body.NodeId[0]
|
2022-09-05 11:00:23 +00:00
|
|
|
}
|
|
|
|
return ordered
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("depth = 1, only root", func(t *testing.T) {
|
|
|
|
actual := testGetSubTree(t, 0, 1, -1)
|
|
|
|
require.Equal(t, []uint64{0}, actual)
|
|
|
|
|
|
|
|
t.Run("custom root", func(t *testing.T) {
|
|
|
|
actual := testGetSubTree(t, tree[2].id, 1, -1)
|
|
|
|
require.Equal(t, []uint64{tree[2].id}, actual)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("depth = 2", func(t *testing.T) {
|
|
|
|
actual := testGetSubTree(t, 0, 2, -1)
|
|
|
|
require.Equal(t, []uint64{0, tree[0].id, tree[1].id}, actual)
|
|
|
|
|
|
|
|
t.Run("error in the middle", func(t *testing.T) {
|
|
|
|
actual := testGetSubTree(t, 0, 2, 0)
|
|
|
|
require.Equal(t, []uint64{0}, actual)
|
|
|
|
|
|
|
|
actual = testGetSubTree(t, 0, 2, 1)
|
|
|
|
require.Equal(t, []uint64{0, tree[0].id}, actual)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("depth = 0 (unrestricted)", func(t *testing.T) {
|
|
|
|
actual := testGetSubTree(t, 0, 0, -1)
|
|
|
|
expected := []uint64{
|
|
|
|
0,
|
|
|
|
tree[0].id, // dir1
|
|
|
|
tree[2].id, // dir1/sub1
|
|
|
|
tree[1].id, // dir2
|
|
|
|
tree[3].id, // dir2/sub1
|
|
|
|
tree[5].id, // dir2/sub1/subsub1
|
|
|
|
tree[4].id, // dir2/sub2
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-13 08:51:19 +00:00
|
|
|
func TestGetSubTreeOrderAsc(t *testing.T) {
|
2024-04-02 07:41:38 +00:00
|
|
|
t.Run("memory forest", func(t *testing.T) {
|
|
|
|
testGetSubTreeOrderAsc(t, pilorama.NewMemoryForest())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("boltdb forest", func(t *testing.T) {
|
|
|
|
p := pilorama.NewBoltForest(pilorama.WithPath(filepath.Join(t.TempDir(), "pilorama")))
|
2024-04-03 08:44:02 +00:00
|
|
|
require.NoError(t, p.Open(context.Background(), 0o644))
|
2024-04-02 07:41:38 +00:00
|
|
|
require.NoError(t, p.Init())
|
|
|
|
testGetSubTreeOrderAsc(t, p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetSubTreeOrderAsc(t *testing.T, p pilorama.ForestStorage) {
|
2023-07-13 08:51:19 +00:00
|
|
|
d := pilorama.CIDDescriptor{CID: cidtest.ID(), Size: 1}
|
|
|
|
treeID := "sometree"
|
|
|
|
|
|
|
|
tree := []struct {
|
|
|
|
path []string
|
|
|
|
id uint64
|
|
|
|
}{
|
|
|
|
{path: []string{"dir1"}},
|
|
|
|
{path: []string{"dir2"}},
|
|
|
|
{path: []string{"dir1", "sub1"}},
|
|
|
|
{path: []string{"dir2", "sub1"}},
|
|
|
|
{path: []string{"dir2", "sub2"}},
|
|
|
|
{path: []string{"dir2", "sub1", "subsub1"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tree {
|
|
|
|
path := tree[i].path
|
|
|
|
meta := []pilorama.KeyValue{
|
2023-10-31 11:56:55 +00:00
|
|
|
{Key: pilorama.AttributeFilename, Value: []byte(path[len(path)-1])},
|
|
|
|
}
|
2023-07-13 08:51:19 +00:00
|
|
|
|
|
|
|
lm, err := p.TreeAddByPath(context.Background(), d, treeID, pilorama.AttributeFilename, path[:len(path)-1], meta)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(lm))
|
|
|
|
tree[i].id = lm[0].Child
|
|
|
|
}
|
|
|
|
|
2024-04-02 07:41:38 +00:00
|
|
|
t.Run("total", func(t *testing.T) {
|
|
|
|
t.Skip()
|
|
|
|
acc := subTreeAcc{errIndex: -1}
|
|
|
|
err := getSubTree(context.Background(), &acc, d.CID, &GetSubTreeRequest_Body{
|
|
|
|
TreeId: treeID,
|
|
|
|
OrderBy: &GetSubTreeRequest_Body_Order{
|
|
|
|
Direction: GetSubTreeRequest_Body_Order_Asc,
|
|
|
|
},
|
|
|
|
}, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// GetSubTree must return child only after is has returned the parent.
|
|
|
|
require.Equal(t, uint64(0), acc.seen[0].Body.NodeId)
|
|
|
|
|
|
|
|
paths := make([]string, 0, len(acc.seen))
|
|
|
|
for i := range acc.seen {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
2023-07-13 08:51:19 +00:00
|
|
|
}
|
2024-04-02 07:41:38 +00:00
|
|
|
found := false
|
|
|
|
for j := range tree {
|
2024-07-10 06:30:01 +00:00
|
|
|
if acc.seen[i].Body.NodeId[0] == tree[j].id {
|
2024-04-02 07:41:38 +00:00
|
|
|
found = true
|
|
|
|
paths = append(paths, path.Join(tree[j].path...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, found, "unknown node %d %v", i, acc.seen[i].GetBody().GetNodeId())
|
2023-07-13 08:51:19 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 11:15:59 +00:00
|
|
|
require.True(t, slices.IsSorted(paths))
|
2024-04-02 07:41:38 +00:00
|
|
|
})
|
|
|
|
t.Run("depth=1", func(t *testing.T) {
|
|
|
|
acc := subTreeAcc{errIndex: -1}
|
|
|
|
err := getSubTree(context.Background(), &acc, d.CID, &GetSubTreeRequest_Body{
|
|
|
|
TreeId: treeID,
|
|
|
|
Depth: 1,
|
|
|
|
OrderBy: &GetSubTreeRequest_Body_Order{
|
|
|
|
Direction: GetSubTreeRequest_Body_Order_Asc,
|
|
|
|
},
|
|
|
|
}, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, acc.seen, 1)
|
2024-07-10 06:30:01 +00:00
|
|
|
require.Equal(t, uint64(0), acc.seen[0].Body.NodeId[0])
|
2024-04-02 07:41:38 +00:00
|
|
|
})
|
|
|
|
t.Run("depth=2", func(t *testing.T) {
|
|
|
|
acc := subTreeAcc{errIndex: -1}
|
|
|
|
err := getSubTree(context.Background(), &acc, d.CID, &GetSubTreeRequest_Body{
|
|
|
|
TreeId: treeID,
|
|
|
|
Depth: 2,
|
|
|
|
OrderBy: &GetSubTreeRequest_Body_Order{
|
|
|
|
Direction: GetSubTreeRequest_Body_Order_Asc,
|
|
|
|
},
|
|
|
|
}, p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, acc.seen, 3)
|
2024-07-10 06:30:01 +00:00
|
|
|
require.Equal(t, uint64(0), acc.seen[0].Body.NodeId[0])
|
|
|
|
require.Equal(t, uint64(0), acc.seen[1].GetBody().GetParentId()[0])
|
|
|
|
require.Equal(t, uint64(0), acc.seen[2].GetBody().GetParentId()[0])
|
2024-04-02 07:41:38 +00:00
|
|
|
})
|
2023-07-13 08:51:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 14:16:03 +00:00
|
|
|
var (
|
|
|
|
errSubTreeSend = errors.New("send finished with error")
|
|
|
|
errSubTreeSendAfterError = errors.New("send was invoked after an error occurred")
|
2024-07-10 06:30:01 +00:00
|
|
|
errInvalidResponse = errors.New("send got invalid response")
|
2023-03-28 14:16:03 +00:00
|
|
|
)
|
2022-09-05 11:00:23 +00:00
|
|
|
|
|
|
|
type subTreeAcc struct {
|
|
|
|
grpc.ServerStream // to satisfy the interface
|
|
|
|
// IDs of the seen nodes.
|
|
|
|
seen []*GetSubTreeResponse
|
|
|
|
errIndex int
|
|
|
|
}
|
|
|
|
|
2023-03-28 14:16:03 +00:00
|
|
|
var _ TreeService_GetSubTreeServer = &subTreeAcc{}
|
|
|
|
|
2022-09-05 11:00:23 +00:00
|
|
|
func (s *subTreeAcc) Send(r *GetSubTreeResponse) error {
|
2024-07-10 06:30:01 +00:00
|
|
|
b := r.GetBody()
|
|
|
|
if len(b.GetNodeId()) > 1 {
|
|
|
|
return errInvalidResponse
|
|
|
|
}
|
|
|
|
if len(b.GetParentId()) > 1 {
|
|
|
|
return errInvalidResponse
|
|
|
|
}
|
|
|
|
if len(b.GetTimestamp()) > 1 {
|
|
|
|
return errInvalidResponse
|
|
|
|
}
|
2022-09-05 11:00:23 +00:00
|
|
|
s.seen = append(s.seen, r)
|
|
|
|
if s.errIndex >= 0 {
|
|
|
|
if len(s.seen) == s.errIndex+1 {
|
|
|
|
return errSubTreeSend
|
|
|
|
}
|
|
|
|
if s.errIndex >= 0 && len(s.seen) > s.errIndex {
|
2023-03-28 14:16:03 +00:00
|
|
|
return errSubTreeSendAfterError
|
2022-09-05 11:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|