ExZarr.Codecs.PipelineV3 (ExZarr v1.1.0)
View SourceZarr v3 codec pipeline implementation.
The v3 specification introduces a unified codec pipeline with strict ordering requirements to ensure predictable data transformations.
Codec Ordering
The pipeline must follow this exact order:
Array → Array codecs (zero or more): Transforms that operate on array data, such as transpose, delta encoding, bit rounding, etc.
Array → Bytes codec (exactly one, required): Serializes array data to bytes. The standard codec is "bytes" which handles endianness and packing.
Bytes → Bytes codecs (zero or more): Compression codecs that operate on byte streams, such as gzip, zstd, blosc, etc.
Example Pipeline
codecs = [
# Array → Array: shuffle bytes for better compression
%{name: "shuffle", configuration: %{elementsize: 8}},
# Array → Array: delta encoding
%{name: "delta", configuration: %{dtype: "int64"}},
# Array → Bytes: serialize to bytes (required)
%{name: "bytes", configuration: %{}},
# Bytes → Bytes: compress with gzip
%{name: "gzip", configuration: %{level: 5}}
]
{:ok, pipeline} = ExZarr.Codecs.PipelineV3.parse_codecs(codecs)
{:ok, encoded} = ExZarr.Codecs.PipelineV3.encode(data, pipeline)
{:ok, decoded} = ExZarr.Codecs.PipelineV3.decode(encoded, pipeline)Specification
Zarr v3 Core Specification - Codecs: https://zarr-specs.readthedocs.io/en/latest/v3/core/index.html#codecs
Summary
Functions
Decodes data through the codec pipeline.
Encodes data through the codec pipeline.
Converts v2-style filters and compressor to v3 codec list.
Parses and validates a list of codec specifications.
Types
Functions
@spec decode(binary(), ExZarr.Codecs.PipelineV3.Pipeline.t(), keyword()) :: {:ok, binary()} | {:error, term()}
Decodes data through the codec pipeline.
Applies codecs in reverse order:
- Bytes→Bytes decompression
- Bytes→Array deserialization
- Array→Array reverse transformations
Parameters
data- Binary data to decodepipeline- Validated pipeline structopts- Additional options (shape, dtype, etc.)
Returns
{:ok, decoded_data}- Successfully decoded binary{:error, reason}- Decoding failure
@spec encode(binary(), ExZarr.Codecs.PipelineV3.Pipeline.t(), keyword()) :: {:ok, binary()} | {:error, term()}
Encodes data through the codec pipeline.
Applies codecs in forward order:
- Array→Array transformations
- Array→Bytes serialization
- Bytes→Bytes compression
Parameters
data- Binary data to encodepipeline- Validated pipeline structopts- Additional options (shape, dtype, etc.)
Returns
{:ok, encoded_data}- Successfully encoded binary{:error, reason}- Encoding failure
@spec from_v2(list() | nil, atom()) :: [codec_spec()]
Converts v2-style filters and compressor to v3 codec list.
Parameters
filters- List of v2 filter tuples{:filter_id, opts}compressor- v2 compressor atom (:zlib,:zstd, etc.)
Returns
- List of v3 codec specifications
Examples
iex> filters = [{:shuffle, [elementsize: 8]}, {:delta, [dtype: :int64]}]
iex> codecs = ExZarr.Codecs.PipelineV3.from_v2(filters, :zlib)
iex> length(codecs)
4
@spec parse_codecs([codec_spec()]) :: {:ok, ExZarr.Codecs.PipelineV3.Pipeline.t()} | {:error, term()}
Parses and validates a list of codec specifications.
Validates:
- At least one codec is present
- Exactly one array→bytes codec exists
- Codecs are in the correct order
- All codec names are recognized
Parameters
codec_specs- List of codec specification maps
Returns
{:ok, pipeline}- Validated pipeline struct{:error, reason}- Validation failure
Examples
iex> codecs = [
...> %{name: "bytes"},
...> %{name: "gzip", configuration: %{level: 5}}
...> ]
iex> {:ok, _pipeline} = ExZarr.Codecs.PipelineV3.parse_codecs(codecs)
iex> codecs = [%{name: "gzip"}] # Missing required bytes codec
iex> ExZarr.Codecs.PipelineV3.parse_codecs(codecs)
{:error, :missing_array_to_bytes_codec}