merkle_patricia_tree v0.2.8 MerklePatriciaTree.DB behaviour

Defines a general key-value storage to back and persist out Merkle Patricia Trie. This is generally LevelDB in the community, but for testing, we'll generally use :ets.

We define a callback that can be implemented by a number of potential backends.

Link to this section Summary

Functions

Retrieves a key from the database.

Retrieves a key from the database, but raises if that key does not exist.

Stores a key in the database.

Link to this section Types

Link to this type

db()

db() :: {t(), db_ref()}
Link to this type

db_name()

db_name() :: any()
Link to this type

db_ref()

db_ref() :: any()
Link to this type

value()

value() :: binary()

Link to this section Functions

Link to this function

get(db, key)

get(db(), MerklePatriciaTree.Trie.key()) :: {:ok, value()} | :not_found

Retrieves a key from the database.

Examples

iex> db = MerklePatriciaTree.Test.random_ets_db()
iex> MerklePatriciaTree.DB.get(db, "name")
:not_found

iex> db = MerklePatriciaTree.Test.random_ets_db()
iex> MerklePatriciaTree.DB.put!(db, "name", "bob")
iex> MerklePatriciaTree.DB.get(db, "name")
{:ok, "bob"}

Retrieves a key from the database, but raises if that key does not exist.

Examples

iex> db = MerklePatriciaTree.Test.random_ets_db()
iex> MerklePatriciaTree.DB.get!(db, "name")
** (MerklePatriciaTree.DB.KeyNotFoundError) cannot find key `name`

iex> db = MerklePatriciaTree.Test.random_ets_db()
iex> MerklePatriciaTree.DB.put!(db, "name", "bob")
iex> MerklePatriciaTree.DB.get!(db, "name")
"bob"
Link to this function

put!(db, key, value)

put!(db(), MerklePatriciaTree.Trie.key(), value()) :: :ok

Stores a key in the database.

Examples

## Examples

iex> db = MerklePatriciaTree.Test.random_ets_db()
iex> MerklePatriciaTree.DB.put!(db, "name", "bob")
iex> MerklePatriciaTree.DB.get(db, "name")
{:ok, "bob"}
iex> MerklePatriciaTree.DB.put!(db, "name", "tom")
iex> MerklePatriciaTree.DB.get(db, "name")
{:ok, "tom"}

Link to this section Callbacks

Link to this callback

get(db_ref, arg2)

get(db_ref(), MerklePatriciaTree.Trie.key()) :: {:ok, value()} | :not_found
Link to this callback

init(db_name)

init(db_name()) :: db()
Link to this callback

put!(db_ref, arg2, value)