2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HeadPrm groups the parameters of Head operation.
|
|
|
|
type HeadPrm struct {
|
|
|
|
addr *objectSDK.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeadRes groups resulting values of Head operation.
|
|
|
|
type HeadRes struct {
|
|
|
|
head *object.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Head option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
|
|
|
func (p *HeadPrm) WithAddress(addr *objectSDK.Address) *HeadPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header returns the requested object header.
|
|
|
|
//
|
|
|
|
// Instance has empty payload.
|
|
|
|
func (r *HeadRes) Header() *object.Object {
|
|
|
|
return r.head
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head reads object header from local storage.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object header.
|
|
|
|
//
|
2020-11-30 16:39:05 +00:00
|
|
|
// Returns ErrNotFound if requested object is missing in local storage.
|
2020-11-17 12:26:03 +00:00
|
|
|
func (e *StorageEngine) Head(prm *HeadPrm) (*HeadRes, error) {
|
2020-12-01 10:48:40 +00:00
|
|
|
var (
|
2020-12-08 11:43:56 +00:00
|
|
|
head *object.Object
|
|
|
|
siErr *objectSDK.SplitInfoError
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2020-12-03 10:01:09 +00:00
|
|
|
outError = object.ErrNotFound
|
2020-12-01 10:48:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
shPrm := new(shard.HeadPrm).
|
2020-11-17 12:26:03 +00:00
|
|
|
WithAddress(prm.addr)
|
|
|
|
|
2020-12-01 10:18:25 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh *shard.Shard) (stop bool) {
|
2020-12-01 10:48:40 +00:00
|
|
|
res, err := sh.Head(shPrm)
|
2020-11-17 12:26:03 +00:00
|
|
|
if err != nil {
|
2020-12-01 10:48:40 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, object.ErrNotFound):
|
|
|
|
return false // ignore, go to next shard
|
2020-12-08 11:43:56 +00:00
|
|
|
case
|
|
|
|
errors.Is(err, object.ErrAlreadyRemoved),
|
|
|
|
errors.As(err, &siErr):
|
2020-12-03 10:01:09 +00:00
|
|
|
outError = err
|
2020-12-01 10:48:40 +00:00
|
|
|
|
|
|
|
return true // stop, return it back
|
|
|
|
default:
|
|
|
|
// TODO: smth wrong with shard, need to be processed, but
|
|
|
|
// still go to next shard
|
|
|
|
e.log.Warn("could not head object from shard",
|
2020-11-17 12:26:03 +00:00
|
|
|
zap.Stringer("shard", sh.ID()),
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2020-12-01 10:48:40 +00:00
|
|
|
|
|
|
|
return false
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:48:40 +00:00
|
|
|
head = res.Object()
|
|
|
|
|
|
|
|
return true
|
2020-11-17 12:26:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if head == nil {
|
2020-12-03 10:01:09 +00:00
|
|
|
return nil, outError
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &HeadRes{
|
|
|
|
head: head,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
|
|
|
// Head reads object header from local storage by provided address.
|
|
|
|
func Head(storage *StorageEngine, addr *objectSDK.Address) (*object.Object, error) {
|
|
|
|
res, err := storage.Head(new(HeadPrm).
|
|
|
|
WithAddress(addr),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
|
|
|
}
|