Nebulex.Adapters.Mnesia.Table (nebulex_mnesia_adapter v2.6.5)

View Source

This module provides basic operations for interacting with Mnesia tables.

Summary

Functions

Deletes an entry from the specified Mnesia table by key.

Retrieves the first key from the specified Mnesia table.

Retrieves the next key in the specified Mnesia table after the given key.

Reads an entry from the specified Mnesia table by key.

Selects entries from the specified Mnesia table based on given options.

Streams entries from the specified Mnesia table based on given options.

Wraps a function in a Mnesia transaction.

Writes a key-value pair to the specified Mnesia table.

Functions

delete(table, key)

@spec delete(atom(), term()) :: :ok

Deletes an entry from the specified Mnesia table by key.

Parameters

  • table: The name of the Mnesia table (atom).
  • key: The key to delete (term).

Returns

  • :ok: If the delete operation is successful.

Examples

iex> Nebulex.Adapters.Mnesia.Table.delete(:table, :key)
:ok

first(table)

@spec first(atom()) :: {:ok, term()} | {:error, :not_found}

Retrieves the first key from the specified Mnesia table.

Parameters

  • table: The name of the Mnesia table (atom).

Returns

  • {:ok, key}: If the table has entries and the first key is found.
  • {:error, :not_found}: If the table is empty.

Examples

iex> Nebulex.Adapters.Mnesia.Table.first(:table)
{:ok, :first_key}

iex> Nebulex.Adapters.Mnesia.Table.first(:empty_table)
{:error, :not_found}

next(table, key)

@spec next(atom(), term()) :: {:ok, term()} | {:error, :not_found}

Retrieves the next key in the specified Mnesia table after the given key.

Parameters

  • table: The name of the Mnesia table (atom).
  • key: The current key (term).

Returns

  • {:ok, next_key}: If the next key is found.
  • {:error, :not_found}: If there is no next key (end of table).

Examples

iex> Nebulex.Adapters.Mnesia.Table.next(:table, :current_key)
{:ok, :next_key}

iex> Nebulex.Adapters.Mnesia.Table.next(:table, :last_key)
{:error, :not_found}

read(table, key)

@spec read(atom(), term()) :: {:ok, tuple()} | {:error, :not_found}

Reads an entry from the specified Mnesia table by key.

Parameters

  • table: The name of the Mnesia table (atom).
  • key: The key to look up (term).

Returns

  • {:ok, entry}: If the entry is found.
  • {:error, :not_found}: If the entry is not found.

Examples

iex> Nebulex.Adapters.Mnesia.Table.read(:table, :key)
{:ok, {:table, :key, "value", 1759420681791, :infinity}}

iex> Nebulex.Adapters.Mnesia.Table.read(:table, :unknown_key)
{:error, :not_found}

select(table, opts \\ [])

@spec select(
  atom(),
  keyword()
) :: [term()]

Selects entries from the specified Mnesia table based on given options.

Parameters

  • table: The name of the Mnesia table (atom).
  • opts: A keyword list of options.
    • :guards - A list of guard conditions (default: []).
    • :return - A list specifying which attributes to return (default: [:"$1"]).

Returns

  • A list of selected entries.

Examples

iex> guards = [:"$2" > 10]
iex> return = [:"$1", :"$2"]
iex> Nebulex.Adapters.Mnesia.Table.select(:table, guards: guards, return: return)
[{:key1, 15}, {:key2, 20}]

iex> Nebulex.Adapters.Mnesia.Table.select(:table)
[:key1, :key2, :key3]

stream(table, opts)

@spec stream(
  atom(),
  keyword()
) :: Enumerable.t()

Streams entries from the specified Mnesia table based on given options.

Parameters

  • table: The name of the Mnesia table (atom).
  • opts: A keyword list of options (same as in select/2).

Returns

  • A stream of selected entries.

Examples

iex> opts = [return: :key]
iex> Nebulex.Adapters.Mnesia.Table.stream(:table, opts) |> Enum.to_list()
[:key1, :key2, :key3]

iex> opts = [return: :value]
iex> Nebulex.Adapters.Mnesia.Table.stream(:table, opts) |> Enum.to_list()
[value1, value2, value3]

iex> opts = [return: {:key, :value}]
iex> Nebulex.Adapters.Mnesia.Table.stream(:table, opts) |> Enum.to_list()
[{:key1, value1}, {:key2, value2}, {:key3, value3}]

transaction(fun)

@spec transaction((-> any())) :: any() | {:error, term()}

Wraps a function in a Mnesia transaction.

Parameters

  • fun: The function to execute within the transaction (arity 0).

Returns

  • The result of the function if the transaction is successful.
  • {:error, reason} if the transaction is aborted.

Examples

iex> Nebulex.Adapters.Mnesia.Table.transaction(fn -> :ok end)
:ok

iex> Nebulex.Adapters.Mnesia.Table.transaction(fn -> Mnesia.abort(:some_reason) end)
{:error, :some_reason}

write(table, key, value, touched, ttl)

@spec write(atom(), term(), term(), integer(), term() | integer()) :: :ok

Writes a key-value pair to the specified Mnesia table.

Parameters

  • table: The name of the Mnesia table (atom).
  • key: The key to write (term).
  • value: The value to write (term).
  • touched: The timestamp when the entry was last touched (integer).
  • ttl: The time-to-live for the entry (term or integer).

Returns

  • :ok: If the write operation is successful.

Examples

iex> Nebulex.Adapters.Mnesia.Table.write(:table, :key, "value", 1759420681791, :infinity)
:ok