2020-12-02 23:45:25 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
2021-01-11 13:50:49 +00:00
|
|
|
"io"
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-06-21 14:13:08 +00:00
|
|
|
coreclient "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2021-01-12 14:55:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-12-02 23:45:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
2021-11-01 08:35:33 +00:00
|
|
|
internal "github.com/nspcc-dev/neofs-node/pkg/services/object/internal/client"
|
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/pkg/services/object/internal/client"
|
2020-12-02 23:45:25 +00:00
|
|
|
)
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type SimpleObjectWriter struct {
|
2020-12-02 23:45:25 +00:00
|
|
|
obj *object.RawObject
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
pld []byte
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientCacheWrapper struct {
|
2021-03-23 18:40:36 +00:00
|
|
|
cache ClientConstructor
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientWrapper struct {
|
2021-06-21 14:13:08 +00:00
|
|
|
client coreclient.Client
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storageEngineWrapper struct {
|
|
|
|
engine *engine.StorageEngine
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type partWriter struct {
|
2020-12-07 17:49:47 +00:00
|
|
|
ObjectWriter
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
headWriter HeaderWriter
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
chunkWriter ChunkWriter
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
type hasherWrapper struct {
|
2021-01-11 13:50:49 +00:00
|
|
|
hash io.Writer
|
2020-12-08 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
type nmSrcWrapper struct {
|
|
|
|
nmSrc netmap.Source
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func NewSimpleObjectWriter() *SimpleObjectWriter {
|
|
|
|
return &SimpleObjectWriter{
|
2020-12-07 17:49:47 +00:00
|
|
|
obj: object.NewRaw(),
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (s *SimpleObjectWriter) WriteHeader(obj *object.Object) error {
|
2020-12-02 23:45:25 +00:00
|
|
|
s.obj = object.NewRawFromObject(obj)
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
s.pld = make([]byte, 0, obj.PayloadSize())
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (s *SimpleObjectWriter) WriteChunk(p []byte) error {
|
2020-12-07 17:49:47 +00:00
|
|
|
s.pld = append(s.pld, p...)
|
2020-12-02 23:45:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (s *SimpleObjectWriter) Object() *object.Object {
|
2020-12-07 17:49:47 +00:00
|
|
|
if len(s.pld) > 0 {
|
|
|
|
s.obj.SetPayload(s.pld)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.obj.Object()
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
func (c *clientCacheWrapper) get(info coreclient.NodeInfo) (getClient, error) {
|
|
|
|
clt, err := c.cache.Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
return &clientWrapper{
|
|
|
|
client: clt,
|
2021-09-28 04:46:10 +00:00
|
|
|
}, nil
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
func (c *clientWrapper) getObject(exec *execCtx, info coreclient.NodeInfo) (*objectSDK.Object, error) {
|
2021-09-27 06:52:03 +00:00
|
|
|
if exec.isForwardingEnabled() {
|
2021-09-28 04:46:10 +00:00
|
|
|
return exec.prm.forwarder(info, c.client)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
key, err := exec.key()
|
2021-10-26 12:07:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
if exec.headOnly() {
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.HeadObjectPrm
|
|
|
|
|
|
|
|
prm.SetContext(exec.context())
|
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetAddress(exec.address())
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
|
|
|
|
if exec.isRaw() {
|
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.HeadObject(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Header(), nil
|
2020-12-09 10:32:33 +00:00
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
// we don't specify payload writer because we accumulate
|
|
|
|
// the object locally (even huge).
|
2020-12-09 10:32:33 +00:00
|
|
|
if rng := exec.ctxRange(); rng != nil {
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.PayloadRangePrm
|
|
|
|
|
|
|
|
prm.SetContext(exec.context())
|
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetAddress(exec.address())
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
prm.SetRange(rng)
|
|
|
|
|
|
|
|
if exec.isRaw() {
|
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internalclient.PayloadRange(prm)
|
2020-12-07 17:49:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return payloadOnlyObject(res.PayloadRange()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm internalclient.GetObjectPrm
|
|
|
|
|
|
|
|
prm.SetContext(exec.context())
|
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetAddress(exec.address())
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
|
|
|
|
if exec.isRaw() {
|
|
|
|
prm.SetRawFlag()
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := internal.GetObject(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
2021-01-11 15:21:06 +00:00
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
return res.Object(), nil
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (e *storageEngineWrapper) get(exec *execCtx) (*object.Object, error) {
|
|
|
|
if exec.headOnly() {
|
|
|
|
r, err := e.engine.Head(new(engine.HeadPrm).
|
|
|
|
WithAddress(exec.address()).
|
|
|
|
WithRaw(exec.isRaw()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Header(), nil
|
|
|
|
} else if rng := exec.ctxRange(); rng != nil {
|
2020-12-07 17:49:47 +00:00
|
|
|
r, err := e.engine.GetRange(new(engine.RngPrm).
|
2020-12-09 10:32:33 +00:00
|
|
|
WithAddress(exec.address()).
|
|
|
|
WithPayloadRange(rng),
|
2020-12-07 17:49:47 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Object(), nil
|
|
|
|
} else {
|
|
|
|
r, err := e.engine.Get(new(engine.GetPrm).
|
2020-12-09 10:32:33 +00:00
|
|
|
WithAddress(exec.address()),
|
2020-12-07 17:49:47 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Object(), nil
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (w *partWriter) WriteChunk(p []byte) error {
|
|
|
|
return w.chunkWriter.WriteChunk(p)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (w *partWriter) WriteHeader(o *object.Object) error {
|
|
|
|
return w.headWriter.WriteHeader(o)
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func payloadOnlyObject(payload []byte) *objectSDK.Object {
|
|
|
|
rawObj := object.NewRaw()
|
|
|
|
rawObj.SetPayload(payload)
|
|
|
|
|
|
|
|
return rawObj.Object().SDK()
|
|
|
|
}
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
func (h *hasherWrapper) WriteChunk(p []byte) error {
|
|
|
|
_, err := h.hash.Write(p)
|
|
|
|
return err
|
|
|
|
}
|
2021-01-12 14:55:02 +00:00
|
|
|
|
|
|
|
func (n *nmSrcWrapper) currentEpoch() (uint64, error) {
|
|
|
|
return n.nmSrc.Epoch()
|
|
|
|
}
|