2020-12-01 08:34:38 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2021-08-31 11:04:58 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-03-17 08:03:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
|
2022-03-03 14:19:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-12-01 08:34:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HeadPrm groups the parameters of Head operation.
|
|
|
|
type HeadPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-12-08 11:42:44 +00:00
|
|
|
raw bool
|
2020-12-01 08:34:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// HeadRes groups the resulting values of Head operation.
|
2020-12-01 08:34:38 +00:00
|
|
|
type HeadRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2020-12-01 08:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Head option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *HeadPrm) WithAddress(addr oid.Address) {
|
2020-12-01 08:34:38 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:42:44 +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.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *HeadPrm) WithRaw(raw bool) {
|
2020-12-08 11:42:44 +00:00
|
|
|
if p != nil {
|
|
|
|
p.raw = raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 08:34:38 +00:00
|
|
|
// Object returns the requested object header.
|
2022-03-03 14:19:05 +00:00
|
|
|
func (r *HeadRes) Object() *objectSDK.Object {
|
2020-12-01 08:34:38 +00:00
|
|
|
return r.obj
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head reads header of the object from the shard.
|
|
|
|
//
|
|
|
|
// Returns any error encountered.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if object is missing in Shard.
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if the requested object has been marked as removed in shard.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (s *Shard) Head(prm HeadPrm) (*HeadRes, error) {
|
2021-08-31 11:04:58 +00:00
|
|
|
// object can be saved in write-cache (if enabled) or in metabase
|
|
|
|
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
// try to read header from write-cache
|
|
|
|
header, err := s.writeCache.Head(prm.addr)
|
|
|
|
if err == nil {
|
|
|
|
return &HeadRes{
|
|
|
|
obj: header,
|
|
|
|
}, nil
|
2022-03-17 08:03:58 +00:00
|
|
|
} else if !writecache.IsErrNotFound(err) {
|
2021-08-31 11:04:58 +00:00
|
|
|
// in this case we think that object is presented in write-cache, but corrupted
|
|
|
|
return nil, fmt.Errorf("could not read header from write-cache: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise object seems to be flushed to metabase
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:48:14 +00:00
|
|
|
var headParams meta.GetPrm
|
|
|
|
headParams.WithAddress(prm.addr)
|
|
|
|
headParams.WithRaw(prm.raw)
|
2020-12-08 11:42:44 +00:00
|
|
|
|
2021-08-31 11:04:58 +00:00
|
|
|
res, err := s.metaBase.Get(headParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-01 08:34:38 +00:00
|
|
|
|
|
|
|
return &HeadRes{
|
2021-08-31 11:04:58 +00:00
|
|
|
obj: res.Header(),
|
|
|
|
}, nil
|
2020-12-01 08:34:38 +00:00
|
|
|
}
|