ChDriver.Protocol.Varint (ch_driver v0.1.0)

Copy Markdown

LEB128-style unsigned varint encoding used throughout ClickHouse's native protocol for packet-type discriminants, lengths, revisions, and other small-to-medium unsigned integers.

Each byte contributes 7 bits of the value, low-order bits first. The high bit of a byte is a continuation flag: 1 means "more bytes follow", 0 means "this is the last byte".

Also provides ClickHouse's length-prefixed string encoding: a varint byte length followed by that many raw bytes (no encoding assumptions, no trailing NUL).

Summary

Functions

Decodes a varint from the front of binary.

Decodes a ClickHouse length-prefixed string from the front of binary.

Encodes a non-negative integer as a varint, returning a binary.

Encodes a binary as a ClickHouse length-prefixed string: a varint byte length followed by the raw bytes.

Functions

decode(binary)

@spec decode(binary()) :: {:ok, non_neg_integer(), binary()} | {:incomplete, binary()}

Decodes a varint from the front of binary.

Returns {:ok, value, rest} on success, or {:incomplete, binary} if the buffer doesn't yet contain a complete varint (no byte with the continuation bit unset).

decode_string(binary)

@spec decode_string(binary()) :: {:ok, binary(), binary()} | {:incomplete, binary()}

Decodes a ClickHouse length-prefixed string from the front of binary.

Returns {:ok, string, rest} on success, or {:incomplete, binary} if the buffer doesn't yet contain the full length prefix and/or string bytes.

encode(value)

@spec encode(non_neg_integer()) :: binary()

Encodes a non-negative integer as a varint, returning a binary.

encode_string(binary)

@spec encode_string(binary()) :: iodata()

Encodes a binary as a ClickHouse length-prefixed string: a varint byte length followed by the raw bytes.