2021-11-08 10:52:44 +00:00
|
|
|
package storagegroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/storagegroup"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/checksum"
|
2022-01-25 16:20:32 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-08 10:52:44 +00:00
|
|
|
)
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
// StorageGroup represents storage group of the NeoFS objects.
|
|
|
|
//
|
|
|
|
// StorageGroup is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/storagegroup.StorageGroup
|
|
|
|
// message. See ReadFromMessageV2 / WriteToMessageV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
|
|
|
//
|
|
|
|
// Note that direct typecast is not safe and may result in loss of compatibility:
|
|
|
|
// _ = StorageGroup(storagegroup.StorageGroup) // not recommended
|
2021-11-08 10:52:44 +00:00
|
|
|
type StorageGroup storagegroup.StorageGroup
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
// ReadFromV2 reads StorageGroup from the storagegroup.StorageGroup message.
|
2021-11-08 10:52:44 +00:00
|
|
|
//
|
2021-03-22 15:04:16 +00:00
|
|
|
// See also WriteToV2.
|
|
|
|
func (sg *StorageGroup) ReadFromV2(m storagegroup.StorageGroup) {
|
|
|
|
*sg = StorageGroup(m)
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 15:04:16 +00:00
|
|
|
// WriteToV2 writes StorageGroup to the storagegroup.StorageGroup message.
|
|
|
|
// The message must not be nil.
|
2021-11-08 10:52:44 +00:00
|
|
|
//
|
2021-03-22 15:04:16 +00:00
|
|
|
// See also ReadFromV2.
|
|
|
|
func (sg StorageGroup) WriteToV2(m *storagegroup.StorageGroup) {
|
|
|
|
*m = (storagegroup.StorageGroup)(sg)
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidationDataSize returns total size of the payloads
|
|
|
|
// of objects in the storage group.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// Zero StorageGroup has 0 data size.
|
|
|
|
//
|
|
|
|
// See also SetValidationDataSize.
|
|
|
|
func (sg StorageGroup) ValidationDataSize() uint64 {
|
|
|
|
v2 := (storagegroup.StorageGroup)(sg)
|
|
|
|
return v2.GetValidationDataSize()
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetValidationDataSize sets total size of the payloads
|
|
|
|
// of objects in the storage group.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// See also ValidationDataSize.
|
2021-11-08 10:52:44 +00:00
|
|
|
func (sg *StorageGroup) SetValidationDataSize(epoch uint64) {
|
|
|
|
(*storagegroup.StorageGroup)(sg).SetValidationDataSize(epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidationDataHash returns homomorphic hash from the
|
2022-04-12 13:08:09 +00:00
|
|
|
// concatenation of the payloads of the storage group members
|
|
|
|
// and bool that indicates checksum presence in the storage
|
|
|
|
// group.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
2022-04-12 13:08:09 +00:00
|
|
|
// Zero StorageGroup does not have validation data checksum.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
|
|
|
// See also SetValidationDataHash.
|
2022-04-12 13:08:09 +00:00
|
|
|
func (sg StorageGroup) ValidationDataHash() (v checksum.Checksum, isSet bool) {
|
2021-03-04 11:38:23 +00:00
|
|
|
v2 := (storagegroup.StorageGroup)(sg)
|
|
|
|
if checksumV2 := v2.GetValidationHash(); checksumV2 != nil {
|
|
|
|
v.ReadFromV2(*checksumV2)
|
2022-04-12 13:08:09 +00:00
|
|
|
isSet = true
|
2021-03-22 10:56:18 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 11:38:23 +00:00
|
|
|
return
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetValidationDataHash sets homomorphic hash from the
|
|
|
|
// concatenation of the payloads of the storage group members.
|
2021-03-04 11:38:23 +00:00
|
|
|
//
|
|
|
|
// See also ValidationDataHash.
|
|
|
|
func (sg *StorageGroup) SetValidationDataHash(hash checksum.Checksum) {
|
2021-03-22 10:56:18 +00:00
|
|
|
var v2 refs.Checksum
|
|
|
|
hash.WriteToV2(&v2)
|
|
|
|
|
|
|
|
(*storagegroup.StorageGroup)(sg).SetValidationHash(&v2)
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExpirationEpoch returns last NeoFS epoch number
|
|
|
|
// of the storage group lifetime.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// Zero StorageGroup has 0 expiration epoch.
|
|
|
|
//
|
|
|
|
// See also SetExpirationEpoch.
|
|
|
|
func (sg StorageGroup) ExpirationEpoch() uint64 {
|
|
|
|
v2 := (storagegroup.StorageGroup)(sg)
|
|
|
|
return v2.GetExpirationEpoch()
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetExpirationEpoch sets last NeoFS epoch number
|
|
|
|
// of the storage group lifetime.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// See also ExpirationEpoch.
|
2021-11-08 10:52:44 +00:00
|
|
|
func (sg *StorageGroup) SetExpirationEpoch(epoch uint64) {
|
|
|
|
(*storagegroup.StorageGroup)(sg).SetExpirationEpoch(epoch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Members returns strictly ordered list of
|
|
|
|
// storage group member objects.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// Zero StorageGroup has nil members value.
|
|
|
|
//
|
|
|
|
// See also SetMembers.
|
|
|
|
func (sg StorageGroup) Members() []oid.ID {
|
|
|
|
v2 := (storagegroup.StorageGroup)(sg)
|
|
|
|
mV2 := v2.GetMembers()
|
2021-11-08 10:52:44 +00:00
|
|
|
|
|
|
|
if mV2 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 08:54:54 +00:00
|
|
|
m := make([]oid.ID, len(mV2))
|
2021-11-08 10:52:44 +00:00
|
|
|
|
|
|
|
for i := range mV2 {
|
2022-04-11 16:25:14 +00:00
|
|
|
_ = m[i].ReadFromV2(mV2[i])
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMembers sets strictly ordered list of
|
|
|
|
// storage group member objects.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// See also Members.
|
2022-03-11 08:54:54 +00:00
|
|
|
func (sg *StorageGroup) SetMembers(members []oid.ID) {
|
2021-11-08 10:52:44 +00:00
|
|
|
mV2 := (*storagegroup.StorageGroup)(sg).GetMembers()
|
|
|
|
|
|
|
|
if members == nil {
|
|
|
|
mV2 = nil
|
|
|
|
} else {
|
|
|
|
ln := len(members)
|
|
|
|
|
|
|
|
if cap(mV2) >= ln {
|
|
|
|
mV2 = mV2[:0]
|
|
|
|
} else {
|
2022-03-11 08:54:54 +00:00
|
|
|
mV2 = make([]refs.ObjectID, ln)
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
var oidV2 refs.ObjectID
|
|
|
|
|
2021-11-08 10:52:44 +00:00
|
|
|
for i := 0; i < ln; i++ {
|
2022-04-11 16:25:14 +00:00
|
|
|
members[i].WriteToV2(&oidV2)
|
|
|
|
mV2[i] = oidV2
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*storagegroup.StorageGroup)(sg).SetMembers(mV2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals StorageGroup into a protobuf binary form.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// See also Unmarshal.
|
|
|
|
func (sg StorageGroup) Marshal() ([]byte, error) {
|
|
|
|
v2 := (storagegroup.StorageGroup)(sg)
|
|
|
|
return v2.StableMarshal(nil)
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of StorageGroup.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// See also Marshal.
|
2021-11-08 10:52:44 +00:00
|
|
|
func (sg *StorageGroup) Unmarshal(data []byte) error {
|
2021-03-22 15:04:16 +00:00
|
|
|
v2 := (*storagegroup.StorageGroup)(sg)
|
|
|
|
err := v2.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatCheck(v2)
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes StorageGroup to protobuf JSON format.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// See also UnmarshalJSON.
|
|
|
|
func (sg StorageGroup) MarshalJSON() ([]byte, error) {
|
|
|
|
v2 := (storagegroup.StorageGroup)(sg)
|
|
|
|
return v2.MarshalJSON()
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes StorageGroup from protobuf JSON format.
|
2021-03-22 15:04:16 +00:00
|
|
|
//
|
|
|
|
// See also MarshalJSON.
|
2021-11-08 10:52:44 +00:00
|
|
|
func (sg *StorageGroup) UnmarshalJSON(data []byte) error {
|
2021-03-22 15:04:16 +00:00
|
|
|
v2 := (*storagegroup.StorageGroup)(sg)
|
|
|
|
err := v2.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatCheck(v2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatCheck(v2 *storagegroup.StorageGroup) error {
|
|
|
|
var oID oid.ID
|
|
|
|
|
|
|
|
for _, m := range v2.GetMembers() {
|
|
|
|
err := oID.ReadFromV2(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-11-08 10:52:44 +00:00
|
|
|
}
|