codec

package
Version: v0.0.0-...-7f9930a Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2015 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// InfiniteValue is the greatest than any other encoded value.
	InfiniteValue = []byte{0xFF, 0xFF}
	// NilValue is the smallest than any other encoded value.
	NilValue = []byte{0x00, 0x00}
	// SmallestNoneNilValue is smaller than any other encoded value except nil value.
	SmallestNoneNilValue = []byte{0x00, 0x01}
)

Functions

func DecodeBytes

func DecodeBytes(b []byte) ([]byte, []byte, error)

DecodeBytes decodes bytes which is encoded by EncodeBytes before, returns the leftover bytes and decoded value if no error.

func DecodeBytesDesc

func DecodeBytesDesc(b []byte) ([]byte, []byte, error)

DecodeBytesDesc decodes bytes which is encoded by EncodeBytesDesc before, returns the leftover bytes and decoded value if no error.

func DecodeFloat

func DecodeFloat(b []byte) ([]byte, float64, error)

DecodeFloat decodes a float from a byte slice generated with EncodeFloat before.

func DecodeFloatDesc

func DecodeFloatDesc(b []byte) ([]byte, float64, error)

DecodeFloatDesc decodes a float from a byte slice generated with EncodeFloatDesc before.

func DecodeInt

func DecodeInt(b []byte) ([]byte, int64, error)

DecodeInt decodes value encoded by EncodeInt before. It returns the leftover un-decoded slice, decoded value if no error.

func DecodeIntDesc

func DecodeIntDesc(b []byte) ([]byte, int64, error)

DecodeIntDesc decodes value encoded by EncodeInt before. It returns the leftover un-decoded slice, decoded value if no error.

func DecodeKey

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

DecodeKey decodes values from a byte slice generated with EncodeKey before.

func DecodeUint

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

DecodeUint decodes value encoded by EncodeUint before. It returns the leftover un-decoded slice, decoded value if no error.

func DecodeUintDesc

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

DecodeUintDesc decodes value encoded by EncodeInt before. It returns the leftover un-decoded slice, decoded value if no error.

func EncodeBytes

func EncodeBytes(b []byte, data []byte) []byte

EncodeBytes encodes the slice value using an escape based encoding, with the following rule:

\x00 -> \x00\xFF
\xFF -> \xFF\x00 if the first byte is \xFF

EncodeBytes will append \x00\x01 at the end of the encoded value to indicate the termination. EncodeBytes guarantees the encoded value is in ascending order for comparison, The encoded value is >= SmallestNoneNilValue and < InfiniteValue.

func EncodeBytesDesc

func EncodeBytesDesc(b []byte, data []byte) []byte

EncodeBytesDesc first encodes bytes using EncodeBytes, then bitwise reverses encoded value to guarentee the encoded value is in descending order for comparison, The encoded value is >= SmallestNoneNilValue and < InfiniteValue.

func EncodeFloat

func EncodeFloat(b []byte, v float64) []byte

EncodeFloat encodes a float v into a byte slice which can be sorted lexicographically later. EncodeFloat guarantees that the encoded value is in ascending order for comparison.

func EncodeFloatDesc

func EncodeFloatDesc(b []byte, v float64) []byte

EncodeFloatDesc encodes a float v into a byte slice which can be sorted lexicographically later. EncodeFloatDesc guarantees that the encoded value is in descending order for comparison.

func EncodeInt

func EncodeInt(b []byte, v int64) []byte

EncodeInt encodes the int64 value with variable length format. The encoded bytes format is: length flag(1 byte) + encoded data. The length flag is calculated with following way

flag  Value Range
8  -> [MinInt64, MinInt32)
12 -> [MinInt32, MinInt16)
14 -> [MinInt16, MinInt8)
15 -> [MinInt8, 0)
16 -> 0
17 -> (0, MaxInt8]
18 -> (MaxInt8, MaxInt16]
20 -> (MaxInt16, MaxInt32]
24 -> (MaxInt32, MaxInt64]

EncodeInt appends the encoded value to slice b and returns the appended slice. EncodeInt guarantees that the encoded value is in ascending order for comparison.

func EncodeIntDesc

func EncodeIntDesc(b []byte, v int64) []byte

EncodeIntDesc encodes the int64 value with variable length format. The encoded bytes format is: length flag(1 byte) + encoded data. The length flag is calculated with following way

flag  Value Range
24 -> [MinInt64, MinInt32)
20 -> [MinInt32, MinInt16)
18 -> [MinInt16, MinInt8)
17 -> [MinInt8, 0)
16 -> 0
15 -> (0, MaxInt8]
14 -> (MaxInt8, MaxInt16]
12 -> (MaxInt16, MaxInt32]
8  -> (MaxInt32, MaxInt64]

EncodeIntDesc appends the encoded value to slice b and returns the appended slice. EncodeIntDesc guarantees that the encoded value is in descending order for comparison.

func EncodeKey

func EncodeKey(args ...interface{}) ([]byte, error)

EncodeKey encodes args to a slice which can be sorted lexicographically later. EncodeKey guarantees the encoded slice is in ascending order for comparison. TODO: we may add more test to check its valiadation, especially for null type and multi indices.

func EncodeUint

func EncodeUint(b []byte, v uint64) []byte

EncodeUint encodes the uint64 value with variable length format. The encoded bytes format is: length flag(1 byte) + encoded data. The length flag is calculated with following way:

flag  Value Range
16 -> 0
17 -> (0, MaxUint8]
18 -> (MaxUint8, MaxUint16]
20 -> (MaxUint16, MaxUint32]
24 -> (MaxUint32, MaxUint64]

EncodeUint appends the encoded value to slice b and returns the appended slice. EncodeUint guarantees that the encoded value is in ascending order for comparison.

func EncodeUintDesc

func EncodeUintDesc(b []byte, v uint64) []byte

EncodeUintDesc encodes the int64 value with variable length format. The encoded bytes format is: length flag(1 byte) + encoded data. The length flag is calculated with following way

flag  Value Range
16 -> 0
15 -> (0, MaxUint8]
14 -> (MaxUint8, MaxUint16]
12 -> (MaxUint16, MaxUint32]
8  -> (MaxUint32, MaxUint64]

EncodeUintDesc appends the encoded value to slice b and returns the appended slice. EncodeUintDesc guarantees that the encoded value is in descending order for comparison.

func Float64ToUint64

func Float64ToUint64(f float64) uint64

Float64ToUint64 converts float to a uint for lexicographical comparation.

func StripEnd

func StripEnd(b []byte) ([]byte, error)

StripEnd splits a slice b into two substrings separated by sepKey and returns a slice byte of the previous substrings.

func Uint64ToFloat64

func Uint64ToFloat64(u uint64) float64

Uint64ToFloat64 converts the uint generated with Float64ToUint64 before to a float.

Types

This section is empty.

Jump to

Keyboard shortcuts

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