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

     1  package tombstone
     2  
     3  import (
     4  	"github.com/TrueCloudLab/frostfs-api-go/v2/rpc/message"
     5  	tombstone "github.com/TrueCloudLab/frostfs-api-go/v2/tombstone/grpc"
     6  	"github.com/TrueCloudLab/frostfs-api-go/v2/util/proto"
     7  )
     8  
     9  const (
    10  	expFNum     = 1
    11  	splitIDFNum = 2
    12  	membersFNum = 3
    13  )
    14  
    15  // StableMarshal marshals unified tombstone message in a protobuf
    16  // compatible way without field order shuffle.
    17  func (s *Tombstone) StableMarshal(buf []byte) []byte {
    18  	if s == nil {
    19  		return []byte{}
    20  	}
    21  
    22  	if buf == nil {
    23  		buf = make([]byte, s.StableSize())
    24  	}
    25  
    26  	var offset int
    27  
    28  	offset += proto.UInt64Marshal(expFNum, buf[offset:], s.exp)
    29  	offset += proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID)
    30  
    31  	for i := range s.members {
    32  		offset += proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i])
    33  	}
    34  
    35  	return buf
    36  }
    37  
    38  // StableSize returns size of tombstone message marshalled by StableMarshal function.
    39  func (s *Tombstone) StableSize() (size int) {
    40  	if s == nil {
    41  		return 0
    42  	}
    43  
    44  	size += proto.UInt64Size(expFNum, s.exp)
    45  	size += proto.BytesSize(splitIDFNum, s.splitID)
    46  	for i := range s.members {
    47  		size += proto.NestedStructureSize(membersFNum, &s.members[i])
    48  	}
    49  
    50  	return size
    51  }
    52  
    53  // Unmarshal unmarshal tombstone message from its binary representation.
    54  func (s *Tombstone) Unmarshal(data []byte) error {
    55  	return message.Unmarshal(s, data, new(tombstone.Tombstone))
    56  }