View Source ETS.KeyValueSet (ets v0.9.0)

The Key Value Set is an extension of ETS.Set which abstracts the concept of tuple records away, replacing it with the standard concept of key/value. Behind the scenes, the set stores its records as {key, value}.

examples

Examples

iex> {:ok, kvset} = KeyValueSet.new()
iex> KeyValueSet.put(kvset, :my_key, :my_val)
iex> KeyValueSet.get(kvset, :my_key)
{:ok, :my_val}

KeyValueSet implements [Access] behaviour.

examples-1

Examples

iex> set =
...>   KeyValueSet.new!()
...>   |> KeyValueSet.put!(:k1, :v1)
...>   |> KeyValueSet.put!(:k2, :v2)
...>   |> KeyValueSet.put!(:k3, :v3)
iex> get_in(set, [:k1])
:v1
iex> get_in(set, [:z])
nil
iex> with {:v2, set} <-
...>   pop_in(set, [:k2]), do: KeyValueSet.to_list!(set)
[k3: :v3, k1: :v1]
iex> with {nil, set} <- pop_in(set, [:z]), do: KeyValueSet.to_list!(set)
[k3: :v3, k1: :v1]
iex> with {:v1, set} <-
...>     get_and_update_in(set, [:k1], &{&1, :v42}),
...>   do: KeyValueSet.to_list!(set)
[k3: :v3, k1: :v42]
iex> with {:v42, set} <-
...>     get_and_update_in(set, [:k1], fn _ -> :pop end),
...>   do: KeyValueSet.to_list!(set)
[k3: :v3]

Link to this section Summary

Functions

Waits to accept ownership of a table after it is given away. Successful receipt will return {:ok, %{kv_set: kv_set, from: from, gift: gift}} where from is the pid of the previous owner, and gift is any additional metadata sent with the table.

For processes which may receive ownership of a KeyValueSet unexpectedly - either via give_away/3 or by being named the KeyValueSet's heir (see new/1) - the module should include at least one accept clause. For example, if we want a server to inherit KeyValueSets after their previous owner dies

Deletes KeyValueSet. See ETS.Set.delete/1.

Deletes record with specified key in specified Set.

Same as delete/1 but unwraps or raises on error.

Same as delete/2 but unwraps or raises on error.

Deletes all records in specified Set.

Same as delete_all/1 but unwraps or raises on error.

Returns first key in KeyValueSet. See ETS.Set.first/1.

Same as first/1 but unwraps or raises on error.

Returns value for specified key or the provided default (nil if not specified) if no record found.

Same as get/3 but unwraps or raises on error

Returns underlying :ets table reference. See ETS.Set.get_table/1.

Same as get_table/1 but unwraps or raises on error.

Transfers ownership of a KeyValueSet to another process.

Same as give_away/3 but unwraps or raises on error.

Determines if specified key exists in KeyValueSet. See ETS.Set.has_key/2.

Same as has_key/2 but unwraps or raises on error.

Same as info/2 but unwraps or raises on error.

Returns last key in KeyValueSet. See ETS.Set.last/1.

Same as last/1 but unwraps or raises on error.

Creates new Key Value Set module with the specified options.

Same as new/1 but unwraps or raises on error.

Returns next key in KeyValueSet. See ETS.Set.next/2.

Same as next/2 but unwraps or raises on error.

Returns previous key in KeyValueSet. See ETS.Set.previous/2.

Same as previous/2 but unwraps or raises on error.

Puts given value into table for given key.

Same as put/3 but unwraps or raises on error.

Same as put/3 but doesn't put record if the key already exists.

Same as put_new/3 but unwraps or raises on error.

Returns contents of table as a list. See ETS.Set.to_list/1.

Same as to_list/1 but unwraps or raises on error.

Wraps an existing :ets :set or :ordered_set in a KeyValueSet struct.

Same as wrap_existing/1 but unwraps or raises on error.

Link to this section Types

Specs

set_options() :: [ETS.Base.option() | {:ordered, boolean()}]

Specs

t() :: %ETS.KeyValueSet{set: ETS.Set.t()}

Link to this section Functions

Link to this function

accept(timeout \\ :infinity)

View Source

Waits to accept ownership of a table after it is given away. Successful receipt will return {:ok, %{kv_set: kv_set, from: from, gift: gift}} where from is the pid of the previous owner, and gift is any additional metadata sent with the table.

A timeout may be given in milliseconds, which will return {:error, :timeout} if reached.

See give_away/3 for more information.

Link to this macro

accept(id, table, from, state, list)

View Source (macro)

For processes which may receive ownership of a KeyValueSet unexpectedly - either via give_away/3 or by being named the KeyValueSet's heir (see new/1) - the module should include at least one accept clause. For example, if we want a server to inherit KeyValueSets after their previous owner dies:

defmodule Receiver do
  use GenServer
  alias ETS.KeyValueSet
  require ETS.KeyValueSet

  ...

  KeyValueSet.accept :owner_crashed, kv_set, _from, state do
    new_state = Map.update!(state, :crashed_sets, &[kv_set | &1])
    {:noreply, new_state}
  end

The first argument is a unique identifier which should match either the "heir_data" in new/1, or the "gift" in give_away/3. The other arguments declare the variables which may be used in the do block: the received KeyValueSet, the pid of the previous owner, and the current state of the process.

The return value should be in the form {:noreply, new_state}, or one of the similar returns expected by handle_info/handle_cast.

Specs

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

Deletes KeyValueSet. See ETS.Set.delete/1.

Link to this function

delete(key_value_set, key)

View Source

Specs

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

Deletes record with specified key in specified Set.

examples

Examples

iex> set = KeyValueSet.new!()
iex> KeyValueSet.put(set, :a, :b)
iex> KeyValueSet.delete(set, :a)
iex> KeyValueSet.get!(set, :a)
nil

Specs

delete!(t()) :: any()

Same as delete/1 but unwraps or raises on error.

Specs

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

Same as delete/2 but unwraps or raises on error.

Link to this function

delete_all(key_value_set)

View Source

Specs

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

Deletes all records in specified Set.

examples

Examples

iex> set = KeyValueSet.new!()
iex> set
iex> |> KeyValueSet.put!(:a, :d)
iex> |> KeyValueSet.put!(:b, :d)
iex> |> KeyValueSet.put!(:c, :d)
iex> |> KeyValueSet.to_list!()
[c: :d, b: :d, a: :d]
iex> KeyValueSet.delete_all(set)
iex> KeyValueSet.to_list!(set)
[]

Specs

delete_all!(t()) :: t()

Same as delete_all/1 but unwraps or raises on error.

Specs

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

Returns first key in KeyValueSet. See ETS.Set.first/1.

Specs

first!(t()) :: any()

Same as first/1 but unwraps or raises on error.

Link to this function

get(key_value_set, key, default \\ nil)

View Source

Specs

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

Returns value for specified key or the provided default (nil if not specified) if no record found.

examples

Examples

iex> KeyValueSet.new!()
iex> |> KeyValueSet.put!(:a, :b)
iex> |> KeyValueSet.put!(:c, :d)
iex> |> KeyValueSet.put!(:e, :f)
iex> |> KeyValueSet.get(:c)
{:ok, :d}
Link to this function

get!(key_value_set, key, default \\ nil)

View Source

Specs

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

Same as get/3 but unwraps or raises on error

Link to this function

get_table(key_value_set)

View Source

Specs

get_table(t()) :: {:ok, ETS.table_reference()} | {:error, any()}

Returns underlying :ets table reference. See ETS.Set.get_table/1.

Link to this function

get_table!(key_value_set)

View Source

Specs

get_table!(t()) :: ETS.table_reference()

Same as get_table/1 but unwraps or raises on error.

Link to this function

give_away(key_value_set, pid, gift \\ [])

View Source

Specs

give_away(t(), pid(), any()) :: {:ok, t()} | {:error, any()}

Transfers ownership of a KeyValueSet to another process.

examples

Examples

iex> kv_set = KeyValueSet.new!()
iex> receiver_pid = spawn(fn -> KeyValueSet.accept() end)
iex> KeyValueSet.give_away(kv_set, receiver_pid)
{:ok, kv_set}

iex> kv_set = KeyValueSet.new!()
iex> dead_pid = ETS.TestUtils.dead_pid()
iex> KeyValueSet.give_away(kv_set, dead_pid)
{:error, :recipient_not_alive}
Link to this function

give_away!(kv_set, pid, gift \\ [])

View Source

Specs

give_away!(t(), pid(), any()) :: t()

Same as give_away/3 but unwraps or raises on error.

Link to this function

has_key(key_value_set, key)

View Source

Specs

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

Determines if specified key exists in KeyValueSet. See ETS.Set.has_key/2.

Link to this function

has_key!(key_value_set, key)

View Source

Specs

has_key!(t(), any()) :: any()

Same as has_key/2 but unwraps or raises on error.

Link to this function

info(key_value_set, force_update \\ false)

View Source

Specs

info(t(), boolean()) :: {:ok, keyword()} | {:error, any()}

Returns info on set. See ETS.Set.info/2.

Link to this function

info!(key_value_set, force_update \\ false)

View Source

Specs

info!(t(), boolean()) :: keyword()

Same as info/2 but unwraps or raises on error.

Specs

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

Returns last key in KeyValueSet. See ETS.Set.last/1.

Specs

last!(t()) :: any()

Same as last/1 but unwraps or raises on error.

Specs

new(set_options()) :: {:error, any()} | {:ok, t()}

Creates new Key Value Set module with the specified options.

Possible Options can be found in ETS.Set with the difference that specifying a keypos will result in an error.

examples

Examples

iex> {:ok, kvset} = KeyValueSet.new(ordered: true,read_concurrency: true, compressed: false)
iex> KeyValueSet.info!(kvset)[:read_concurrency]
true

# Named :ets tables via the name keyword
iex> {:ok, kvset} = KeyValueSet.new(name: :my_ets_table)
iex> KeyValueSet.info!(kvset)[:name]
:my_ets_table

Specs

new!(set_options()) :: t()

Same as new/1 but unwraps or raises on error.

Link to this function

next(key_value_set, key)

View Source

Specs

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

Returns next key in KeyValueSet. See ETS.Set.next/2.

Link to this function

next!(key_value_set, key)

View Source

Specs

next!(t(), any()) :: any()

Same as next/2 but unwraps or raises on error.

Link to this function

previous(key_value_set, key)

View Source

Specs

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

Returns previous key in KeyValueSet. See ETS.Set.previous/2.

Link to this function

previous!(key_value_set, key)

View Source

Specs

previous!(t(), any()) :: any()

Same as previous/2 but unwraps or raises on error.

Link to this function

put(key_value_set, key, value)

View Source

Puts given value into table for given key.

examples

Examples

iex> kvset = KeyValueSet.new!(ordered: true)
iex> {:ok, kvset} = KeyValueSet.put(kvset, :a, :b)
iex> KeyValueSet.get!(kvset, :a)
:b
Link to this function

put!(key_value_set, key, value)

View Source

Specs

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

Same as put/3 but unwraps or raises on error.

Link to this function

put_new(key_value_set, key, value)

View Source

Specs

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

Same as put/3 but doesn't put record if the key already exists.

examples

Examples

iex> set = KeyValueSet.new!(ordered: true)
iex> {:ok, _} = KeyValueSet.put_new(set, :a, :b)
iex> {:ok, _} = KeyValueSet.put_new(set, :a, :c) # skips due toduplicate :a key
iex> KeyValueSet.to_list!(set)
[{:a, :b}]
Link to this function

put_new!(key_value_set, key, value)

View Source

Specs

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

Same as put_new/3 but unwraps or raises on error.

Specs

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

Returns contents of table as a list. See ETS.Set.to_list/1.

Specs

to_list!(t()) :: any()

Same as to_list/1 but unwraps or raises on error.

Link to this function

wrap_existing(table_identifier)

View Source

Specs

wrap_existing(ETS.table_identifier()) :: {:ok, t()} | {:error, any()}

Wraps an existing :ets :set or :ordered_set in a KeyValueSet struct.

examples

Examples

iex> :ets.new(:my_ets_table, [:set, :named_table])
iex> {:ok, set} = KeyValueSet.wrap_existing(:my_ets_table)
iex> KeyValueSet.info!(set)[:name]
:my_ets_table
Link to this function

wrap_existing!(table_identifier)

View Source

Specs

wrap_existing!(ETS.table_identifier()) :: t()

Same as wrap_existing/1 but unwraps or raises on error.