XEts.KV (x_ets v0.2.0)

A module for working with ETS tables.

This module is a wrapper around the :shards module which is provides a high level of concurrency and scalability without the need for locks.

Supports the Access protocol for getting and setting values.

Link to this section Summary

Functions

Delete an item from the table.

Delete all items from the table.

Get an item from the table.

Get an item from the table.

Get and update an item in the table.

Get information about the table.

Get information about a key in the table.

Delete an item or items from the table.

Test if key is a member.

Pop an item from the table.

Put one or more items into the table.

Put an item into the table.

Put one or more new items into the table.

Put an item into the table if it doesn't exist.

Convert the table to a list.

Link to this section Types

Specs

t() :: %XEts.KV{tab: atom()}

Specs

tab() :: atom() | :ets.tid()

Link to this section Functions

Specs

delete(t(), any()) :: t()

Delete an item from the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(:k, :v) |> XEts.KV.delete(:k) |> XEts.KV.to_list()
[]

Specs

delete_all(t()) :: t()

Delete all items from the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(:k, :v) |> XEts.KV.delete_all() |> XEts.KV.to_list()
[]
Link to this function

fetch(tab, key)

Specs

fetch(t(), any()) :: {:ok, any()} | :error

Get an item from the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(:k, :v) |> XEts.KV.fetch(:k)
{:ok, :v}

iex> XEts.KV.new(:foo, []) |> XEts.KV.fetch(:k)
:error
Link to this function

get(tab, value, default \\ nil)

Specs

get(t(), any(), any()) :: any()

Get an item from the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(:k, :v) |> XEts.KV.get(:k)
:v
Link to this function

get_and_update(tab, key, fun)

Specs

get_and_update(t(), any(), any()) :: any()

Get and update an item in the table.

iex> tab = XEts.KV.new(:foo, []) |> XEts.KV.put(:k, 2)
iex> XEts.KV.get_and_update(tab, :k, & { &1, &1 + 1 })
{2, tab}

Specs

info(t()) :: list()
info(t()) :: list()

Get information about the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.info() |> length()
18

Get information about a key in the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.info(:name)
:foo
Link to this function

match(tab, key, wildcard \\ :"$2", default \\ [])

Specs

match(t(), any(), any(), any()) :: any()

Get an item from the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(:k, :v) |> XEts.KV.get(:k)
:v
Link to this function

match_delete(tab, match_spec)

Specs

match_delete(t() | tab(), term()) :: t() | boolean()

Delete an item or items from the table.

iex> XEts.KV.new(:foo, []) |>
...> XEts.KV.put(:k, :v) |>
...> XEts.KV.match_delete({:_, :_}) |>
...> XEts.KV.to_list()
[]
Link to this function

member?(tab, key)

Specs

member?(t() | tab(), any()) :: boolean()

Test if key is a member.

iex> :foo |> XEts.KV.new([]) |> XEts.KV.put(:x, nil) |> XEts.KV.member?(:x)
true

iex> :bar |> XEts.KV.new([]) |> XEts.KV.member?(:x)
false
Link to this function

new(tab, opts \\ [:named_table, parallel: true, read_concurrency: true, write_concurrency: true])

Specs

new(atom(), list()) :: t() | none()
Link to this function

pop(tab, key, default \\ nil)

Specs

pop(t(), any(), any()) :: {any(), t()}

Pop an item from the table.

iex> tab = XEts.KV.new(:foo, [])
iex> tab |> XEts.KV.put(:k, :v) |> XEts.KV.pop(:k)
{:v, tab}
Link to this function

put(t, item_or_items)

Specs

put(t(), any()) :: t()

Put one or more items into the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put({:k, :v}) |> XEts.KV.to_list()
[{:k, :v}]

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(k: :v, k2: :v2) |> XEts.KV.to_list()
[{:k, :v}, {:k2, :v2}]
Link to this function

put(t, key, value)

Specs

put(t(), any(), any()) :: t()

Put an item into the table.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(:k, :v) |> XEts.KV.to_list()
[{:k, :v}]
Link to this function

put_new(tab, item_or_items)

Specs

put_new(t(), any()) :: t()
put_new(t(), any()) :: t()

Put one or more new items into the table.

iex> XEts.KV.new(:foo, []) |>
...> XEts.KV.put({:k, :v}) |>
...> XEts.KV.put_new(:k, :v2) |>
...> XEts.KV.to_list()
[{:k, :v}]

iex> XEts.KV.new(:foo, [])  |>
...> XEts.KV.put(k: :v, k2: :v2) |>
...> XEts.KV.put_new([k: :v3, k2: :vv]) |>
...>XEts.KV.to_list()
[{:k, :v}, {:k2, :v2}]
Link to this function

put_new(tab, key, value)

Put an item into the table if it doesn't exist.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put_new(:k, :v) |> XEts.KV.to_list()
[{:k, :v}]

Specs

to_list(t()) :: list()

Convert the table to a list.

iex> XEts.KV.new(:foo, []) |> XEts.KV.put(:k, :v) |> XEts.KV.to_list()
[{:k, :v}]