ChDriver.Protocol.Block.Compressed (ch_driver v0.2.0)

Copy Markdown

Encodes and decodes ClickHouse's compressed block envelope: a checksum and a method marker wrapped around a block's bytes, LZ4-compressed or plain.

Multiple blocks are concatenated back-to-back with no extra framing — call decode/1 repeatedly, threading the returned rest through, until the buffer is exhausted.

This is what backs the :compression option on ChDriver.start_link/1 and ChDriver.query/4 — you won't normally call encode/2/decode/1 directly. Built on ChDriver.Codec's LZ4/CityHash primitives.

Summary

Functions

Decodes a single block envelope from the front of binary.

Encodes iodata into a single compressed block envelope using method (defaults to :lz4).

Types

method()

@type method() :: :none | :lz4

Functions

decode(binary)

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

Decodes a single block envelope from the front of binary.

Returns:

  • {:ok, decompressed, rest} on success, where rest is any unconsumed bytes following this block (e.g. the start of the next one).
  • {:incomplete, missing_byte_count} if not enough bytes are buffered yet to make progress. missing_byte_count is how many more bytes are needed before calling decode/1 again is worth doing.
  • {:error, reason} if the checksum doesn't match or the method byte is unrecognized.

encode(iodata, method \\ :lz4)

@spec encode(iodata(), method()) :: binary()

Encodes iodata into a single compressed block envelope using method (defaults to :lz4).