Mnemonix v0.8.0 Mnemonix.Stores.Map

A Mnemonix.Store that uses a map to store state.

Intended to be an example for implementing the Mnemonix.Store.Behaviour and experimenting with the Mnemonix API rather than production usage.

It intentionally doesn’t override any optional callback with native versions so that the default implementations can be easily tested.

iex> {:ok, store} = Mnemonix.Stores.Map.start_link
iex> Mnemonix.put(store, "foo", "bar")
iex> Mnemonix.get(store, "foo")
"bar"
iex> Mnemonix.delete(store, "foo")
iex> Mnemonix.get(store, "foo")
nil

This store supports the functions in Mnemonix.Features.Enumerable.

Summary

Functions

Callback implementation for c:Mnemonix.Store.Behaviours.Map.delete/2

Callback implementation for c:Mnemonix.Store.Behaviours.Map.fetch/2

Callback implementation for c:Mnemonix.Store.Behaviours.Map.put/3

Constructs a map to store data using provided opts

Starts a new Mnemonix.Store.Server using the Mnemonix.Stores.Map module with options

Starts a new Mnemonix.Store.Server using Mnemonix.Stores.Map with store and server options

Functions

delete(store, key)
delete(Mnemonix.Store.t, Mnemonix.key) ::
  {:ok, Mnemonix.Store.t} |
  Mnemonix.Store.Behaviour.exception

Callback implementation for c:Mnemonix.Store.Behaviours.Map.delete/2.

fetch(store, key)
fetch(Mnemonix.Store.t, Mnemonix.key) ::
  {:ok, Mnemonix.Store.t, {:ok, Mnemonix.value} | :error} |
  Mnemonix.Store.Behaviour.exception

Callback implementation for c:Mnemonix.Store.Behaviours.Map.fetch/2.

put(store, key, value)
put(Mnemonix.Store.t, Mnemonix.key, Mnemonix.Store.value) ::
  {:ok, Mnemonix.Store.t} |
  Mnemonix.Store.Behaviour.exception

Callback implementation for c:Mnemonix.Store.Behaviours.Map.put/3.

setup(opts)
setup(Mnemonix.Store.options) ::
  {:ok, state :: term} |
  {:stop, reason :: any}

Constructs a map to store data using provided opts.

Options

  • initial: An existing map to start the store with.

    • Default: %{}
start_link(options \\ [])

Starts a new Mnemonix.Store.Server using the Mnemonix.Stores.Map module with options.

The options are the same as described in Mnemonix.Store.Server.start_link/2. The :store options are used in config/1 to start the store; the :server options are passed directly to GenServer.start_link/2.

The returned GenServer.server/0 reference can be used as the primary argument to the Mnemonix API.

Examples

iex> {:ok, store} = Mnemonix.Stores.Map.start_link()
iex> Mnemonix.put(store, "foo", "bar")
iex> Mnemonix.get(store, "foo")
"bar"

iex> {:ok, _store} = Mnemonix.Stores.Map.start_link(server: [name: My.Mnemonix.Stores.Map])
iex> Mnemonix.put(My.Mnemonix.Stores.Map, "foo", "bar")
iex> Mnemonix.get(My.Mnemonix.Stores.Map, "foo")
"bar"
start_link(store, server)

Starts a new Mnemonix.Store.Server using Mnemonix.Stores.Map with store and server options.

The options are the same as described in Mnemonix.Store.Server.start_link/3. The store options are used in config/1 to start the store; the server options are passed directly to GenServer.start_link/2.

The returned GenServer.server/0 reference can be used as the primary argument to the Mnemonix API.

Examples

iex> {:ok, store} = Mnemonix.Stores.Map.start_link([], [])
iex> Mnemonix.put(store, "foo", "bar")
iex> Mnemonix.get(store, "foo")
"bar"

iex> {:ok, _store} = Mnemonix.Stores.Map.start_link([], [name: My.Mnemonix.Stores.Map])
iex> Mnemonix.put(My.Mnemonix.Stores.Map, "foo", "bar")
iex> Mnemonix.get(My.Mnemonix.Stores.Map, "foo")
"bar"