Forge.Source behaviour (Forge v0.1.1)
View SourceBehaviour for sample sources.
Sources provide samples to pipelines. They can be static lists, generators, database queries, or any other data provider.
Lifecycle
init/1- Initialize the source with configuration optionsfetch/1- Retrieve the next batch of samples (called repeatedly)cleanup/1- Clean up resources when done
Examples
defmodule MySource do
@behaviour Forge.Source
def init(opts) do
{:ok, %{data: Keyword.fetch!(opts, :data), index: 0}}
end
def fetch(%{data: data, index: index} = state) do
if index < length(data) do
sample = Enum.at(data, index)
{:ok, [sample], %{state | index: index + 1}}
else
{:done, state}
end
end
def cleanup(_state), do: :ok
end
Summary
Callbacks
Clean up any resources held by the source.
Fetch the next batch of samples.
Initialize the source with the given options.
Callbacks
@callback cleanup(state :: any()) :: :ok
Clean up any resources held by the source.
@callback fetch(state :: any()) :: {:ok, samples :: [map()], new_state :: any()} | {:done, state :: any()}
Fetch the next batch of samples.
Returns:
{:ok, samples, new_state}- Batch of sample data maps with updated state{:done, state}- No more samples available
Initialize the source with the given options.
Returns {:ok, state} with initial state or {:error, reason} on failure.