2021-11-08 10:04:45 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
2022-04-12 16:23:16 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-09-16 11:59:30 +00:00
|
|
|
"slices"
|
|
|
|
"strings"
|
2022-04-12 16:23:16 +00:00
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
2021-11-08 10:04:45 +00:00
|
|
|
)
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Object represents in-memory structure of the FrostFS object.
|
|
|
|
// Type is compatible with FrostFS API V2 protocol.
|
2022-03-05 14:21:16 +00:00
|
|
|
//
|
|
|
|
// Instance can be created depending on scenario:
|
2022-08-24 14:17:40 +00:00
|
|
|
// - InitCreation (an object to be placed in container);
|
|
|
|
// - New (blank instance, usually needed for decoding);
|
2022-12-29 10:46:18 +00:00
|
|
|
// - NewFromV2 (when working under FrostFS API V2 protocol).
|
2022-03-03 11:48:55 +00:00
|
|
|
type Object object.Object
|
2021-11-08 10:04:45 +00:00
|
|
|
|
2022-03-05 14:21:16 +00:00
|
|
|
// RequiredFields contains the minimum set of object data that must be set
|
2022-12-29 10:46:18 +00:00
|
|
|
// by the FrostFS user at the stage of creation.
|
2022-03-05 14:21:16 +00:00
|
|
|
type RequiredFields struct {
|
2022-12-29 10:46:18 +00:00
|
|
|
// Identifier of the FrostFS container associated with the object.
|
2022-03-05 14:21:16 +00:00
|
|
|
Container cid.ID
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// Object owner's user ID in the FrostFS system.
|
2022-04-11 06:30:22 +00:00
|
|
|
Owner user.ID
|
2022-03-05 14:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitCreation initializes the object instance with minimum set of required fields.
|
|
|
|
// Object is expected (but not required) to be blank. Object must not be nil.
|
|
|
|
func InitCreation(dst *Object, rf RequiredFields) {
|
2022-04-11 16:25:14 +00:00
|
|
|
dst.SetContainerID(rf.Container)
|
2023-11-21 08:35:10 +00:00
|
|
|
dst.SetOwnerID(rf.Owner)
|
2022-03-05 14:21:16 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 10:04:45 +00:00
|
|
|
// NewFromV2 wraps v2 Object message to Object.
|
|
|
|
func NewFromV2(oV2 *object.Object) *Object {
|
2022-03-03 11:48:55 +00:00
|
|
|
return (*Object)(oV2)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates and initializes blank Object.
|
|
|
|
//
|
|
|
|
// Works similar as NewFromV2(new(Object)).
|
|
|
|
func New() *Object {
|
|
|
|
return NewFromV2(new(object.Object))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts Object to v2 Object message.
|
|
|
|
func (o *Object) ToV2() *object.Object {
|
2022-03-03 11:48:55 +00:00
|
|
|
return (*object.Object)(o)
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalHeaderJSON marshals object's header
|
|
|
|
// into JSON format.
|
|
|
|
func (o *Object) MarshalHeaderJSON() ([]byte, error) {
|
2022-03-03 11:48:55 +00:00
|
|
|
return (*object.Object)(o).GetHeader().MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Object) setHeaderField(setter func(*object.Header)) {
|
|
|
|
obj := (*object.Object)(o)
|
|
|
|
h := obj.GetHeader()
|
|
|
|
|
|
|
|
if h == nil {
|
|
|
|
h = new(object.Header)
|
|
|
|
obj.SetHeader(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
setter(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Object) setSplitFields(setter func(*object.SplitHeader)) {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
split := h.GetSplit()
|
|
|
|
if split == nil {
|
|
|
|
split = new(object.SplitHeader)
|
|
|
|
h.SetSplit(split)
|
|
|
|
}
|
|
|
|
|
|
|
|
setter(split)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns object identifier.
|
2022-04-12 16:23:16 +00:00
|
|
|
func (o *Object) ID() (v oid.ID, isSet bool) {
|
2022-04-11 16:25:14 +00:00
|
|
|
v2 := (*object.Object)(o)
|
|
|
|
if id := v2.GetObjectID(); id != nil {
|
|
|
|
_ = v.ReadFromV2(*v2.GetObjectID())
|
2022-04-12 16:23:16 +00:00
|
|
|
isSet = true
|
2022-04-11 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
return
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets object identifier.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (o *Object) SetID(v oid.ID) {
|
|
|
|
var v2 refs.ObjectID
|
|
|
|
v.WriteToV2(&v2)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
(*object.Object)(o).
|
2022-04-11 16:25:14 +00:00
|
|
|
SetObjectID(&v2)
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Signature returns signature of the object identifier.
|
2022-12-13 14:36:35 +00:00
|
|
|
func (o *Object) Signature() *frostfscrypto.Signature {
|
2022-04-05 11:13:34 +00:00
|
|
|
sigv2 := (*object.Object)(o).GetSignature()
|
|
|
|
if sigv2 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:36:35 +00:00
|
|
|
var sig frostfscrypto.Signature
|
2023-03-03 09:44:36 +00:00
|
|
|
_ = sig.ReadFromV2(*sigv2) // FIXME(@cthulhu-rider): #226 handle error
|
2022-04-05 11:13:34 +00:00
|
|
|
|
|
|
|
return &sig
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSignature sets signature of the object identifier.
|
2022-12-13 14:36:35 +00:00
|
|
|
func (o *Object) SetSignature(v *frostfscrypto.Signature) {
|
2022-04-05 11:13:34 +00:00
|
|
|
var sigv2 *refs.Signature
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
sigv2 = new(refs.Signature)
|
|
|
|
|
|
|
|
v.WriteToV2(sigv2)
|
|
|
|
}
|
|
|
|
|
|
|
|
(*object.Object)(o).SetSignature(sigv2)
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Payload returns payload bytes.
|
|
|
|
func (o *Object) Payload() []byte {
|
|
|
|
return (*object.Object)(o).GetPayload()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPayload sets payload bytes.
|
|
|
|
func (o *Object) SetPayload(v []byte) {
|
|
|
|
(*object.Object)(o).SetPayload(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns version of the object.
|
|
|
|
func (o *Object) Version() *version.Version {
|
2022-03-23 15:35:44 +00:00
|
|
|
var ver version.Version
|
|
|
|
if verV2 := (*object.Object)(o).GetHeader().GetVersion(); verV2 != nil {
|
2023-03-03 09:44:36 +00:00
|
|
|
_ = ver.ReadFromV2(*verV2) // FIXME(@cthulhu-rider): #226 handle error
|
2022-03-23 15:35:44 +00:00
|
|
|
}
|
|
|
|
return &ver
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetVersion sets version of the object.
|
|
|
|
func (o *Object) SetVersion(v *version.Version) {
|
2022-03-23 15:35:44 +00:00
|
|
|
var verV2 refs.Version
|
|
|
|
v.WriteToV2(&verV2)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
2022-03-23 15:35:44 +00:00
|
|
|
h.SetVersion(&verV2)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PayloadSize returns payload length of the object.
|
|
|
|
func (o *Object) PayloadSize() uint64 {
|
|
|
|
return (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetPayloadLength()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPayloadSize sets payload length of the object.
|
|
|
|
func (o *Object) SetPayloadSize(v uint64) {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetPayloadLength(v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerID returns identifier of the related container.
|
2022-04-12 16:23:16 +00:00
|
|
|
func (o *Object) ContainerID() (v cid.ID, isSet bool) {
|
2022-04-11 16:25:14 +00:00
|
|
|
v2 := (*object.Object)(o)
|
|
|
|
|
|
|
|
cidV2 := v2.GetHeader().GetContainerID()
|
|
|
|
if cidV2 != nil {
|
2022-04-12 16:23:16 +00:00
|
|
|
_ = v.ReadFromV2(*cidV2)
|
|
|
|
isSet = true
|
2022-04-11 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
return
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID sets identifier of the related container.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (o *Object) SetContainerID(v cid.ID) {
|
|
|
|
var cidV2 refs.ContainerID
|
|
|
|
v.WriteToV2(&cidV2)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
2022-04-11 16:25:14 +00:00
|
|
|
h.SetContainerID(&cidV2)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:35:10 +00:00
|
|
|
// OwnerID returns identifier of the object owner and True.
|
|
|
|
func (o *Object) OwnerID() user.ID {
|
2022-04-11 06:30:22 +00:00
|
|
|
var id user.ID
|
|
|
|
|
|
|
|
m := (*object.Object)(o).GetHeader().GetOwnerID()
|
|
|
|
if m != nil {
|
|
|
|
_ = id.ReadFromV2(*m)
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:35:10 +00:00
|
|
|
return id
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOwnerID sets identifier of the object owner.
|
2023-11-21 08:35:10 +00:00
|
|
|
func (o *Object) SetOwnerID(v user.ID) {
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
2022-04-11 06:30:22 +00:00
|
|
|
var m refs.OwnerID
|
|
|
|
v.WriteToV2(&m)
|
|
|
|
|
|
|
|
h.SetOwnerID(&m)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreationEpoch returns epoch number in which object was created.
|
|
|
|
func (o *Object) CreationEpoch() uint64 {
|
|
|
|
return (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetCreationEpoch()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCreationEpoch sets epoch number in which object was created.
|
|
|
|
func (o *Object) SetCreationEpoch(v uint64) {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetCreationEpoch(v)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:08:09 +00:00
|
|
|
// PayloadChecksum returns checksum of the object payload and
|
|
|
|
// bool that indicates checksum presence in the object.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
2022-04-12 13:08:09 +00:00
|
|
|
// Zero Object does not have payload checksum.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
|
|
|
// See also SetPayloadChecksum.
|
2022-04-12 13:08:09 +00:00
|
|
|
func (o *Object) PayloadChecksum() (checksum.Checksum, bool) {
|
2021-03-22 10:56:18 +00:00
|
|
|
var v checksum.Checksum
|
2021-03-04 11:38:23 +00:00
|
|
|
v2 := (*object.Object)(o)
|
2021-03-22 10:56:18 +00:00
|
|
|
|
2021-03-04 11:38:23 +00:00
|
|
|
if hash := v2.GetHeader().GetPayloadHash(); hash != nil {
|
2023-03-03 09:44:36 +00:00
|
|
|
_ = v.ReadFromV2(*hash) // FIXME(@cthulhu-rider): #226 handle error
|
2022-04-12 13:08:09 +00:00
|
|
|
return v, true
|
2021-03-04 11:38:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 13:08:09 +00:00
|
|
|
return v, false
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPayloadChecksum sets checksum of the object payload.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
|
|
|
// See also PayloadChecksum.
|
|
|
|
func (o *Object) SetPayloadChecksum(v checksum.Checksum) {
|
2021-03-22 10:56:18 +00:00
|
|
|
var v2 refs.Checksum
|
|
|
|
v.WriteToV2(&v2)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
2021-03-22 10:56:18 +00:00
|
|
|
h.SetPayloadHash(&v2)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:08:09 +00:00
|
|
|
// PayloadHomomorphicHash returns homomorphic hash of the object
|
|
|
|
// payload and bool that indicates checksum presence in the object.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
2022-04-12 13:08:09 +00:00
|
|
|
// Zero Object does not have payload homomorphic checksum.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
|
|
|
// See also SetPayloadHomomorphicHash.
|
2022-04-12 13:08:09 +00:00
|
|
|
func (o *Object) PayloadHomomorphicHash() (checksum.Checksum, bool) {
|
2021-03-22 10:56:18 +00:00
|
|
|
var v checksum.Checksum
|
2021-03-04 11:38:23 +00:00
|
|
|
v2 := (*object.Object)(o)
|
2021-03-22 10:56:18 +00:00
|
|
|
|
2021-03-04 11:38:23 +00:00
|
|
|
if hash := v2.GetHeader().GetHomomorphicHash(); hash != nil {
|
2023-03-03 09:44:36 +00:00
|
|
|
_ = v.ReadFromV2(*hash) // FIXME(@cthulhu-rider): #226 handle error
|
2022-04-12 13:08:09 +00:00
|
|
|
return v, true
|
2021-03-04 11:38:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 13:08:09 +00:00
|
|
|
return v, false
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPayloadHomomorphicHash sets homomorphic hash of the object payload.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
|
|
|
// See also PayloadHomomorphicHash.
|
|
|
|
func (o *Object) SetPayloadHomomorphicHash(v checksum.Checksum) {
|
2021-03-22 10:56:18 +00:00
|
|
|
var v2 refs.Checksum
|
|
|
|
v.WriteToV2(&v2)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
2021-03-22 10:56:18 +00:00
|
|
|
h.SetHomomorphicHash(&v2)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes returns object attributes.
|
2022-03-11 09:00:54 +00:00
|
|
|
func (o *Object) Attributes() []Attribute {
|
2022-03-03 11:48:55 +00:00
|
|
|
attrs := (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetAttributes()
|
|
|
|
|
2022-03-11 09:00:54 +00:00
|
|
|
res := make([]Attribute, len(attrs))
|
2022-03-03 11:48:55 +00:00
|
|
|
|
|
|
|
for i := range attrs {
|
2022-03-11 09:00:54 +00:00
|
|
|
res[i] = *NewAttributeFromV2(&attrs[i])
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-09-16 11:59:30 +00:00
|
|
|
// UserAttributes returns object user attributes.
|
|
|
|
func (o *Object) UserAttributes() []Attribute {
|
|
|
|
attrs := (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetAttributes()
|
|
|
|
|
|
|
|
res := make([]Attribute, 0, len(attrs))
|
|
|
|
|
|
|
|
for _, attr := range attrs {
|
2024-09-18 07:52:44 +00:00
|
|
|
if !strings.HasPrefix(attr.GetKey(), container.SysAttributePrefix) {
|
2024-09-16 11:59:30 +00:00
|
|
|
res = append(res, *NewAttributeFromV2(&attr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return slices.Clip(res)
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
// SetAttributes sets object attributes.
|
2022-03-11 09:00:54 +00:00
|
|
|
func (o *Object) SetAttributes(v ...Attribute) {
|
|
|
|
attrs := make([]object.Attribute, len(v))
|
2022-03-03 11:48:55 +00:00
|
|
|
|
|
|
|
for i := range v {
|
2022-03-11 09:00:54 +00:00
|
|
|
attrs[i] = *v[i].ToV2()
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetAttributes(attrs)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// PreviousID returns identifier of the previous sibling object.
|
2022-04-12 16:23:16 +00:00
|
|
|
func (o *Object) PreviousID() (v oid.ID, isSet bool) {
|
2022-04-11 16:25:14 +00:00
|
|
|
v2 := (*object.Object)(o)
|
|
|
|
|
|
|
|
v2Prev := v2.GetHeader().GetSplit().GetPrevious()
|
|
|
|
if v2Prev != nil {
|
|
|
|
_ = v.ReadFromV2(*v2Prev)
|
2022-04-12 16:23:16 +00:00
|
|
|
isSet = true
|
2022-04-11 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
return
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPreviousID sets identifier of the previous sibling object.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (o *Object) SetPreviousID(v oid.ID) {
|
|
|
|
var v2 refs.ObjectID
|
|
|
|
v.WriteToV2(&v2)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
2022-04-11 16:25:14 +00:00
|
|
|
split.SetPrevious(&v2)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Children return list of the identifiers of the child objects.
|
2022-03-11 09:00:54 +00:00
|
|
|
func (o *Object) Children() []oid.ID {
|
2022-04-11 16:25:14 +00:00
|
|
|
v2 := (*object.Object)(o)
|
|
|
|
ids := v2.GetHeader().GetSplit().GetChildren()
|
2022-03-03 11:48:55 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var (
|
|
|
|
id oid.ID
|
|
|
|
res = make([]oid.ID, len(ids))
|
|
|
|
)
|
2022-03-03 11:48:55 +00:00
|
|
|
|
|
|
|
for i := range ids {
|
2022-04-11 16:25:14 +00:00
|
|
|
_ = id.ReadFromV2(ids[i])
|
|
|
|
res[i] = id
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-02-28 11:51:48 +00:00
|
|
|
func (o *Object) GetECHeader() *ECHeader {
|
|
|
|
v2 := (*object.Object)(o).GetHeader().GetEC()
|
|
|
|
|
|
|
|
var ec ECHeader
|
|
|
|
_ = ec.ReadFromV2(v2) // Errors is checked on unmarshal.
|
|
|
|
return &ec
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
// SetChildren sets list of the identifiers of the child objects.
|
2022-03-11 09:00:54 +00:00
|
|
|
func (o *Object) SetChildren(v ...oid.ID) {
|
2022-04-11 16:25:14 +00:00
|
|
|
var (
|
|
|
|
v2 refs.ObjectID
|
|
|
|
ids = make([]refs.ObjectID, len(v))
|
|
|
|
)
|
2022-03-03 11:48:55 +00:00
|
|
|
|
|
|
|
for i := range v {
|
2022-04-11 16:25:14 +00:00
|
|
|
v[i].WriteToV2(&v2)
|
|
|
|
ids[i] = v2
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
|
|
|
split.SetChildren(ids)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotificationInfo groups information about object notification
|
|
|
|
// that can be written to object.
|
|
|
|
//
|
|
|
|
// Topic is an optional field.
|
|
|
|
type NotificationInfo struct {
|
|
|
|
ni object.NotificationInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Epoch returns object notification tick
|
|
|
|
// epoch.
|
|
|
|
func (n NotificationInfo) Epoch() uint64 {
|
|
|
|
return n.ni.Epoch()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpoch sets object notification tick
|
|
|
|
// epoch.
|
|
|
|
func (n *NotificationInfo) SetEpoch(epoch uint64) {
|
|
|
|
n.ni.SetEpoch(epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Topic return optional object notification
|
|
|
|
// topic.
|
|
|
|
func (n NotificationInfo) Topic() string {
|
|
|
|
return n.ni.Topic()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTopic sets optional object notification
|
|
|
|
// topic.
|
|
|
|
func (n *NotificationInfo) SetTopic(topic string) {
|
|
|
|
n.ni.SetTopic(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotificationInfo returns notification info
|
|
|
|
// read from the object structure.
|
|
|
|
// Returns any error that appeared during notification
|
|
|
|
// information parsing.
|
|
|
|
func (o *Object) NotificationInfo() (*NotificationInfo, error) {
|
|
|
|
ni, err := object.GetNotificationInfo((*object.Object)(o))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &NotificationInfo{
|
|
|
|
ni: *ni,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNotification writes NotificationInfo to the object structure.
|
|
|
|
func (o *Object) SetNotification(ni NotificationInfo) {
|
|
|
|
object.WriteNotificationInfo((*object.Object)(o), ni.ni)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitID return split identity of split object. If object is not split
|
|
|
|
// returns nil.
|
|
|
|
func (o *Object) SplitID() *SplitID {
|
|
|
|
return NewSplitIDFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit().
|
|
|
|
GetSplitID(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSplitID sets split identifier for the split object.
|
|
|
|
func (o *Object) SetSplitID(id *SplitID) {
|
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
|
|
|
split.SetSplitID(id.ToV2())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParentID returns identifier of the parent object.
|
2022-04-12 16:23:16 +00:00
|
|
|
func (o *Object) ParentID() (v oid.ID, isSet bool) {
|
2022-04-11 16:25:14 +00:00
|
|
|
v2 := (*object.Object)(o)
|
|
|
|
|
|
|
|
v2Par := v2.GetHeader().GetSplit().GetParent()
|
|
|
|
if v2Par != nil {
|
|
|
|
_ = v.ReadFromV2(*v2Par)
|
2022-04-12 16:23:16 +00:00
|
|
|
isSet = true
|
2022-04-11 16:25:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:23:16 +00:00
|
|
|
return
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetParentID sets identifier of the parent object.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (o *Object) SetParentID(v oid.ID) {
|
|
|
|
var v2 refs.ObjectID
|
|
|
|
v.WriteToV2(&v2)
|
|
|
|
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
2022-04-11 16:25:14 +00:00
|
|
|
split.SetParent(&v2)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent returns parent object w/o payload.
|
|
|
|
func (o *Object) Parent() *Object {
|
|
|
|
h := (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit()
|
|
|
|
|
|
|
|
parSig := h.GetParentSignature()
|
|
|
|
parHdr := h.GetParentHeader()
|
|
|
|
|
|
|
|
if parSig == nil && parHdr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
oV2 := new(object.Object)
|
|
|
|
oV2.SetObjectID(h.GetParent())
|
|
|
|
oV2.SetSignature(parSig)
|
|
|
|
oV2.SetHeader(parHdr)
|
|
|
|
|
|
|
|
return NewFromV2(oV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetParent sets parent object w/o payload.
|
|
|
|
func (o *Object) SetParent(v *Object) {
|
|
|
|
o.setSplitFields(func(split *object.SplitHeader) {
|
|
|
|
split.SetParent((*object.Object)(v).GetObjectID())
|
|
|
|
split.SetParentSignature((*object.Object)(v).GetSignature())
|
|
|
|
split.SetParentHeader((*object.Object)(v).GetHeader())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Object) initRelations() {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetSplit(new(object.SplitHeader))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Object) resetRelations() {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetSplit(nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionToken returns token of the session
|
|
|
|
// within which object was created.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (o *Object) SessionToken() *session.Object {
|
|
|
|
tokv2 := (*object.Object)(o).GetHeader().GetSessionToken()
|
|
|
|
if tokv2 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var res session.Object
|
|
|
|
|
2022-05-25 15:51:21 +00:00
|
|
|
_ = res.ReadFromV2(*tokv2)
|
2022-04-07 16:09:15 +00:00
|
|
|
|
|
|
|
return &res
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSessionToken sets token of the session
|
|
|
|
// within which object was created.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (o *Object) SetSessionToken(v *session.Object) {
|
2022-03-03 11:48:55 +00:00
|
|
|
o.setHeaderField(func(h *object.Header) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var tokv2 *v2session.Token
|
|
|
|
|
|
|
|
if v != nil {
|
|
|
|
tokv2 = new(v2session.Token)
|
|
|
|
v.WriteToV2(tokv2)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.SetSessionToken(tokv2)
|
2022-03-03 11:48:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns type of the object.
|
|
|
|
func (o *Object) Type() Type {
|
|
|
|
return TypeFromV2(
|
|
|
|
(*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetObjectType(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetType sets type of the object.
|
|
|
|
func (o *Object) SetType(v Type) {
|
|
|
|
o.setHeaderField(func(h *object.Header) {
|
|
|
|
h.SetObjectType(v.ToV2())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CutPayload returns Object w/ empty payload.
|
|
|
|
//
|
|
|
|
// Changes of non-payload fields affect source object.
|
|
|
|
func (o *Object) CutPayload() *Object {
|
|
|
|
ov2 := new(object.Object)
|
|
|
|
*ov2 = *(*object.Object)(o)
|
|
|
|
ov2.SetPayload(nil)
|
2024-02-15 12:12:22 +00:00
|
|
|
ov2.SetMarshalData(nil)
|
2022-03-03 11:48:55 +00:00
|
|
|
|
|
|
|
return (*Object)(ov2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Object) HasParent() bool {
|
|
|
|
return (*object.Object)(o).
|
|
|
|
GetHeader().
|
|
|
|
GetSplit() != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetRelations removes all fields of links with other objects.
|
|
|
|
func (o *Object) ResetRelations() {
|
|
|
|
o.resetRelations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitRelations initializes relation field.
|
|
|
|
func (o *Object) InitRelations() {
|
|
|
|
o.initRelations()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals object into a protobuf binary form.
|
|
|
|
func (o *Object) Marshal() ([]byte, error) {
|
2022-05-30 19:05:35 +00:00
|
|
|
return (*object.Object)(o).StableMarshal(nil), nil
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of object.
|
|
|
|
func (o *Object) Unmarshal(data []byte) error {
|
2022-04-12 16:23:16 +00:00
|
|
|
err := (*object.Object)(o).Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatCheck((*object.Object)(o))
|
2022-03-03 11:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes object to protobuf JSON format.
|
|
|
|
func (o *Object) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*object.Object)(o).MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes object from protobuf JSON format.
|
|
|
|
func (o *Object) UnmarshalJSON(data []byte) error {
|
2022-04-12 16:23:16 +00:00
|
|
|
err := (*object.Object)(o).UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatCheck((*object.Object)(o))
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:35:10 +00:00
|
|
|
var (
|
|
|
|
errOIDNotSet = errors.New("object ID is not set")
|
|
|
|
errCIDNotSet = errors.New("container ID is not set")
|
|
|
|
)
|
2022-04-12 16:23:16 +00:00
|
|
|
|
|
|
|
func formatCheck(v2 *object.Object) error {
|
|
|
|
var (
|
|
|
|
oID oid.ID
|
|
|
|
cID cid.ID
|
|
|
|
)
|
|
|
|
|
|
|
|
oidV2 := v2.GetObjectID()
|
|
|
|
if oidV2 == nil {
|
|
|
|
return errOIDNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
err := oID.ReadFromV2(*oidV2)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert V2 object ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cidV2 := v2.GetHeader().GetContainerID()
|
|
|
|
if cidV2 == nil {
|
|
|
|
return errCIDNotSet
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cID.ReadFromV2(*cidV2)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert V2 container ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if prev := v2.GetHeader().GetSplit().GetPrevious(); prev != nil {
|
|
|
|
err = oID.ReadFromV2(*prev)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert previous object ID: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent := v2.GetHeader().GetSplit().GetParent(); parent != nil {
|
|
|
|
err = oID.ReadFromV2(*parent)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert parent object ID: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-11-08 10:04:45 +00:00
|
|
|
}
|