[#58] object/delete: Process linking object in Delete service

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-10-01 13:54:18 +03:00 committed by Alex Vanin
parent 16252ad09a
commit f251645def
3 changed files with 81 additions and 2 deletions

View file

@ -3,7 +3,9 @@ package deletesvc
import (
"context"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head"
putsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/put"
objutil "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
@ -16,6 +18,10 @@ type Service struct {
type Option func(*cfg)
type RelationHeader interface {
HeadRelation(context.Context, *objectSDK.Address) (*object.Object, error)
}
type cfg struct {
ownerID *owner.ID
@ -24,6 +30,8 @@ type cfg struct {
putSvc *putsvc.Service
headSvc *headsvc.Service
hdrLinking RelationHeader
}
func defaultCfg() *cfg {
@ -60,8 +68,24 @@ func (s *Service) Delete(ctx context.Context, prm *Prm) (*Response, error) {
commonPrm: prm.common,
}
if err := s.deleteAll(tool); err != nil {
return nil, errors.Wrapf(err, "(%T) could not delete all objcet relations", s)
if linking, err := s.hdrLinking.HeadRelation(ctx, prm.addr); err != nil {
if err := s.deleteAll(tool); err != nil {
return nil, errors.Wrapf(err, "(%T) could not delete all object relations", s)
}
} else {
if err := tool.delete(prm.addr.GetObjectID()); err != nil {
return nil, errors.Wrapf(err, "(%T) could not delete object", s)
}
for _, child := range linking.GetChildren() {
if err := tool.delete(child); err != nil {
return nil, errors.Wrapf(err, "(%T) could not delete child object", s)
}
}
if err := tool.delete(linking.GetID()); err != nil {
return nil, errors.Wrapf(err, "(%T) could not delete linking object", s)
}
}
return new(Response), nil
@ -124,3 +148,9 @@ func WitHeadService(v *headsvc.Service) Option {
c.headSvc = v
}
}
func WithLinkingHeader(v RelationHeader) Option {
return func(c *cfg) {
c.hdrLinking = v
}
}

View file

@ -0,0 +1,40 @@
package headsvc
import (
"context"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/pkg/errors"
)
type RelationHeader struct {
srch RelationSearcher
svc *Service
}
func NewRelationHeader(srch RelationSearcher, svc *Service) *RelationHeader {
return &RelationHeader{
srch: srch,
svc: svc,
}
}
func (h *RelationHeader) HeadRelation(ctx context.Context, addr *objectSDK.Address) (*object.Object, error) {
id, err := h.srch.SearchRelation(ctx, addr)
if err != nil {
return nil, errors.Wrapf(err, "(%T) could not find relation", h)
}
a := objectSDK.NewAddress()
a.SetContainerID(addr.GetContainerID())
a.SetObjectID(id)
r, err := h.svc.Head(ctx, new(Prm).WithAddress(a))
if err != nil {
return nil, errors.Wrapf(err, "(%T) could not receive relation header", h)
}
return r.Header(), nil
}

View file

@ -62,3 +62,12 @@ func NewRightChildSearcher(svc *Service) *RelationSearcher {
},
}
}
func NewLinkingSearcher(svc *Service) *RelationSearcher {
return &RelationSearcher{
svc: svc,
queryGenerator: func(addr *object.Address) query.Query {
return queryV1.NewLinkingQuery(addr.GetObjectID())
},
}
}