2020-08-17 12:05:32 +00:00
|
|
|
package storagegroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/proto"
|
2020-12-21 07:51:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-11-13 14:20:24 +00:00
|
|
|
storagegroup "github.com/nspcc-dev/neofs-api-go/v2/storagegroup/grpc"
|
|
|
|
goproto "google.golang.org/protobuf/proto"
|
2020-08-17 12:05:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-20 08:04:16 +00:00
|
|
|
n, err = proto.NestedStructureMarshal(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
|
|
|
|
|
2021-01-11 16:32:37 +00:00
|
|
|
_, err = refs.ObjectIDNestedListMarshal(objectIDsField, buf[offset:], s.members)
|
2020-12-21 07:51:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-17 12:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-08-20 08:04:16 +00:00
|
|
|
size += proto.NestedStructureSize(hashField, s.hash)
|
2020-08-18 07:42:47 +00:00
|
|
|
size += proto.UInt64Size(expirationField, s.exp)
|
2020-12-21 07:51:45 +00:00
|
|
|
size += refs.ObjectIDNestedListSize(objectIDsField, s.members)
|
2020-08-17 12:05:32 +00:00
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
2020-11-13 14:20:24 +00:00
|
|
|
|
|
|
|
func (s *StorageGroup) Unmarshal(data []byte) error {
|
|
|
|
m := new(storagegroup.StorageGroup)
|
|
|
|
if err := goproto.Unmarshal(data, m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*s = *StorageGroupFromGRPCMessage(m)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|