2020-12-02 23:45:25 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
2022-06-29 17:33:48 +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-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-12-02 23:45:25 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (exec *execCtx) assemble() {
|
|
|
|
if !exec.canAssemble() {
|
|
|
|
exec.log.Debug("can not assemble the object")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-27 06:52:03 +00:00
|
|
|
// Do not use forwarding during assembly stage.
|
|
|
|
// Request forwarding closure inherited in produced
|
|
|
|
// `execCtx` so it should be disabled there.
|
|
|
|
exec.disableForwarding()
|
2021-04-29 12:18:29 +00:00
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
exec.log.Debug("trying to assemble the object...")
|
|
|
|
|
|
|
|
splitInfo := exec.splitInfo()
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
childID, ok := splitInfo.Link()
|
|
|
|
if !ok {
|
2022-05-31 17:00:41 +00:00
|
|
|
childID, ok = splitInfo.LastPart()
|
|
|
|
if !ok {
|
|
|
|
exec.log.Debug("neither linking nor last part of split-chain is presented in split info")
|
|
|
|
return
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
prev, children := exec.initFromChild(childID)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
if len(children) > 0 {
|
2020-12-07 17:49:47 +00:00
|
|
|
if exec.ctxRange() == nil {
|
|
|
|
if ok := exec.writeCollectedHeader(); ok {
|
2020-12-18 11:48:28 +00:00
|
|
|
exec.overtakePayloadDirectly(children, nil, true)
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1155 choose one-by-one restoring algorithm according to size
|
2020-12-07 17:49:47 +00:00
|
|
|
// * if size > MAX => go right-to-left with HEAD and back with GET
|
|
|
|
// * else go right-to-left with GET and compose in single object before writing
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
if ok := exec.overtakePayloadInReverse(children[len(children)-1]); ok {
|
2020-12-07 17:49:47 +00:00
|
|
|
// payload of all children except the last are written, write last payload
|
|
|
|
exec.writeObjectPayload(exec.collectedObject)
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
} else if prev != nil {
|
|
|
|
if ok := exec.writeCollectedHeader(); ok {
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1155 choose one-by-one restoring algorithm according to size
|
2020-12-02 23:45:25 +00:00
|
|
|
// * if size > MAX => go right-to-left with HEAD and back with GET
|
|
|
|
// * else go right-to-left with GET and compose in single object before writing
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
if ok := exec.overtakePayloadInReverse(*prev); ok {
|
2020-12-08 15:02:07 +00:00
|
|
|
// payload of all children except the last are written, write last payloa
|
2020-12-02 23:45:25 +00:00
|
|
|
exec.writeObjectPayload(exec.collectedObject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exec.log.Debug("could not init parent from child")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (exec *execCtx) initFromChild(obj oid.ID) (prev *oid.ID, children []oid.ID) {
|
|
|
|
log := exec.log.With(zap.Stringer("child ID", obj))
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
log.Debug("starting assembling from child")
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
child, ok := exec.getChild(obj, nil, true)
|
2020-12-02 23:45:25 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
par := child.Parent()
|
2020-12-02 23:45:25 +00:00
|
|
|
if par == nil {
|
|
|
|
exec.status = statusUndefined
|
|
|
|
|
|
|
|
log.Debug("received child with empty parent")
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
exec.collectedObject = par
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
var payload []byte
|
|
|
|
|
|
|
|
if rng := exec.ctxRange(); rng != nil {
|
|
|
|
seekOff := rng.GetOffset()
|
2022-11-08 08:23:56 +00:00
|
|
|
seekLen := rng.GetLength()
|
|
|
|
seekTo := seekOff + seekLen
|
2020-12-07 17:49:47 +00:00
|
|
|
parSize := par.PayloadSize()
|
|
|
|
|
2022-11-08 08:23:56 +00:00
|
|
|
if seekTo < seekOff || parSize < seekOff || parSize < seekTo {
|
2022-06-29 17:33:48 +00:00
|
|
|
var errOutOfRange apistatus.ObjectOutOfRange
|
|
|
|
|
|
|
|
exec.err = errOutOfRange
|
2020-12-07 17:49:47 +00:00
|
|
|
exec.status = statusOutOfRange
|
2022-06-29 17:33:48 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
childSize := child.PayloadSize()
|
|
|
|
|
2020-12-08 15:02:07 +00:00
|
|
|
exec.curOff = parSize - childSize
|
|
|
|
|
|
|
|
from := uint64(0)
|
|
|
|
if exec.curOff < seekOff {
|
|
|
|
from = seekOff - exec.curOff
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 15:02:07 +00:00
|
|
|
to := uint64(0)
|
|
|
|
if seekOff+seekLen > exec.curOff+from {
|
|
|
|
to = seekOff + seekLen - exec.curOff
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = child.Payload()[from:to]
|
|
|
|
rng.SetLength(rng.GetLength() - to + from)
|
2020-12-07 17:49:47 +00:00
|
|
|
} else {
|
|
|
|
payload = child.Payload()
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
exec.collectedObject.SetPayload(payload)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
idPrev, ok := child.PreviousID()
|
|
|
|
if ok {
|
|
|
|
return &idPrev, child.Children()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, child.Children()
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (exec *execCtx) overtakePayloadDirectly(children []oid.ID, rngs []objectSDK.Range, checkRight bool) {
|
2020-12-18 11:51:45 +00:00
|
|
|
withRng := len(rngs) > 0 && exec.ctxRange() != nil
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
for i := range children {
|
2020-12-07 17:49:47 +00:00
|
|
|
var r *objectSDK.Range
|
|
|
|
if withRng {
|
2022-03-15 12:11:35 +00:00
|
|
|
r = &rngs[i]
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
child, ok := exec.getChild(children[i], r, !withRng && checkRight)
|
2020-12-02 23:45:25 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok := exec.writeObjectPayload(child); !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exec.status = statusOK
|
|
|
|
exec.err = nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (exec *execCtx) overtakePayloadInReverse(prev oid.ID) bool {
|
2020-12-08 15:02:07 +00:00
|
|
|
chain, rngs, ok := exec.buildChainInReverse(prev)
|
|
|
|
if !ok {
|
2020-12-07 17:49:47 +00:00
|
|
|
return false
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
reverseRngs := len(rngs) > 0
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
// reverse chain
|
|
|
|
for left, right := 0, len(chain)-1; left < right; left, right = left+1, right-1 {
|
|
|
|
chain[left], chain[right] = chain[right], chain[left]
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
if reverseRngs {
|
|
|
|
rngs[left], rngs[right] = rngs[right], rngs[left]
|
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 11:48:28 +00:00
|
|
|
exec.overtakePayloadDirectly(chain, rngs, false)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2022-01-12 12:27:57 +00:00
|
|
|
return exec.status == statusOK
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (exec *execCtx) buildChainInReverse(prev oid.ID) ([]oid.ID, []objectSDK.Range, bool) {
|
2020-12-07 17:49:47 +00:00
|
|
|
var (
|
2022-05-31 17:00:41 +00:00
|
|
|
chain = make([]oid.ID, 0)
|
2022-03-15 12:11:35 +00:00
|
|
|
rngs = make([]objectSDK.Range, 0)
|
2020-12-07 17:49:47 +00:00
|
|
|
seekRng = exec.ctxRange()
|
|
|
|
from = seekRng.GetOffset()
|
|
|
|
to = from + seekRng.GetLength()
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
withPrev = true
|
2020-12-07 17:49:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// fill the chain end-to-start
|
2022-05-12 16:37:46 +00:00
|
|
|
for withPrev {
|
2020-12-07 17:49:47 +00:00
|
|
|
if exec.curOff < from {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
head, ok := exec.headChild(prev)
|
2020-12-07 17:49:47 +00:00
|
|
|
if !ok {
|
2020-12-08 15:02:07 +00:00
|
|
|
return nil, nil, false
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if seekRng != nil {
|
|
|
|
sz := head.PayloadSize()
|
|
|
|
|
|
|
|
exec.curOff -= sz
|
|
|
|
|
2020-12-08 15:02:07 +00:00
|
|
|
if exec.curOff < to {
|
2020-12-07 17:49:47 +00:00
|
|
|
off := uint64(0)
|
|
|
|
if from > exec.curOff {
|
|
|
|
off = from - exec.curOff
|
|
|
|
sz -= from - exec.curOff
|
|
|
|
}
|
|
|
|
|
|
|
|
if to < exec.curOff+off+sz {
|
|
|
|
sz = to - off - exec.curOff
|
|
|
|
}
|
|
|
|
|
2022-03-15 12:11:35 +00:00
|
|
|
index := len(rngs)
|
|
|
|
rngs = append(rngs, objectSDK.Range{})
|
|
|
|
rngs[index].SetOffset(off)
|
|
|
|
rngs[index].SetLength(sz)
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := head.ID()
|
|
|
|
chain = append(chain, id)
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := head.ID()
|
|
|
|
chain = append(chain, id)
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
prev, withPrev = head.PreviousID()
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 15:02:07 +00:00
|
|
|
return chain, rngs, true
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func equalAddresses(a, b oid.Address) bool {
|
|
|
|
return a.Container().Equals(b.Container()) && a.Object().Equals(b.Object())
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|