From 87487cde00a3f7c6f1bc22faf2b0c3fcd39ebec1 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 16 Sep 2020 15:11:54 +0300 Subject: [PATCH] [#30] localstorage: Refactor source code Move the serialization of the address and object into separate functions and use them in methods. Use recently implemented RawObject constructor and method in metaFromObject function. Signed-off-by: Leonard Lyubich --- pkg/local_object_storage/localstore/meta.go | 10 ++++------ pkg/local_object_storage/localstore/methods.go | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/local_object_storage/localstore/meta.go b/pkg/local_object_storage/localstore/meta.go index a8eccdd38..b11675d52 100644 --- a/pkg/local_object_storage/localstore/meta.go +++ b/pkg/local_object_storage/localstore/meta.go @@ -46,14 +46,12 @@ func metaFromObject(o *object.Object) *ObjectMeta { meta := new(ObjectMeta) meta.savedAtEpoch = 10 - raw := objectSDK.NewRaw() + raw := object.NewRaw() raw.SetContainerID(o.GetContainerID()) raw.SetOwnerID(o.GetOwnerID()) // TODO: set other meta fields - meta.head = &object.Object{ - Object: raw.Object(), - } + meta.head = raw.Object() return meta } @@ -63,12 +61,12 @@ func metaToBytes(m *ObjectMeta) ([]byte, error) { binary.BigEndian.PutUint64(data, m.savedAtEpoch) - addrBytes, err := m.head.MarshalStableV2() + objBytes, err := objectBytes(m.head) if err != nil { return nil, err } - return append(data, addrBytes...), nil + return append(data, objBytes...), nil } func metaFromBytes(data []byte) (*ObjectMeta, error) { diff --git a/pkg/local_object_storage/localstore/methods.go b/pkg/local_object_storage/localstore/methods.go index 94625f727..2634096a3 100644 --- a/pkg/local_object_storage/localstore/methods.go +++ b/pkg/local_object_storage/localstore/methods.go @@ -9,13 +9,21 @@ import ( "go.uber.org/zap" ) +func addressBytes(a *objectSDK.Address) ([]byte, error) { + return a.ToV2().StableMarshal(nil) +} + +func objectBytes(o *object.Object) ([]byte, error) { + return o.ToV2().StableMarshal(nil) +} + func (s *Storage) Put(obj *object.Object) error { - addrBytes, err := obj.Address().ToV2().StableMarshal(nil) + addrBytes, err := addressBytes(obj.Address()) if err != nil { return errors.Wrap(err, "could not marshal object address") } - objBytes, err := obj.MarshalStableV2() + objBytes, err := objectBytes(obj) if err != nil { return errors.Wrap(err, "could not marshal the object") } @@ -37,7 +45,7 @@ func (s *Storage) Put(obj *object.Object) error { } func (s *Storage) Delete(addr *objectSDK.Address) error { - addrBytes, err := addr.ToV2().StableMarshal(nil) + addrBytes, err := addressBytes(addr) if err != nil { return errors.Wrap(err, "could not marshal object address") } @@ -56,7 +64,7 @@ func (s *Storage) Delete(addr *objectSDK.Address) error { } func (s *Storage) Get(addr *objectSDK.Address) (*object.Object, error) { - addrBytes, err := addr.ToV2().StableMarshal(nil) + addrBytes, err := addressBytes(addr) if err != nil { return nil, errors.Wrap(err, "could not marshal object address") } @@ -70,7 +78,7 @@ func (s *Storage) Get(addr *objectSDK.Address) (*object.Object, error) { } func (s *Storage) Head(addr *objectSDK.Address) (*ObjectMeta, error) { - addrBytes, err := addr.ToV2().StableMarshal(nil) + addrBytes, err := addressBytes(addr) if err != nil { return nil, errors.Wrap(err, "could not marshal object address") }