protolog

package module
v0.0.0-...-ce1a9ba Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2019 License: Apache-2.0 Imports: 7 Imported by: 1

README

protolog

Package protolog implements a simple file format for a sequence of blobs with ability to store a message type with each blob as well as a checksum. It is intended for logging protobuf messages. Unlike other formats, it tries to be simple, so writing a reader/writer in other languages is trivial. This design is a modified form of Eric Lesh's recordio Go implementation (github.com/eclesh/recordio). This package swaps out a single varint header for fixed size headers with support for a uint16 ID of the message type and a CRC-32C checksum.

Example: reading with a Scanner

const fooTypeID uint16 = 13 // ids that tell us type of message

f, _ := os.Open("data.log")
defer f.Close()
scanner := protolog.NewScanner(f)
for scanner.Scan() {
	data := scanner.Bytes()
	switch scanner.TypeID() {
		case fooTypeID:
			var foo myFooProtobufType
			foo.Unmarshal(data)
		// Check other types
	}
}
if err := scanner.Error(); err != nil {
	// Do error handling
}

Example: reading with a Reader

const fooTypeID uint16 = 13 // ids that tell us type of message

f, _ := os.Open("data.log")
f.Close()
r := protolog.NewReader(f)
for {
	data, typeID, err := r.Next()
	if err == io.EOF {
		break
	}
	switch typeID {
		case fooTypeID:
			var foo myFooProtobufType
			foo.Unmarshal(data)
		// Check other types
	}
}

Example: writing one type of message

const fooTypeID uint16 = 13 // ids that tell us type of message

f, _ := os.Create("data.log")
w := protolog.NewTypedWriter(fooTypeID, f)

data, _ := myFooProtobufObj.Marshal()
w.Write(data)
data, _ = my2ndFooProtobufObj.Marshal()
w.Write(data)

f.Close()

Example: writing different messages

const (
	fooTypeID uint16 = 13
	barTypeID uint16 = 14
)

f, _ := os.Create("data.log")
w := protolog.NewMultiTypeWriter(f)

typebuf := make([]byte, 2)

serialization, _ := myFooProtobufObj.Marshal()
w.Write(fooTypeID, serialization)

serialization, _ = myBarProtobufObj.Marshal()
w.Write(barTypeID, serialization)

f.Close()

File Format

Header 1 (10 bytes)
	Message # Bytes excluding fixed header (uint32, 4 bytes)
	Checksum (4 bytes)
	Message Type ID (uint16, 2 bytes)
Binary Data 1 (variable # bytes)
...
Header N (10 bytes)
Binary Data N (variable # bytes)

License

Copyright (C) 2019 Bill Katz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package protolog implements a simple file format for a sequence of blobs with ability to store a message type with each blob as well as a checksum. It is intended for logging protobuf messages. Unlike other formats, it tries to be simple, so writing a reader/writer in other languages is trivial. This design is a modified form of Eric Lesh's recordio Go implementation (github.com/eclesh/recordio). It uses fixed size headers with support for a uint16 ID of the message type and a CRC-32C checksum. Each blob must be less than 4 GiB (2^32 bytes).

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadChecksum is an error returned when a bad checksum is detected.
	ErrBadChecksum = fmt.Errorf("bad checksum detected while reading data")
)

Functions

This section is empty.

Types

type MultiTypedWriter

type MultiTypedWriter struct {
	// contains filtered or unexported fields
}

MultiTypedWriter writes records that can have different optional type identifiers

func NewMultiTypedWriter

func NewMultiTypedWriter(w io.Writer) *MultiTypedWriter

NewMultiTypedWriter returns a new writer that could have different record types.

func (*MultiTypedWriter) Write

func (w *MultiTypedWriter) Write(typeID uint16, data []byte) (int, error)

Write writes a data record with a type identifier.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader allows reading of checksummed binary blobs with optional uint16 type IDs.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new reader. If r doesn't implement io.ByteReader, it will be wrapped in a bufio.Reader.

func (*Reader) Next

func (r *Reader) Next() (uint16, []byte, error)

Next returns the next data record's type ID (if set) and data. It returns io.EOF if there are no more records.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

A Scanner is a convenient method for reading records sequentially.

func NewScanner

func NewScanner(r io.Reader) *Scanner

NewScanner creates a new Scanner from reader r.

func (*Scanner) Bytes

func (s *Scanner) Bytes() []byte

Bytes returns the data of the most recently scanned record. Subsequent calls may overwrite the returned data, so you must copy it if not using it immediately.

func (*Scanner) Error

func (s *Scanner) Error() error

Error returns the most recent error or nil if the error was EOF.

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan chugs through the input record by record and stops at the first error or EOF.

func (*Scanner) TypeID

func (s *Scanner) TypeID() uint16

TypeID returns the optionally set type ID of the most recently scanned record.

type TypedWriter

type TypedWriter struct {
	// contains filtered or unexported fields
}

TypedWriter writes records that have a single optional type identifier

func NewTypedWriter

func NewTypedWriter(recordType uint16, w io.Writer) *TypedWriter

NewTypedWriter returns a new writer that uses the same record type.

func (*TypedWriter) Write

func (w *TypedWriter) Write(data []byte) (int, error)

Write writes a data record associated with an optional type id.

Jump to

Keyboard shortcuts

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