2020-11-30 15:56:32 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-30 16:39:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
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-30 15:56:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetRangePrm groups the parameters of GetRange operation.
|
|
|
|
type GetRangePrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-11-30 15:56:32 +00:00
|
|
|
|
|
|
|
rng *objectSDK.Range
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRangeRes groups resulting values of GetRange operation.
|
|
|
|
type GetRangeRes struct {
|
|
|
|
rngData []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets address of the requested object.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *GetRangePrm) SetAddress(addr *addressSDK.Address) {
|
2020-11-30 15:56:32 +00:00
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// SetRange sets range of the requested payload data .
|
2020-11-30 15:56:32 +00:00
|
|
|
func (p *GetRangePrm) SetRange(rng *objectSDK.Range) {
|
|
|
|
p.rng = rng
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeData returns the requested payload data range.
|
|
|
|
func (p *GetRangeRes) RangeData() []byte {
|
|
|
|
return p.rngData
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// GetRange reads range of the object from Blobovnicza by address.
|
2020-11-30 15:56:32 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object.
|
2020-11-30 16:39:05 +00:00
|
|
|
//
|
|
|
|
// Returns ErrNotFound if requested object is not
|
|
|
|
// presented in Blobovnicza. Returns ErrRangeOutOfBounds
|
|
|
|
// if requested range is outside the payload.
|
2020-11-30 15:56:32 +00:00
|
|
|
func (b *Blobovnicza) GetRange(prm *GetRangePrm) (*GetRangeRes, error) {
|
|
|
|
res, err := b.Get(&GetPrm{
|
|
|
|
addr: prm.addr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
// FIXME: code below is incorrect because Get returns raw object data
|
|
|
|
// so we should unmarshal payload from it before. If blobovnicza
|
|
|
|
// stores objects in non-protocol format (e.g. compressed)
|
|
|
|
// then it should not provide GetRange method.
|
|
|
|
|
2020-11-30 15:56:32 +00:00
|
|
|
from := prm.rng.GetOffset()
|
|
|
|
to := from + prm.rng.GetLength()
|
2020-12-02 09:58:42 +00:00
|
|
|
payload := res.obj
|
2020-11-30 15:56:32 +00:00
|
|
|
|
|
|
|
if from > to {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("invalid range [%d:%d]", from, to)
|
2020-11-30 15:56:32 +00:00
|
|
|
} else if uint64(len(payload)) < to {
|
2020-11-30 16:39:05 +00:00
|
|
|
return nil, object.ErrRangeOutOfBounds
|
2020-11-30 15:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &GetRangeRes{
|
|
|
|
rngData: payload[from:to],
|
|
|
|
}, nil
|
|
|
|
}
|