-
Notifications
You must be signed in to change notification settings - Fork 38
/
get_range.go
71 lines (58 loc) · 1.75 KB
/
get_range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package blobovnicza
import (
"fmt"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
)
// GetRangePrm groups the parameters of GetRange operation.
type GetRangePrm struct {
addr *objectSDK.Address
rng *objectSDK.Range
}
// GetRangeRes groups resulting values of GetRange operation.
type GetRangeRes struct {
rngData []byte
}
// SetAddress sets address of the requested object.
func (p *GetRangePrm) SetAddress(addr *objectSDK.Address) {
p.addr = addr
}
// SetRange sets range of the requested payload data .
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
}
// GetRange reads range of the object from Blobovnicza by address.
//
// Returns any error encountered that
// did not allow to completely read the object.
//
// Returns ErrNotFound if requested object is not
// presented in Blobovnicza. Returns ErrRangeOutOfBounds
// if requested range is outside the payload.
func (b *Blobovnicza) GetRange(prm *GetRangePrm) (*GetRangeRes, error) {
res, err := b.Get(&GetPrm{
addr: prm.addr,
})
if err != nil {
return nil, err
}
// 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.
from := prm.rng.GetOffset()
to := from + prm.rng.GetLength()
payload := res.obj
if from > to {
return nil, fmt.Errorf("invalid range [%d:%d]", from, to)
} else if uint64(len(payload)) < to {
return nil, object.ErrRangeOutOfBounds
}
return &GetRangeRes{
rngData: payload[from:to],
}, nil
}