namedtuple

package
v0.0.0-...-49e565d Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2015 License: MIT, Apache-2.0 Imports: 11 Imported by: 0

README

namedtuple

namedtuple provides a binary encoding for nested, named tuples

Documentation

Index

Constants

View Source
const (

	// VersionOneTupleHeaderSize is the size of the header for version one.
	VersionOneTupleHeaderSize = 13

	// DefaultMaxSize is the default maximum size of a tuple being decoded.
	DefaultMaxSize uint64 = 4096
)
View Source
const ProtocolSizeEnumMask = 192

ProtocolVersionMask is the upper 2 bits of the first byte of the ptotocol header (0b11000000)

View Source
const ProtocolVersionMask = 63

ProtocolVersionMask is the lower 6 bits of the first byte of the ptotocol header (0b00111111)

Variables

View Source
var (
	// ErrTupleExceedsMaxSize is returned if the length of a Tuple of greater than the maximum allowable size
	// for the Decoder.
	ErrTupleExceedsMaxSize = errors.New("Tuple exceeds maximum allowable length")

	// ErrInvalidProtocolVersion is returned from Decode() if the Tuple version is unknown.
	ErrInvalidProtocolVersion = errors.New("Invalid protocol version in Tuple header")

	// ErrTupleLengthTooSmall is returned from the Decode() method if the decoded length is too small to include all the required information
	ErrTupleLengthTooSmall = errors.New("Tuple length is too short to include all the required information")

	// ErrUnknownTupleType is returned when the Tuple being decoded is of an unknown type.
	ErrUnknownTupleType = errors.New("Unknown tuple type")

	// ErrInvalidLength is returned if the byte count for the length is not 1, 2, 4 or 8.
	ErrInvalidLength = errors.New("Invalid Tuple Size: tuple length must be encoded as 1,2,4 or 8 bytes")

	// EmptyTuple is returned along with an error from the Decode() method.
	EmptyTuple = Tuple{}
)
View Source
var (

	// ErrFieldDoesNotExist is returned when Tuple.Offset is called
	// with an unknown field name.
	ErrFieldDoesNotExist = errors.New("Field does not exist")

	// ErrInvalidFieldIndex is returned when the field offset
	// is greater than the number of fields.
	ErrInvalidFieldIndex = errors.New("Invalid field index")
)
View Source
var (
	NilCode                  = TypeCode{0, 0}
	TrueCode                 = TypeCode{1, 0}
	FalseCode                = TypeCode{2, 0}
	BooleanArray8Code        = TypeCode{3, 1}
	BooleanArray16Code       = TypeCode{4, 2}
	BooleanArray32Code       = TypeCode{5, 4}
	BooleanArray64Code       = TypeCode{6, 8}
	ByteCode                 = TypeCode{7, 0}
	ByteArray8Code           = TypeCode{8, 1}
	ByteArray16Code          = TypeCode{9, 2}
	ByteArray32Code          = TypeCode{10, 4}
	ByteArray64Code          = TypeCode{11, 8}
	UnsignedByteCode         = TypeCode{12, 0}
	UnsignedByteArray8Code   = TypeCode{13, 1}
	UnsignedByteArray16Code  = TypeCode{14, 2}
	UnsignedByteArray32Code  = TypeCode{15, 4}
	UnsignedByteArray64Code  = TypeCode{16, 8}
	Short8Code               = TypeCode{17, 0}
	Short16Code              = TypeCode{18, 0}
	ShortArray8Code          = TypeCode{19, 1}
	ShortArray16Code         = TypeCode{20, 2}
	ShortArray32Code         = TypeCode{21, 4}
	ShortArray64Code         = TypeCode{22, 8}
	UnsignedShort8Code       = TypeCode{23, 0}
	UnsignedShort16Code      = TypeCode{24, 0}
	UnsignedShortArray8Code  = TypeCode{25, 1}
	UnsignedShortArray16Code = TypeCode{26, 2}
	UnsignedShortArray32Code = TypeCode{27, 4}
	UnsignedShortArray64Code = TypeCode{28, 8}
	Int8Code                 = TypeCode{29, 0}
	Int16Code                = TypeCode{30, 0}
	Int32Code                = TypeCode{31, 0}
	IntArray8Code            = TypeCode{32, 1}
	IntArray16Code           = TypeCode{33, 2}
	IntArray32Code           = TypeCode{34, 4}
	IntArray64Code           = TypeCode{35, 8}
	UnsignedInt8Code         = TypeCode{36, 0}
	UnsignedInt16Code        = TypeCode{37, 0}
	UnsignedInt32Code        = TypeCode{38, 0}
	UnsignedIntArray8Code    = TypeCode{39, 1}
	UnsignedIntArray16Code   = TypeCode{40, 2}
	UnsignedIntArray32Code   = TypeCode{41, 4}
	UnsignedIntArray64Code   = TypeCode{42, 8}
	Long8Code                = TypeCode{43, 0}
	Long16Code               = TypeCode{44, 0}
	Long32Code               = TypeCode{45, 0}
	Long64Code               = TypeCode{46, 0}
	LongArray8Code           = TypeCode{47, 1}
	LongArray16Code          = TypeCode{48, 2}
	LongArray32Code          = TypeCode{49, 4}
	LongArray64Code          = TypeCode{50, 8}
	UnsignedLong8Code        = TypeCode{51, 0}
	UnsignedLong16Code       = TypeCode{52, 0}
	UnsignedLong32Code       = TypeCode{53, 0}
	UnsignedLong64Code       = TypeCode{54, 0}
	UnsignedLongArray8Code   = TypeCode{55, 1}
	UnsignedLongArray16Code  = TypeCode{56, 2}
	UnsignedLongArray32Code  = TypeCode{57, 4}
	UnsignedLongArray64Code  = TypeCode{58, 8}
	DoubleCode               = TypeCode{59, 0}
	DoubleArray8Code         = TypeCode{60, 1}
	DoubleArray16Code        = TypeCode{61, 2}
	DoubleArray32Code        = TypeCode{62, 4}
	DoubleArray64Code        = TypeCode{63, 8}
	FloatCode                = TypeCode{64, 0}
	FloatArray8Code          = TypeCode{65, 1}
	FloatArray16Code         = TypeCode{66, 2}
	FloatArray32Code         = TypeCode{67, 4}
	FloatArray64Code         = TypeCode{68, 8}
	String8Code              = TypeCode{69, 1}
	String16Code             = TypeCode{70, 2}
	String32Code             = TypeCode{71, 4}
	String64Code             = TypeCode{72, 8}
	StringArray8Code         = TypeCode{73, 1}
	StringArray16Code        = TypeCode{74, 2}
	StringArray32Code        = TypeCode{75, 4}
	StringArray64Code        = TypeCode{76, 8}
	TimestampCode            = TypeCode{77, 0}
	TimestampArray8Code      = TypeCode{78, 1}
	TimestampArray16Code     = TypeCode{79, 2}
	TimestampArray32Code     = TypeCode{80, 4}
	TimestampArray64Code     = TypeCode{81, 8}
	Tuple8Code               = TypeCode{82, 1}
	Tuple16Code              = TypeCode{83, 2}
	Tuple32Code              = TypeCode{84, 4}
	Tuple64Code              = TypeCode{85, 8}
	TupleArray8Code          = TypeCode{86, 1}
	TupleArray16Code         = TypeCode{87, 2}
	TupleArray32Code         = TypeCode{88, 4}
	TupleArray64Code         = TypeCode{89, 8}
)

Field Type constants

View Source
var (
	// ErrInvalidFieldSize is retured by an encoder if the the field size is invalid
	ErrInvalidFieldSize = errors.New("Invalid Field Size: field size must be 1,2,4 or 8 bytes")
)

Functions

func ParseProtocolHeader

func ParseProtocolHeader(header uint8) (lenBytes uint8, version uint8)

ParseProtocolHeader returns the number of bytes to read for the content length and the protocol version. The upper 2 bits represent the number of bytes in the content length (0-3 bits = 2**n). The lower 6 bits are the protocol version which determines how the bytes are interpreted.

Types

type Decoder

type Decoder interface {
	Decode() (Tuple, error)
}

Decoder decodes data into a Tuple or an error.

func NewDecoder

func NewDecoder(reg Registry, r io.Reader) Decoder

NewDecoder creates a new Decoder using a type Registry and an io.Reader.

func NewDecoderSize

func NewDecoderSize(reg Registry, maxSize uint64, r io.Reader) Decoder

NewDecoderSize creates a new Decoder using a type Registry, a max size and an io.Reader.

type Encoder

type Encoder interface {
	Encode(Tuple) error
}

Encoder encodes tuples normally into a given io.Writer.

func NewEncoder

func NewEncoder(w io.Writer) Encoder

NewEncoder creates a new encoder with the given io.Writer

type Field

type Field struct {
	Name     string
	Required bool
	Type     FieldType
}

type FieldType

type FieldType uint8

FieldType is the byte representation of each field type

const (
	Uint8Field FieldType = iota
	Uint8ArrayField
	Int8Field
	Int8ArrayField
	Uint16Field
	Uint16ArrayField
	Int16Field
	Int16ArrayField
	Uint32Field
	Uint32ArrayField
	Int32Field
	Int32ArrayField
	Uint64Field
	Uint64ArrayField
	Int64Field
	Int64ArrayField
	Float32Field
	Float32ArrayField
	Float64Field
	Float64ArrayField
	TimestampField
	TimestampArrayField
	TupleField
	TupleArrayField
	StringField
	StringArrayField
	BooleanField
	BooleanArrayField
)

type Registry

type Registry struct {
	// contains filtered or unexported fields
}
var DefaultRegistry Registry

func NewRegistry

func NewRegistry() Registry

func (*Registry) Contains

func (r *Registry) Contains(t TupleType) bool

func (*Registry) ContainsHash

func (r *Registry) ContainsHash(hash uint64) bool

func (*Registry) ContainsName

func (r *Registry) ContainsName(namespace, name string) bool

func (*Registry) Get

func (r *Registry) Get(namespace, name string) (tupleType TupleType, exists bool)

func (*Registry) GetWithHash

func (r *Registry) GetWithHash(namespace, name uint32) (tupleType TupleType, exists bool)

func (*Registry) Register

func (r *Registry) Register(t TupleType)

func (*Registry) Size

func (r *Registry) Size() int

func (*Registry) Unregister

func (r *Registry) Unregister(t TupleType)

type SynchronizedHash

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

func NewHasher

func NewHasher(hasher hash.Hash32) SynchronizedHash

func (*SynchronizedHash) Hash

func (s *SynchronizedHash) Hash(data []byte) uint32

type Tuple

type Tuple struct {
	Header TupleHeader
	// contains filtered or unexported fields
}

Tuple is the data representation used by the encoder and decoder.

var NIL Tuple = Tuple{}

Empty Tuple

func (*Tuple) Is

func (t *Tuple) Is(tupleType TupleType) bool

Is determines if a tuple is a certain type.

func (*Tuple) Offset

func (t *Tuple) Offset(field string) (int, error)

Offset returns the byte offset for the given field

func (*Tuple) Payload

func (t *Tuple) Payload() []byte

Payload returns the bytes representing the tuple. The tuple header is not included.

func (*Tuple) Size

func (t *Tuple) Size() int

Size returns the number of bytes used to store the tuple data

func (Tuple) WriteTo

func (t Tuple) WriteTo(w io.Writer) (n int, err error)

WriteTo sends the binary representation of the Tuple to the given io.Writer.

type TupleBuilder

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

func NewBuilder

func NewBuilder(t TupleType, buffer []byte) TupleBuilder

func (*TupleBuilder) Build

func (b *TupleBuilder) Build() (Tuple, error)

func (*TupleBuilder) PutFloat32

func (b *TupleBuilder) PutFloat32(field string, value float32) (wrote uint64, err error)

PutFloat32 writes a 32-bit float for the given string field. The field type must be `Float32Field`, otherwise an error is returned. The type code is written first then the value. Upon success, the number of bytes written is returned along with a nil error.

func (*TupleBuilder) PutFloat32Array

func (b *TupleBuilder) PutFloat32Array(field string, value []float32) (wrote int, err error)

PutFloat32Array writes a float array for the given field. The field type must be a 'Float32ArrayField', otherwise as error will be returned. The type code is written first followed by the array size in bytes. If the size of the array is less than `math.MaxUint8`, a byte will be used to represent the length. If the size of the array is less than `math.MaxUint16`, a 16-bit unsigned integer will be used to represent the length and so on. If the buffer is too small to store the entire array, an `xbinary.ErrOutOfRange` error will be returned. If the write is successful, the total number of bytes will be returned as well as a nil error.

func (*TupleBuilder) PutFloat64

func (b *TupleBuilder) PutFloat64(field string, value float64) (wrote uint64, err error)

PutFloat64 writes a 64-bit float (or double in some languages) for the given string field. The field type must be `Float64Field`, otherwise an error is returned. The type code is written first then the value. Upon success, the number of bytes written is returned along with a nil error.

func (*TupleBuilder) PutFloat64Array

func (b *TupleBuilder) PutFloat64Array(field string, value []float64) (wrote int, err error)

PutFloat64Array writes a float array for the given field. The field type must be a 'Float64ArrayField', otherwise as error will be returned. The type code is written first followed by the array size in bytes. If the size of the array is less than `math.MaxUint8`, a byte will be used to represent the length. If the size of the array is less than `math.MaxUint16`, a 16-bit unsigned integer will be used to represent the length and so on. If the buffer is too small to store the entire array, an `xbinary.ErrOutOfRange` error will be returned. If the write is successful, the total number of bytes will be returned as well as a nil error.

func (*TupleBuilder) PutInt16

func (b *TupleBuilder) PutInt16(field string, value int16) (wrote uint64, err error)

PutInt16 sets a 16-bit signed value for the given field name.The field name must be an Int16Field; otherwise, an error will be returned. If the type buffer no longer has enough space to write the value, an xbinary.ErrOutOfRange error will be returned. Upon success, the number of bytes written as well as a nil error will be returned. The type code will be written first. If the value is `< math.MaxUint8`, only 1 byte will be written. Otherwise, the entire 16-bit value will be written.

func (*TupleBuilder) PutInt16Array

func (b *TupleBuilder) PutInt16Array(field string, value []int16) (wrote int, err error)

func (*TupleBuilder) PutInt32

func (b *TupleBuilder) PutInt32(field string, value int32) (wrote uint64, err error)

PutInt32 sets a 32-bit signed value for the given field name. The field name must be a Int32Field. Otherwise, an error will be returned. If the type buffer no longer has enough space to write the value, an `xbinary.ErrOutOfRange` error will be returned. Upon success, the number of bytes written as well as a nil error will be returned. The type code will be written first. If the absolute value is `< math.MaxUint8`, only 1 byte will be written. If the absolute value is `< math.MaxUint16`, only 2 bytes will be written. Otherwise, the entire 32-bit value will be written.

func (*TupleBuilder) PutInt32Array

func (b *TupleBuilder) PutInt32Array(field string, value []int32) (wrote int, err error)

func (*TupleBuilder) PutInt64

func (b *TupleBuilder) PutInt64(field string, value int64) (wrote uint64, err error)

PutInt64 sets a 64-bit signed integer for the given field name. The field name must be a Int64Field. Otherwise, an error will be returned. If the type buffer no longer has enough space to write the value, an `xbinary.ErrOutOfRange` error will be returned. Upon success, the number of bytes written as well as a nil error will be returned. The type code will be written first. If the absolute value is `< math.MaxUint8`, only 1 byte will be written. If the absolute value is `< math.MaxUint16`, only 2 bytes will be written. If the absolute value is `< math.MaxUint32`, only 4 bytes will be written. Otherwise, the entire 64-bit value will be written.

func (*TupleBuilder) PutInt64Array

func (b *TupleBuilder) PutInt64Array(field string, value []int64) (wrote int, err error)

func (*TupleBuilder) PutInt8

func (b *TupleBuilder) PutInt8(field string, value int8) (wrote uint64, err error)

PutInt8 sets an 8-bit signed value for the given string name. The field name must be an Int8Field otherwise an error will be returned. If the type buffer no longer has enough space to write this value, an xbinary.ErrOutOfRange error will be returned. Upon success, 2 bytes should be written into the buffer and the returned error should be nil. The type code is written first then the byte value.

func (*TupleBuilder) PutInt8Array

func (b *TupleBuilder) PutInt8Array(field string, value []int8) (wrote int, err error)

func (*TupleBuilder) PutString

func (b *TupleBuilder) PutString(field string, value string) (wrote int, err error)

PutString writes a string value for the given field. The field type must be a `StringField` otherwise an error will be returned. The type code is written first, then a length, and finally the value. If the size of the string is `< math.MaxUint8`, a single byte will represent the length. If the size of the string is `< math.MaxUint16`, a uint16 value will represent the length and so on. If the buffer does not have enough space for the entire string and field header, an error will be returned. If successful, the number of bytes written will be returned as well as a nil error.

func (*TupleBuilder) PutTimestamp

func (b *TupleBuilder) PutTimestamp(field string, value time.Time) (wrote uint64, err error)

PutTimestamp writes a 64-bit signed integer (using `time.UnixNano()`) for the given `time.Time` value. The timestamp field type must be a `TimestampField`. If the buffer does not have enough space available an error is returned. Upon successful write, the number of bytes written will be returned as well as a nil error.

func (*TupleBuilder) PutTimestampArray

func (b *TupleBuilder) PutTimestampArray(field string, times []time.Time) (wrote int, err error)

func (*TupleBuilder) PutTuple

func (b *TupleBuilder) PutTuple(field string, value Tuple) (wrote int, err error)

PutTuple writes a tuple into the given field. The field type must be a TupleField, otherwise an error will be returned. The type code is written first, then the length, then the value. If the tuple length is less than `math.MaxUint8`, a single byte is used to represent the length. If the tuple length is less than `math.MaxUint16`, an unsigned 16-bit integer is used to represent the length and so on as the length increases. If the buffer is not large enough to store the entire tuple an `xbinary.ErrOutOfRange` error is returned. If the write is successful, the number of bytes written is returned as well as a nil error.

func (*TupleBuilder) PutTupleArray

func (b *TupleBuilder) PutTupleArray(field string, value []Tuple) (wrote int, err error)

PutTupleArray writes an array of tuples for the given field. The field type must be `TupleArrayField`, otherwise an error will be returned.

func (*TupleBuilder) PutUint16

func (b *TupleBuilder) PutUint16(field string, value uint16) (wrote uint64, err error)

PutUint16 sets a 16-bit unsigned value for the given field name.The field name must be a Uint16Field otherwise an error will be returned. If the type buffer no longer has enough space to write the value, an xbinary.ErrOutOfRange error will be returned. Upon success, the number of bytes written as well as a nil error will be returned. The type code will be writtn first. If the value is `< math.MaxUint8`, only 1 byte will be written. Otherwise, the entire 16-bit value will be written.

func (*TupleBuilder) PutUint16Array

func (b *TupleBuilder) PutUint16Array(field string, value []uint16) (wrote int, err error)

func (*TupleBuilder) PutUint32

func (b *TupleBuilder) PutUint32(field string, value uint32) (wrote uint64, err error)

PutUint32 sets a 32-bit unsigned value for the given field name. The field name must be a Uint32Field, otherwise, an error will be returned. If the type buffer no longer has enough space to write the value, an `xbinary.ErrOutOfRange` error will be returned. Upon success, the number of bytes written as well as a nil error will be returned. The type code will be written first. If the value is `< math.MaxUint8`, only 1 byte will be written. If the value is `< math.MaxUint16`, only 2 bytes will be written. Otherwise, the entire 32-bit value will be written.

func (*TupleBuilder) PutUint32Array

func (b *TupleBuilder) PutUint32Array(field string, value []uint32) (wrote int, err error)

func (*TupleBuilder) PutUint64

func (b *TupleBuilder) PutUint64(field string, value uint64) (wrote uint64, err error)

PutUint64 sets a 64-bit unsigned integer for the given field name. The field name must be a Uint64Field. Otherwise, an error will be returned. If the type buffer no longer has enough space to write the value, an `xbinary.ErrOutOfRange` error will be returned. Upon success, the number of bytes written as well as a nil error will be returned. The type code will be written first. If the absolute value is `< math.MaxUint8`, only 1 byte will be written. If the absolute value is `< math.MaxUint16`, only 2 bytes will be written. If the absolute value is `< math.MaxUint32`, only 4 bytes will be written. Otherwise, the entire 64-bit value will be written.

func (*TupleBuilder) PutUint64Array

func (b *TupleBuilder) PutUint64Array(field string, value []uint64) (wrote int, err error)

func (*TupleBuilder) PutUint8

func (b *TupleBuilder) PutUint8(field string, value uint8) (wrote uint64, err error)

PutUint8 sets an 8-bit unsigned value for the given string name. The field name must be a Uint8Field otherwise an error will be returned. If the type buffer no longer has enough space to write this value an xbinary.ErrOutOfRange error will be returned. Upon success 2 bytes should be written into the buffer and the returned error should be nil. The type code is written first then the byte value.

func (*TupleBuilder) PutUint8Array

func (b *TupleBuilder) PutUint8Array(field string, value []uint8) (wrote int, err error)

type TupleHeader

type TupleHeader struct {
	ProtocolVersion uint8
	TupleVersion    uint8
	NamespaceHash   uint32
	Hash            uint32
	FieldCount      uint32
	FieldSize       uint8
	ContentLength   uint64
	Offsets         []uint64
	Type            TupleType
}

TupleHeader stores meta data about the tuple such as the version, the hashes and the number of fields.

func (*TupleHeader) Size

func (t *TupleHeader) Size() int

Size returns the Version 1 header size plus the size of all the offsets

func (*TupleHeader) WriteTo

func (t *TupleHeader) WriteTo(w io.Writer) (int64, error)

WriteTo writes the TupleHeader into the given writer.

type TupleType

type TupleType struct {
	Namespace     string // Tuple Namespace
	Name          string // Tuple Name
	NamespaceHash uint32
	Hash          uint32
	// contains filtered or unexported fields
}

func New

func New(namespace string, name string) (t TupleType)

New creates a new TupleType with the given namespace and type name

func (*TupleType) AddVersion

func (t *TupleType) AddVersion(fields ...Field)

AddVersion adds a version to the tuple type

func (*TupleType) Builder

func (t *TupleType) Builder(buffer []byte) TupleBuilder

Builder creates a new builder from the TupleType

func (*TupleType) Contains

func (t *TupleType) Contains(field string) bool

Contains determines is a field exists in the TupleType

func (*TupleType) NumVersions

func (t *TupleType) NumVersions() int

NumVersions returns the number of version in the tuple type

func (*TupleType) Offset

func (t *TupleType) Offset(field string) (offset int, exists bool)

Offset determines the numerical offset for the given field

func (*TupleType) Versions

func (t *TupleType) Versions() (vers []Version)

Versions returns an array of versions contained in this type

type TypeCode

type TypeCode struct {
	OpCode uint8
	Size   uint8
}

TypeCode represents a field type

type Version

type Version struct {
	Num    uint8
	Fields []Field
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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