WardrikStore.Adapter behaviour (wardrik_store v0.1.0-rc.1)

Copy Markdown

Decides where a store keeps its keys: in memory, on disk or in Postgres. Callers use WardrikStore, not the adapter.

An adapter also has a new/1 that builds the store:

defmodule MyApp.Store do
  @behaviour WardrikStore.Adapter

  def new(opts), do: WardrikStore.new(__MODULE__, Keyword.fetch!(opts, :name))

  # child_spec/1, put/4, lookup/2, delete/3 and keys/2 follow
end

Each callback gets that config, and a full path that already holds the store's scopes.

lookup_many/2 and entries/2 are optional. Implement them when reading many keys at once is cheaper than one at a time, as in a database. Otherwise WardrikStore reads keys one at a time.

Summary

Types

The config the adapter's new/1 built. Each callback gets it.

Callbacks

Returns the child that runs the adapter.

Removes path. Returns :ok if it isn't there. Takes :if_vsn as WardrikStore.delete/3 does.

Returns {path, value, vsn} for every path that starts with prefix.

Returns every path that starts with prefix.

Returns {value, vsn} for path, or nil.

Returns {path, value, vsn} for each of paths. Paths that aren't there are left out.

Writes value under path. Takes :if_vsn as WardrikStore.put/4 does.

Types

config()

@type config() :: term()

The config the adapter's new/1 built. Each callback gets it.

Callbacks

child_spec(config)

@callback child_spec(config()) :: Supervisor.child_spec()

Returns the child that runs the adapter.

delete(config, path, keyword)

@callback delete(config(), path :: String.t(), keyword()) ::
  :ok | {:error, :conflict | term()}

Removes path. Returns :ok if it isn't there. Takes :if_vsn as WardrikStore.delete/3 does.

entries(config, prefix)

(optional)
@callback entries(config(), prefix :: String.t()) :: [
  {String.t(), term(), WardrikStore.vsn()}
]

Returns {path, value, vsn} for every path that starts with prefix.

keys(config, prefix)

@callback keys(config(), prefix :: String.t()) :: [String.t()]

Returns every path that starts with prefix.

lookup(config, path)

@callback lookup(config(), path :: String.t()) :: {term(), WardrikStore.vsn()} | nil

Returns {value, vsn} for path, or nil.

lookup_many(config, paths)

(optional)
@callback lookup_many(config(), paths :: [String.t()]) :: [
  {String.t(), term(), WardrikStore.vsn()}
]

Returns {path, value, vsn} for each of paths. Paths that aren't there are left out.

put(config, path, value, keyword)

@callback put(config(), path :: String.t(), value :: term(), keyword()) ::
  {:ok, WardrikStore.vsn() | nil} | {:error, :conflict | term()}

Writes value under path. Takes :if_vsn as WardrikStore.put/4 does.