Documentation ¶
Overview ¶
Package serialize provide general serialize and de-serialize functions.
It adds a header for every object to serialize. The header contains version, header length and data length. Thus we de-serializing, total size can be accquired at once, without reading the entire data.
Index ¶
- Constants
- func GetMarshalHeaderSize() int64
- func GetMarshalSize(obj proto.Message) int64
- func Marshal(writer io.Writer, obj proto.Message) (cnt int64, err error)
- func MarshalAt(writer io.WriterAt, offset int64, obj proto.Message) (cnt int64, err error)
- func Unmarshal(reader io.Reader, obj proto.Message) (err error)
- func UnmarshalAt(reader io.ReaderAt, offset int64, obj proto.Message) (n int64, err error)
- type DataHeader
Constants ¶
const ( // MaxMarshalledSize defines the max marshaled size for an object. MaxMarshalledSize int64 = 1024 * 1024 * 1024 )
Variables ¶
This section is empty.
Functions ¶
func GetMarshalHeaderSize ¶
func GetMarshalHeaderSize() int64
GetMarshalHeaderSize returns the serialized size of a DataHeader struct.
func GetMarshalSize ¶
GetMarshalSize calculates the total size for a serialized object.
func Marshal ¶
Marshal serializes a protobuf object into a io.Writer .
It returns number of bytes actually written, and encountered error.
The content written to writer may be wrong if there were error during Marshal(). So make a temp copy, and copy it to destination if everything is ok.
func MarshalAt ¶
MarshalAt is similar to Marshal except it writes data into io.WriterAt interface instead of io.Writer .
Types ¶
type DataHeader ¶
type DataHeader struct { // Version of this serialized data, for compatibility check. // It is in form of <major>.<minor>.<release>;. // As long as version is a string, its max size is 16. // // See: https://semver.org/ Version [version.MAXLEN]byte // HeaderSize is the serialized size in byte for DataHeader. HeaderSize uint64 // DataSize is the size in byte of user data. DataSize uint64 }
DataHeader defines the header format of a serialized byte stream.
It contains version, header size(size of this struct) and data size(size of the user data).
To ensure Compatibility:
- do NOT change type of fields
- do NOT reuse any ever existing names
- do NOT adjust fields order
- only append fields
- only use fixed-size type, e.g. not int, use int32 or int64
- test Every version of dataHeader ever existed
func UnmarshalHeader ¶
func UnmarshalHeader(reader io.Reader) (header *DataHeader, err error)
UnmarshalHeader reads just enough bytes from reader and load the data into a DataHeader object.