Skip to content

Commit

Permalink
[nspcc-dev#1460] blobstor: Do not use pointers as the results
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
  • Loading branch information
carpawell authored and aprasolova committed Oct 19, 2022
1 parent 5dcfe4e commit 2d58a1a
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 63 deletions.
60 changes: 30 additions & 30 deletions pkg/local_object_storage/blobstor/blobovnicza.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (b *blobovniczas) put(addr oid.Address, data []byte) (*blobovnicza.ID, erro
//
// If blobocvnicza ID is specified, only this blobovnicza is processed.
// Otherwise, all blobovniczas are processed descending weight.
func (b *blobovniczas) get(prm GetSmallPrm) (res *GetSmallRes, err error) {
func (b *blobovniczas) get(prm GetSmallPrm) (res GetSmallRes, err error) {
var bPrm blobovnicza.GetPrm
bPrm.SetAddress(prm.addr)

Expand Down Expand Up @@ -241,7 +241,7 @@ func (b *blobovniczas) get(prm GetSmallPrm) (res *GetSmallRes, err error) {
return err == nil, nil
})

if err == nil && res == nil {
if err == nil && res.Object() == nil {
// not found in any blobovnicza
var errNotFound apistatus.ObjectNotFound

Expand All @@ -255,7 +255,7 @@ func (b *blobovniczas) get(prm GetSmallPrm) (res *GetSmallRes, err error) {
//
// If blobocvnicza ID is specified, only this blobovnicza is processed.
// Otherwise, all blobovniczas are processed descending weight.
func (b *blobovniczas) delete(prm DeleteSmallPrm) (res *DeleteSmallRes, err error) {
func (b *blobovniczas) delete(prm DeleteSmallPrm) (res DeleteSmallRes, err error) {
var bPrm blobovnicza.DeletePrm
bPrm.SetAddress(prm.addr)

Expand Down Expand Up @@ -301,7 +301,7 @@ func (b *blobovniczas) delete(prm DeleteSmallPrm) (res *DeleteSmallRes, err erro
// not found in any blobovnicza
var errNotFound apistatus.ObjectNotFound

return nil, errNotFound
return DeleteSmallRes{}, errNotFound
}

return
Expand All @@ -311,11 +311,11 @@ func (b *blobovniczas) delete(prm DeleteSmallPrm) (res *DeleteSmallRes, err erro
//
// If blobocvnicza ID is specified, only this blobovnicza is processed.
// Otherwise, all blobovniczas are processed descending weight.
func (b *blobovniczas) getRange(prm GetRangeSmallPrm) (res *GetRangeSmallRes, err error) {
func (b *blobovniczas) getRange(prm GetRangeSmallPrm) (res GetRangeSmallRes, err error) {
if prm.blobovniczaID != nil {
blz, err := b.openBlobovnicza(prm.blobovniczaID.String())
if err != nil {
return nil, err
return GetRangeSmallRes{}, err
}

return b.getObjectRange(blz, prm)
Expand Down Expand Up @@ -355,7 +355,7 @@ func (b *blobovniczas) getRange(prm GetRangeSmallPrm) (res *GetRangeSmallRes, er
// not found in any blobovnicza
var errNotFound apistatus.ObjectNotFound

return nil, errNotFound
return GetRangeSmallRes{}, errNotFound
}

return
Expand All @@ -364,7 +364,7 @@ func (b *blobovniczas) getRange(prm GetRangeSmallPrm) (res *GetRangeSmallRes, er
// tries to delete object from particular blobovnicza.
//
// returns no error if object was removed from some blobovnicza of the same level.
func (b *blobovniczas) deleteObjectFromLevel(prm blobovnicza.DeletePrm, blzPath string, tryActive bool, dp DeleteSmallPrm) (*DeleteSmallRes, error) {
func (b *blobovniczas) deleteObjectFromLevel(prm blobovnicza.DeletePrm, blzPath string, tryActive bool, dp DeleteSmallPrm) (DeleteSmallRes, error) {
lvlPath := filepath.Dir(blzPath)

// try to remove from blobovnicza if it is opened
Expand Down Expand Up @@ -410,13 +410,13 @@ func (b *blobovniczas) deleteObjectFromLevel(prm blobovnicza.DeletePrm, blzPath
b.log.Debug("index is too big", zap.String("path", blzPath))
var errNotFound apistatus.ObjectNotFound

return nil, errNotFound
return DeleteSmallRes{}, errNotFound
}

// open blobovnicza (cached inside)
blz, err := b.openBlobovnicza(blzPath)
if err != nil {
return nil, err
return DeleteSmallRes{}, err
}

return b.deleteObject(blz, prm, dp)
Expand All @@ -425,7 +425,7 @@ func (b *blobovniczas) deleteObjectFromLevel(prm blobovnicza.DeletePrm, blzPath
// tries to read object from particular blobovnicza.
//
// returns error if object could not be read from any blobovnicza of the same level.
func (b *blobovniczas) getObjectFromLevel(prm blobovnicza.GetPrm, blzPath string, tryActive bool) (*GetSmallRes, error) {
func (b *blobovniczas) getObjectFromLevel(prm blobovnicza.GetPrm, blzPath string, tryActive bool) (GetSmallRes, error) {
lvlPath := filepath.Dir(blzPath)

// try to read from blobovnicza if it is opened
Expand Down Expand Up @@ -472,13 +472,13 @@ func (b *blobovniczas) getObjectFromLevel(prm blobovnicza.GetPrm, blzPath string
b.log.Debug("index is too big", zap.String("path", blzPath))
var errNotFound apistatus.ObjectNotFound

return nil, errNotFound
return GetSmallRes{}, errNotFound
}

// open blobovnicza (cached inside)
blz, err := b.openBlobovnicza(blzPath)
if err != nil {
return nil, err
return GetSmallRes{}, err
}

return b.getObject(blz, prm)
Expand All @@ -487,7 +487,7 @@ func (b *blobovniczas) getObjectFromLevel(prm blobovnicza.GetPrm, blzPath string
// tries to read range of object payload data from particular blobovnicza.
//
// returns error if object could not be read from any blobovnicza of the same level.
func (b *blobovniczas) getRangeFromLevel(prm GetRangeSmallPrm, blzPath string, tryActive bool) (*GetRangeSmallRes, error) {
func (b *blobovniczas) getRangeFromLevel(prm GetRangeSmallPrm, blzPath string, tryActive bool) (GetRangeSmallRes, error) {
lvlPath := filepath.Dir(blzPath)

// try to read from blobovnicza if it is opened
Expand Down Expand Up @@ -545,23 +545,23 @@ func (b *blobovniczas) getRangeFromLevel(prm GetRangeSmallPrm, blzPath string, t

var errNotFound apistatus.ObjectNotFound

return nil, errNotFound
return GetRangeSmallRes{}, errNotFound
}

// open blobovnicza (cached inside)
blz, err := b.openBlobovnicza(blzPath)
if err != nil {
return nil, err
return GetRangeSmallRes{}, err
}

return b.getObjectRange(blz, prm)
}

// removes object from blobovnicza and returns DeleteSmallRes.
func (b *blobovniczas) deleteObject(blz *blobovnicza.Blobovnicza, prm blobovnicza.DeletePrm, dp DeleteSmallPrm) (*DeleteSmallRes, error) {
func (b *blobovniczas) deleteObject(blz *blobovnicza.Blobovnicza, prm blobovnicza.DeletePrm, dp DeleteSmallPrm) (DeleteSmallRes, error) {
_, err := blz.Delete(prm)
if err != nil {
return nil, err
return DeleteSmallRes{}, err
}

storagelog.Write(b.log,
Expand All @@ -570,37 +570,37 @@ func (b *blobovniczas) deleteObject(blz *blobovnicza.Blobovnicza, prm blobovnicz
zap.Stringer("blobovnicza ID", dp.blobovniczaID),
)

return nil, nil
return DeleteSmallRes{}, nil
}

// reads object from blobovnicza and returns GetSmallRes.
func (b *blobovniczas) getObject(blz *blobovnicza.Blobovnicza, prm blobovnicza.GetPrm) (*GetSmallRes, error) {
func (b *blobovniczas) getObject(blz *blobovnicza.Blobovnicza, prm blobovnicza.GetPrm) (GetSmallRes, error) {
res, err := blz.Get(prm)
if err != nil {
return nil, err
return GetSmallRes{}, err
}

// decompress the data
data, err := b.decompressor(res.Object())
if err != nil {
return nil, fmt.Errorf("could not decompress object data: %w", err)
return GetSmallRes{}, fmt.Errorf("could not decompress object data: %w", err)
}

// unmarshal the object
obj := objectSDK.New()
if err := obj.Unmarshal(data); err != nil {
return nil, fmt.Errorf("could not unmarshal the object: %w", err)
return GetSmallRes{}, fmt.Errorf("could not unmarshal the object: %w", err)
}

return &GetSmallRes{
return GetSmallRes{
roObject: roObject{
obj: obj,
},
}, nil
}

// reads range of object payload data from blobovnicza and returns GetRangeSmallRes.
func (b *blobovniczas) getObjectRange(blz *blobovnicza.Blobovnicza, prm GetRangeSmallPrm) (*GetRangeSmallRes, error) {
func (b *blobovniczas) getObjectRange(blz *blobovnicza.Blobovnicza, prm GetRangeSmallPrm) (GetRangeSmallRes, error) {
var gPrm blobovnicza.GetPrm
gPrm.SetAddress(prm.addr)

Expand All @@ -610,30 +610,30 @@ func (b *blobovniczas) getObjectRange(blz *blobovnicza.Blobovnicza, prm GetRange
// we can start using GetRange.
res, err := blz.Get(gPrm)
if err != nil {
return nil, err
return GetRangeSmallRes{}, err
}

// decompress the data
data, err := b.decompressor(res.Object())
if err != nil {
return nil, fmt.Errorf("could not decompress object data: %w", err)
return GetRangeSmallRes{}, fmt.Errorf("could not decompress object data: %w", err)
}

// unmarshal the object
obj := objectSDK.New()
if err := obj.Unmarshal(data); err != nil {
return nil, fmt.Errorf("could not unmarshal the object: %w", err)
return GetRangeSmallRes{}, fmt.Errorf("could not unmarshal the object: %w", err)
}

from := prm.rng.GetOffset()
to := from + prm.rng.GetLength()
payload := obj.Payload()

if uint64(len(payload)) < to {
return nil, object.ErrRangeOutOfBounds
return GetRangeSmallRes{}, object.ErrRangeOutOfBounds
}

return &GetRangeSmallRes{
return GetRangeSmallRes{
rangeData: rangeData{
data: payload[from:to],
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/local_object_storage/blobstor/delete_big.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DeleteBigRes struct{}
// to completely remove the object.
//
// Returns an error of type apistatus.ObjectNotFound if there is no object to delete.
func (b *BlobStor) DeleteBig(prm DeleteBigPrm) (*DeleteBigRes, error) {
func (b *BlobStor) DeleteBig(prm DeleteBigPrm) (DeleteBigRes, error) {
err := b.fsTree.Delete(prm.addr)
if errors.Is(err, fstree.ErrFileNotFound) {
var errNotFound apistatus.ObjectNotFound
Expand All @@ -34,5 +34,5 @@ func (b *BlobStor) DeleteBig(prm DeleteBigPrm) (*DeleteBigRes, error) {
storagelog.Write(b.log, storagelog.AddressField(prm.addr), storagelog.OpField("fstree DELETE"))
}

return nil, err
return DeleteBigRes{}, err
}
2 changes: 1 addition & 1 deletion pkg/local_object_storage/blobstor/delete_small.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ type DeleteSmallRes struct{}
// to completely remove the object.
//
// Returns an error of type apistatus.ObjectNotFound if there is no object to delete.
func (b *BlobStor) DeleteSmall(prm DeleteSmallPrm) (*DeleteSmallRes, error) {
func (b *BlobStor) DeleteSmall(prm DeleteSmallPrm) (DeleteSmallRes, error) {
return b.blobovniczas.delete(prm)
}
7 changes: 4 additions & 3 deletions pkg/local_object_storage/blobstor/exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r ExistsRes) Exists() bool {
//
// Returns any error encountered that did not allow
// to completely check object existence.
func (b *BlobStor) Exists(prm ExistsPrm) (*ExistsRes, error) {
func (b *BlobStor) Exists(prm ExistsPrm) (ExistsRes, error) {
// check presence in shallow dir first (cheaper)
exists, err := b.existsBig(prm.addr)

Expand Down Expand Up @@ -58,9 +58,10 @@ func (b *BlobStor) Exists(prm ExistsPrm) (*ExistsRes, error) {
}

if err != nil {
return nil, err
return ExistsRes{}, err
}
return &ExistsRes{exists: exists}, err

return ExistsRes{exists: exists}, err
}

// checks if object is presented in shallow dir.
Expand Down
12 changes: 6 additions & 6 deletions pkg/local_object_storage/blobstor/get_big.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@ type GetBigRes struct {
//
// Returns an error of type apistatus.ObjectNotFound if the requested object is not
// presented in shallow dir.
func (b *BlobStor) GetBig(prm GetBigPrm) (*GetBigRes, error) {
func (b *BlobStor) GetBig(prm GetBigPrm) (GetBigRes, error) {
// get compressed object data
data, err := b.fsTree.Get(prm.addr)
if err != nil {
if errors.Is(err, fstree.ErrFileNotFound) {
var errNotFound apistatus.ObjectNotFound

return nil, errNotFound
return GetBigRes{}, errNotFound
}

return nil, fmt.Errorf("could not read object from fs tree: %w", err)
return GetBigRes{}, fmt.Errorf("could not read object from fs tree: %w", err)
}

data, err = b.decompressor(data)
if err != nil {
return nil, fmt.Errorf("could not decompress object data: %w", err)
return GetBigRes{}, fmt.Errorf("could not decompress object data: %w", err)
}

// unmarshal the object
obj := objectSDK.New()
if err := obj.Unmarshal(data); err != nil {
return nil, fmt.Errorf("could not unmarshal the object: %w", err)
return GetBigRes{}, fmt.Errorf("could not unmarshal the object: %w", err)
}

return &GetBigRes{
return GetBigRes{
roObject: roObject{
obj: obj,
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/local_object_storage/blobstor/get_range_big.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,38 @@ type GetRangeBigRes struct {
//
// Returns ErrRangeOutOfBounds if the requested object range is out of bounds.
// Returns an error of type apistatus.ObjectNotFound if object is missing.
func (b *BlobStor) GetRangeBig(prm GetRangeBigPrm) (*GetRangeBigRes, error) {
func (b *BlobStor) GetRangeBig(prm GetRangeBigPrm) (GetRangeBigRes, error) {
// get compressed object data
data, err := b.fsTree.Get(prm.addr)
if err != nil {
if errors.Is(err, fstree.ErrFileNotFound) {
var errNotFound apistatus.ObjectNotFound

return nil, errNotFound
return GetRangeBigRes{}, errNotFound
}

return nil, fmt.Errorf("could not read object from fs tree: %w", err)
return GetRangeBigRes{}, fmt.Errorf("could not read object from fs tree: %w", err)
}

data, err = b.decompressor(data)
if err != nil {
return nil, fmt.Errorf("could not decompress object data: %w", err)
return GetRangeBigRes{}, fmt.Errorf("could not decompress object data: %w", err)
}

// unmarshal the object
obj := objectSDK.New()
if err := obj.Unmarshal(data); err != nil {
return nil, fmt.Errorf("could not unmarshal the object: %w", err)
return GetRangeBigRes{}, fmt.Errorf("could not unmarshal the object: %w", err)
}

payload := obj.Payload()
ln, off := prm.rng.GetLength(), prm.rng.GetOffset()

if pLen := uint64(len(payload)); pLen < ln+off {
return nil, object.ErrRangeOutOfBounds
return GetRangeBigRes{}, object.ErrRangeOutOfBounds
}

return &GetRangeBigRes{
return GetRangeBigRes{
rangeData: rangeData{
data: payload[off : off+ln],
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/local_object_storage/blobstor/get_range_small.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ type GetRangeSmallRes struct {
//
// Returns ErrRangeOutOfBounds if the requested object range is out of bounds.
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in blobovnicza(s).
func (b *BlobStor) GetRangeSmall(prm GetRangeSmallPrm) (*GetRangeSmallRes, error) {
func (b *BlobStor) GetRangeSmall(prm GetRangeSmallPrm) (GetRangeSmallRes, error) {
return b.blobovniczas.getRange(prm)
}
2 changes: 1 addition & 1 deletion pkg/local_object_storage/blobstor/get_small.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ type GetSmallRes struct {
// did not allow to completely read the object.
//
// Returns an error of type apistatus.ObjectNotFound if the requested object is missing in blobovnicza(s).
func (b *BlobStor) GetSmall(prm GetSmallPrm) (*GetSmallRes, error) {
func (b *BlobStor) GetSmall(prm GetSmallPrm) (GetSmallRes, error) {
return b.blobovniczas.get(prm)
}
Loading

0 comments on commit 2d58a1a

Please sign in to comment.