From 08bf8a68f174fd301f937392a183703a66b848aa Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 31 May 2022 14:56:59 +0300 Subject: [PATCH] [#1460] engine: Do not use pointers as the results Signed-off-by: Pavel Karpy --- pkg/local_object_storage/engine/container.go | 14 ++++++-------- pkg/local_object_storage/engine/delete.go | 8 ++++---- pkg/local_object_storage/engine/get.go | 12 ++++++------ pkg/local_object_storage/engine/head.go | 10 +++++----- pkg/local_object_storage/engine/inhume.go | 12 ++++++------ pkg/local_object_storage/engine/list.go | 8 ++++---- pkg/local_object_storage/engine/put.go | 8 ++++---- pkg/local_object_storage/engine/range.go | 12 ++++++------ pkg/local_object_storage/engine/select.go | 12 ++++++------ 9 files changed, 47 insertions(+), 49 deletions(-) diff --git a/pkg/local_object_storage/engine/container.go b/pkg/local_object_storage/engine/container.go index ced3f3cdb..81acb71a1 100644 --- a/pkg/local_object_storage/engine/container.go +++ b/pkg/local_object_storage/engine/container.go @@ -42,7 +42,7 @@ func (r ListContainersRes) Containers() []cid.ID { // ContainerSize returns the sum of estimation container sizes among all shards. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) ContainerSize(prm ContainerSizePrm) (res *ContainerSizeRes, err error) { +func (e *StorageEngine) ContainerSize(prm ContainerSizePrm) (res ContainerSizeRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.containerSize(prm) return err @@ -65,13 +65,11 @@ func ContainerSize(e *StorageEngine, id cid.ID) (uint64, error) { return res.Size(), nil } -func (e *StorageEngine) containerSize(prm ContainerSizePrm) (*ContainerSizeRes, error) { +func (e *StorageEngine) containerSize(prm ContainerSizePrm) (res ContainerSizeRes, err error) { if e.metrics != nil { defer elapsed(e.metrics.AddEstimateContainerSizeDuration)() } - res := new(ContainerSizeRes) - e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) { size, err := shard.ContainerSize(sh.Shard, prm.cnr) if err != nil { @@ -86,13 +84,13 @@ func (e *StorageEngine) containerSize(prm ContainerSizePrm) (*ContainerSizeRes, return false }) - return res, nil + return } // ListContainers returns a unique container IDs presented in the engine objects. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) ListContainers(_ ListContainersPrm) (res *ListContainersRes, err error) { +func (e *StorageEngine) ListContainers(_ ListContainersPrm) (res ListContainersRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.listContainers() return err @@ -113,7 +111,7 @@ func ListContainers(e *StorageEngine) ([]cid.ID, error) { return res.Containers(), nil } -func (e *StorageEngine) listContainers() (*ListContainersRes, error) { +func (e *StorageEngine) listContainers() (ListContainersRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddListContainersDuration)() } @@ -142,7 +140,7 @@ func (e *StorageEngine) listContainers() (*ListContainersRes, error) { result = append(result, v) } - return &ListContainersRes{ + return ListContainersRes{ containers: result, }, nil } diff --git a/pkg/local_object_storage/engine/delete.go b/pkg/local_object_storage/engine/delete.go index 51e6058d2..def9d284a 100644 --- a/pkg/local_object_storage/engine/delete.go +++ b/pkg/local_object_storage/engine/delete.go @@ -31,7 +31,7 @@ func (p *DeletePrm) WithAddresses(addr ...oid.Address) { // // Returns apistatus.ObjectLocked if at least one object is locked. // In this case no object from the list is marked to be deleted. -func (e *StorageEngine) Delete(prm DeletePrm) (res *DeleteRes, err error) { +func (e *StorageEngine) Delete(prm DeletePrm) (res DeleteRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.delete(prm) return err @@ -40,7 +40,7 @@ func (e *StorageEngine) Delete(prm DeletePrm) (res *DeleteRes, err error) { return } -func (e *StorageEngine) delete(prm DeletePrm) (*DeleteRes, error) { +func (e *StorageEngine) delete(prm DeletePrm) (DeleteRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddDeleteDuration)() } @@ -80,8 +80,8 @@ func (e *StorageEngine) delete(prm DeletePrm) (*DeleteRes, error) { } if locked.is { - return nil, locked.err + return DeleteRes{}, locked.err } - return nil, nil + return DeleteRes{}, nil } diff --git a/pkg/local_object_storage/engine/get.go b/pkg/local_object_storage/engine/get.go index ee722601f..9bf42a3f6 100644 --- a/pkg/local_object_storage/engine/get.go +++ b/pkg/local_object_storage/engine/get.go @@ -44,7 +44,7 @@ func (r GetRes) Object() *objectSDK.Object { // Returns an error of type apistatus.ObjectAlreadyRemoved if the object has been marked as removed. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) Get(prm GetPrm) (res *GetRes, err error) { +func (e *StorageEngine) Get(prm GetPrm) (res GetRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.get(prm) return err @@ -53,7 +53,7 @@ func (e *StorageEngine) Get(prm GetPrm) (res *GetRes, err error) { return } -func (e *StorageEngine) get(prm GetPrm) (*GetRes, error) { +func (e *StorageEngine) get(prm GetPrm) (GetRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddGetDuration)() } @@ -118,12 +118,12 @@ func (e *StorageEngine) get(prm GetPrm) (*GetRes, error) { }) if outSI != nil { - return nil, objectSDK.NewSplitInfoError(outSI) + return GetRes{}, objectSDK.NewSplitInfoError(outSI) } if obj == nil { if shardWithMeta.Shard == nil || !shard.IsErrNotFound(outError) { - return nil, outError + return GetRes{}, outError } // If the object is not found but is present in metabase, @@ -137,13 +137,13 @@ func (e *StorageEngine) get(prm GetPrm) (*GetRes, error) { return err == nil }) if obj == nil { - return nil, outError + return GetRes{}, outError } e.reportShardError(shardWithMeta, "meta info was present, but object is missing", metaError, zap.Stringer("address", prm.addr)) } - return &GetRes{ + return GetRes{ obj: obj, }, nil } diff --git a/pkg/local_object_storage/engine/head.go b/pkg/local_object_storage/engine/head.go index 250cd0742..d17acc1d3 100644 --- a/pkg/local_object_storage/engine/head.go +++ b/pkg/local_object_storage/engine/head.go @@ -55,7 +55,7 @@ func (r HeadRes) Header() *objectSDK.Object { // Returns an error of type apistatus.ObjectAlreadyRemoved if the requested object was inhumed. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) Head(prm HeadPrm) (res *HeadRes, err error) { +func (e *StorageEngine) Head(prm HeadPrm) (res HeadRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.head(prm) return err @@ -64,7 +64,7 @@ func (e *StorageEngine) Head(prm HeadPrm) (res *HeadRes, err error) { return } -func (e *StorageEngine) head(prm HeadPrm) (*HeadRes, error) { +func (e *StorageEngine) head(prm HeadPrm) (HeadRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddHeadDuration)() } @@ -123,14 +123,14 @@ func (e *StorageEngine) head(prm HeadPrm) (*HeadRes, error) { }) if outSI != nil { - return nil, objectSDK.NewSplitInfoError(outSI) + return HeadRes{}, objectSDK.NewSplitInfoError(outSI) } if head == nil { - return nil, outError + return HeadRes{}, outError } - return &HeadRes{ + return HeadRes{ head: head, }, nil } diff --git a/pkg/local_object_storage/engine/inhume.go b/pkg/local_object_storage/engine/inhume.go index 32d942caa..85c33bbaf 100644 --- a/pkg/local_object_storage/engine/inhume.go +++ b/pkg/local_object_storage/engine/inhume.go @@ -51,7 +51,7 @@ var errInhumeFailure = errors.New("inhume operation failed") // if at least one object is locked. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) Inhume(prm InhumePrm) (res *InhumeRes, err error) { +func (e *StorageEngine) Inhume(prm InhumePrm) (res InhumeRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.inhume(prm) return err @@ -60,7 +60,7 @@ func (e *StorageEngine) Inhume(prm InhumePrm) (res *InhumeRes, err error) { return } -func (e *StorageEngine) inhume(prm InhumePrm) (*InhumeRes, error) { +func (e *StorageEngine) inhume(prm InhumePrm) (InhumeRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddInhumeDuration)() } @@ -76,18 +76,18 @@ func (e *StorageEngine) inhume(prm InhumePrm) (*InhumeRes, error) { switch e.inhumeAddr(prm.addrs[i], shPrm, true) { case 1: - return nil, apistatus.ObjectLocked{} + return InhumeRes{}, apistatus.ObjectLocked{} case 0: switch e.inhumeAddr(prm.addrs[i], shPrm, false) { case 1: - return nil, apistatus.ObjectLocked{} + return InhumeRes{}, apistatus.ObjectLocked{} case 0: - return nil, errInhumeFailure + return InhumeRes{}, errInhumeFailure } } } - return new(InhumeRes), nil + return InhumeRes{}, nil } // Returns: diff --git a/pkg/local_object_storage/engine/list.go b/pkg/local_object_storage/engine/list.go index 6e6798029..6abb9167c 100644 --- a/pkg/local_object_storage/engine/list.go +++ b/pkg/local_object_storage/engine/list.go @@ -59,7 +59,7 @@ func (l ListWithCursorRes) Cursor() *Cursor { // // Returns ErrEndOfListing if there are no more objects to return or count // parameter set to zero. -func (e *StorageEngine) ListWithCursor(prm ListWithCursorPrm) (*ListWithCursorRes, error) { +func (e *StorageEngine) ListWithCursor(prm ListWithCursorPrm) (ListWithCursorRes, error) { result := make([]oid.Address, 0, prm.count) // 1. Get available shards and sort them. @@ -71,7 +71,7 @@ func (e *StorageEngine) ListWithCursor(prm ListWithCursorPrm) (*ListWithCursorRe e.mtx.RUnlock() if len(shardIDs) == 0 { - return nil, ErrEndOfListing + return ListWithCursorRes{}, ErrEndOfListing } sort.Slice(shardIDs, func(i, j int) bool { @@ -119,10 +119,10 @@ func (e *StorageEngine) ListWithCursor(prm ListWithCursorPrm) (*ListWithCursorRe } if len(result) == 0 { - return nil, ErrEndOfListing + return ListWithCursorRes{}, ErrEndOfListing } - return &ListWithCursorRes{ + return ListWithCursorRes{ addrList: result, cursor: cursor, }, nil diff --git a/pkg/local_object_storage/engine/put.go b/pkg/local_object_storage/engine/put.go index 475ef0921..16d05ee31 100644 --- a/pkg/local_object_storage/engine/put.go +++ b/pkg/local_object_storage/engine/put.go @@ -36,7 +36,7 @@ func (p *PutPrm) WithObject(obj *objectSDK.Object) { // Returns an error if executions are blocked (see BlockExecution). // // Returns an error of type apistatus.ObjectAlreadyRemoved if the object has been marked as removed. -func (e *StorageEngine) Put(prm PutPrm) (res *PutRes, err error) { +func (e *StorageEngine) Put(prm PutPrm) (res PutRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.put(prm) return err @@ -45,7 +45,7 @@ func (e *StorageEngine) Put(prm PutPrm) (res *PutRes, err error) { return } -func (e *StorageEngine) put(prm PutPrm) (*PutRes, error) { +func (e *StorageEngine) put(prm PutPrm) (PutRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddPutDuration)() } @@ -56,7 +56,7 @@ func (e *StorageEngine) put(prm PutPrm) (*PutRes, error) { // much slower on fast machines for 4 shards. _, err := e.exists(addr) if err != nil { - return nil, err + return PutRes{}, err } var existPrm shard.ExistsPrm @@ -125,7 +125,7 @@ func (e *StorageEngine) put(prm PutPrm) (*PutRes, error) { err = errPutShard } - return nil, err + return PutRes{}, err } // Put writes provided object to local storage. diff --git a/pkg/local_object_storage/engine/range.go b/pkg/local_object_storage/engine/range.go index 63a52ffbd..721ad713c 100644 --- a/pkg/local_object_storage/engine/range.go +++ b/pkg/local_object_storage/engine/range.go @@ -60,7 +60,7 @@ func (r RngRes) Object() *objectSDK.Object { // Returns ErrRangeOutOfBounds if the requested object range is out of bounds. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) GetRange(prm RngPrm) (res *RngRes, err error) { +func (e *StorageEngine) GetRange(prm RngPrm) (res RngRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.getRange(prm) return err @@ -69,7 +69,7 @@ func (e *StorageEngine) GetRange(prm RngPrm) (res *RngRes, err error) { return } -func (e *StorageEngine) getRange(prm RngPrm) (*RngRes, error) { +func (e *StorageEngine) getRange(prm RngPrm) (RngRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddRangeDuration)() } @@ -137,12 +137,12 @@ func (e *StorageEngine) getRange(prm RngPrm) (*RngRes, error) { }) if outSI != nil { - return nil, objectSDK.NewSplitInfoError(outSI) + return RngRes{}, objectSDK.NewSplitInfoError(outSI) } if obj == nil { if shardWithMeta.Shard == nil || !shard.IsErrNotFound(outError) { - return nil, outError + return RngRes{}, outError } // If the object is not found but is present in metabase, @@ -160,7 +160,7 @@ func (e *StorageEngine) getRange(prm RngPrm) (*RngRes, error) { return err == nil }) if obj == nil { - return nil, outError + return RngRes{}, outError } e.reportShardError(shardWithMeta, "meta info was present, but object is missing", metaError, @@ -168,7 +168,7 @@ func (e *StorageEngine) getRange(prm RngPrm) (*RngRes, error) { ) } - return &RngRes{ + return RngRes{ obj: obj, }, nil } diff --git a/pkg/local_object_storage/engine/select.go b/pkg/local_object_storage/engine/select.go index a2879897c..5ea698ba9 100644 --- a/pkg/local_object_storage/engine/select.go +++ b/pkg/local_object_storage/engine/select.go @@ -42,7 +42,7 @@ func (r SelectRes) AddressList() []oid.Address { // Returns any error encountered that did not allow to completely select the objects. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) Select(prm SelectPrm) (res *SelectRes, err error) { +func (e *StorageEngine) Select(prm SelectPrm) (res SelectRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e._select(prm) return err @@ -51,7 +51,7 @@ func (e *StorageEngine) Select(prm SelectPrm) (res *SelectRes, err error) { return } -func (e *StorageEngine) _select(prm SelectPrm) (*SelectRes, error) { +func (e *StorageEngine) _select(prm SelectPrm) (SelectRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddSearchDuration)() } @@ -82,7 +82,7 @@ func (e *StorageEngine) _select(prm SelectPrm) (*SelectRes, error) { return false }) - return &SelectRes{ + return SelectRes{ addrList: addrList, }, outError } @@ -91,7 +91,7 @@ func (e *StorageEngine) _select(prm SelectPrm) (*SelectRes, error) { // If limit is zero, then returns all available object addresses. // // Returns an error if executions are blocked (see BlockExecution). -func (e *StorageEngine) List(limit uint64) (res *SelectRes, err error) { +func (e *StorageEngine) List(limit uint64) (res SelectRes, err error) { err = e.execIfNotBlocked(func() error { res, err = e.list(limit) return err @@ -100,7 +100,7 @@ func (e *StorageEngine) List(limit uint64) (res *SelectRes, err error) { return } -func (e *StorageEngine) list(limit uint64) (*SelectRes, error) { +func (e *StorageEngine) list(limit uint64) (SelectRes, error) { if e.metrics != nil { defer elapsed(e.metrics.AddListObjectsDuration)() } @@ -131,7 +131,7 @@ func (e *StorageEngine) list(limit uint64) (*SelectRes, error) { return false }) - return &SelectRes{ + return SelectRes{ addrList: addrList, }, nil }