From 0b130150ae5f43fee1f3c0abec56d423985b676c Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 24 Nov 2020 17:15:58 +0300 Subject: [PATCH] [#211] blobstor: Rename GetRange to GetRangeBig Signed-off-by: Leonard Lyubich --- .../blobstor/get_range_big.go | 57 +++++++++++++ pkg/local_object_storage/blobstor/getrange.go | 82 ------------------- pkg/local_object_storage/blobstor/util.go | 27 ++++++ pkg/local_object_storage/shard/get.go | 16 ++-- 4 files changed, 95 insertions(+), 87 deletions(-) create mode 100644 pkg/local_object_storage/blobstor/get_range_big.go delete mode 100644 pkg/local_object_storage/blobstor/getrange.go diff --git a/pkg/local_object_storage/blobstor/get_range_big.go b/pkg/local_object_storage/blobstor/get_range_big.go new file mode 100644 index 00000000..ee3d5af4 --- /dev/null +++ b/pkg/local_object_storage/blobstor/get_range_big.go @@ -0,0 +1,57 @@ +package blobstor + +import ( + "github.com/nspcc-dev/neofs-node/pkg/core/object" + "github.com/pkg/errors" +) + +// GetRangeBigPrm groups the parameters of GetRangeBig operation. +type GetRangeBigPrm struct { + address + rwRange +} + +// GetRangeBigRes groups resulting values of GetRangeBig operation. +type GetRangeBigRes struct { + rangeData +} + +// GetRangeBig reads data of object payload range from shallow dir of BLOB storage. +// +// Returns any error encountered that +// did not allow to completely read the object payload range. +func (b *BlobStor) GetRangeBig(prm *GetRangeBigPrm) (*GetRangeBigRes, error) { + b.mtx.RLock() + defer b.mtx.RUnlock() + + // get compressed object data + data, err := b.fsTree.get(prm.addr) + if err != nil { + return nil, errors.Wrap(err, "could not read object from fs tree") + } + + data, err = b.decompressor(data) + if err != nil { + return nil, errors.Wrap(err, "could not decompress object data") + } + + // unmarshal the object + obj := object.New() + if err := obj.Unmarshal(data); err != nil { + return nil, errors.Wrap(err, "could not unmarshal the object") + } + + payload := obj.Payload() + ln, off := prm.rng.GetLength(), prm.rng.GetOffset() + + if pLen := uint64(len(payload)); pLen < ln+off { + return nil, errors.Errorf("range is out-of-bounds (payload %d, off %d, ln %d)", + pLen, off, ln) + } + + return &GetRangeBigRes{ + rangeData: rangeData{ + data: payload[off : off+ln], + }, + }, nil +} diff --git a/pkg/local_object_storage/blobstor/getrange.go b/pkg/local_object_storage/blobstor/getrange.go deleted file mode 100644 index 021608ef..00000000 --- a/pkg/local_object_storage/blobstor/getrange.go +++ /dev/null @@ -1,82 +0,0 @@ -package blobstor - -import ( - objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" - "github.com/nspcc-dev/neofs-node/pkg/core/object" - "github.com/pkg/errors" -) - -// GetRangePrm groups the parameters of GetRange operation. -type GetRangePrm struct { - off, ln uint64 - - addr *objectSDK.Address -} - -// GetRangeRes groups resulting values of GetRange operation. -type GetRangeRes struct { - rngData []byte -} - -// WithAddress is a GetRange option to set the address of the requested object. -// -// Option is required. -func (p *GetRangePrm) WithAddress(addr *objectSDK.Address) *GetRangePrm { - if p != nil { - p.addr = addr - } - - return p -} - -// WithPayloadRange is a GetRange option to set range of requested payload data. -// -// Option is required. -func (p *GetRangePrm) WithPayloadRange(off, ln uint64) *GetRangePrm { - if p != nil { - p.off, p.ln = off, ln - } - - return p -} - -// RangeData returns data of the requested payload range. -func (r *GetRangeRes) RangeData() []byte { - return r.rngData -} - -// GetRange reads data of object payload range from BLOB storage. -// -// Returns any error encountered that -// did not allow to completely read the object payload range. -func (b *BlobStor) GetRange(prm *GetRangePrm) (*GetRangeRes, error) { - b.mtx.RLock() - defer b.mtx.RUnlock() - - // get compressed object data - data, err := b.fsTree.get(prm.addr) - if err != nil { - return nil, errors.Wrap(err, "could not read object from fs tree") - } - - data, err = b.decompressor(data) - if err != nil { - return nil, errors.Wrap(err, "could not decompress object data") - } - - // unmarshal the object - obj := object.New() - if err := obj.Unmarshal(data); err != nil { - return nil, errors.Wrap(err, "could not unmarshal the object") - } - - payload := obj.Payload() - if pLen := uint64(len(payload)); pLen < prm.ln+prm.off { - return nil, errors.Errorf("range is out-of-bounds (payload %d, off %d, ln %d)", - pLen, prm.off, prm.ln) - } - - return &GetRangeRes{ - rngData: payload[prm.off : prm.off+prm.ln], - }, nil -} diff --git a/pkg/local_object_storage/blobstor/util.go b/pkg/local_object_storage/blobstor/util.go index 86bf23cf..3822afc1 100644 --- a/pkg/local_object_storage/blobstor/util.go +++ b/pkg/local_object_storage/blobstor/util.go @@ -50,3 +50,30 @@ type rwBlobovniczaID struct { func (v *rwBlobovniczaID) SetBlobovniczaID(id *blobovnicza.ID) { v.blobovniczaID = id } + +type roRange struct { + rng *objectSDK.Range +} + +// Range returns range of the object payload. +func (r roRange) Range() *objectSDK.Range { + return r.rng +} + +type rwRange struct { + roRange +} + +// SetRange sets range of the object payload. +func (r *rwRange) SetRange(rng *objectSDK.Range) { + r.rng = rng +} + +type rangeData struct { + data []byte +} + +// RangeData returns data of the requested payload range. +func (d rangeData) RangeData() []byte { + return d.data +} diff --git a/pkg/local_object_storage/shard/get.go b/pkg/local_object_storage/shard/get.go index d30c3826..5bd7fa4f 100644 --- a/pkg/local_object_storage/shard/get.go +++ b/pkg/local_object_storage/shard/get.go @@ -108,11 +108,17 @@ func (s *Shard) Get(prm *GetPrm) (*GetRes, error) { // try to read from WriteCache // TODO: implement - res, err := s.blobStor.GetRange( - new(blobstor.GetRangePrm). - WithAddress(prm.addr). - WithPayloadRange(prm.off, uint64(prm.ln)), - ) + // form GetRangeBig parameters + getRngBigPrm := new(blobstor.GetRangeBigPrm) + getRngBigPrm.SetAddress(prm.addr) + + rng := objectSDK.NewRange() + rng.SetOffset(prm.off) + rng.SetLength(uint64(prm.ln)) + + getRngBigPrm.SetRange(rng) + + res, err := s.blobStor.GetRangeBig(getRngBigPrm) if err != nil { if errors.Is(err, blobstor.ErrObjectNotFound) { err = ErrObjectNotFound