serialize

package
v0.5.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2019 License: MIT Imports: 7 Imported by: 0

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

View Source
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

func GetMarshalSize(obj proto.Message) int64

GetMarshalSize calculates the total size for a serialized object.

func Marshal

func Marshal(writer io.Writer, obj proto.Message) (cnt int64, err error)

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

func MarshalAt(writer io.WriterAt, offset int64, obj proto.Message) (cnt int64, err error)

MarshalAt is similar to Marshal except it writes data into io.WriterAt interface instead of io.Writer .

func Unmarshal

func Unmarshal(reader io.Reader, obj proto.Message) (err error)

Unmarshal deserialize data from an io.Reader and load the data into a protobuf object.

One must specifies the type of the object before Unmarshal it. TODO: return the number of byte read. Since all other [un]marhshal[at] functions return the number of byte written or read.

func UnmarshalAt

func UnmarshalAt(reader io.ReaderAt, offset int64, obj proto.Message) (n int64, err error)

UnmarshalAt is similar to Unmarshal except it reads from io.ReaderAt thus it is able to specify where to start to read.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL