frostfs-sdk-go/api/tombstone/marshal.go
Pavel Pogodaev 6ce73790ea
All checks were successful
DCO / DCO (pull_request) Successful in 38s
Tests and linters / Tests (pull_request) Successful in 1m13s
Tests and linters / Lint (pull_request) Successful in 2m36s
[#276] Merge repo with frostfs-api-go
Signed-off-by: Pavel Pogodaev <p.pogodaev@yadro.com>
2024-10-22 14:05:12 +00:00

56 lines
1.4 KiB
Go

package tombstone
import (
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/message"
tombstone "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/tombstone/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/util/proto"
)
const (
expFNum = 1
splitIDFNum = 2
membersFNum = 3
)
// StableMarshal marshals unified tombstone message in a protobuf
// compatible way without field order shuffle.
func (s *Tombstone) StableMarshal(buf []byte) []byte {
if s == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, s.StableSize())
}
var offset int
offset += proto.UInt64Marshal(expFNum, buf[offset:], s.exp)
offset += proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID)
for i := range s.members {
offset += proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i])
}
return buf
}
// StableSize returns size of tombstone message marshalled by StableMarshal function.
func (s *Tombstone) StableSize() (size int) {
if s == nil {
return 0
}
size += proto.UInt64Size(expFNum, s.exp)
size += proto.BytesSize(splitIDFNum, s.splitID)
for i := range s.members {
size += proto.NestedStructureSize(membersFNum, &s.members[i])
}
return size
}
// Unmarshal unmarshal tombstone message from its binary representation.
func (s *Tombstone) Unmarshal(data []byte) error {
return message.Unmarshal(s, data, new(tombstone.Tombstone))
}