From 431e96f3c5b72438e398c2d07323d69591f4105b Mon Sep 17 00:00:00 2001 From: Aleksey Savchuk Date: Wed, 25 Dec 2024 21:49:17 +0300 Subject: [PATCH] [#xx] metabase: Refactor `selectNFromBucket` The function is riskly long. so adding just a few lines of code might trigger the `funlen` linter. Kept the changes minimal: - Moved the logic for filling object info into a separate function - Preserved all existing side effects Signed-off-by: Aleksey Savchuk --- pkg/local_object_storage/metabase/list.go | 51 ++++++++++++++--------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/pkg/local_object_storage/metabase/list.go b/pkg/local_object_storage/metabase/list.go index a7ff2222f..0b99bce53 100644 --- a/pkg/local_object_storage/metabase/list.go +++ b/pkg/local_object_storage/metabase/list.go @@ -243,34 +243,47 @@ func selectNFromBucket(bkt *bbolt.Bucket, // main bucket continue } - var isLinkingObj bool - var ecInfo *objectcore.ECInfo - if objType == objectSDK.TypeRegular { - var o objectSDK.Object - if err := o.Unmarshal(bytes.Clone(v)); err != nil { - return nil, nil, nil, err - } - isLinkingObj = isLinkObject(&o) - ecHeader := o.ECHeader() - if ecHeader != nil { - ecInfo = &objectcore.ECInfo{ - ParentID: ecHeader.Parent(), - Index: ecHeader.Index(), - Total: ecHeader.Total(), - } - } - } - var a oid.Address a.SetContainer(cnt) a.SetObject(obj) - to = append(to, objectcore.Info{Address: a, Type: objType, IsLinkingObject: isLinkingObj, ECInfo: ecInfo}) + + info, err := prepareObjectInfo(objType, a, v) + if err != nil { + return nil, nil, nil, err + } + + to = append(to, info) count++ } return to, offset, cursor, nil } +func prepareObjectInfo(objType objectSDK.Type, addr oid.Address, obj []byte) (objectcore.Info, error) { + if objType != objectSDK.TypeRegular { + return objectcore.Info{Address: addr, Type: objType, IsLinkingObject: false, ECInfo: nil}, nil + } + + var o objectSDK.Object + if err := o.Unmarshal(bytes.Clone(obj)); err != nil { + return objectcore.Info{}, err + } + + info := objectcore.Info{ + Address: addr, + Type: objType, + IsLinkingObject: isLinkObject(&o), + } + if ecHeader := o.ECHeader(); ecHeader != nil { + info.ECInfo = &objectcore.ECInfo{ + ParentID: ecHeader.Parent(), + Index: ecHeader.Index(), + Total: ecHeader.Total(), + } + } + return info, nil +} + func parseContainerIDWithPrefix(containerID *cid.ID, name []byte) ([]byte, byte) { if len(name) < bucketKeySize { return nil, 0