2022-05-24 06:58:33 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
errorsStd "errors"
|
2022-05-26 13:11:14 +00:00
|
|
|
|
2022-05-24 06:58:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
2022-05-25 01:58:25 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"go.uber.org/zap"
|
2022-05-24 06:58:33 +00:00
|
|
|
)
|
|
|
|
|
2022-06-07 09:12:07 +00:00
|
|
|
func (n *layer) GetObjectTagging(ctx context.Context, p *ObjectVersion) (string, map[string]string, error) {
|
2022-05-24 06:58:33 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
tags map[string]string
|
|
|
|
)
|
2022-06-07 09:12:07 +00:00
|
|
|
|
2022-08-08 22:35:26 +00:00
|
|
|
if len(p.VersionID) != 0 && p.VersionID != data.UnversionedObjectVersionID {
|
2022-08-05 02:19:17 +00:00
|
|
|
tags = n.systemCache.GetTagging(objectTaggingCacheKey(p))
|
|
|
|
if tags != nil {
|
|
|
|
return p.VersionID, tags, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
version, err := n.getNodeVersion(ctx, p)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
2022-06-07 09:12:07 +00:00
|
|
|
return "", nil, err
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
2022-06-07 09:12:07 +00:00
|
|
|
p.VersionID = version.OID.EncodeToString()
|
2022-05-24 06:58:33 +00:00
|
|
|
|
2022-08-03 21:26:14 +00:00
|
|
|
tags = n.systemCache.GetTagging(objectTaggingCacheKey(p))
|
|
|
|
if tags != nil {
|
|
|
|
return p.VersionID, tags, nil
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
tags, err = n.treeService.GetObjectTagging(ctx, p.BktInfo.CID, version)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if errorsStd.Is(err, ErrNodeNotFound) {
|
2022-06-07 09:12:07 +00:00
|
|
|
return "", nil, errors.GetAPIError(errors.ErrNoSuchKey)
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
2022-06-07 09:12:07 +00:00
|
|
|
return "", nil, err
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:58:25 +00:00
|
|
|
if err = n.systemCache.PutTagging(objectTaggingCacheKey(p), tags); err != nil {
|
2022-05-24 06:58:33 +00:00
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2022-06-07 09:12:07 +00:00
|
|
|
return p.VersionID, tags, nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 14:51:34 +00:00
|
|
|
func (n *layer) PutObjectTagging(ctx context.Context, p *ObjectVersion, tagSet map[string]string) (*data.NodeVersion, error) {
|
2022-05-26 13:11:14 +00:00
|
|
|
version, err := n.getNodeVersion(ctx, p)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
2022-07-18 14:51:34 +00:00
|
|
|
return nil, err
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
2022-06-07 09:12:07 +00:00
|
|
|
p.VersionID = version.OID.EncodeToString()
|
2022-05-24 06:58:33 +00:00
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
err = n.treeService.PutObjectTagging(ctx, p.BktInfo.CID, version, tagSet)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if errorsStd.Is(err, ErrNodeNotFound) {
|
2022-07-18 14:51:34 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchKey)
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
2022-07-18 14:51:34 +00:00
|
|
|
return nil, err
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:58:25 +00:00
|
|
|
if err = n.systemCache.PutTagging(objectTaggingCacheKey(p), tagSet); err != nil {
|
2022-05-24 06:58:33 +00:00
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:51:34 +00:00
|
|
|
return version, nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 14:51:34 +00:00
|
|
|
func (n *layer) DeleteObjectTagging(ctx context.Context, p *ObjectVersion) (*data.NodeVersion, error) {
|
2022-05-26 13:11:14 +00:00
|
|
|
version, err := n.getNodeVersion(ctx, p)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
2022-07-18 14:51:34 +00:00
|
|
|
return nil, err
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
err = n.treeService.DeleteObjectTagging(ctx, p.BktInfo.CID, version)
|
2022-05-24 06:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if errorsStd.Is(err, ErrNodeNotFound) {
|
2022-07-18 14:51:34 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchKey)
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
2022-07-18 14:51:34 +00:00
|
|
|
return nil, err
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 00:01:29 +00:00
|
|
|
p.VersionID = version.OID.EncodeToString()
|
|
|
|
|
2022-05-24 06:58:33 +00:00
|
|
|
n.systemCache.Delete(objectTaggingCacheKey(p))
|
|
|
|
|
2022-07-18 14:51:34 +00:00
|
|
|
return version, nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
func (n *layer) GetBucketTagging(ctx context.Context, cnrID cid.ID) (map[string]string, error) {
|
2022-05-25 01:58:25 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
tags map[string]string
|
|
|
|
)
|
|
|
|
|
|
|
|
tags = n.systemCache.GetTagging(bucketTaggingCacheKey(cnrID))
|
|
|
|
if tags != nil {
|
|
|
|
return tags, nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:58:25 +00:00
|
|
|
if tags, err = n.treeService.GetBucketTagging(ctx, cnrID); err != nil && !errorsStd.Is(err, ErrNodeNotFound) {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-24 06:58:33 +00:00
|
|
|
|
2022-05-25 01:58:25 +00:00
|
|
|
if err := n.systemCache.PutTagging(bucketTaggingCacheKey(cnrID), tags); err != nil {
|
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 01:58:25 +00:00
|
|
|
return tags, nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
func (n *layer) PutBucketTagging(ctx context.Context, cnrID cid.ID, tagSet map[string]string) error {
|
2022-05-25 01:58:25 +00:00
|
|
|
if err := n.treeService.PutBucketTagging(ctx, cnrID, tagSet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := n.systemCache.PutTagging(bucketTaggingCacheKey(cnrID), tagSet); err != nil {
|
|
|
|
n.log.Error("couldn't cache system object", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
func (n *layer) DeleteBucketTagging(ctx context.Context, cnrID cid.ID) error {
|
2022-05-25 01:58:25 +00:00
|
|
|
n.systemCache.Delete(bucketTaggingCacheKey(cnrID))
|
2022-05-24 06:58:33 +00:00
|
|
|
|
2022-05-25 01:58:25 +00:00
|
|
|
return n.treeService.DeleteBucketTagging(ctx, cnrID)
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
func objectTaggingCacheKey(p *ObjectVersion) string {
|
|
|
|
return ".tagset." + p.BktInfo.CID.EncodeToString() + "." + p.ObjectName + "." + p.VersionID
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:08:26 +00:00
|
|
|
func bucketTaggingCacheKey(cnrID cid.ID) string {
|
2022-05-25 01:58:25 +00:00
|
|
|
return ".tagset." + cnrID.EncodeToString()
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
func (n *layer) getNodeVersion(ctx context.Context, objVersion *ObjectVersion) (*data.NodeVersion, error) {
|
|
|
|
var err error
|
|
|
|
var version *data.NodeVersion
|
2022-05-24 06:58:33 +00:00
|
|
|
|
2022-08-08 22:35:26 +00:00
|
|
|
if objVersion.VersionID == data.UnversionedObjectVersionID {
|
2022-06-27 09:08:26 +00:00
|
|
|
version, err = n.treeService.GetUnversioned(ctx, objVersion.BktInfo.CID, objVersion.ObjectName)
|
2022-05-26 13:11:14 +00:00
|
|
|
} else if len(objVersion.VersionID) == 0 {
|
2022-06-27 09:08:26 +00:00
|
|
|
version, err = n.treeService.GetLatestVersion(ctx, objVersion.BktInfo.CID, objVersion.ObjectName)
|
2022-05-24 06:58:33 +00:00
|
|
|
} else {
|
2022-06-27 09:08:26 +00:00
|
|
|
versions, err2 := n.treeService.GetVersions(ctx, objVersion.BktInfo.CID, objVersion.ObjectName)
|
2022-05-26 13:11:14 +00:00
|
|
|
if err2 != nil {
|
|
|
|
return nil, err2
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
for _, v := range versions {
|
2022-05-26 13:11:14 +00:00
|
|
|
if v.OID.EncodeToString() == objVersion.VersionID {
|
2022-05-24 06:58:33 +00:00
|
|
|
version = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 13:11:14 +00:00
|
|
|
if version == nil {
|
2022-06-15 12:17:29 +00:00
|
|
|
err = errors.GetAPIError(errors.ErrNoSuchVersion)
|
2022-05-26 13:11:14 +00:00
|
|
|
}
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 12:10:04 +00:00
|
|
|
if err == nil && version.IsDeleteMarker() && !objVersion.NoErrorOnDeleteMarker || errorsStd.Is(err, ErrNodeNotFound) {
|
2022-05-24 06:58:33 +00:00
|
|
|
return nil, errors.GetAPIError(errors.ErrNoSuchKey)
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
return version, err
|
2022-05-24 06:58:33 +00:00
|
|
|
}
|