typedbuffer

package module
v0.0.0-...-6fb355a Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2015 License: MIT Imports: 2 Imported by: 1

README

typedbuffer

A set of methods to encode/decode basic types into a byte slice, compacting values and preserving order where possible

Installation

$ go get github.com/gobs/typedbuffer

Documentation

http://godoc.org/github.com/gobs/typedbuffer

Documentation

Overview

*

  • A set of utility methods that encodes the type of an object and the object value
  • in a byte slice. For small values the object value is encoded with the type. *
  • Note that the encoded buffer preserves the order of the native object,
  • so that sorting buffers according to their natural order should match
  • sorting objects of the same type according to their natural order *
  • Sorting objects of different types is not supported. Also, sorting byte slices of different length
  • doesn't return the expected results (longer slices win) but sorting buffer contains different types
  • in the same order (records) should work as expected. *
  • Long are "compressed" and only use enough bytes to represent the value
  • (i.e. 255 will use 1 byte, 65535 will use 2 bytes and so long). To do this
  • and also preserve order the type encodes a representation of the number "size"
  • so that larger number will have a higher type. *
  • Supported types: *
  • Nil
  • byte 00 - nil first (nil comes before any other value)
  • byte FF - nil last (nil comes after any other value) *
  • Boolean:
  • byte 0E - bool false
  • byte 0F - bool true *
  • Bytes:
  • byte 10+size : 0 to 15 bytes
  • byte 20+size : 16 to 31 bytes
  • byte 30+size : 32 to 47 bytes
  • byte 40+size : 48 to 60 bytes
  • byte 4D XX : 61+XX bytes
  • byte 4E XXXX : 316+XXXX bytes
  • byte 4F XXXXXXXX : 65851+XXXXXXXXXX bytes *
  • Date:
  • byte 50+size [n bytes] - Date from bytes as unsigned long *
  • Delta Date (see Long) :
  • byte D8+size [n bytes] - Date as delta after 1/1/2015 (long)
  • byte 58+size [n bytes] - Date as delta before 1/1/2015 (long) *
  • Long:
  • byte E0 - Long 0L
  • byte E1 - Long 1L
  • ...
  • byte E7 - Long 7L
  • byte E8 [1 byte] Long 8L to 255L
  • byte E9 [2 bytes] Long 256L to 65535L
  • ...
  • byte EF [8 bytes] Long from bytes
  • byte 6F - Long -1L
  • byte 6E - Long -2L
  • ...
  • byte 68 - Long -8L
  • byte 67 [1 byte] - Long -9L to -256
  • byte 66 [2 bytes] - Long -256 to -65536L
  • ...
  • byte 60 [8 bytes] - Long from bytes (negative value) *
  • Unsigned Long:
  • byte 80 - Unsigned long 0
  • byte 81 - Unsigned long 1
  • ...
  • byte 8F - Unsigned long 15
  • byte 90 - Unsigned long 16
  • byte 91 [1 byte] 17 to 255
  • byte 92 [2 bytes] 256 to 65535
  • ...
  • byte 98 [8 bytes] Unsigned long from bytes *
  • Double:
  • byte F7 - Double.NaN
  • byte F6 - Double.POSITIVE_INFINITY
  • byte F5 + bytes[8] - Double from bytes (positive value)
  • byte F4 - Double +0.0
  • byte 76 - Double -0.0
  • byte 75 + bytes[8] - Double from bytes (negative value)
  • byte 74 - Double.NEGATIVE_INFINITY *

Index

Constants

View Source
const (
	/** All positive values have this bit set */
	BB_POSITIVE byte = 0x80
	BB_NEGATIVE byte = 0x00

	BB_TYPE_MASK byte = 0xF0 // this is actually type + cardinality

	/** Nil values */
	BB_NIL_FIRST = 0x00
	BB_NIL_LAST  = 0xFF

	/** Boolean values */
	BB_BOOLEAN       = 0x0E
	BB_BOOLEAN_FALSE = BB_BOOLEAN | 0
	BB_BOOLEAN_TRUE  = BB_BOOLEAN | 1

	/** Bytes values */
	BB_BYTES       = 0x10
	BB_BYTES_LEN_1 = 0x4D
	BB_BYTES_LEN_2 = 0x4E
	BB_BYTES_LEN_4 = 0x4E

	/** Date values */
	BB_DATE = 0x50

	/** Compact date values */
	BB_DELTA_DATE    = 0x58
	BB_POSITIVE_DATE = BB_DELTA_DATE | BB_POSITIVE
	BB_NEGATIVE_DATE = BB_DELTA_DATE | BB_NEGATIVE

	BB_DATE_MASK = 0xF8

	/** Integer values */
	BB_INT                = 0x60
	BB_INT_POSITIVE_VALUE = BB_INT | BB_POSITIVE | 0x08
	BB_INT_NEGATIVE_VALUE = BB_INT | BB_NEGATIVE
	BB_SMALL_POSITIVE     = BB_INT | BB_POSITIVE
	BB_SMALL_NEGATIVE     = BB_INT | BB_NEGATIVE | 0x08

	BB_INT_MASK = BB_INT_POSITIVE_VALUE

	SMALL_NEGATIVE_INT = -8
	SMALL_POSITIVE_INT = +7
	SMALL_INT_MASK     = 0x07
	SMALL_NEG_MASK     = ^SMALL_INT_MASK

	MIN_SMALL_POSITIVE = BB_SMALL_POSITIVE | (0 & SMALL_INT_MASK)
	MAX_SMALL_POSITIVE = BB_SMALL_POSITIVE | (SMALL_POSITIVE_INT & SMALL_INT_MASK)

	MIN_SMALL_NEGATIVE = BB_SMALL_NEGATIVE | (SMALL_NEGATIVE_INT & SMALL_INT_MASK)
	MAX_SMALL_NEGATIVE = BB_SMALL_NEGATIVE | (-1 & SMALL_INT_MASK)

	/** Double values */
	BB_DOUBLE                   = 0x70
	BB_DOUBLE_NAN               = (BB_DOUBLE | BB_POSITIVE) + 0x03
	BB_DOUBLE_POSITIVE_INFINITY = (BB_DOUBLE | BB_POSITIVE) + 0x02
	BB_DOUBLE_POSITIVE_ZERO     = (BB_DOUBLE | BB_POSITIVE) + 0x00
	BB_DOUBLE_POSITIVE_VALUE    = (BB_DOUBLE | BB_POSITIVE) + 0x01
	BB_DOUBLE_NEGATIVE_ZERO     = (BB_DOUBLE | BB_NEGATIVE) + 0x02
	BB_DOUBLE_NEGATIVE_VALUE    = (BB_DOUBLE | BB_NEGATIVE) + 0x01
	BB_DOUBLE_NEGATIVE_INFINITY = (BB_DOUBLE | BB_NEGATIVE) + 0x00

	/** Unsigned values */
	BB_UINT     = 0x80
	BB_UINT_VAR = 0x90

	BB_UINT_MASK = 0xE0

	SMALL_UINT      = 16
	SMALL_UINT_MASK = 0x1F

	MIN_SMALL_UINT = BB_UINT
	MAX_SMALL_UINT = BB_UINT + SMALL_UINT
)

Variables

View Source
var (
	NilFirst = []byte{BB_NIL_FIRST}
	NilLast  = []byte{BB_NIL_LAST}

	True  = []byte{BB_BOOLEAN_TRUE}
	False = []byte{BB_BOOLEAN_FALSE}

	Zero          = []byte{BB_SMALL_POSITIVE | 0}
	One           = []byte{BB_SMALL_POSITIVE | 1}
	MinusOne      = []byte{BB_SMALL_NEGATIVE | (-1 & SMALL_INT_MASK)}
	SmallNegative = []byte{BB_SMALL_NEGATIVE | (SMALL_NEGATIVE_INT & SMALL_INT_MASK)}

	Bytes1 = []byte{BB_BYTES_LEN_1}
	Bytes2 = []byte{BB_BYTES_LEN_2}
	Bytes4 = []byte{BB_BYTES_LEN_4}

	NoEncoding           = errors.New("no encoding")
	EmptyBufferError     = errors.New("empty buffer")
	CorruptedBufferError = errors.New("corrupted buffer")

	DELTA_DATE = time.Date(2015, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()
)

Functions

func Decode

func Decode(b []byte) (interface{}, []byte, error)

Decode first value in typed buffer. Returns decoded value and remaining buffer

func DecodeAll

func DecodeAll(strings bool, b []byte) ([]interface{}, error)

Decode all values in a type buffer. Return an array of decoded values. If strings is true, byte arrays are converterd to string

func DecodeUintArray

func DecodeUintArray(b []byte) ([]uint64, error)

Decode all values in a typed buffer as an arrya of uint64 values.

func Encode

func Encode(values ...interface{}) ([]byte, error)

Encode one or more values according to their type (strings are encoded as []byte)

func EncodeBool

func EncodeBool(b bool) []byte

Encode boolean (true / false)

func EncodeBytes

func EncodeBytes(bb []byte) []byte

Encode slice of bytes

func EncodeInt

func EncodeInt(i int) []byte

Encode int value (as compacted int64)

func EncodeInt64

func EncodeInt64(i int64) []byte

Encoce int64 value

func EncodeNil

func EncodeNil(first bool) []byte

Encode nil, depending on value of first

func EncodeNils

func EncodeNils(nilFirst bool, values ...interface{}) ([]byte, error)

func EncodeTime

func EncodeTime(t time.Time) []byte

Encode Time

func EncodeTimeDelta

func EncodeTimeDelta(t time.Time) []byte

Encode Time as delta from 2015-01-01

func EncodeUint

func EncodeUint(u uint) []byte

Encode uint value (as compacted uint64)

func EncodeUint64

func EncodeUint64(u uint64) []byte

Encode uint64

Types

This section is empty.

Jump to

Keyboard shortcuts

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