2020-08-17 12:05:32 +00:00
|
|
|
package storagegroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-18 07:42:47 +00:00
|
|
|
sizeField = 1
|
|
|
|
hashField = 2
|
|
|
|
expirationField = 3
|
|
|
|
objectIDsField = 4
|
2020-08-17 12:05:32 +00:00
|
|
|
)
|
|
|
|
|
2020-08-17 12:19:36 +00:00
|
|
|
// StableMarshal marshals unified storage group structure in a protobuf
|
|
|
|
// compatible way without field order shuffle.
|
2020-08-17 12:05:32 +00:00
|
|
|
func (s *StorageGroup) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if s == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, s.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.UInt64Marshal(sizeField, buf[offset:], s.size)
|
2020-08-17 12:05:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-17 12:19:36 +00:00
|
|
|
|
2020-08-17 12:05:32 +00:00
|
|
|
offset += n
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.BytesMarshal(hashField, buf[offset:], s.hash)
|
2020-08-17 12:05:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-17 12:19:36 +00:00
|
|
|
|
2020-08-17 12:05:32 +00:00
|
|
|
offset += n
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.UInt64Marshal(expirationField, buf[offset:], s.exp)
|
2020-08-17 12:05:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-17 12:19:36 +00:00
|
|
|
|
2020-08-17 12:05:32 +00:00
|
|
|
offset += n
|
|
|
|
|
|
|
|
for i := range s.members {
|
2020-08-18 07:42:47 +00:00
|
|
|
n, err = proto.NestedStructureMarshal(objectIDsField, buf[offset:], s.members[i])
|
2020-08-17 12:05:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 12:19:36 +00:00
|
|
|
// StableSize of storage group structure marshalled by StableMarshal function.
|
2020-08-17 12:05:32 +00:00
|
|
|
func (s *StorageGroup) StableSize() (size int) {
|
|
|
|
if s == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-08-18 07:42:47 +00:00
|
|
|
size += proto.UInt64Size(sizeField, s.size)
|
|
|
|
size += proto.BytesSize(hashField, s.hash)
|
|
|
|
size += proto.UInt64Size(expirationField, s.exp)
|
2020-08-17 12:19:36 +00:00
|
|
|
|
2020-08-17 12:05:32 +00:00
|
|
|
for i := range s.members {
|
2020-08-18 07:42:47 +00:00
|
|
|
size += proto.NestedStructureSize(objectIDsField, s.members[i])
|
2020-08-17 12:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|