Documentation
¶
Overview ¶
Package binfmt defines the binary format used for encoding and decoding rows and their data to disk as well as over the network.
Index ¶
Examples ¶
Constants ¶
const ( // SetRowIdent is the row identifier for a set row. SetRowIdent = 'S' // DeleteRowIdent is the row identifier for a delete row. DeleteRowIdent = 'D' )
Variables ¶
var ( // ErrDecodeIntInvalid is returned when an int cannot be decoded due to invalid reader data. ErrDecodeIntInvalid = errors.New("binfmt: decode int failed due to invalid reader data") // ErrDecodeIntOverflow is returned when a decoded int is larger than int64. ErrDecodeIntOverflow = errors.New("binfmt: decoded int does not fit in int64") )
var ( // ErrDecodeUintInvalid is returned when a uint cannot be decoded due to invalid reader data. ErrDecodeUintInvalid = errors.New("binfmt: decode uint failed due to invalid reader data") // ErrDecodeUintOverflow is returned when a decoded uint is larger uint64. ErrDecodeUintOverflow = errors.New("binfmt: decoded uint does not fit in uint64") )
var ErrRowIdentInvalid = errors.New("binfmt: row has invalid row identifier")
ErrRowIdentInvalid is returned when an invalid row identifier has been decoded.
var ErrRowValueIdentInvalid = errors.New("binfmt: row has invalid value type identifier")
ErrRowValueIdentInvalid is returned when a row is decoded that has an invalid value type identifier.
var ( // ErrStringInvalid is returned when a string is not a valid UTF-8 string. ErrStringInvalid = errors.New("binfmt: string contains invalid data") )
Functions ¶
This section is empty.
Types ¶
type Binary ¶
type Binary struct{}
Binary implements ValueType[[]byte] and is able to encode and decode []byte values. Keep in sync with type alias in kvega package.
func (Binary) Decode ¶
Decode decodes arbitrary bytes from the decoders reader. First by reading the length using Int, followed by reading the actual bytes. If createCopy is false the returned byte slice is only valid until the next call made to the Decoder.
func (Binary) Encode ¶
Encode encodes value by adding its length to the encoders current buffer and then moves the current buffer to the writes list, followed by adding value to the writes list without a copy of value, it finally creates a new empty current buffer for further writes. value may contain any byte including NUL. Encode returns nil error.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder is used to efficiently read and decode row data from a reader.
Example ¶
package main
import (
"bytes"
"fmt"
"github.com/larzconwell/kvega/binfmt"
)
func main() {
var buf bytes.Buffer
buf.WriteByte(binfmt.SetRowIdent)
buf.WriteByte(16)
buf.WriteString("greeting")
buf.WriteByte('S')
buf.WriteByte(12)
buf.WriteString("你好")
dec := binfmt.NewDecoder(&buf)
ident, key, valueDecoder, _, err := dec.Row()
if err != nil {
fmt.Println("failed to decode set row:", err)
return
}
value, _, err := valueDecoder.Get[binfmt.String]()
if err != nil {
fmt.Println("failed to decode value:", err)
return
}
fmt.Println("row identifier:", string(ident))
fmt.Println("key:", key)
fmt.Println("value:", value)
}
Output: row identifier: S key: greeting value: 你好
func NewDecoder ¶
NewDecoder creates a decoder that reads from reader.
func (*Decoder) Row ¶
Row decodes a row identifier and key from the decodes reader and returns it along with a row value decoder if the row has a value associated with it.
io.EOF is returned if the end of the decoders reader has been reached.
ErrRowIdentInvalid is returned if the row identifier is invalid, the read row identifier is then unread to enable the caller to inspect, retry, etc.
type Encoder ¶
type Encoder struct {
// Buffer contains the current buffer that may be written to.
Buffer *bytes.Buffer
// contains filtered or unexported fields
}
Encoder is used to efficiently encode and write row data with minimal copying.
Example ¶
package main
import (
"bytes"
"fmt"
"github.com/larzconwell/kvega/binfmt"
)
func main() {
var buf bytes.Buffer
enc := binfmt.NewEncoder()
err := enc.SetRow[binfmt.String]("greeting", "你好")
if err != nil {
fmt.Println("failed to encode set row:", err)
return
}
_, err = enc.WriteTo(&buf)
if err != nil {
fmt.Println("failed to write set row:", err)
return
}
fmt.Println("row identifier:", string(buf.Bytes()[0]))
fmt.Println("key length:", buf.Bytes()[1])
fmt.Println("key:", string(buf.Bytes()[2:10]))
fmt.Println("value type identifier:", string(buf.Bytes()[10]))
fmt.Println("value length:", buf.Bytes()[11])
fmt.Println("value:", string(buf.Bytes()[12:]))
}
Output: row identifier: S key length: 16 key: greeting value type identifier: S value length: 12 value: 你好
type Int ¶
type Int struct{}
Int implements ValueType[int64] and is able to encode and decode int64 values. Keep in sync with type alias in kvega package.
func (Int) Decode ¶
Decode decodes a zigzgag encoded value stored in a big-endian variable length integer encoded value that's read from the decoders reader. Up to 10 bytes are read from the decoders reader.
ErrDecodeIntInvalid is returned if the end of the encoded int is not encountered within 10 bytes. ErrDecodeIntOverflow is returned if the decoded value would exceed int64.
func (Int) Encode ¶
Encode encodes value using zigzag encoding along with big-endian variable length integer encoding and adds it to the encoder. 1 to 10 bytes may be added to the encoder depending on the value. Encode returns nil error.
The first step is zigzag encoding which is a method to represent a signed integer as an unsigned integer by evenly spreading positive and negative values across the unsigned integer range. Negative values become double their absolute value minus one. Positive values meanwhile become double their absolute value.
After the int64 has been converted to a uint64, big-endian variable length integer encoding is done, context to which can be gathered by reading Uint.Encode.
type RowValueDecoder ¶
type RowValueDecoder struct {
HasValue bool
// contains filtered or unexported fields
}
RowValueDecoder is used by Decoder.Row to enable decoding a rows value, or skip it depending on what the caller wants to do once they've received the row identifier and key.
func (RowValueDecoder) Get ¶
func (rvd RowValueDecoder) Get[VT ValueType[T], T any]() (T, int, error)
Get decodes a value from the decoders reader using the provided ValueType parameter.
ValueTypeMismatchError is returned if the value type identifier s not valid for the provided ValueType and the value type identifier is then unread to enable the caller to inspect, retry, etc.
func (RowValueDecoder) Skip ¶
func (rvd RowValueDecoder) Skip() (int, error)
Skip decodes a value from the decoders reader and discards it.
ErrRowValueIdentInvalid is returned if the value type identifier is not valid, the read type value identifier is then unread to enable the caller to inspect, retry, etc.
type String ¶
type String struct{}
String implements ValueType[string] and is able to encode and decode string values. Keep in sync with type alias in kvega package.
func (String) Decode ¶
Decode decodes a string from the decoders reader. First by reading the length using Int.Decode, followed by reading the bytes for the string.
ErrStringInvalid is returned when the decoded bytes are not valid UTF-8.
func (String) Encode ¶
Encode encodes value by adding its length to the encoders current buffer and then moves the current buffer to the writes list, followed by adding value to the writes list, it finally creates a new empty current buffer for further writes.
ErrStringInvalid is returned when value is not valid UTF-8.
type Uint ¶
type Uint struct{}
Uint implements ValueType[uint64] and is able to encode and decode uint64 values. Keep in sync with type alias in kvega package.
func (Uint) Decode ¶
Decode decodes a big-endian variable length integer encoded value from the decoders reader. It will read bytes until it detects the end of the encoded uint, up to 10 bytes.
ErrDecodeUintInvalid is returned if the end of the encoded uint is not encountered within 10 bytes. ErrDecodeUintOverflow is returned if the decoded value would exceed uint64.
func (Uint) Encode ¶
Encode encodes value using big-endian variable length integer encoding and adds it to the encoder. 1 to 10 bytes may be added to the encoder depending on the value. Encode returns nil error.
Variable length integer encoding works by splitting value into 7 bit chunks, where the 7 bits of value data are located in the least significant portion of a byte, the most significant bit in the chunk being reserved as a continuation bit to signal that more bytes should be read. Encode splits the value up and orders the resulting bytes in big-endian order.
Here is an example of how a uint would be encoded. Given a value 0x108a, the following is a mapping between the values binary representation and the encoded bytes binary representation.
[___10000] [10001010] // Binary representation of value 0x108a. [__100001] [_0001010] // Splitting value into 7 bit chunks. [10100001] [00001010] // Adding continuation bits to the 7 bit chunks.
type ValueType ¶
type ValueType[T any] interface { Ident() byte Encode(enc *Encoder, value T) error Decode(dec *Decoder, createCopy bool) (T, int, error) }
ValueType describes a type that can encode and decode values of type T. Keep in sync with type alias in kvega package.
type ValueTypeMismatchError ¶
ValueTypeMismatchError is returned when getting a value that has a different value type than expected.
func (ValueTypeMismatchError) Error ¶
func (vtme ValueTypeMismatchError) Error() string