2023-03-14 14:31:15 +00:00
|
|
|
package tree
|
2022-04-22 07:18:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-10-09 06:57:33 +00:00
|
|
|
"io"
|
2023-04-03 14:26:17 +00:00
|
|
|
"sort"
|
2022-04-22 07:18:21 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-05-20 15:02:00 +00:00
|
|
|
"time"
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2023-04-03 14:26:17 +00:00
|
|
|
"go.uber.org/zap"
|
2022-04-22 07:18:21 +00:00
|
|
|
)
|
|
|
|
|
2022-05-12 01:48:17 +00:00
|
|
|
type (
|
2023-03-14 14:31:15 +00:00
|
|
|
Tree struct {
|
|
|
|
service ServiceClient
|
2023-04-03 14:26:17 +00:00
|
|
|
log *zap.Logger
|
2022-05-12 01:48:17 +00:00
|
|
|
}
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
// ServiceClient is a client to interact with tree service.
|
|
|
|
// Each method must return ErrNodeNotFound or ErrNodeAccessDenied if relevant.
|
|
|
|
ServiceClient interface {
|
|
|
|
GetNodes(ctx context.Context, p *GetNodesParams) ([]NodeResponse, error)
|
|
|
|
GetSubTree(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID uint64, depth uint32) ([]NodeResponse, error)
|
2023-10-09 06:57:33 +00:00
|
|
|
GetSubTreeStream(ctx context.Context, bktInfo *data.BucketInfo, treeID string, rootID uint64, depth uint32) (SubTreeStream, error)
|
2023-03-14 14:31:15 +00:00
|
|
|
AddNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, parent uint64, meta map[string]string) (uint64, error)
|
|
|
|
AddNodeByPath(ctx context.Context, bktInfo *data.BucketInfo, treeID string, path []string, meta map[string]string) (uint64, error)
|
|
|
|
MoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID, parentID uint64, meta map[string]string) error
|
|
|
|
RemoveNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID uint64) error
|
|
|
|
}
|
|
|
|
|
2023-10-09 06:57:33 +00:00
|
|
|
SubTreeStream interface {
|
|
|
|
Next() (NodeResponse, error)
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
treeNode struct {
|
2022-05-12 01:48:17 +00:00
|
|
|
ID uint64
|
2022-09-07 08:46:08 +00:00
|
|
|
ParentID uint64
|
2022-05-18 07:48:30 +00:00
|
|
|
ObjID oid.ID
|
2022-05-12 01:48:17 +00:00
|
|
|
TimeStamp uint64
|
2023-06-01 13:45:28 +00:00
|
|
|
Size uint64
|
2022-05-12 01:48:17 +00:00
|
|
|
Meta map[string]string
|
|
|
|
}
|
2022-05-25 01:58:25 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
GetNodesParams struct {
|
2022-09-13 09:44:18 +00:00
|
|
|
BktInfo *data.BucketInfo
|
2022-05-25 01:58:25 +00:00
|
|
|
TreeID string
|
|
|
|
Path []string
|
|
|
|
Meta []string
|
|
|
|
LatestOnly bool
|
|
|
|
AllAttrs bool
|
|
|
|
}
|
2022-05-12 01:48:17 +00:00
|
|
|
)
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
const (
|
|
|
|
FileNameKey = "FileName"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNodeNotFound is returned from ServiceClient in case of not found error.
|
|
|
|
ErrNodeNotFound = layer.ErrNodeNotFound
|
|
|
|
|
|
|
|
// ErrNodeAccessDenied is returned from ServiceClient service in case of access denied error.
|
|
|
|
ErrNodeAccessDenied = layer.ErrNodeAccessDenied
|
2023-06-13 14:47:31 +00:00
|
|
|
|
|
|
|
// ErrGatewayTimeout is returned from ServiceClient service in case of timeout error.
|
|
|
|
ErrGatewayTimeout = layer.ErrGatewayTimeout
|
2023-03-14 14:31:15 +00:00
|
|
|
)
|
|
|
|
|
2022-04-22 07:18:21 +00:00
|
|
|
const (
|
2022-07-19 14:58:18 +00:00
|
|
|
versioningKV = "Versioning"
|
2022-06-15 14:43:51 +00:00
|
|
|
lockConfigurationKV = "LockConfiguration"
|
2022-05-17 14:56:05 +00:00
|
|
|
oidKV = "OID"
|
2023-03-14 14:31:15 +00:00
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
isCombinedKV = "IsCombined"
|
2023-03-14 14:31:15 +00:00
|
|
|
isUnversionedKV = "IsUnversioned"
|
|
|
|
isTagKV = "IsTag"
|
|
|
|
uploadIDKV = "UploadId"
|
|
|
|
partNumberKV = "Number"
|
|
|
|
sizeKV = "Size"
|
|
|
|
etagKV = "ETag"
|
2023-10-02 08:52:07 +00:00
|
|
|
md5KV = "MD5"
|
2023-12-04 06:42:25 +00:00
|
|
|
finishedKV = "Finished"
|
2022-05-20 15:02:00 +00:00
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
// keys for lock.
|
|
|
|
isLockKV = "IsLock"
|
|
|
|
legalHoldOIDKV = "LegalHoldOID"
|
|
|
|
retentionOIDKV = "RetentionOID"
|
|
|
|
untilDateKV = "UntilDate"
|
|
|
|
isComplianceKV = "IsCompliance"
|
|
|
|
|
2022-05-23 14:34:13 +00:00
|
|
|
// keys for delete marker nodes.
|
2022-06-15 14:43:51 +00:00
|
|
|
isDeleteMarkerKV = "IsDeleteMarker"
|
2022-05-20 15:02:00 +00:00
|
|
|
ownerKV = "Owner"
|
|
|
|
createdKV = "Created"
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2022-05-25 01:58:25 +00:00
|
|
|
settingsFileName = "bucket-settings"
|
|
|
|
notifConfFileName = "bucket-notifications"
|
|
|
|
corsFilename = "bucket-cors"
|
|
|
|
bucketTaggingFilename = "bucket-tagging"
|
2022-05-12 01:50:52 +00:00
|
|
|
|
2022-05-18 07:48:30 +00:00
|
|
|
// versionTree -- ID of a tree with object versions.
|
2022-05-17 14:56:05 +00:00
|
|
|
versionTree = "version"
|
2022-05-18 07:48:30 +00:00
|
|
|
|
|
|
|
// systemTree -- ID of a tree with system objects
|
|
|
|
// i.e. bucket settings with versioning and lock configuration, cors, notifications.
|
|
|
|
systemTree = "system"
|
2022-05-17 14:56:05 +00:00
|
|
|
|
2022-05-25 10:16:51 +00:00
|
|
|
separator = "/"
|
2022-06-15 14:43:51 +00:00
|
|
|
userDefinedTagPrefix = "User-Tag-"
|
2022-05-20 08:26:35 +00:00
|
|
|
|
2022-09-05 13:48:05 +00:00
|
|
|
maxGetSubTreeDepth = 0 // means all subTree
|
2022-04-22 07:18:21 +00:00
|
|
|
)
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
// NewTree creates instance of Tree using provided address and create grpc connection.
|
2023-04-03 14:26:17 +00:00
|
|
|
func NewTree(service ServiceClient, log *zap.Logger) *Tree {
|
|
|
|
return &Tree{
|
|
|
|
service: service,
|
|
|
|
log: log,
|
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
}
|
2022-04-22 07:18:21 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
type Meta interface {
|
|
|
|
GetKey() string
|
|
|
|
GetValue() []byte
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 08:26:35 +00:00
|
|
|
type NodeResponse interface {
|
2023-03-14 14:31:15 +00:00
|
|
|
GetMeta() []Meta
|
|
|
|
GetNodeID() uint64
|
|
|
|
GetParentID() uint64
|
2022-05-20 08:26:35 +00:00
|
|
|
GetTimestamp() uint64
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func newTreeNode(nodeInfo NodeResponse) (*treeNode, error) {
|
|
|
|
treeNode := &treeNode{
|
|
|
|
ID: nodeInfo.GetNodeID(),
|
|
|
|
ParentID: nodeInfo.GetParentID(),
|
2022-07-18 14:51:34 +00:00
|
|
|
TimeStamp: nodeInfo.GetTimestamp(),
|
|
|
|
Meta: make(map[string]string, len(nodeInfo.GetMeta())),
|
|
|
|
}
|
2022-04-22 07:18:21 +00:00
|
|
|
|
|
|
|
for _, kv := range nodeInfo.GetMeta() {
|
2022-07-18 14:51:34 +00:00
|
|
|
switch kv.GetKey() {
|
|
|
|
case oidKV:
|
|
|
|
if err := treeNode.ObjID.DecodeString(string(kv.GetValue())); err != nil {
|
2022-05-12 01:48:17 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-18 14:51:34 +00:00
|
|
|
case sizeKV:
|
|
|
|
if sizeStr := string(kv.GetValue()); len(sizeStr) > 0 {
|
|
|
|
var err error
|
2023-06-01 13:45:28 +00:00
|
|
|
if treeNode.Size, err = strconv.ParseUint(sizeStr, 10, 64); err != nil {
|
2022-07-18 14:51:34 +00:00
|
|
|
return nil, fmt.Errorf("invalid size value '%s': %w", sizeStr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
treeNode.Meta[kv.GetKey()] = string(kv.GetValue())
|
2022-05-12 01:48:17 +00:00
|
|
|
}
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 14:51:34 +00:00
|
|
|
return treeNode, nil
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (n *treeNode) Get(key string) (string, bool) {
|
2022-05-12 01:48:17 +00:00
|
|
|
value, ok := n.Meta[key]
|
2022-04-22 07:18:21 +00:00
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (n *treeNode) FileName() (string, bool) {
|
|
|
|
value, ok := n.Meta[FileNameKey]
|
2022-06-10 11:57:41 +00:00
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNodeVersion(filePath string, node NodeResponse) (*data.NodeVersion, error) {
|
2022-05-17 14:56:05 +00:00
|
|
|
treeNode, err := newTreeNode(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid tree node: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-10 11:57:41 +00:00
|
|
|
return newNodeVersionFromTreeNode(filePath, treeNode), nil
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func newNodeVersionFromTreeNode(filePath string, treeNode *treeNode) *data.NodeVersion {
|
2022-05-17 14:56:05 +00:00
|
|
|
_, isUnversioned := treeNode.Get(isUnversionedKV)
|
|
|
|
_, isDeleteMarker := treeNode.Get(isDeleteMarkerKV)
|
2023-06-27 12:49:20 +00:00
|
|
|
_, isCombined := treeNode.Get(isCombinedKV)
|
2022-07-18 14:51:34 +00:00
|
|
|
eTag, _ := treeNode.Get(etagKV)
|
2023-10-02 08:52:07 +00:00
|
|
|
md5, _ := treeNode.Get(md5KV)
|
2022-05-17 14:56:05 +00:00
|
|
|
|
2022-05-20 15:02:00 +00:00
|
|
|
version := &data.NodeVersion{
|
|
|
|
BaseNodeVersion: data.BaseNodeVersion{
|
2024-01-17 14:26:02 +00:00
|
|
|
ID: treeNode.ID,
|
|
|
|
ParenID: treeNode.ParentID,
|
|
|
|
OID: treeNode.ObjID,
|
|
|
|
Timestamp: treeNode.TimeStamp,
|
|
|
|
ETag: eTag,
|
|
|
|
MD5: md5,
|
|
|
|
Size: treeNode.Size,
|
|
|
|
FilePath: filePath,
|
|
|
|
IsDeleteMarker: isDeleteMarker,
|
2022-05-17 14:56:05 +00:00
|
|
|
},
|
2022-05-20 15:02:00 +00:00
|
|
|
IsUnversioned: isUnversioned,
|
2023-06-27 12:49:20 +00:00
|
|
|
IsCombined: isCombined,
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 14:26:02 +00:00
|
|
|
if createdStr, ok := treeNode.Get(createdKV); ok {
|
|
|
|
if utcMilli, err := strconv.ParseInt(createdStr, 10, 64); err == nil {
|
|
|
|
created := time.UnixMilli(utcMilli)
|
|
|
|
version.Created = &created
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
2024-01-17 14:26:02 +00:00
|
|
|
}
|
2022-05-20 15:02:00 +00:00
|
|
|
|
2024-01-17 14:26:02 +00:00
|
|
|
if ownerStr, ok := treeNode.Get(ownerKV); ok {
|
2022-05-20 15:02:00 +00:00
|
|
|
var owner user.ID
|
2024-01-17 14:26:02 +00:00
|
|
|
if err := owner.DecodeString(ownerStr); err == nil {
|
|
|
|
version.Owner = &owner
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-17 14:26:02 +00:00
|
|
|
|
2022-05-20 15:02:00 +00:00
|
|
|
return version
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 10:59:19 +00:00
|
|
|
func newMultipartInfoFromTreeNode(filePath string, treeNode *treeNode) (*data.MultipartInfo, error) {
|
|
|
|
uploadID, _ := treeNode.Get(uploadIDKV)
|
|
|
|
if uploadID == "" {
|
|
|
|
return nil, fmt.Errorf("it's not a multipart node")
|
|
|
|
}
|
|
|
|
|
|
|
|
multipartInfo := &data.MultipartInfo{
|
|
|
|
ID: treeNode.ID,
|
|
|
|
Key: filePath,
|
|
|
|
UploadID: uploadID,
|
|
|
|
Meta: treeNode.Meta,
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerID, _ := treeNode.Get(ownerKV)
|
|
|
|
_ = multipartInfo.Owner.DecodeString(ownerID)
|
|
|
|
|
|
|
|
created, _ := treeNode.Get(createdKV)
|
|
|
|
if utcMilli, err := strconv.ParseInt(created, 10, 64); err == nil {
|
|
|
|
multipartInfo.Created = time.UnixMilli(utcMilli)
|
|
|
|
}
|
|
|
|
|
2023-12-04 06:42:25 +00:00
|
|
|
finished, _ := treeNode.Get(finishedKV)
|
|
|
|
if flag, err := strconv.ParseBool(finished); err == nil {
|
|
|
|
multipartInfo.Finished = flag
|
|
|
|
}
|
|
|
|
|
2023-08-16 10:59:19 +00:00
|
|
|
return multipartInfo, nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:41:10 +00:00
|
|
|
func newMultipartInfo(node NodeResponse) (*data.MultipartInfo, error) {
|
|
|
|
multipartInfo := &data.MultipartInfo{
|
2023-03-14 14:31:15 +00:00
|
|
|
ID: node.GetNodeID(),
|
2022-05-24 08:41:10 +00:00
|
|
|
Meta: make(map[string]string, len(node.GetMeta())),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range node.GetMeta() {
|
|
|
|
switch kv.GetKey() {
|
|
|
|
case uploadIDKV:
|
|
|
|
multipartInfo.UploadID = string(kv.GetValue())
|
2023-03-14 14:31:15 +00:00
|
|
|
case FileNameKey:
|
2022-09-07 08:16:42 +00:00
|
|
|
multipartInfo.Key = string(kv.GetValue())
|
2022-05-24 08:41:10 +00:00
|
|
|
case createdKV:
|
|
|
|
if utcMilli, err := strconv.ParseInt(string(kv.GetValue()), 10, 64); err == nil {
|
|
|
|
multipartInfo.Created = time.UnixMilli(utcMilli)
|
|
|
|
}
|
|
|
|
case ownerKV:
|
|
|
|
_ = multipartInfo.Owner.DecodeString(string(kv.GetValue()))
|
2023-12-04 06:42:25 +00:00
|
|
|
case finishedKV:
|
|
|
|
if isFinished, err := strconv.ParseBool(string(kv.GetValue())); err == nil {
|
|
|
|
multipartInfo.Finished = isFinished
|
|
|
|
}
|
2022-05-24 08:41:10 +00:00
|
|
|
default:
|
|
|
|
multipartInfo.Meta[kv.GetKey()] = string(kv.GetValue())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if multipartInfo.UploadID == "" {
|
|
|
|
return nil, fmt.Errorf("it's not a multipart node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return multipartInfo, nil
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
func newPartInfo(node NodeResponse) (*data.PartInfo, error) {
|
2022-05-31 12:38:06 +00:00
|
|
|
var err error
|
2022-05-24 11:30:37 +00:00
|
|
|
partInfo := &data.PartInfo{}
|
|
|
|
|
|
|
|
for _, kv := range node.GetMeta() {
|
2022-05-31 12:38:06 +00:00
|
|
|
value := string(kv.GetValue())
|
2022-05-24 11:30:37 +00:00
|
|
|
switch kv.GetKey() {
|
|
|
|
case partNumberKV:
|
2022-05-31 12:38:06 +00:00
|
|
|
if partInfo.Number, err = strconv.Atoi(value); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid part number: %w", err)
|
|
|
|
}
|
2022-05-24 11:30:37 +00:00
|
|
|
case oidKV:
|
2022-05-31 12:38:06 +00:00
|
|
|
if err = partInfo.OID.DecodeString(value); err != nil {
|
2022-05-24 11:30:37 +00:00
|
|
|
return nil, fmt.Errorf("invalid oid: %w", err)
|
|
|
|
}
|
2022-05-31 12:38:06 +00:00
|
|
|
case etagKV:
|
|
|
|
partInfo.ETag = value
|
|
|
|
case sizeKV:
|
2023-06-01 13:45:28 +00:00
|
|
|
if partInfo.Size, err = strconv.ParseUint(value, 10, 64); err != nil {
|
2022-05-31 12:38:06 +00:00
|
|
|
return nil, fmt.Errorf("invalid part size: %w", err)
|
|
|
|
}
|
|
|
|
case createdKV:
|
|
|
|
var utcMilli int64
|
|
|
|
if utcMilli, err = strconv.ParseInt(value, 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid created timestamp: %w", err)
|
|
|
|
}
|
|
|
|
partInfo.Created = time.UnixMilli(utcMilli)
|
2023-10-02 08:52:07 +00:00
|
|
|
case md5KV:
|
|
|
|
partInfo.MD5 = value
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if partInfo.Number <= 0 {
|
|
|
|
return nil, fmt.Errorf("it's not a part node")
|
|
|
|
}
|
|
|
|
|
|
|
|
return partInfo, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetSettingsNode(ctx context.Context, bktInfo *data.BucketInfo) (*data.BucketSettings, error) {
|
2022-07-19 14:58:18 +00:00
|
|
|
keysToReturn := []string{versioningKV, lockConfigurationKV}
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{settingsFileName}, keysToReturn)
|
2022-04-22 07:18:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get node: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:30:19 +00:00
|
|
|
settings := &data.BucketSettings{Versioning: data.VersioningUnversioned}
|
2022-07-19 14:58:18 +00:00
|
|
|
if versioningValue, ok := node.Get(versioningKV); ok {
|
|
|
|
settings.Versioning = versioningValue
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if lockConfigurationValue, ok := node.Get(lockConfigurationKV); ok {
|
|
|
|
if settings.LockConfiguration, err = parseLockConfiguration(lockConfigurationValue); err != nil {
|
|
|
|
return nil, fmt.Errorf("settings node: invalid lock configuration: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) PutSettingsNode(ctx context.Context, bktInfo *data.BucketInfo, settings *data.BucketSettings) error {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{settingsFileName}, []string{})
|
2022-05-12 01:48:17 +00:00
|
|
|
isErrNotFound := errors.Is(err, layer.ErrNodeNotFound)
|
2022-04-22 07:18:21 +00:00
|
|
|
if err != nil && !isErrNotFound {
|
|
|
|
return fmt.Errorf("couldn't get node: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := metaFromSettings(settings)
|
|
|
|
|
|
|
|
if isErrNotFound {
|
2023-03-14 14:31:15 +00:00
|
|
|
_, err = c.service.AddNode(ctx, bktInfo, systemTree, 0, meta)
|
2022-04-22 07:18:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
return c.service.MoveNode(ctx, bktInfo, systemTree, node.ID, 0, meta)
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetNotificationConfigurationNode(ctx context.Context, bktInfo *data.BucketInfo) (oid.ID, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{notifConfFileName}, []string{oidKV})
|
2022-05-12 01:50:52 +00:00
|
|
|
if err != nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
2022-05-12 01:50:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
return node.ObjID, nil
|
2022-05-17 13:52:51 +00:00
|
|
|
}
|
2022-05-12 01:50:52 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) PutNotificationConfigurationNode(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID) (oid.ID, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{notifConfFileName}, []string{oidKV})
|
2022-05-17 13:52:51 +00:00
|
|
|
isErrNotFound := errors.Is(err, layer.ErrNodeNotFound)
|
|
|
|
if err != nil && !isErrNotFound {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("couldn't get node: %w", err)
|
2022-05-12 01:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
meta := make(map[string]string)
|
2023-03-14 14:31:15 +00:00
|
|
|
meta[FileNameKey] = notifConfFileName
|
2022-05-17 14:56:05 +00:00
|
|
|
meta[oidKV] = objID.EncodeToString()
|
2022-05-12 01:50:52 +00:00
|
|
|
|
2022-05-17 13:52:51 +00:00
|
|
|
if isErrNotFound {
|
2023-03-14 14:31:15 +00:00
|
|
|
if _, err = c.service.AddNode(ctx, bktInfo, systemTree, 0, meta); err != nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
|
|
|
}
|
|
|
|
return oid.ID{}, layer.ErrNoNodeToRemove
|
2022-05-17 13:52:51 +00:00
|
|
|
}
|
2022-05-12 01:50:52 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
return node.ObjID, c.service.MoveNode(ctx, bktInfo, systemTree, node.ID, 0, meta)
|
2022-05-12 01:50:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetBucketCORS(ctx context.Context, bktInfo *data.BucketInfo) (oid.ID, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{corsFilename}, []string{oidKV})
|
2022-05-12 02:33:03 +00:00
|
|
|
if err != nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
2022-05-12 02:33:03 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
return node.ObjID, nil
|
2022-05-17 13:52:51 +00:00
|
|
|
}
|
2022-05-12 02:33:03 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) PutBucketCORS(ctx context.Context, bktInfo *data.BucketInfo, objID oid.ID) (oid.ID, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{corsFilename}, []string{oidKV})
|
2022-05-17 13:52:51 +00:00
|
|
|
isErrNotFound := errors.Is(err, layer.ErrNodeNotFound)
|
|
|
|
if err != nil && !isErrNotFound {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, fmt.Errorf("couldn't get node: %w", err)
|
2022-05-12 02:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
meta := make(map[string]string)
|
2023-03-14 14:31:15 +00:00
|
|
|
meta[FileNameKey] = corsFilename
|
2022-05-17 14:56:05 +00:00
|
|
|
meta[oidKV] = objID.EncodeToString()
|
2022-05-12 02:33:03 +00:00
|
|
|
|
2022-05-17 13:52:51 +00:00
|
|
|
if isErrNotFound {
|
2023-03-14 14:31:15 +00:00
|
|
|
if _, err = c.service.AddNode(ctx, bktInfo, systemTree, 0, meta); err != nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
|
|
|
}
|
|
|
|
return oid.ID{}, layer.ErrNoNodeToRemove
|
2022-05-17 13:52:51 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
return node.ObjID, c.service.MoveNode(ctx, bktInfo, systemTree, node.ID, 0, meta)
|
2022-05-12 02:33:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) DeleteBucketCORS(ctx context.Context, bktInfo *data.BucketInfo) (oid.ID, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{corsFilename}, []string{oidKV})
|
2022-05-17 13:52:51 +00:00
|
|
|
if err != nil && !errors.Is(err, layer.ErrNodeNotFound) {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
2022-05-17 13:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node != nil {
|
2023-03-14 14:31:15 +00:00
|
|
|
return node.ObjID, c.service.RemoveNode(ctx, bktInfo, systemTree, node.ID)
|
2022-05-17 13:52:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, layer.ErrNoNodeToRemove
|
2022-05-12 02:33:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetObjectTagging(ctx context.Context, bktInfo *data.BucketInfo, objVersion *data.NodeVersion) (map[string]string, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
tagNode, err := c.getTreeNode(ctx, bktInfo, objVersion.ID, isTagKV)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:25:47 +00:00
|
|
|
return getObjectTagging(tagNode), nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func getObjectTagging(tagNode *treeNode) map[string]string {
|
2022-05-24 06:58:33 +00:00
|
|
|
if tagNode == nil {
|
2022-05-31 14:25:47 +00:00
|
|
|
return nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 10:16:51 +00:00
|
|
|
meta := make(map[string]string)
|
|
|
|
|
|
|
|
for key, val := range tagNode.Meta {
|
2022-06-15 14:43:51 +00:00
|
|
|
if strings.HasPrefix(key, userDefinedTagPrefix) {
|
|
|
|
meta[strings.TrimPrefix(key, userDefinedTagPrefix)] = val
|
2022-05-25 10:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-24 06:58:33 +00:00
|
|
|
|
2022-05-31 14:25:47 +00:00
|
|
|
return meta
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) PutObjectTagging(ctx context.Context, bktInfo *data.BucketInfo, objVersion *data.NodeVersion, tagSet map[string]string) error {
|
2022-09-13 09:44:18 +00:00
|
|
|
tagNode, err := c.getTreeNode(ctx, bktInfo, objVersion.ID, isTagKV)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-25 10:16:51 +00:00
|
|
|
treeTagSet := make(map[string]string)
|
|
|
|
treeTagSet[isTagKV] = "true"
|
|
|
|
|
|
|
|
for key, val := range tagSet {
|
2022-06-15 14:43:51 +00:00
|
|
|
treeTagSet[userDefinedTagPrefix+key] = val
|
2022-05-25 10:16:51 +00:00
|
|
|
}
|
2022-05-24 06:58:33 +00:00
|
|
|
|
|
|
|
if tagNode == nil {
|
2023-03-14 14:31:15 +00:00
|
|
|
_, err = c.service.AddNode(ctx, bktInfo, versionTree, objVersion.ID, treeTagSet)
|
2022-05-24 06:58:33 +00:00
|
|
|
} else {
|
2023-03-14 14:31:15 +00:00
|
|
|
err = c.service.MoveNode(ctx, bktInfo, versionTree, tagNode.ID, objVersion.ID, treeTagSet)
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) DeleteObjectTagging(ctx context.Context, bktInfo *data.BucketInfo, objVersion *data.NodeVersion) error {
|
2023-10-18 06:52:37 +00:00
|
|
|
return c.PutObjectTagging(ctx, bktInfo, objVersion, nil)
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetBucketTagging(ctx context.Context, bktInfo *data.BucketInfo) (map[string]string, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNodeWithAllAttributes(ctx, bktInfo, []string{bucketTaggingFilename})
|
2022-05-25 01:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-25 10:16:51 +00:00
|
|
|
tags := make(map[string]string)
|
2022-05-25 01:58:25 +00:00
|
|
|
|
2022-05-25 10:16:51 +00:00
|
|
|
for key, val := range node.Meta {
|
2022-06-15 14:43:51 +00:00
|
|
|
if strings.HasPrefix(key, userDefinedTagPrefix) {
|
|
|
|
tags[strings.TrimPrefix(key, userDefinedTagPrefix)] = val
|
2022-05-25 10:16:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
2022-05-25 01:58:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) PutBucketTagging(ctx context.Context, bktInfo *data.BucketInfo, tagSet map[string]string) error {
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getSystemNode(ctx, bktInfo, []string{bucketTaggingFilename}, []string{})
|
2022-05-25 01:58:25 +00:00
|
|
|
isErrNotFound := errors.Is(err, layer.ErrNodeNotFound)
|
|
|
|
if err != nil && !isErrNotFound {
|
|
|
|
return fmt.Errorf("couldn't get node: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-25 10:16:51 +00:00
|
|
|
treeTagSet := make(map[string]string)
|
2023-03-14 14:31:15 +00:00
|
|
|
treeTagSet[FileNameKey] = bucketTaggingFilename
|
2022-05-25 10:16:51 +00:00
|
|
|
|
|
|
|
for key, val := range tagSet {
|
2022-06-15 14:43:51 +00:00
|
|
|
treeTagSet[userDefinedTagPrefix+key] = val
|
2022-05-25 10:16:51 +00:00
|
|
|
}
|
2022-05-25 01:58:25 +00:00
|
|
|
|
|
|
|
if isErrNotFound {
|
2023-03-14 14:31:15 +00:00
|
|
|
_, err = c.service.AddNode(ctx, bktInfo, systemTree, 0, treeTagSet)
|
2022-05-25 01:58:25 +00:00
|
|
|
} else {
|
2023-03-14 14:31:15 +00:00
|
|
|
err = c.service.MoveNode(ctx, bktInfo, systemTree, node.ID, 0, treeTagSet)
|
2022-05-25 01:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) DeleteBucketTagging(ctx context.Context, bktInfo *data.BucketInfo) error {
|
2023-10-18 06:52:37 +00:00
|
|
|
return c.PutBucketTagging(ctx, bktInfo, nil)
|
2022-05-25 01:58:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getTreeNode(ctx context.Context, bktInfo *data.BucketInfo, nodeID uint64, key string) (*treeNode, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
nodes, err := c.getTreeNodes(ctx, bktInfo, nodeID, key)
|
2022-06-01 10:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// if there will be many allocations, consider having separate
|
|
|
|
// implementations of 'getTreeNode' and 'getTreeNodes'
|
|
|
|
return nodes[key], nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getTreeNodes(ctx context.Context, bktInfo *data.BucketInfo, nodeID uint64, keys ...string) (map[string]*treeNode, error) {
|
|
|
|
subtree, err := c.service.GetSubTree(ctx, bktInfo, versionTree, nodeID, 2)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
treeNodes := make(map[string]*treeNode, len(keys))
|
2022-05-24 06:58:33 +00:00
|
|
|
|
|
|
|
for _, s := range subtree {
|
|
|
|
node, err := newTreeNode(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-01 10:57:23 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
if _, ok := node.Get(key); ok {
|
|
|
|
treeNodes[key] = node
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(treeNodes) == len(keys) {
|
2022-05-24 06:58:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:57:23 +00:00
|
|
|
return treeNodes, nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetVersions(ctx context.Context, bktInfo *data.BucketInfo, filepath string) ([]*data.NodeVersion, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
return c.getVersions(ctx, bktInfo, versionTree, filepath, false)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetLatestVersion(ctx context.Context, bktInfo *data.BucketInfo, objectName string) (*data.NodeVersion, error) {
|
2023-10-02 08:52:07 +00:00
|
|
|
meta := []string{oidKV, isUnversionedKV, isDeleteMarkerKV, etagKV, sizeKV, md5KV}
|
2022-05-23 09:53:48 +00:00
|
|
|
path := pathFromName(objectName)
|
2022-05-17 14:56:05 +00:00
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
p := &GetNodesParams{
|
2022-09-13 09:44:18 +00:00
|
|
|
BktInfo: bktInfo,
|
2022-06-10 11:57:41 +00:00
|
|
|
TreeID: versionTree,
|
|
|
|
Path: path,
|
|
|
|
Meta: meta,
|
2023-09-28 09:12:29 +00:00
|
|
|
LatestOnly: false,
|
2022-06-10 11:57:41 +00:00
|
|
|
AllAttrs: false,
|
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
nodes, err := c.service.GetNodes(ctx, p)
|
2022-06-10 11:57:41 +00:00
|
|
|
if err != nil {
|
2022-09-13 10:35:30 +00:00
|
|
|
return nil, err
|
2022-06-10 11:57:41 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 09:12:29 +00:00
|
|
|
latestNode, err := getLatestNode(nodes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNodeVersion(objectName, latestNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLatestNode(nodes []NodeResponse) (NodeResponse, error) {
|
|
|
|
var (
|
|
|
|
maxCreationTime uint64
|
|
|
|
targetIndexNode = -1
|
|
|
|
)
|
|
|
|
|
|
|
|
for i, node := range nodes {
|
|
|
|
currentCreationTime := node.GetTimestamp()
|
|
|
|
if checkExistOID(node.GetMeta()) && currentCreationTime > maxCreationTime {
|
|
|
|
maxCreationTime = currentCreationTime
|
|
|
|
targetIndexNode = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if targetIndexNode == -1 {
|
2022-06-10 11:57:41 +00:00
|
|
|
return nil, layer.ErrNodeNotFound
|
|
|
|
}
|
|
|
|
|
2023-09-28 09:12:29 +00:00
|
|
|
return nodes[targetIndexNode], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkExistOID(meta []Meta) bool {
|
|
|
|
for _, kv := range meta {
|
|
|
|
if kv.GetKey() == "OID" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 08:16:42 +00:00
|
|
|
// pathFromName splits name by '/'.
|
2022-05-23 09:53:48 +00:00
|
|
|
func pathFromName(objectName string) []string {
|
2022-09-07 08:16:42 +00:00
|
|
|
return strings.Split(objectName, separator)
|
2022-05-23 09:53:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetLatestVersionsByPrefix(ctx context.Context, bktInfo *data.BucketInfo, prefix string) ([]*data.NodeVersion, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
return c.getVersionsByPrefix(ctx, bktInfo, prefix, true)
|
2022-05-24 08:41:10 +00:00
|
|
|
}
|
2022-05-20 15:02:00 +00:00
|
|
|
|
2023-10-09 06:57:33 +00:00
|
|
|
type DummySubTreeStream struct {
|
|
|
|
data NodeResponse
|
|
|
|
read bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DummySubTreeStream) Next() (NodeResponse, error) {
|
|
|
|
if s.read {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
s.read = true
|
|
|
|
return s.data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type LatestVersionsByPrefixStreamImpl struct {
|
|
|
|
ctx context.Context
|
|
|
|
rootID uint64
|
|
|
|
intermediateRootID uint64
|
|
|
|
service ServiceClient
|
|
|
|
bktInfo *data.BucketInfo
|
|
|
|
mainStream SubTreeStream
|
|
|
|
innerStream SubTreeStream
|
|
|
|
headPrefix string
|
|
|
|
tailPrefix string
|
|
|
|
namesMap map[uint64]string
|
|
|
|
ended bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LatestVersionsByPrefixStreamImpl) Next(ctx context.Context) (*data.NodeVersion, error) {
|
|
|
|
const latestOnly = true
|
|
|
|
|
|
|
|
if s.ended {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.innerStream == nil {
|
|
|
|
node, err := s.mainStream.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("main stream next: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.GetNodeID() == s.rootID || !strings.HasPrefix(getFilename(node), s.tailPrefix) {
|
|
|
|
return s.Next(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.GetParentID() == s.rootID {
|
|
|
|
s.intermediateRootID = node.GetNodeID()
|
|
|
|
}
|
|
|
|
|
|
|
|
if isIntermediate(node) {
|
|
|
|
s.innerStream, err = s.service.GetSubTreeStream(s.ctx, s.bktInfo, versionTree, node.GetNodeID(), maxGetSubTreeDepth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get sub tree node from main stream: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.innerStream = &DummySubTreeStream{data: node}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node, err := s.innerStream.Next()
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
s.innerStream = nil
|
2023-10-17 07:43:58 +00:00
|
|
|
s.namesMap = map[uint64]string{}
|
2023-10-09 06:57:33 +00:00
|
|
|
return s.Next(ctx)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("inner stream: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
treeNode, fileName, err := parseTreeNode(node)
|
|
|
|
if err != nil {
|
|
|
|
return s.Next(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
var parentPrefix string
|
|
|
|
if s.headPrefix != "" { // The root of subTree can also have a parent
|
|
|
|
parentPrefix = strings.TrimSuffix(s.headPrefix, separator) + separator // To avoid 'foo//bar'
|
|
|
|
}
|
|
|
|
|
|
|
|
var filepath string
|
|
|
|
if treeNode.ID != s.intermediateRootID {
|
|
|
|
if filepath, err = formFilePath(node, fileName, s.namesMap); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid node order: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filepath = parentPrefix + fileName
|
|
|
|
s.namesMap[treeNode.ID] = filepath
|
|
|
|
}
|
|
|
|
|
|
|
|
if treeNode.ObjID.Equals(oid.ID{}) { // The node can be intermediate but we still want to update namesMap
|
|
|
|
return s.Next(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newNodeVersionFromTreeNode(filepath, treeNode), nil
|
|
|
|
}
|
|
|
|
|
2023-10-16 06:27:21 +00:00
|
|
|
func (c *Tree) GetLatestVersionsByPrefixStream(ctx context.Context, bktInfo *data.BucketInfo, prefix string) (data.VersionsStream, error) {
|
2023-10-09 06:57:33 +00:00
|
|
|
mainStream, tailPrefix, rootID, err := c.getSubTreeByPrefixMainStream(ctx, bktInfo, versionTree, prefix)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return &LatestVersionsByPrefixStreamImpl{ended: true}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LatestVersionsByPrefixStreamImpl{
|
|
|
|
ctx: ctx,
|
|
|
|
namesMap: map[uint64]string{},
|
|
|
|
rootID: rootID,
|
|
|
|
service: c.service,
|
|
|
|
bktInfo: bktInfo,
|
|
|
|
mainStream: mainStream,
|
|
|
|
headPrefix: strings.TrimSuffix(prefix, tailPrefix),
|
|
|
|
tailPrefix: tailPrefix,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Tree) getSubTreeByPrefixMainStream(ctx context.Context, bktInfo *data.BucketInfo, treeID, prefix string) (SubTreeStream, string, uint64, error) {
|
|
|
|
rootID, tailPrefix, err := c.determinePrefixNode(ctx, bktInfo, treeID, prefix)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, layer.ErrNodeNotFound) {
|
|
|
|
return nil, "", 0, io.EOF
|
|
|
|
}
|
|
|
|
return nil, "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
subTree, err := c.service.GetSubTreeStream(ctx, bktInfo, treeID, rootID, 2)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, layer.ErrNodeNotFound) {
|
|
|
|
return nil, "", 0, io.EOF
|
|
|
|
}
|
|
|
|
return nil, "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return subTree, tailPrefix, rootID, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) determinePrefixNode(ctx context.Context, bktInfo *data.BucketInfo, treeID, prefix string) (uint64, string, error) {
|
2022-05-24 08:41:10 +00:00
|
|
|
var rootID uint64
|
|
|
|
path := strings.Split(prefix, separator)
|
|
|
|
tailPrefix := path[len(path)-1]
|
|
|
|
|
|
|
|
if len(path) > 1 {
|
|
|
|
var err error
|
2022-09-13 09:44:18 +00:00
|
|
|
rootID, err = c.getPrefixNodeID(ctx, bktInfo, treeID, path[:len(path)-1])
|
2022-05-24 08:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, "", err
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:41:10 +00:00
|
|
|
return rootID, tailPrefix, nil
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getPrefixNodeID(ctx context.Context, bktInfo *data.BucketInfo, treeID string, prefixPath []string) (uint64, error) {
|
|
|
|
p := &GetNodesParams{
|
2022-09-13 09:44:18 +00:00
|
|
|
BktInfo: bktInfo,
|
2022-05-24 08:41:10 +00:00
|
|
|
TreeID: treeID,
|
2022-05-25 01:58:25 +00:00
|
|
|
Path: prefixPath,
|
|
|
|
LatestOnly: false,
|
2022-05-24 08:41:10 +00:00
|
|
|
AllAttrs: true,
|
2022-05-25 01:58:25 +00:00
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
nodes, err := c.service.GetNodes(ctx, p)
|
2022-05-20 15:02:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var intermediateNodes []uint64
|
|
|
|
for _, node := range nodes {
|
2022-06-07 15:34:43 +00:00
|
|
|
if isIntermediate(node) {
|
2023-03-14 14:31:15 +00:00
|
|
|
intermediateNodes = append(intermediateNodes, node.GetNodeID())
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(intermediateNodes) == 0 {
|
|
|
|
return 0, layer.ErrNodeNotFound
|
|
|
|
}
|
|
|
|
if len(intermediateNodes) > 1 {
|
|
|
|
return 0, fmt.Errorf("found more than one intermediate nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
return intermediateNodes[0], nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getSubTreeByPrefix(ctx context.Context, bktInfo *data.BucketInfo, treeID, prefix string, latestOnly bool) ([]NodeResponse, string, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
rootID, tailPrefix, err := c.determinePrefixNode(ctx, bktInfo, treeID, prefix)
|
2022-05-24 08:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, layer.ErrNodeNotFound) {
|
2022-06-10 11:57:41 +00:00
|
|
|
return nil, "", nil
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
2022-06-10 11:57:41 +00:00
|
|
|
return nil, "", err
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
subTree, err := c.service.GetSubTree(ctx, bktInfo, treeID, rootID, 2)
|
2022-05-24 08:41:10 +00:00
|
|
|
if err != nil {
|
2022-06-06 14:07:38 +00:00
|
|
|
if errors.Is(err, layer.ErrNodeNotFound) {
|
2022-06-10 11:57:41 +00:00
|
|
|
return nil, "", nil
|
2022-06-06 14:07:38 +00:00
|
|
|
}
|
2022-06-10 11:57:41 +00:00
|
|
|
return nil, "", err
|
2022-05-24 08:41:10 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
nodesMap := make(map[string][]NodeResponse, len(subTree))
|
2022-05-24 08:41:10 +00:00
|
|
|
for _, node := range subTree {
|
2023-03-14 14:31:15 +00:00
|
|
|
if node.GetNodeID() == rootID {
|
2022-07-21 08:18:41 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := getFilename(node)
|
|
|
|
if !strings.HasPrefix(fileName, tailPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := nodesMap[fileName]
|
|
|
|
|
2022-07-26 15:36:05 +00:00
|
|
|
// Add all nodes if flag latestOnly is false.
|
|
|
|
// Add all intermediate nodes (actually should be exactly one intermediate node with the same name)
|
|
|
|
// and only latest leaf (object) nodes. To do this store and replace last leaf (object) node in nodes[0]
|
|
|
|
if len(nodes) == 0 {
|
2023-03-14 14:31:15 +00:00
|
|
|
nodes = []NodeResponse{node}
|
2022-07-26 15:36:05 +00:00
|
|
|
} else if !latestOnly || isIntermediate(node) {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
} else if isIntermediate(nodes[0]) {
|
2023-03-14 14:31:15 +00:00
|
|
|
nodes = append([]NodeResponse{node}, nodes...)
|
2022-07-26 15:36:05 +00:00
|
|
|
} else if node.GetTimestamp() > nodes[0].GetTimestamp() {
|
|
|
|
nodes[0] = node
|
2022-05-24 08:41:10 +00:00
|
|
|
}
|
2022-07-21 08:18:41 +00:00
|
|
|
|
|
|
|
nodesMap[fileName] = nodes
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
result := make([]NodeResponse, 0, len(subTree))
|
2022-07-21 08:18:41 +00:00
|
|
|
for _, nodes := range nodesMap {
|
|
|
|
result = append(result, nodes...)
|
2022-05-24 08:41:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 11:57:41 +00:00
|
|
|
return result, strings.TrimSuffix(prefix, tailPrefix), nil
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func getFilename(node NodeResponse) string {
|
2022-05-20 15:02:00 +00:00
|
|
|
for _, kv := range node.GetMeta() {
|
2023-03-14 14:31:15 +00:00
|
|
|
if kv.GetKey() == FileNameKey {
|
2022-07-21 08:18:41 +00:00
|
|
|
return string(kv.GetValue())
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 08:18:41 +00:00
|
|
|
return ""
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 15:36:05 +00:00
|
|
|
func isIntermediate(node NodeResponse) bool {
|
2022-05-24 08:41:10 +00:00
|
|
|
if len(node.GetMeta()) != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
return node.GetMeta()[0].GetKey() == FileNameKey
|
2022-05-24 08:41:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 06:57:33 +00:00
|
|
|
func (c *Tree) getSubTreeVersionsOld(ctx context.Context, bktInfo *data.BucketInfo, node NodeResponse, parentFilePath string, latestOnly bool) ([]*data.NodeVersion, error) {
|
|
|
|
return c.getSubTreeVersions(ctx, bktInfo, node, parentFilePath, latestOnly, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Tree) getSubTreeVersions(ctx context.Context, bktInfo *data.BucketInfo, node NodeResponse, parentFilePath string, latestOnly, skipLeafs bool) ([]*data.NodeVersion, error) {
|
|
|
|
var err error
|
|
|
|
subTree := []NodeResponse{node}
|
|
|
|
if !skipLeafs || isIntermediate(node) {
|
|
|
|
subTree, err = c.service.GetSubTree(ctx, bktInfo, versionTree, node.GetNodeID(), maxGetSubTreeDepth)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 11:57:41 +00:00
|
|
|
var parentPrefix string
|
|
|
|
if parentFilePath != "" { // The root of subTree can also have a parent
|
|
|
|
parentPrefix = strings.TrimSuffix(parentFilePath, separator) + separator // To avoid 'foo//bar'
|
|
|
|
}
|
2022-05-23 06:20:57 +00:00
|
|
|
|
2022-06-10 11:57:41 +00:00
|
|
|
var emptyOID oid.ID
|
|
|
|
var filepath string
|
|
|
|
namesMap := make(map[uint64]string, len(subTree))
|
2022-05-20 15:02:00 +00:00
|
|
|
versions := make(map[string][]*data.NodeVersion, len(subTree))
|
2022-06-10 11:57:41 +00:00
|
|
|
|
|
|
|
for i, node := range subTree {
|
|
|
|
treeNode, fileName, err := parseTreeNode(node)
|
|
|
|
if err != nil {
|
2022-05-20 08:26:35 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-06-10 11:57:41 +00:00
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
if filepath, err = formFilePath(node, fileName, namesMap); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid node order: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filepath = parentPrefix + fileName
|
|
|
|
namesMap[treeNode.ID] = filepath
|
|
|
|
}
|
|
|
|
|
|
|
|
if treeNode.ObjID.Equals(emptyOID) { // The node can be intermediate but we still want to update namesMap
|
2022-05-20 08:26:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
key := formLatestNodeKey(node.GetParentID(), fileName)
|
2022-05-20 15:02:00 +00:00
|
|
|
versionNodes, ok := versions[key]
|
|
|
|
if !ok {
|
2022-06-10 11:57:41 +00:00
|
|
|
versionNodes = []*data.NodeVersion{newNodeVersionFromTreeNode(filepath, treeNode)}
|
2022-05-20 15:02:00 +00:00
|
|
|
} else if !latestOnly {
|
2022-06-10 11:57:41 +00:00
|
|
|
versionNodes = append(versionNodes, newNodeVersionFromTreeNode(filepath, treeNode))
|
2022-05-20 15:02:00 +00:00
|
|
|
} else if versionNodes[0].Timestamp <= treeNode.TimeStamp {
|
2022-06-10 11:57:41 +00:00
|
|
|
versionNodes[0] = newNodeVersionFromTreeNode(filepath, treeNode)
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
2022-05-20 15:02:00 +00:00
|
|
|
|
|
|
|
versions[key] = versionNodes
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:02:00 +00:00
|
|
|
result := make([]*data.NodeVersion, 0, len(versions)) // consider use len(subTree)
|
|
|
|
for _, version := range versions {
|
2024-01-17 14:26:02 +00:00
|
|
|
if latestOnly && version[0].IsDeleteMarker {
|
2022-05-20 08:26:35 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-05-20 15:02:00 +00:00
|
|
|
result = append(result, version...)
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func formFilePath(node NodeResponse, fileName string, namesMap map[uint64]string) (string, error) {
|
|
|
|
parentPath, ok := namesMap[node.GetParentID()]
|
2022-06-10 11:57:41 +00:00
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("couldn't get parent path")
|
|
|
|
}
|
|
|
|
|
|
|
|
filepath := parentPath + separator + fileName
|
2023-03-14 14:31:15 +00:00
|
|
|
namesMap[node.GetNodeID()] = filepath
|
2022-06-10 11:57:41 +00:00
|
|
|
|
|
|
|
return filepath, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func parseTreeNode(node NodeResponse) (*treeNode, string, error) {
|
2022-06-10 11:57:41 +00:00
|
|
|
treeNode, err := newTreeNode(node)
|
|
|
|
if err != nil { // invalid OID attribute
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName, ok := treeNode.FileName()
|
|
|
|
if !ok {
|
|
|
|
return nil, "", fmt.Errorf("doesn't contain FileName")
|
|
|
|
}
|
|
|
|
|
|
|
|
return treeNode, fileName, nil
|
|
|
|
}
|
|
|
|
|
2022-05-20 08:26:35 +00:00
|
|
|
func formLatestNodeKey(parentID uint64, fileName string) string {
|
2022-06-10 11:57:41 +00:00
|
|
|
return strconv.FormatUint(parentID, 10) + "." + fileName
|
2022-05-20 08:26:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetAllVersionsByPrefix(ctx context.Context, bktInfo *data.BucketInfo, prefix string) ([]*data.NodeVersion, error) {
|
2023-10-09 06:57:33 +00:00
|
|
|
return c.getVersionsByPrefixOld(ctx, bktInfo, prefix, false)
|
2022-07-21 08:18:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 06:26:58 +00:00
|
|
|
func (c *Tree) GetAllVersionsByPrefixStream(ctx context.Context, bktInfo *data.BucketInfo, prefix string) (data.VersionsStream, error) {
|
|
|
|
//return c.getVersionsByPrefixOld(ctx, bktInfo, prefix, false)
|
|
|
|
|
|
|
|
mainStream, tailPrefix, rootID, err := c.getSubTreeByPrefixMainStream(ctx, bktInfo, versionTree, prefix)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
return &LatestVersionsByPrefixStreamImpl{ended: true}, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LatestVersionsByPrefixStreamImpl{
|
|
|
|
ctx: ctx,
|
|
|
|
namesMap: map[uint64]string{},
|
|
|
|
rootID: rootID,
|
|
|
|
service: c.service,
|
|
|
|
bktInfo: bktInfo,
|
|
|
|
mainStream: mainStream,
|
|
|
|
headPrefix: strings.TrimSuffix(prefix, tailPrefix),
|
|
|
|
tailPrefix: tailPrefix,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getVersionsByPrefix(ctx context.Context, bktInfo *data.BucketInfo, prefix string, latestOnly bool) ([]*data.NodeVersion, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
prefixNodes, headPrefix, err := c.getSubTreeByPrefix(ctx, bktInfo, versionTree, prefix, latestOnly)
|
2022-05-20 15:02:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*data.NodeVersion
|
2022-06-10 11:57:41 +00:00
|
|
|
for _, node := range prefixNodes {
|
2023-10-09 06:57:33 +00:00
|
|
|
versions, err := c.getSubTreeVersions(ctx, bktInfo, node, headPrefix, latestOnly, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, versions...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Tree) getVersionsByPrefixOld(ctx context.Context, bktInfo *data.BucketInfo, prefix string, latestOnly bool) ([]*data.NodeVersion, error) {
|
|
|
|
prefixNodes, headPrefix, err := c.getSubTreeByPrefix(ctx, bktInfo, versionTree, prefix, latestOnly)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*data.NodeVersion
|
|
|
|
for _, node := range prefixNodes {
|
|
|
|
versions, err := c.getSubTreeVersionsOld(ctx, bktInfo, node, headPrefix, latestOnly)
|
2022-05-24 08:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
2022-05-24 08:41:10 +00:00
|
|
|
result = append(result, versions...)
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetUnversioned(ctx context.Context, bktInfo *data.BucketInfo, filepath string) (*data.NodeVersion, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
return c.getUnversioned(ctx, bktInfo, versionTree, filepath)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getUnversioned(ctx context.Context, bktInfo *data.BucketInfo, treeID, filepath string) (*data.NodeVersion, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
nodes, err := c.getVersions(ctx, bktInfo, treeID, filepath, true)
|
2022-05-17 14:56:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-03 14:26:17 +00:00
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nil, layer.ErrNodeNotFound
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:26:17 +00:00
|
|
|
if len(nodes) > 1 {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.reqLogger(ctx).Debug(logs.FoundMoreThanOneUnversionedNode,
|
2023-04-03 14:26:17 +00:00
|
|
|
zap.String("treeID", treeID), zap.String("filepath", filepath))
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 14:26:17 +00:00
|
|
|
sort.Slice(nodes, func(i, j int) bool {
|
|
|
|
return nodes[i].Timestamp > nodes[j].Timestamp
|
|
|
|
})
|
|
|
|
|
2022-05-17 14:56:05 +00:00
|
|
|
return nodes[0], nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) AddVersion(ctx context.Context, bktInfo *data.BucketInfo, version *data.NodeVersion) (uint64, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
return c.addVersion(ctx, bktInfo, versionTree, version)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) RemoveVersion(ctx context.Context, bktInfo *data.BucketInfo, id uint64) error {
|
|
|
|
return c.service.RemoveNode(ctx, bktInfo, versionTree, id)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) CreateMultipartUpload(ctx context.Context, bktInfo *data.BucketInfo, info *data.MultipartInfo) error {
|
2022-05-24 08:41:10 +00:00
|
|
|
path := pathFromName(info.Key)
|
2022-08-23 14:54:24 +00:00
|
|
|
meta := metaFromMultipart(info, path[len(path)-1])
|
2023-03-14 14:31:15 +00:00
|
|
|
_, err := c.service.AddNodeByPath(ctx, bktInfo, systemTree, path[:len(path)-1], meta)
|
2022-05-23 14:34:13 +00:00
|
|
|
|
2022-08-29 10:37:17 +00:00
|
|
|
return err
|
2022-05-24 08:41:10 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetMultipartUploadsByPrefix(ctx context.Context, bktInfo *data.BucketInfo, prefix string) ([]*data.MultipartInfo, error) {
|
2023-08-16 10:59:19 +00:00
|
|
|
subTreeNodes, headPrefix, err := c.getSubTreeByPrefix(ctx, bktInfo, systemTree, prefix, false)
|
2022-05-24 08:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*data.MultipartInfo
|
|
|
|
for _, node := range subTreeNodes {
|
2023-08-16 10:59:19 +00:00
|
|
|
multipartUploads, err := c.getSubTreeMultipartUploads(ctx, bktInfo, node.GetNodeID(), headPrefix)
|
2022-05-24 08:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, multipartUploads...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-08-16 10:59:19 +00:00
|
|
|
func (c *Tree) getSubTreeMultipartUploads(ctx context.Context, bktInfo *data.BucketInfo, nodeID uint64, parentFilePath string) ([]*data.MultipartInfo, error) {
|
2023-03-14 14:31:15 +00:00
|
|
|
subTree, err := c.service.GetSubTree(ctx, bktInfo, systemTree, nodeID, maxGetSubTreeDepth)
|
2022-05-24 08:41:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-16 10:59:19 +00:00
|
|
|
var parentPrefix string
|
|
|
|
if parentFilePath != "" { // The root of subTree can also have a parent
|
|
|
|
parentPrefix = strings.TrimSuffix(parentFilePath, separator) + separator // To avoid 'foo//bar'
|
|
|
|
}
|
|
|
|
|
|
|
|
var filepath string
|
|
|
|
namesMap := make(map[uint64]string, len(subTree))
|
|
|
|
multiparts := make(map[string][]*data.MultipartInfo, len(subTree))
|
|
|
|
|
|
|
|
for i, node := range subTree {
|
|
|
|
treeNode, fileName, err := parseTreeNode(node)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if i != 0 {
|
|
|
|
if filepath, err = formFilePath(node, fileName, namesMap); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid node order: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filepath = parentPrefix + fileName
|
|
|
|
namesMap[treeNode.ID] = filepath
|
|
|
|
}
|
|
|
|
|
|
|
|
multipartInfo, err := newMultipartInfoFromTreeNode(filepath, treeNode)
|
2023-12-04 06:42:25 +00:00
|
|
|
if err != nil || multipartInfo.Finished {
|
2022-05-24 08:41:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-08-16 10:59:19 +00:00
|
|
|
|
|
|
|
key := formLatestNodeKey(node.GetParentID(), fileName)
|
|
|
|
multipartInfos, ok := multiparts[key]
|
|
|
|
if !ok {
|
|
|
|
multipartInfos = []*data.MultipartInfo{multipartInfo}
|
|
|
|
} else {
|
|
|
|
multipartInfos = append(multipartInfos, multipartInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
multiparts[key] = multipartInfos
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*data.MultipartInfo, 0, len(multiparts))
|
|
|
|
for _, multipartInfo := range multiparts {
|
|
|
|
result = append(result, multipartInfo...)
|
2022-05-24 08:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2022-05-23 14:34:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetMultipartUpload(ctx context.Context, bktInfo *data.BucketInfo, objectName, uploadID string) (*data.MultipartInfo, error) {
|
2022-05-24 11:30:37 +00:00
|
|
|
path := pathFromName(objectName)
|
2023-03-14 14:31:15 +00:00
|
|
|
p := &GetNodesParams{
|
2022-09-13 09:44:18 +00:00
|
|
|
BktInfo: bktInfo,
|
2022-05-24 11:30:37 +00:00
|
|
|
TreeID: systemTree,
|
|
|
|
Path: path,
|
|
|
|
AllAttrs: true,
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
nodes, err := c.service.GetNodes(ctx, p)
|
2022-05-24 11:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
info, err := newMultipartInfo(node)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if info.UploadID == uploadID {
|
2023-12-04 06:42:25 +00:00
|
|
|
if info.Finished {
|
|
|
|
break
|
|
|
|
}
|
2022-05-24 11:30:37 +00:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, layer.ErrNodeNotFound
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) AddPart(ctx context.Context, bktInfo *data.BucketInfo, multipartNodeID uint64, info *data.PartInfo) (oldObjIDToDelete oid.ID, err error) {
|
|
|
|
parts, err := c.service.GetSubTree(ctx, bktInfo, systemTree, multipartNodeID, 2)
|
2022-05-24 11:30:37 +00:00
|
|
|
if err != nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return oid.ID{}, err
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
meta := map[string]string{
|
|
|
|
partNumberKV: strconv.Itoa(info.Number),
|
|
|
|
oidKV: info.OID.EncodeToString(),
|
2023-06-01 13:45:28 +00:00
|
|
|
sizeKV: strconv.FormatUint(info.Size, 10),
|
2022-05-31 12:38:06 +00:00
|
|
|
createdKV: strconv.FormatInt(info.Created.UTC().UnixMilli(), 10),
|
|
|
|
etagKV: info.ETag,
|
2023-10-02 08:52:07 +00:00
|
|
|
md5KV: info.MD5,
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, part := range parts {
|
2023-03-14 14:31:15 +00:00
|
|
|
if part.GetNodeID() == multipartNodeID {
|
2022-05-24 11:30:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
partInfo, err := newPartInfo(part)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if partInfo.Number == info.Number {
|
2023-07-21 13:16:55 +00:00
|
|
|
return partInfo.OID, c.service.MoveNode(ctx, bktInfo, systemTree, part.GetNodeID(), multipartNodeID, meta)
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 13:16:55 +00:00
|
|
|
if _, err = c.service.AddNode(ctx, bktInfo, systemTree, multipartNodeID, meta); err != nil {
|
|
|
|
return oid.ID{}, err
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 13:16:55 +00:00
|
|
|
return oid.ID{}, layer.ErrNoNodeToRemove
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetParts(ctx context.Context, bktInfo *data.BucketInfo, multipartNodeID uint64) ([]*data.PartInfo, error) {
|
|
|
|
parts, err := c.service.GetSubTree(ctx, bktInfo, systemTree, multipartNodeID, 2)
|
2022-05-24 13:07:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*data.PartInfo, 0, len(parts))
|
|
|
|
for _, part := range parts {
|
2023-03-14 14:31:15 +00:00
|
|
|
if part.GetNodeID() == multipartNodeID {
|
2022-05-24 13:07:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
partInfo, err := newPartInfo(part)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result = append(result, partInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-12-04 06:42:25 +00:00
|
|
|
func (c *Tree) DeleteMultipartUpload(ctx context.Context, bktInfo *data.BucketInfo, multipartInfo *data.MultipartInfo) error {
|
|
|
|
err := c.service.RemoveNode(ctx, bktInfo, systemTree, multipartInfo.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
multipartInfo.Finished = true
|
|
|
|
|
|
|
|
return c.CreateMultipartUpload(ctx, bktInfo, multipartInfo)
|
2022-05-24 13:27:15 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) PutLock(ctx context.Context, bktInfo *data.BucketInfo, nodeID uint64, lock *data.LockInfo) error {
|
2022-05-26 13:11:14 +00:00
|
|
|
meta := map[string]string{isLockKV: "true"}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if lock.IsLegalHoldSet() {
|
|
|
|
meta[legalHoldOIDKV] = lock.LegalHold().EncodeToString()
|
2022-06-08 12:33:00 +00:00
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
if lock.IsRetentionSet() {
|
|
|
|
meta[retentionOIDKV] = lock.Retention().EncodeToString()
|
|
|
|
meta[untilDateKV] = lock.UntilDate()
|
|
|
|
if lock.IsCompliance() {
|
2022-05-26 13:11:14 +00:00
|
|
|
meta[isComplianceKV] = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if lock.ID() == 0 {
|
2023-03-14 14:31:15 +00:00
|
|
|
_, err := c.service.AddNode(ctx, bktInfo, versionTree, nodeID, meta)
|
2022-05-26 13:11:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
return c.service.MoveNode(ctx, bktInfo, versionTree, lock.ID(), nodeID, meta)
|
2022-05-26 13:11:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetLock(ctx context.Context, bktInfo *data.BucketInfo, nodeID uint64) (*data.LockInfo, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
lockNode, err := c.getTreeNode(ctx, bktInfo, nodeID, isLockKV)
|
2022-05-26 13:11:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 14:25:47 +00:00
|
|
|
return getLock(lockNode)
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func getLock(lockNode *treeNode) (*data.LockInfo, error) {
|
2022-05-26 13:11:14 +00:00
|
|
|
if lockNode == nil {
|
2022-06-27 09:33:36 +00:00
|
|
|
return &data.LockInfo{}, nil
|
2022-05-26 13:11:14 +00:00
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
lockInfo := data.NewLockInfo(lockNode.ID)
|
2022-05-26 13:11:14 +00:00
|
|
|
|
|
|
|
if legalHold, ok := lockNode.Get(legalHoldOIDKV); ok {
|
|
|
|
var legalHoldOID oid.ID
|
2022-05-31 14:25:47 +00:00
|
|
|
if err := legalHoldOID.DecodeString(legalHold); err != nil {
|
2022-05-26 13:11:14 +00:00
|
|
|
return nil, fmt.Errorf("invalid legal hold object id: %w", err)
|
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
lockInfo.SetLegalHold(legalHoldOID)
|
2022-05-26 13:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if retention, ok := lockNode.Get(retentionOIDKV); ok {
|
|
|
|
var retentionOID oid.ID
|
2022-05-31 14:25:47 +00:00
|
|
|
if err := retentionOID.DecodeString(retention); err != nil {
|
2022-05-26 13:11:14 +00:00
|
|
|
return nil, fmt.Errorf("invalid retention object id: %w", err)
|
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
_, isCompliance := lockNode.Get(isComplianceKV)
|
|
|
|
untilDate, _ := lockNode.Get(untilDateKV)
|
|
|
|
lockInfo.SetRetention(retentionOID, untilDate, isCompliance)
|
2022-05-26 13:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lockInfo, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) GetObjectTaggingAndLock(ctx context.Context, bktInfo *data.BucketInfo, objVersion *data.NodeVersion) (map[string]string, *data.LockInfo, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
nodes, err := c.getTreeNodes(ctx, bktInfo, objVersion.ID, isTagKV, isLockKV)
|
2022-05-31 14:25:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lockInfo, err := getLock(nodes[isLockKV])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return getObjectTagging(nodes[isTagKV]), lockInfo, nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) addVersion(ctx context.Context, bktInfo *data.BucketInfo, treeID string, version *data.NodeVersion) (uint64, error) {
|
2022-06-10 11:57:41 +00:00
|
|
|
path := pathFromName(version.FilePath)
|
2022-05-17 14:56:05 +00:00
|
|
|
meta := map[string]string{
|
2023-03-14 14:31:15 +00:00
|
|
|
oidKV: version.OID.EncodeToString(),
|
|
|
|
FileNameKey: path[len(path)-1],
|
2024-01-17 14:26:02 +00:00
|
|
|
ownerKV: version.Owner.EncodeToString(),
|
|
|
|
createdKV: strconv.FormatInt(version.Created.UTC().UnixMilli(), 10),
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 14:51:34 +00:00
|
|
|
if version.Size > 0 {
|
2023-06-01 13:45:28 +00:00
|
|
|
meta[sizeKV] = strconv.FormatUint(version.Size, 10)
|
2022-07-18 14:51:34 +00:00
|
|
|
}
|
|
|
|
if len(version.ETag) > 0 {
|
|
|
|
meta[etagKV] = version.ETag
|
|
|
|
}
|
2023-10-02 08:52:07 +00:00
|
|
|
if len(version.MD5) > 0 {
|
|
|
|
meta[md5KV] = version.MD5
|
|
|
|
}
|
2022-07-18 14:51:34 +00:00
|
|
|
|
2024-01-17 14:26:02 +00:00
|
|
|
if version.IsDeleteMarker {
|
2022-05-18 06:51:12 +00:00
|
|
|
meta[isDeleteMarkerKV] = "true"
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:49:20 +00:00
|
|
|
if version.IsCombined {
|
|
|
|
meta[isCombinedKV] = "true"
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:56:05 +00:00
|
|
|
if version.IsUnversioned {
|
|
|
|
meta[isUnversionedKV] = "true"
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
node, err := c.getUnversioned(ctx, bktInfo, treeID, version.FilePath)
|
2022-05-17 14:56:05 +00:00
|
|
|
if err == nil {
|
2023-03-14 14:31:15 +00:00
|
|
|
if err = c.service.MoveNode(ctx, bktInfo, treeID, node.ID, node.ParenID, meta); err != nil {
|
2022-08-29 10:57:11 +00:00
|
|
|
return 0, err
|
2022-08-10 00:04:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 09:44:18 +00:00
|
|
|
return node.ID, c.clearOutdatedVersionInfo(ctx, bktInfo, treeID, node.ID)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 14:56:05 +00:00
|
|
|
if !errors.Is(err, layer.ErrNodeNotFound) {
|
2022-08-29 10:57:11 +00:00
|
|
|
return 0, err
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
return c.service.AddNodeByPath(ctx, bktInfo, treeID, path[:len(path)-1], meta)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) clearOutdatedVersionInfo(ctx context.Context, bktInfo *data.BucketInfo, treeID string, nodeID uint64) error {
|
2022-09-13 09:44:18 +00:00
|
|
|
taggingNode, err := c.getTreeNode(ctx, bktInfo, nodeID, isTagKV)
|
2022-08-10 00:04:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if taggingNode != nil {
|
2023-03-14 14:31:15 +00:00
|
|
|
return c.service.RemoveNode(ctx, bktInfo, treeID, taggingNode.ID)
|
2022-08-10 00:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getVersions(ctx context.Context, bktInfo *data.BucketInfo, treeID, filepath string, onlyUnversioned bool) ([]*data.NodeVersion, error) {
|
2023-10-02 08:52:07 +00:00
|
|
|
keysToReturn := []string{oidKV, isUnversionedKV, isDeleteMarkerKV, etagKV, sizeKV, md5KV}
|
2022-05-23 09:53:48 +00:00
|
|
|
path := pathFromName(filepath)
|
2023-03-14 14:31:15 +00:00
|
|
|
p := &GetNodesParams{
|
2022-09-13 09:44:18 +00:00
|
|
|
BktInfo: bktInfo,
|
2022-05-25 01:58:25 +00:00
|
|
|
TreeID: treeID,
|
|
|
|
Path: path,
|
|
|
|
Meta: keysToReturn,
|
|
|
|
LatestOnly: false,
|
|
|
|
AllAttrs: false,
|
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
nodes, err := c.service.GetNodes(ctx, p)
|
2022-05-17 14:56:05 +00:00
|
|
|
if err != nil {
|
2022-09-13 10:35:30 +00:00
|
|
|
if errors.Is(err, layer.ErrNodeNotFound) {
|
2022-05-17 14:56:05 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2022-09-13 10:35:30 +00:00
|
|
|
return nil, err
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:02:00 +00:00
|
|
|
result := make([]*data.NodeVersion, 0, len(nodes))
|
2022-05-17 14:56:05 +00:00
|
|
|
for _, node := range nodes {
|
2022-06-10 11:57:41 +00:00
|
|
|
nodeVersion, err := newNodeVersion(filepath, node)
|
2022-05-17 14:56:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 14:56:05 +00:00
|
|
|
if onlyUnversioned && !nodeVersion.IsUnversioned {
|
2022-05-17 14:56:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:56:05 +00:00
|
|
|
result = append(result, nodeVersion)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-04-22 07:18:21 +00:00
|
|
|
func metaFromSettings(settings *data.BucketSettings) map[string]string {
|
|
|
|
results := make(map[string]string, 3)
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
results[FileNameKey] = settingsFileName
|
2022-07-19 14:58:18 +00:00
|
|
|
results[versioningKV] = settings.Versioning
|
2022-04-22 07:18:21 +00:00
|
|
|
results[lockConfigurationKV] = encodeLockConfiguration(settings.LockConfiguration)
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2022-08-23 14:54:24 +00:00
|
|
|
func metaFromMultipart(info *data.MultipartInfo, fileName string) map[string]string {
|
2023-03-14 14:31:15 +00:00
|
|
|
info.Meta[FileNameKey] = fileName
|
2022-05-23 14:34:13 +00:00
|
|
|
info.Meta[uploadIDKV] = info.UploadID
|
|
|
|
info.Meta[ownerKV] = info.Owner.EncodeToString()
|
|
|
|
info.Meta[createdKV] = strconv.FormatInt(info.Created.UTC().UnixMilli(), 10)
|
2023-12-04 06:42:25 +00:00
|
|
|
if info.Finished {
|
|
|
|
info.Meta[finishedKV] = strconv.FormatBool(info.Finished)
|
|
|
|
}
|
2022-05-23 14:34:13 +00:00
|
|
|
|
|
|
|
return info.Meta
|
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getSystemNode(ctx context.Context, bktInfo *data.BucketInfo, path, meta []string) (*treeNode, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
return c.getNode(ctx, bktInfo, systemTree, path, meta, false)
|
2022-05-17 14:56:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getSystemNodeWithAllAttributes(ctx context.Context, bktInfo *data.BucketInfo, path []string) (*treeNode, error) {
|
2022-09-13 09:44:18 +00:00
|
|
|
return c.getNode(ctx, bktInfo, systemTree, path, []string{}, true)
|
2022-05-25 01:58:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 14:31:15 +00:00
|
|
|
func (c *Tree) getNode(ctx context.Context, bktInfo *data.BucketInfo, treeID string, path, meta []string, allAttrs bool) (*treeNode, error) {
|
|
|
|
p := &GetNodesParams{
|
2022-09-13 09:44:18 +00:00
|
|
|
BktInfo: bktInfo,
|
2022-05-25 01:58:25 +00:00
|
|
|
TreeID: treeID,
|
|
|
|
Path: path,
|
|
|
|
Meta: meta,
|
|
|
|
LatestOnly: false,
|
|
|
|
AllAttrs: allAttrs,
|
|
|
|
}
|
2023-03-14 14:31:15 +00:00
|
|
|
nodes, err := c.service.GetNodes(ctx, p)
|
2022-04-22 07:18:21 +00:00
|
|
|
if err != nil {
|
2022-09-13 10:35:30 +00:00
|
|
|
return nil, err
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
2022-05-17 14:56:05 +00:00
|
|
|
if len(nodes) == 0 {
|
2022-05-12 01:48:17 +00:00
|
|
|
return nil, layer.ErrNodeNotFound
|
2022-04-22 07:18:21 +00:00
|
|
|
}
|
2022-05-17 14:56:05 +00:00
|
|
|
if len(nodes) != 1 {
|
2022-04-22 07:18:21 +00:00
|
|
|
return nil, fmt.Errorf("found more than one node")
|
|
|
|
}
|
|
|
|
|
2022-05-17 14:56:05 +00:00
|
|
|
return newTreeNode(nodes[0])
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
func (c *Tree) reqLogger(ctx context.Context) *zap.Logger {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqLogger := middleware.GetReqLog(ctx)
|
2023-06-09 13:19:23 +00:00
|
|
|
if reqLogger != nil {
|
|
|
|
return reqLogger
|
|
|
|
}
|
|
|
|
return c.log
|
|
|
|
}
|
|
|
|
|
2022-04-22 07:18:21 +00:00
|
|
|
func parseLockConfiguration(value string) (*data.ObjectLockConfiguration, error) {
|
|
|
|
result := &data.ObjectLockConfiguration{}
|
|
|
|
if len(value) == 0 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lockValues := strings.Split(value, ",")
|
|
|
|
result.ObjectLockEnabled = lockValues[0]
|
|
|
|
|
|
|
|
if len(lockValues) == 1 {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lockValues) != 4 {
|
|
|
|
return nil, fmt.Errorf("invalid lock configuration: %s", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var days, years int64
|
|
|
|
|
|
|
|
if len(lockValues[1]) > 0 {
|
|
|
|
if days, err = strconv.ParseInt(lockValues[1], 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid lock configuration: %s", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(lockValues[3]) > 0 {
|
|
|
|
if years, err = strconv.ParseInt(lockValues[3], 10, 64); err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid lock configuration: %s", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Rule = &data.ObjectLockRule{
|
|
|
|
DefaultRetention: &data.DefaultRetention{
|
|
|
|
Days: days,
|
|
|
|
Mode: lockValues[2],
|
|
|
|
Years: years,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeLockConfiguration(conf *data.ObjectLockConfiguration) string {
|
|
|
|
if conf == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Rule == nil || conf.Rule.DefaultRetention == nil {
|
|
|
|
return conf.ObjectLockEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
defaults := conf.Rule.DefaultRetention
|
|
|
|
return fmt.Sprintf("%s,%d,%s,%d", conf.ObjectLockEnabled, defaults.Days, defaults.Mode, defaults.Years)
|
|
|
|
}
|