2022-04-22 13:22:40 +00:00
|
|
|
package pilorama
|
|
|
|
|
2022-05-27 12:55:02 +00:00
|
|
|
import (
|
2023-04-13 12:36:20 +00:00
|
|
|
"context"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
cidSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2022-05-27 12:55:02 +00:00
|
|
|
)
|
2022-04-22 13:22:40 +00:00
|
|
|
|
|
|
|
// Forest represents CRDT tree.
|
|
|
|
type Forest interface {
|
|
|
|
// TreeMove moves node in the tree.
|
|
|
|
// If the parent of the move operation is TrashID, the node is removed.
|
|
|
|
// If the child of the move operation is RootID, new ID is generated and added to a tree.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeMove(ctx context.Context, d CIDDescriptor, treeID string, m *Move) (*Move, error)
|
2022-04-22 13:22:40 +00:00
|
|
|
// TreeAddByPath adds new node in the tree using provided path.
|
|
|
|
// The path is constructed by descending from the root using the values of the attr in meta.
|
2022-05-23 11:32:24 +00:00
|
|
|
// Internal nodes in path should have exactly one attribute, otherwise a new node is created.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeAddByPath(ctx context.Context, d CIDDescriptor, treeID string, attr string, path []string, meta []KeyValue) ([]Move, error)
|
2022-04-22 13:22:40 +00:00
|
|
|
// TreeApply applies replicated operation from another node.
|
2022-12-19 16:01:57 +00:00
|
|
|
// If background is true, TreeApply will first check whether an operation exists.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeApply(ctx context.Context, cnr cidSDK.ID, treeID string, m *Move, backgroundSync bool) error
|
2024-10-30 08:02:52 +00:00
|
|
|
// TreeApplyBatch applies replicated operations from another node.
|
|
|
|
TreeApplyBatch(ctx context.Context, cnr cidSDK.ID, treeID string, m []*Move) error
|
2022-04-22 13:22:40 +00:00
|
|
|
// TreeGetByPath returns all nodes corresponding to the path.
|
|
|
|
// The path is constructed by descending from the root using the values of the
|
|
|
|
// AttributeFilename in meta.
|
|
|
|
// The last argument determines whether only the node with the latest timestamp is returned.
|
2022-05-04 09:47:11 +00:00
|
|
|
// Should return ErrTreeNotFound if the tree is not found, and empty result if the path is not in the tree.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeGetByPath(ctx context.Context, cid cidSDK.ID, treeID string, attr string, path []string, latest bool) ([]Node, error)
|
2022-04-22 13:22:40 +00:00
|
|
|
// TreeGetMeta returns meta information of the node with the specified ID.
|
2022-05-04 09:47:11 +00:00
|
|
|
// Should return ErrTreeNotFound if the tree is not found, and empty result if the node is not in the tree.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeGetMeta(ctx context.Context, cid cidSDK.ID, treeID string, nodeID Node) (Meta, Node, error)
|
2022-04-29 10:06:10 +00:00
|
|
|
// TreeGetChildren returns children of the node with the specified ID. The order is arbitrary.
|
2022-05-04 09:47:11 +00:00
|
|
|
// Should return ErrTreeNotFound if the tree is not found, and empty result if the node is not in the tree.
|
2023-07-11 08:39:17 +00:00
|
|
|
TreeGetChildren(ctx context.Context, cid cidSDK.ID, treeID string, nodeID Node) ([]NodeInfo, error)
|
2024-03-28 12:53:26 +00:00
|
|
|
// TreeSortedByFilename returns children of the node with the specified ID. The nodes are sorted by the filename attribute..
|
|
|
|
// Should return ErrTreeNotFound if the tree is not found, and empty result if the node is not in the tree.
|
2024-07-10 06:30:01 +00:00
|
|
|
TreeSortedByFilename(ctx context.Context, cid cidSDK.ID, treeID string, nodeID MultiNode, last *string, count int) ([]MultiNodeInfo, *string, error)
|
2022-05-11 13:29:04 +00:00
|
|
|
// TreeGetOpLog returns first log operation stored at or above the height.
|
|
|
|
// In case no such operation is found, empty Move and nil error should be returned.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeGetOpLog(ctx context.Context, cid cidSDK.ID, treeID string, height uint64) (Move, error)
|
2022-09-07 08:46:13 +00:00
|
|
|
// TreeDrop drops a tree from the database.
|
|
|
|
// If the tree is not found, ErrTreeNotFound should be returned.
|
2022-11-08 12:32:38 +00:00
|
|
|
// In case of empty treeID drops all trees related to container.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeDrop(ctx context.Context, cid cidSDK.ID, treeID string) error
|
2022-10-18 11:59:32 +00:00
|
|
|
// TreeList returns all the tree IDs that have been added to the
|
|
|
|
// passed container ID. Nil slice should be returned if no tree found.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeList(ctx context.Context, cid cidSDK.ID) ([]string, error)
|
2022-10-06 16:06:19 +00:00
|
|
|
// TreeExists checks if a tree exists locally.
|
|
|
|
// If the tree is not found, false and a nil error should be returned.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeExists(ctx context.Context, cid cidSDK.ID, treeID string) (bool, error)
|
2023-01-25 10:25:45 +00:00
|
|
|
// TreeUpdateLastSyncHeight updates last log height synchronized with _all_ container nodes.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeUpdateLastSyncHeight(ctx context.Context, cid cidSDK.ID, treeID string, height uint64) error
|
2023-01-25 10:25:45 +00:00
|
|
|
// TreeLastSyncHeight returns last log height synchronized with _all_ container nodes.
|
2023-04-13 12:36:20 +00:00
|
|
|
TreeLastSyncHeight(ctx context.Context, cid cidSDK.ID, treeID string) (uint64, error)
|
2023-06-13 08:26:59 +00:00
|
|
|
// TreeHeight returns current tree height.
|
|
|
|
TreeHeight(ctx context.Context, cid cidSDK.ID, treeID string) (uint64, error)
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ForestStorage interface {
|
2022-05-16 13:23:09 +00:00
|
|
|
// DumpInfo returns information about the pilorama.
|
|
|
|
DumpInfo() Info
|
2022-04-22 13:22:40 +00:00
|
|
|
Init() error
|
2024-02-09 06:17:17 +00:00
|
|
|
Open(context.Context, mode.Mode) error
|
2022-04-22 13:22:40 +00:00
|
|
|
Close() error
|
2022-07-05 04:55:46 +00:00
|
|
|
SetMode(m mode.Mode) error
|
2023-06-07 11:39:03 +00:00
|
|
|
SetParentID(id string)
|
2022-04-22 13:22:40 +00:00
|
|
|
Forest
|
2024-02-05 11:09:58 +00:00
|
|
|
|
|
|
|
// TreeListTrees returns all pairs "containerID:treeID".
|
|
|
|
TreeListTrees(ctx context.Context, prm TreeListTreesPrm) (*TreeListTreesResult, error)
|
2024-02-06 10:59:50 +00:00
|
|
|
TreeApplyStream(ctx context.Context, cnr cidSDK.ID, treeID string, source <-chan *Move) error
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
AttributeFilename = "FileName"
|
|
|
|
AttributeVersion = "Version"
|
|
|
|
)
|
2022-05-27 12:55:02 +00:00
|
|
|
|
|
|
|
// CIDDescriptor contains container ID and information about the node position
|
|
|
|
// in the list of container nodes.
|
|
|
|
type CIDDescriptor struct {
|
|
|
|
CID cidSDK.ID
|
|
|
|
Position int
|
|
|
|
Size int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrInvalidCIDDescriptor is returned when info about tne node position
|
|
|
|
// in the container is invalid.
|
2022-10-31 07:02:30 +00:00
|
|
|
var ErrInvalidCIDDescriptor = logicerr.New("cid descriptor is invalid")
|
2022-05-27 12:55:02 +00:00
|
|
|
|
|
|
|
func (d CIDDescriptor) checkValid() bool {
|
|
|
|
return 0 <= d.Position && d.Position < d.Size
|
|
|
|
}
|
2024-02-05 11:09:58 +00:00
|
|
|
|
|
|
|
var treeListTreesBatchSizeDefault = 1000
|
|
|
|
|
|
|
|
type ContainerIDTreeID struct {
|
|
|
|
CID cidSDK.ID
|
|
|
|
TreeID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type TreeListTreesPrm struct {
|
|
|
|
NextPageToken []byte
|
|
|
|
// BatchSize is batch size to list trees. If not lower or equals zero, than treeListTreesBatchSizeDefault is used.
|
|
|
|
BatchSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
type TreeListTreesResult struct {
|
|
|
|
NextPageToken []byte
|
|
|
|
Items []ContainerIDTreeID
|
|
|
|
}
|
|
|
|
|
2024-02-06 10:59:50 +00:00
|
|
|
type treeList interface {
|
|
|
|
TreeListTrees(ctx context.Context, prm TreeListTreesPrm) (*TreeListTreesResult, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TreeListAll(ctx context.Context, f treeList) ([]ContainerIDTreeID, error) {
|
2024-02-05 11:09:58 +00:00
|
|
|
return treeListAll(ctx, f, treeListTreesBatchSizeDefault)
|
|
|
|
}
|
|
|
|
|
2024-02-06 10:59:50 +00:00
|
|
|
func treeListAll(ctx context.Context, f treeList, batchSize int) ([]ContainerIDTreeID, error) {
|
2024-02-05 11:09:58 +00:00
|
|
|
var prm TreeListTreesPrm
|
2024-02-06 10:59:50 +00:00
|
|
|
prm.BatchSize = batchSize
|
2024-02-05 11:09:58 +00:00
|
|
|
var result []ContainerIDTreeID
|
|
|
|
first := true
|
|
|
|
|
|
|
|
for len(prm.NextPageToken) > 0 || first {
|
|
|
|
first = false
|
|
|
|
|
|
|
|
res, err := f.TreeListTrees(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
prm.NextPageToken = res.NextPageToken
|
|
|
|
result = append(result, res.Items...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2024-02-06 10:59:50 +00:00
|
|
|
|
|
|
|
func TreeCountAll(ctx context.Context, f treeList) (uint64, error) {
|
|
|
|
var prm TreeListTreesPrm
|
|
|
|
var result uint64
|
|
|
|
first := true
|
|
|
|
|
|
|
|
for len(prm.NextPageToken) > 0 || first {
|
|
|
|
first = false
|
|
|
|
|
|
|
|
res, err := f.TreeListTrees(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
prm.NextPageToken = res.NextPageToken
|
|
|
|
result += uint64(len(res.Items))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|