View Source Haystack.Storage behaviour (Haystack v0.1.0)

A module and behaviour for storing data.

This module acts as a convenient way to delegate to the storage implementation within Haystack.

This has been created as a behaviour to make it easy to provide your own storage implementation. By default Haystack provides a memory storage implementation, which uses a map to store data.

Link to this section Summary

Callbacks

Return the count of items in storage.

Delete an item from storage.

Fetch an item from storage.

Fetch an item from storage.

Insert an item into storage

Create a new storage.

Serialize the storage.

Update an item in storage.

Update an item in storage.

Upsert an item in storage.

Functions

Return the count of items in storage.

Delete an item from storage.

Deserialize the storage.

Fetch an item from storage.

Fetch an item from storage.

Insert an item into storage

Serialize the storage.

Update an item in storage.

Update an item in storage.

Upsert an item in storage.

Link to this section Types

Link to this section Callbacks

@callback count(t()) :: integer()

Return the count of items in storage.

@callback delete(t(), k()) :: t()

Delete an item from storage.

@callback fetch(t(), k()) :: {:ok, v()} | {:error, Haystack.Storage.NotFoundError.t()}

Fetch an item from storage.

@callback fetch!(t(), k()) :: v()

Fetch an item from storage.

@callback insert(t(), k(), v()) :: t()

Insert an item into storage

@callback new(opts()) :: t()

Create a new storage.

@callback serialize(t()) :: binary()

Serialize the storage.

@callback update(t(), k(), function()) :: {:ok, t()}

Update an item in storage.

@callback update!(t(), k(), function()) :: t()

Update an item in storage.

Link to this callback

upsert(t, k, v, function)

View Source
@callback upsert(t(), k(), v(), function()) :: t()

Upsert an item in storage.

Link to this section Functions

@spec count(t()) :: integer()

Return the count of items in storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> Storage.count(storage)
1
@spec delete(t(), k()) :: t()

Delete an item from storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> storage = Storage.delete(storage, :name)
iex> Storage.fetch(storage, :name)
{:error, %Storage.NotFoundError{message: "Not found"}}

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> storage = Storage.delete(storage, :name)
iex> Storage.fetch(storage, :name)
{:error, %Storage.NotFoundError{message: "Not found"}}
@spec deserialize(binary()) :: t()

Deserialize the storage.

examples

Examples

iex> binary = Storage.serialize(Storage.Map.new())
iex> Storage.deserialize(binary)
@spec fetch(t(), k()) :: {:ok, v()} | {:error, Haystack.Storage.NotFoundError.t()}

Fetch an item from storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> Storage.fetch(storage, :name)
{:error, %Storage.NotFoundError{message: "Not found"}}

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> Storage.fetch(storage, :name)
{:ok, "Haystack"}
@spec fetch!(t(), k()) :: v()

Fetch an item from storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> Storage.fetch!(storage, :name)
"Haystack"
@spec insert(t(), k(), v()) :: t()

Insert an item into storage

examples

Examples

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> Storage.fetch!(storage, :name)
"Haystack"
@spec serialize(t()) :: binary()

Serialize the storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> Storage.serialize(storage)
@spec update(t(), k(), function()) :: {:ok, t()}

Update an item in storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> Storage.update(storage, :name, &String.upcase/1)
{:error, %Storage.NotFoundError{message: "Not found"}}

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> {:ok, storage} = Storage.update(storage, :name, &String.upcase/1)
iex> Storage.fetch!(storage, :name)
"HAYSTACK"
@spec update!(t(), k(), function()) :: t()

Update an item in storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> storage = Storage.update!(storage, :name, &String.upcase/1)
iex> Storage.fetch!(storage, :name)
"HAYSTACK"
Link to this function

upsert(storage, k, v, f)

View Source
@spec upsert(t(), k(), v(), function()) :: t()

Upsert an item in storage.

examples

Examples

iex> storage = Storage.Map.new()
iex> storage = Storage.upsert(storage, :name, "HAYSTACK", &String.upcase/1)
iex> Storage.fetch!(storage, :name)
"HAYSTACK"

iex> storage = Storage.Map.new()
iex> storage = Storage.insert(storage, :name, "Haystack")
iex> storage = Storage.upsert(storage, :name, "HAYSTACK", &String.upcase/1)
iex> Storage.fetch!(storage, :name)
"HAYSTACK"