2020-12-02 23:45:25 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-01-11 13:50:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2020-12-02 23:45:25 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get serves a request to get an object by address, and returns Streamer instance.
|
|
|
|
func (s *Service) Get(ctx context.Context, prm Prm) error {
|
2020-12-09 10:32:33 +00:00
|
|
|
return s.get(ctx, prm.commonPrm).err
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRange serves a request to get an object by address, and returns Streamer instance.
|
|
|
|
func (s *Service) GetRange(ctx context.Context, prm RangePrm) error {
|
2021-05-18 12:13:40 +00:00
|
|
|
return s.getRange(ctx, prm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) getRange(ctx context.Context, prm RangePrm, opts ...execOption) error {
|
|
|
|
return s.get(ctx, prm.commonPrm, append(opts, withPayloadRange(prm.rng))...).err
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
func (s *Service) GetRangeHash(ctx context.Context, prm RangeHashPrm) (*RangeHashRes, error) {
|
|
|
|
hashes := make([][]byte, 0, len(prm.rngs))
|
|
|
|
|
|
|
|
for _, rng := range prm.rngs {
|
|
|
|
h := prm.hashGen()
|
|
|
|
|
|
|
|
// TODO: calculating of homomorphic hash (TZ) for "big" ranges can be optimized
|
|
|
|
// by "smaller" range hash requests spawn and response concatenation.
|
|
|
|
// NOTE: for non-homomorphic hashes (SHA256) this won't work with split-range.
|
|
|
|
|
|
|
|
rngPrm := RangePrm{
|
|
|
|
commonPrm: prm.commonPrm,
|
|
|
|
}
|
|
|
|
|
|
|
|
rngPrm.SetRange(rng)
|
|
|
|
rngPrm.SetChunkWriter(&hasherWrapper{
|
2021-01-11 13:50:49 +00:00
|
|
|
hash: util.NewSaltingWriter(h, prm.salt),
|
2020-12-08 16:18:24 +00:00
|
|
|
})
|
|
|
|
|
2021-05-20 08:04:20 +00:00
|
|
|
if err := s.getRange(ctx, rngPrm); err != nil {
|
2020-12-08 16:18:24 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes = append(hashes, h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &RangeHashRes{
|
|
|
|
hashes: hashes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
// Head reads object header from container.
|
|
|
|
//
|
|
|
|
// Returns ErrNotFound if the header was not received for the call.
|
|
|
|
// Returns SplitInfoError if object is virtual and raw flag is set.
|
|
|
|
func (s *Service) Head(ctx context.Context, prm HeadPrm) error {
|
|
|
|
return s.get(ctx, prm.commonPrm, headOnly()).err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) get(ctx context.Context, prm commonPrm, opts ...execOption) statusError {
|
2020-12-02 23:45:25 +00:00
|
|
|
exec := &execCtx{
|
2020-12-09 10:32:33 +00:00
|
|
|
svc: s,
|
|
|
|
ctx: ctx,
|
|
|
|
prm: RangePrm{
|
|
|
|
commonPrm: prm,
|
|
|
|
},
|
2020-12-02 23:45:25 +00:00
|
|
|
infoSplit: objectSDK.NewSplitInfo(),
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i](exec)
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
exec.setLogger(s.log)
|
|
|
|
|
|
|
|
exec.execute()
|
|
|
|
|
|
|
|
return exec.statusError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) execute() {
|
|
|
|
exec.log.Debug("serving request...")
|
|
|
|
|
|
|
|
// perform local operation
|
|
|
|
exec.executeLocal()
|
|
|
|
|
|
|
|
exec.analyzeStatus(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (exec *execCtx) analyzeStatus(execCnr bool) {
|
|
|
|
// analyze local result
|
|
|
|
switch exec.status {
|
|
|
|
case statusOK:
|
|
|
|
exec.log.Debug("operation finished successfully")
|
|
|
|
case statusINHUMED:
|
|
|
|
exec.log.Debug("requested object was marked as removed")
|
|
|
|
case statusVIRTUAL:
|
|
|
|
exec.log.Debug("requested object is virtual")
|
|
|
|
exec.assemble()
|
2020-12-07 17:49:47 +00:00
|
|
|
case statusOutOfRange:
|
|
|
|
exec.log.Debug("requested range is out of object bounds")
|
2020-12-02 23:45:25 +00:00
|
|
|
default:
|
|
|
|
exec.log.Debug("operation finished with error",
|
|
|
|
zap.String("error", exec.err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if execCnr {
|
|
|
|
exec.executeOnContainer()
|
|
|
|
exec.analyzeStatus(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|