2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-04-15 07:23:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2020-11-17 12:26:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HeadPrm groups the parameters of Head operation.
|
|
|
|
type HeadPrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-12-08 11:58:38 +00:00
|
|
|
raw bool
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HeadRes groups resulting values of Head operation.
|
|
|
|
type HeadRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
head *objectSDK.Object
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Head option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *HeadPrm) WithAddress(addr *addressSDK.Address) *HeadPrm {
|
2020-11-17 12:26:03 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:58:38 +00:00
|
|
|
// WithRaw is a Head option to set raw flag value. If flag is unset, then Head
|
|
|
|
// returns header of virtual object, otherwise it returns SplitInfo of virtual
|
|
|
|
// object.
|
|
|
|
func (p *HeadPrm) WithRaw(raw bool) *HeadPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.raw = raw
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
// Header returns the requested object header.
|
|
|
|
//
|
|
|
|
// Instance has empty payload.
|
2022-03-03 14:19:05 +00:00
|
|
|
func (r *HeadRes) Header() *objectSDK.Object {
|
2020-11-17 12:26:03 +00:00
|
|
|
return r.head
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head reads object header from local storage.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object header.
|
|
|
|
//
|
2022-03-17 08:03:58 +00:00
|
|
|
// Returns apistatus.ObjectNotFound if requested object is missing in local storage.
|
|
|
|
// Returns apistatus.ObjectAlreadyRemoved if requested object was inhumed.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
func (e *StorageEngine) Head(prm *HeadPrm) (res *HeadRes, err error) {
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2021-11-09 15:46:12 +00:00
|
|
|
res, err = e.head(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) head(prm *HeadPrm) (*HeadRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddHeadDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 10:48:40 +00:00
|
|
|
var (
|
2022-03-03 14:19:05 +00:00
|
|
|
head *objectSDK.Object
|
2020-12-08 11:43:56 +00:00
|
|
|
siErr *objectSDK.SplitInfoError
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2021-04-14 08:58:14 +00:00
|
|
|
outSI *objectSDK.SplitInfo
|
2022-03-17 08:03:58 +00:00
|
|
|
outError error = errNotFound
|
2020-12-01 10:48:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
shPrm := new(shard.HeadPrm).
|
2020-12-08 11:58:38 +00:00
|
|
|
WithAddress(prm.addr).
|
|
|
|
WithRaw(prm.raw)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr, func(_ int, sh hashedShard) (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 {
|
2022-03-17 08:03:58 +00:00
|
|
|
case shard.IsErrNotFound(err):
|
2020-12-01 10:48:40 +00:00
|
|
|
return false // ignore, go to next shard
|
2021-04-14 08:58:14 +00:00
|
|
|
case errors.As(err, &siErr):
|
|
|
|
siErr = err.(*objectSDK.SplitInfoError)
|
|
|
|
|
|
|
|
if outSI == nil {
|
|
|
|
outSI = objectSDK.NewSplitInfo()
|
|
|
|
}
|
|
|
|
|
2021-04-15 07:23:52 +00:00
|
|
|
util.MergeSplitInfo(siErr.SplitInfo(), outSI)
|
2021-04-14 08:58:14 +00:00
|
|
|
|
|
|
|
// stop iterating over shards if SplitInfo structure is complete
|
|
|
|
if outSI.Link() != nil && outSI.LastPart() != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2022-03-17 08:03:58 +00:00
|
|
|
case shard.IsErrRemoved(err):
|
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:
|
2022-01-31 14:58:32 +00:00
|
|
|
e.reportShardError(sh, "could not head object from shard", err)
|
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
|
|
|
})
|
|
|
|
|
2021-04-14 08:58:14 +00:00
|
|
|
if outSI != nil {
|
|
|
|
return nil, objectSDK.NewSplitInfoError(outSI)
|
|
|
|
}
|
|
|
|
|
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.
|
2022-03-03 14:19:05 +00:00
|
|
|
func Head(storage *StorageEngine, addr *addressSDK.Address) (*objectSDK.Object, error) {
|
2020-11-19 08:15:49 +00:00
|
|
|
res, err := storage.Head(new(HeadPrm).
|
|
|
|
WithAddress(addr),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
|
|
|
}
|
2020-12-08 11:58:38 +00:00
|
|
|
|
|
|
|
// HeadRaw reads object header from local storage by provided address and raw
|
|
|
|
// flag.
|
2022-03-03 14:19:05 +00:00
|
|
|
func HeadRaw(storage *StorageEngine, addr *addressSDK.Address, raw bool) (*objectSDK.Object, error) {
|
2020-12-08 11:58:38 +00:00
|
|
|
res, err := storage.Head(new(HeadPrm).
|
|
|
|
WithAddress(addr).
|
|
|
|
WithRaw(raw),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
|
|
|
}
|