[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-16 15:11:54 +03:00 committed by Alex Vanin
parent 4326ff56a7
commit 87487cde00
2 changed files with 17 additions and 11 deletions

View file

@ -46,14 +46,12 @@ func metaFromObject(o *object.Object) *ObjectMeta {
meta := new(ObjectMeta) meta := new(ObjectMeta)
meta.savedAtEpoch = 10 meta.savedAtEpoch = 10
raw := objectSDK.NewRaw() raw := object.NewRaw()
raw.SetContainerID(o.GetContainerID()) raw.SetContainerID(o.GetContainerID())
raw.SetOwnerID(o.GetOwnerID()) raw.SetOwnerID(o.GetOwnerID())
// TODO: set other meta fields // TODO: set other meta fields
meta.head = &object.Object{ meta.head = raw.Object()
Object: raw.Object(),
}
return meta return meta
} }
@ -63,12 +61,12 @@ func metaToBytes(m *ObjectMeta) ([]byte, error) {
binary.BigEndian.PutUint64(data, m.savedAtEpoch) binary.BigEndian.PutUint64(data, m.savedAtEpoch)
addrBytes, err := m.head.MarshalStableV2() objBytes, err := objectBytes(m.head)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return append(data, addrBytes...), nil return append(data, objBytes...), nil
} }
func metaFromBytes(data []byte) (*ObjectMeta, error) { func metaFromBytes(data []byte) (*ObjectMeta, error) {

View file

@ -9,13 +9,21 @@ import (
"go.uber.org/zap" "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 { func (s *Storage) Put(obj *object.Object) error {
addrBytes, err := obj.Address().ToV2().StableMarshal(nil) addrBytes, err := addressBytes(obj.Address())
if err != nil { if err != nil {
return errors.Wrap(err, "could not marshal object address") return errors.Wrap(err, "could not marshal object address")
} }
objBytes, err := obj.MarshalStableV2() objBytes, err := objectBytes(obj)
if err != nil { if err != nil {
return errors.Wrap(err, "could not marshal the object") 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 { func (s *Storage) Delete(addr *objectSDK.Address) error {
addrBytes, err := addr.ToV2().StableMarshal(nil) addrBytes, err := addressBytes(addr)
if err != nil { if err != nil {
return errors.Wrap(err, "could not marshal object address") 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) { func (s *Storage) Get(addr *objectSDK.Address) (*object.Object, error) {
addrBytes, err := addr.ToV2().StableMarshal(nil) addrBytes, err := addressBytes(addr)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not marshal object address") 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) { func (s *Storage) Head(addr *objectSDK.Address) (*ObjectMeta, error) {
addrBytes, err := addr.ToV2().StableMarshal(nil) addrBytes, err := addressBytes(addr)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not marshal object address") return nil, errors.Wrap(err, "could not marshal object address")
} }