KV (Vik v0.0.1-rc1)

View Source

A modular key-value store for arbitrary, disk-persisted term storage based on :dets.

Usage

Add KV to your supervision tree:

{KV, root: "/tmp"}

Summary

Functions

Returns a specification to start this module under a supervisor.

Deletes all objects from the given table, effectively emptying it.

Deletes the object for the given key from table.

Gets the object for the given key from table.

Same as fetch/2, but returns nil if the given table or key cannot be found.

Lists all objects in the given table.

Puts the given object in table.

Starts the store.

Types

key()

@type key() :: atom()

object()

@type object() :: term()

start_opts()

@type start_opts() :: [{:root, Path.t()}]

table()

@type table() :: atom()

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear(table)

@spec clear(table()) :: :ok | :error

Deletes all objects from the given table, effectively emptying it.

Examples

iex> KV.list(:users)
[%User{id: 1, name: "Robin", age: 18}, %User{id: 2, name: "Gijs", age: 15}]

iex> KV.clear(:users)
iex> KV.list(:users)
[]

delete(table, key)

@spec delete(table(), key()) :: :ok | :error

Deletes the object for the given key from table.

Examples

iex> KV.put(:users, 1, %User{id: 1, name: "Robin", age: 18})
iex> KV.get(:users, 1)
%User{id: 1, name: "Robin", age: 18}

iex> KV.delete(:users, 1)
iex> KV.get(:users, 1)
nil

fetch(table, key)

@spec fetch(table(), key()) :: {:ok, object()} | :error

Gets the object for the given key from table.

Examples

iex> KV.get(:users, 1)
{:ok, %User{id: 1, name: "Robin", age: 18}}

iex> KV.get(:users, 3)
:error

iex> KV.get(:non_existant, 3)
:error

get(table, key)

@spec get(table(), key()) :: object() | nil

Same as fetch/2, but returns nil if the given table or key cannot be found.

Examples

iex> KV.get(:users, 1)
%User{id: 1, name: "Robin", age: 18}

iex> KV.get(:users, 3)
nil

iex> KV.get(:non_existant, 3)
nil

list(table)

@spec list(table()) :: [object()]

Lists all objects in the given table.

Examples

iex> KV.list(:users)
[%User{id: 1, name: "Robin", age: 18}, %User{id: 2, name: "Gijs", age: 15}]

put(table, key, object)

@spec put(table(), key(), object()) :: :ok | :error

Puts the given object in table.

If the table does not exist yet, it is created on-demand.

Examples

iex> KV.put(:users, 1, %User{id: 1, name: "Robin", age: 18})
iex> KV.put(:users, 2, %User{id: 2, name: "Gijs", age: 15})
:ok

start_link(opts)

@spec start_link(start_opts()) :: :ok

Starts the store.