2020-09-22 15:04:08 +00:00
|
|
|
package headsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-09-23 10:53:59 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-09-22 15:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/container"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-09-23 10:53:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-09-22 15:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/localstore"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-09-29 16:44:59 +00:00
|
|
|
objutil "github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-09-22 15:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2020-09-23 10:53:59 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-09-22 15:04:08 +00:00
|
|
|
)
|
|
|
|
|
2020-09-23 10:53:59 +00:00
|
|
|
type RelationSearcher interface {
|
|
|
|
SearchRightChild(context.Context, *objectSDK.Address) (*objectSDK.ID, error)
|
|
|
|
}
|
|
|
|
|
2020-09-22 15:04:08 +00:00
|
|
|
type Service struct {
|
|
|
|
*cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
2020-09-29 16:44:59 +00:00
|
|
|
keyStorage *objutil.KeyStorage
|
2020-09-22 15:04:08 +00:00
|
|
|
|
|
|
|
localStore *localstore.Storage
|
|
|
|
|
|
|
|
cnrSrc container.Source
|
|
|
|
|
|
|
|
netMapSrc netmap.Source
|
|
|
|
|
|
|
|
workerPool util.WorkerPool
|
|
|
|
|
|
|
|
localAddrSrc network.LocalAddressSource
|
2020-09-23 10:53:59 +00:00
|
|
|
|
|
|
|
relSearcher RelationSearcher
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return &cfg{
|
|
|
|
workerPool: new(util.SyncWorkerPool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewService(opts ...Option) *Service {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
cfg: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Head(ctx context.Context, prm *Prm) (*Response, error) {
|
2020-09-23 10:53:59 +00:00
|
|
|
// try to receive header of physically stored
|
|
|
|
r, err := (&distributedHeader{
|
2020-09-22 15:04:08 +00:00
|
|
|
cfg: s.cfg,
|
|
|
|
}).head(ctx, prm)
|
2020-09-29 15:05:22 +00:00
|
|
|
if err == nil || prm.common.LocalOnly() {
|
2020-09-23 10:53:59 +00:00
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to find far right child that carries header of desired object
|
|
|
|
rightChildID, err := s.relSearcher.SearchRightChild(ctx, prm.addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not find right child", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := objectSDK.NewAddress()
|
|
|
|
addr.SetContainerID(prm.addr.GetContainerID())
|
|
|
|
addr.SetObjectID(rightChildID)
|
|
|
|
|
2020-09-25 12:42:28 +00:00
|
|
|
r, err = s.Head(ctx, new(Prm).WithAddress(addr))
|
2020-09-23 10:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "(%T) could not get right child header", s)
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:42:28 +00:00
|
|
|
rightChild := r.Header()
|
|
|
|
|
2020-09-23 10:53:59 +00:00
|
|
|
// TODO: check if received parent has requested address
|
|
|
|
|
|
|
|
return &Response{
|
2020-09-25 12:42:28 +00:00
|
|
|
hdr: object.NewFromSDK(rightChild.GetParent()),
|
|
|
|
rightChild: rightChild,
|
2020-09-23 10:53:59 +00:00
|
|
|
}, nil
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:44:59 +00:00
|
|
|
func WithKeyStorage(v *objutil.KeyStorage) Option {
|
2020-09-22 15:04:08 +00:00
|
|
|
return func(c *cfg) {
|
2020-09-29 16:44:59 +00:00
|
|
|
c.keyStorage = v
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLocalStorage(v *localstore.Storage) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.localStore = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithContainerSource(v container.Source) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.cnrSrc = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithNetworkMapSource(v netmap.Source) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.netMapSrc = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithWorkerPool(v util.WorkerPool) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.workerPool = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLocalAddressSource(v network.LocalAddressSource) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.localAddrSrc = v
|
|
|
|
}
|
|
|
|
}
|
2020-09-23 10:53:59 +00:00
|
|
|
|
|
|
|
func WithRelationSearcher(v RelationSearcher) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.relSearcher = v
|
|
|
|
}
|
|
|
|
}
|