github.com/TrueCloudLab/frostfs-api-go/v2@v2.0.0-20230228134343-196241c4e79a/storagegroup/marshal.go (about)

     1  package storagegroup
     2  
     3  import (
     4  	"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
     5  	"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message"
     6  	storagegroup "github.com/TrueCloudLab/frostfs-api-go/v2/storagegroup/grpc"
     7  	"github.com/TrueCloudLab/frostfs-api-go/v2/util/proto"
     8  )
     9  
    10  const (
    11  	sizeField       = 1
    12  	hashField       = 2
    13  	expirationField = 3
    14  	objectIDsField  = 4
    15  )
    16  
    17  // StableMarshal marshals unified storage group structure in a protobuf
    18  // compatible way without field order shuffle.
    19  func (s *StorageGroup) StableMarshal(buf []byte) []byte {
    20  	if s == nil {
    21  		return []byte{}
    22  	}
    23  
    24  	if buf == nil {
    25  		buf = make([]byte, s.StableSize())
    26  	}
    27  
    28  	var offset int
    29  
    30  	offset += proto.UInt64Marshal(sizeField, buf[offset:], s.size)
    31  	offset += proto.NestedStructureMarshal(hashField, buf[offset:], s.hash)
    32  	offset += proto.UInt64Marshal(expirationField, buf[offset:], s.exp)
    33  	refs.ObjectIDNestedListMarshal(objectIDsField, buf[offset:], s.members)
    34  
    35  	return buf
    36  }
    37  
    38  // StableSize of storage group structure marshalled by StableMarshal function.
    39  func (s *StorageGroup) StableSize() (size int) {
    40  	if s == nil {
    41  		return 0
    42  	}
    43  
    44  	size += proto.UInt64Size(sizeField, s.size)
    45  	size += proto.NestedStructureSize(hashField, s.hash)
    46  	size += proto.UInt64Size(expirationField, s.exp)
    47  	size += refs.ObjectIDNestedListSize(objectIDsField, s.members)
    48  
    49  	return size
    50  }
    51  
    52  func (s *StorageGroup) Unmarshal(data []byte) error {
    53  	return message.Unmarshal(s, data, new(storagegroup.StorageGroup))
    54  }