Forge.Storage behaviour (Forge v0.1.1)
View SourceBehaviour for sample storage backends.
Storage backends persist samples after pipeline processing. They can be in-memory, disk-based, database-backed, or stream to external systems.
Lifecycle
init/1- Initialize storage with configurationstore/2- Store a batch of samplesretrieve/2- Retrieve a sample by ID (optional)list/2- List samples with filters (optional)cleanup/1- Clean up resources
Examples
defmodule MyStorage do
@behaviour Forge.Storage
def init(opts) do
path = Keyword.fetch!(opts, :path)
File.mkdir_p!(path)
{:ok, %{path: path}}
end
def store(samples, %{path: path} = state) do
Enum.each(samples, fn sample ->
file = Path.join(path, "#{sample.id}.json")
File.write!(file, Jason.encode!(sample))
end)
{:ok, state}
end
def cleanup(_state), do: :ok
end
Summary
Callbacks
Clean up any resources held by the storage backend.
Initialize the storage backend with configuration options.
List samples matching the given filters.
Retrieve a sample by ID.
Store a batch of samples.
Callbacks
@callback cleanup(state :: any()) :: :ok
Clean up any resources held by the storage backend.
Initialize the storage backend with configuration options.
Returns {:ok, state} with initial state or {:error, reason} on failure.
@callback list(filters :: keyword(), state :: any()) :: {:ok, [Forge.Sample.t()], state :: any()} | {:error, reason :: any()}
List samples matching the given filters.
Filters are backend-specific keyword lists. Common filters include:
pipeline: atom()- Filter by pipeline namestatus: atom()- Filter by statusafter: DateTime.t()- Samples created after timestampbefore: DateTime.t()- Samples created before timestamp
Returns {:ok, samples, new_state} or {:error, reason}.
This callback is optional - not all storage backends need to support listing.
@callback retrieve(id :: String.t(), state :: any()) :: {:ok, Forge.Sample.t(), state :: any()} | {:error, reason :: any()}
Retrieve a sample by ID.
Returns {:ok, sample, new_state} if found, {:error, :not_found} if not found,
or {:error, reason} on other failures.
This callback is optional - not all storage backends need to support retrieval.
@callback store(samples :: [Forge.Sample.t()], state :: any()) :: {:ok, state :: any()} | {:error, reason :: any()}
Store a batch of samples.
Returns {:ok, new_state} on success or {:error, reason} on failure.