2020-07-10 14:17:51 +00:00
|
|
|
package localstore
|
|
|
|
|
|
|
|
import (
|
2020-09-02 12:50:51 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2020-09-16 11:58:58 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-09-02 12:50:51 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-07-10 14:17:51 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
// ObjectMeta represents meta information about
|
|
|
|
// the object that is stored in meta storage.
|
|
|
|
type ObjectMeta struct {
|
|
|
|
head *object.Object
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
savedAtEpoch uint64
|
|
|
|
}
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
// SavedAtEpoch returns the number of epoch
|
|
|
|
// at which the object was saved locally.
|
|
|
|
func (m *ObjectMeta) SavedAtEpoch() uint64 {
|
|
|
|
if m != nil {
|
|
|
|
return m.savedAtEpoch
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
return 0
|
|
|
|
}
|
2020-07-10 14:17:51 +00:00
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
// Head returns the object w/o payload.
|
|
|
|
func (m *ObjectMeta) Head() *object.Object {
|
|
|
|
if m != nil {
|
|
|
|
return m.head
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressFromMeta extracts the Address from object meta.
|
2020-09-16 12:07:23 +00:00
|
|
|
func AddressFromMeta(m *ObjectMeta) *objectSDK.Address {
|
2020-09-02 12:50:51 +00:00
|
|
|
return m.Head().Address()
|
2020-07-10 14:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 12:50:51 +00:00
|
|
|
func metaFromObject(o *object.Object) *ObjectMeta {
|
|
|
|
// FIXME: remove hard-code
|
2020-07-10 14:17:51 +00:00
|
|
|
meta := new(ObjectMeta)
|
2020-09-02 12:50:51 +00:00
|
|
|
meta.savedAtEpoch = 10
|
2020-09-16 11:58:58 +00:00
|
|
|
|
2020-09-16 12:11:54 +00:00
|
|
|
raw := object.NewRaw()
|
2020-09-16 11:58:58 +00:00
|
|
|
raw.SetContainerID(o.GetContainerID())
|
|
|
|
raw.SetOwnerID(o.GetOwnerID())
|
|
|
|
// TODO: set other meta fields
|
|
|
|
|
2020-09-16 12:11:54 +00:00
|
|
|
meta.head = raw.Object()
|
2020-07-10 14:17:51 +00:00
|
|
|
|
|
|
|
return meta
|
|
|
|
}
|
2020-09-02 12:50:51 +00:00
|
|
|
|
|
|
|
func metaToBytes(m *ObjectMeta) ([]byte, error) {
|
|
|
|
data := make([]byte, 8)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(data, m.savedAtEpoch)
|
|
|
|
|
2020-09-16 12:11:54 +00:00
|
|
|
objBytes, err := objectBytes(m.head)
|
2020-09-02 12:50:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-16 12:11:54 +00:00
|
|
|
return append(data, objBytes...), nil
|
2020-09-02 12:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func metaFromBytes(data []byte) (*ObjectMeta, error) {
|
|
|
|
if len(data) < 8 {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := object.FromBytes(data[8:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get object address from bytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := new(ObjectMeta)
|
|
|
|
meta.savedAtEpoch = binary.BigEndian.Uint64(data)
|
|
|
|
meta.head = obj
|
|
|
|
|
|
|
|
return meta, nil
|
|
|
|
}
|