defmodule Todo.Database do use Supervisor @db_folder "./persist" @pool_size 3 def start_link do Supervisor.start_link(__MODULE__, nil) end @impl Supervisor def init(_) do IO.puts("Starting #{__MODULE__}.") File.mkdir_p!(@db_folder) children = Enum.map(1..@pool_size, &worker_spec/1) Supervisor.init(children, strategy: :one_for_one) end defp worker_spec(worker_id) do default_worker_spec = {Todo.DatabaseWorker, {@db_folder, worker_id}} Supervisor.child_spec(default_worker_spec, id: worker_id) end def child_spec(_) do %{ id: __MODULE__, start: {__MODULE__, :start_link, []}, type: :supervisor } end def store(key, data) do key |> choose_worker() |> Todo.DatabaseWorker.store(key, data) end def get(key) do key |> choose_worker() |> Todo.DatabaseWorker.get(key) end defp choose_worker(key) do :erlang.phash2(key, @pool_size) end end