ExZarr.ChunkKey.Encoder behaviour (ExZarr v1.1.0)

View Source

Behavior for custom chunk key encoding schemes.

Implement this behavior to create custom chunk naming schemes beyond the standard v2 (dot-separated) and v3 (slash-separated) formats.

Example

defmodule MyApp.CustomChunkKey do
  @behaviour ExZarr.ChunkKey.Encoder

  @impl true
  def encode(chunk_index, _opts) do
    # Custom encoding: "chunk_0_1_2" instead of "0.1.2"
    indices = Tuple.to_list(chunk_index)
    "chunk_" <> Enum.join(indices, "_")
  end

  @impl true
  def decode(chunk_key, _opts) do
    case String.split(chunk_key, "_") do
      ["chunk" | indices] ->
        tuple = indices |> Enum.map(&String.to_integer/1) |> List.to_tuple()
        {:ok, tuple}
      _ ->
        {:error, :invalid_chunk_key}
    end
  end

  @impl true
  def pattern(_opts) do
    ~r/^chunk_\d+(_\d+)*$/
  end
end

# Register and use
ExZarr.ChunkKey.register_encoder(:custom, MyApp.CustomChunkKey)

# Use in array creation
{:ok, array} = ExZarr.Array.create(
  shape: {100, 100},
  chunk_key_encoding: :custom
)

Summary

Callbacks

Decodes a string key back into a chunk index tuple.

Encodes a chunk index tuple into a string key.

Returns a regex pattern that matches valid chunk keys for this encoding.

Callbacks

decode(chunk_key, opts)

@callback decode(chunk_key :: String.t(), opts :: keyword()) ::
  {:ok, tuple()} | {:error, term()}

Decodes a string key back into a chunk index tuple.

Parameters

  • chunk_key - String representation of chunk key
  • opts - Keyword list of options

Returns

{:ok, chunk_index} or {:error, reason}

encode(chunk_index, opts)

@callback encode(chunk_index :: tuple(), opts :: keyword()) :: String.t()

Encodes a chunk index tuple into a string key.

Parameters

  • chunk_index - Tuple of integers representing chunk position
  • opts - Keyword list of options

Returns

String representation of the chunk key

pattern(opts)

@callback pattern(opts :: keyword()) :: Regex.t()

Returns a regex pattern that matches valid chunk keys for this encoding.

Parameters

  • opts - Keyword list of options

Returns

Regex pattern