View Source ETS.Set (ets v0.9.0)

Module for creating and interacting with :ets tables of the type :set and :ordered_set.

Sets contain "records" which are tuples. Sets are configured with a key position via the keypos: integer option. If not specified, the default key position is 1. The element of the tuple record at the key position is that records key. For example, setting the keypos to 2 means the key of an inserted record {:a, :b} is :b:

iex> {:ok, set} = Set.new(keypos: 2)
iex> Set.put!(set, {:a, :b})
iex> Set.get(set, :a)
{:ok, nil}
iex> Set.get(set, :b)
{:ok, {:a, :b}}

When a record is added to the table with put, it will overwrite an existing record with the same key. put_new will only put the record if a matching key doesn't already exist.

examples

Examples

iex> {:ok, set} = Set.new(ordered: true)
iex> Set.put_new!(set, {:a, :b, :c})
iex> Set.to_list!(set)
[{:a, :b, :c}]
iex> Set.put_new!(set, {:d, :e, :f})
iex> Set.to_list!(set)
[{:a, :b, :c}, {:d, :e, :f}]
iex> Set.put_new!(set, {:a, :g, :h})
iex> Set.to_list!(set)
[{:a, :b, :c}, {:d, :e, :f}]

put and put_new take either a single tuple or a list of tuples. When inserting multiple records, they are inserted in an atomic an isolated manner. put_new doesn't insert any records if any of the new keys already exist in the set.

To make your set ordered (which maps to the :ets table type :ordered_set), specify ordered: true in the options list. An ordered set will store records in term order of the key of the record. This is helpful when using things like first, last, previous, next, and to_list, but comes with the penalty of log(n) insert time vs consistent insert time of an unordered set.

working-with-named-tables

Working with named tables

The functions on ETS.Set require that you pass in an ETS.Set as the first argument. In some design patterns, you may have the table name but an instance of an ETS.Set may not be available to you. If this is the case, you should use wrap_existing/1 to turn your table name atom into an ETS.Set. For example, a GenServer that handles writes within the server, but reads in the client process would be implemented like this:

defmodule MyExampleGenServer do
  use GenServer

  # Client Functions

  def get_token_for_user(user_id) do
    :my_token_table
    |> ETS.Set.wrap_existing!()
    |> ETS.Set.get!(user_id)
    |> elem(1)
  end

  def set_token_for_user(user_id, token) do
    GenServer.call(__MODULE__, {:set_token_for_user, user_id, token})
  end

  # Server Functions

  def init(_) do
    {:ok, %{set: ETS.Set.new!(name: :my_token_table)}}
  end

  def handle_call({:set_token_for_user, user_id, token}, _from, %{set: set}) do
    ETS.Set.put(set, user_id, token)
  end
end

Link to this section Summary

Functions

Waits to accept ownership of a table after it is given away. Successful receipt will return {:ok, %{set: 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 Set unexpectedly - either via give_away/3 or by being named the Set's heir (see new/1) - the module should include at least one accept clause. For example, if we want a server to inherit Sets after their previous owner dies

Deletes specified Set.

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 record with specified key or an error if no record found.

Returns the first key in the specified Set. Set must be ordered or error is returned.

Same as first/1 but unwraps or raises on error

Returns record with 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 element in specified position of record with specified key.

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

Returns underlying :ets table reference.

Same as get_table/1 but unwraps or raises on error

Transfers ownership of a Set to another process.

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

Determines if specified key exists in specified set.

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

Returns information on the set.

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

Returns the last key in the specified Set. Set must be ordered or error is returned.

Same as last/1 but unwraps or raises on error

Matches next set of records from a match/3 or match/1 continuation.

Returns records in the specified Set that match the specified pattern.

Same as match/2 but limits number of results to the specified limit.

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

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

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

Deletes all records that match the specified pattern.

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

Matches next records from a match_object/3 or match_object/1 continuation.

Returns records in the specified Set that match the specified pattern.

Same as match_object/2 but limits number of results to the specified limit.

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

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

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

Creates new set module with the specified options.

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

Returns the next key in the specified Set.

Same as next/1 but unwraps or raises on error

Returns the previous key in the specified Set.

Same as previous/1 but raises on :error

Puts tuple record or list of tuple records into table. Overwrites records for existing key(s).

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

Same as put/2 but doesn't put any records if one of the given keys already exists.

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

Returns records in the specified Set that match the specified match specification.

Same as select/2 but limits the number of results returned.

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

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

Deletes records in the specified Set that match the specified match specification.

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

Returns contents of table as a list.

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

Updates one or more elements within the record with the given key. The element_spec is a tuple (or list of tuples) of the form {position, value}, which will update the element at position (1-indexed) to have the given value. When a list is given, multiple elements can be updated within the same record. If the same position occurs more than once in the list, the last value in the list is written. If the list is empty or the function fails, no updates are done. The function is also atomic in the sense that other processes can never see any intermediate results.

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

Wraps an existing :ets :set or :ordered_set in a Set 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.Set{
  info: keyword(),
  ordered: boolean(),
  table: ETS.table_reference()
}

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, %{set: 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 Set unexpectedly - either via give_away/3 or by being named the Set's heir (see new/1) - the module should include at least one accept clause. For example, if we want a server to inherit Sets after their previous owner dies:

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

  ...

  Set.accept :owner_crashed, set, _from, state do
    new_state = Map.update!(state, :crashed_sets, &[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 Set, 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, t()} | {:error, any()}

Deletes specified Set.

examples

Examples

iex> {:ok, set} = Set.new()
iex> {:ok, _} = Set.info(set, true)
iex> {:ok, _} = Set.delete(set)
iex> Set.info(set, true)
{:error, :table_not_found}

Specs

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

Deletes record with specified key in specified Set.

examples

Examples

iex> set = Set.new!()
iex> Set.put(set, {:a, :b, :c})
iex> Set.delete(set, :a)
iex> Set.get!(set, :a)
nil

Specs

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

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.

Specs

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

Deletes all records in specified Set.

examples

Examples

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

Specs

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

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

Specs

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

Returns record with specified key or an error if no record found.

examples

Examples

iex> Set.new!()
iex> |> Set.put!({:a, :b, :c})
iex> |> Set.put!({:d, :e, :f})
iex> |> Set.fetch(:d)
{:ok, {:d, :e, :f}}

iex> Set.new!()
iex> |> Set.put!({:a, :b, :c})
iex> |> Set.put!({:d, :e, :f})
iex> |> Set.fetch(:g)
{:error, :key_not_found}

Specs

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

Returns the first key in the specified Set. Set must be ordered or error is returned.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.first(set)
{:error, :empty_table}
iex> Set.put!(set, {:key1, :val})
iex> Set.put!(set, {:key2, :val})
iex> Set.first(set)
{:ok, :key1}

Specs

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

Same as first/1 but unwraps or raises on error

Link to this function

get(set, key, default \\ nil)

View Source

Specs

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

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

examples

Examples

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

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

View Source

Specs

get!(t(), any(), any()) :: tuple() | nil

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

Link to this function

get_element(set, key, pos)

View Source

Specs

get_element(t(), any(), non_neg_integer()) :: {:ok, any()} | {:error, any()}

Returns element in specified position of record with specified key.

examples

Examples

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

get_element!(set, key, pos)

View Source

Specs

get_element!(t(), any(), non_neg_integer()) :: any()

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

Specs

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

Returns underlying :ets table reference.

For use in functions that are not yet implemented. If you find yourself using this, please consider submitting a PR to add the necessary function to ETS.

examples

Examples

iex> set = Set.new!(name: :my_ets_table)
iex> {:ok, table} = Set.get_table(set)
iex> info = :ets.info(table)
iex> info[:name]
:my_ets_table

Specs

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

Same as get_table/1 but unwraps or raises on error

Link to this function

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

View Source

Specs

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

Transfers ownership of a Set to another process.

examples

Examples

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

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

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

View Source

Specs

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

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

Specs

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

Determines if specified key exists in specified set.

examples

Examples

iex> set = Set.new!()
iex> Set.has_key(set, :key)
{:ok, false}
iex> Set.put(set, {:key, :value})
iex> Set.has_key(set, :key)
{:ok, true}

Specs

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

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

Link to this function

info(set, force_update \\ false)

View Source

Specs

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

Returns information on the set.

Second parameter forces updated information from ets, default (false) uses in-struct cached information. Force should be used when requesting size and memory.

examples

Examples

iex> {:ok, set} = Set.new(ordered: true, keypos: 3, read_concurrency: true, compressed: false)
iex> {:ok, info} = Set.info(set)
iex> info[:read_concurrency]
true
iex> {:ok, _} = Set.put(set, {:a, :b, :c})
iex> {:ok, info} = Set.info(set)
iex> info[:size]
0
iex> {:ok, info} = Set.info(set, true)
iex> info[:size]
1
Link to this function

info!(set, force_update \\ false)

View Source

Specs

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

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

Specs

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

Returns the last key in the specified Set. Set must be ordered or error is returned.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.last(set)
{:error, :empty_table}
iex> Set.put!(set, {:key1, :val})
iex> Set.put!(set, {:key2, :val})
iex> Set.last(set)
{:ok, :key2}

Specs

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

Same as last/1 but unwraps or raises on error

Specs

match(any()) :: {:ok, {[tuple()], any() | :end_of_table}} | {:error, any()}

Matches next set of records from a match/3 or match/1 continuation.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.put!(set, [{:a, :b, :c, :d}, {:e, :b, :f, :g}, {:h, :b, :i, :j}])
iex> {:ok, {results, continuation}} = Set.match(set, {:"$1", :b, :"$2", :_}, 2)
iex> results
[[:a, :c], [:e, :f]]
iex> {:ok, {records2, continuation2}} = Set.match(continuation)
iex> records2
[[:h, :i]]
iex> continuation2
:end_of_table

Specs

match(t(), ETS.match_pattern()) :: {:ok, [tuple()]} | {:error, any()}

Returns records in the specified Set that match the specified pattern.

For more information on the match pattern, see the erlang documentation

examples

Examples

iex> Set.new!(ordered: true)
iex> |> Set.put!([{:a, :b, :c, :d}, {:e, :c, :f, :g}, {:h, :b, :i, :j}])
iex> |> Set.match({:"$1", :b, :"$2", :_})
{:ok, [[:a, :c], [:h, :i]]}
Link to this function

match(set, pattern, limit)

View Source

Specs

match(t(), ETS.match_pattern(), non_neg_integer()) ::
  {:ok, {[tuple()], any() | :end_of_table}} | {:error, any()}

Same as match/2 but limits number of results to the specified limit.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.put!(set, [{:a, :b, :c, :d}, {:e, :b, :f, :g}, {:h, :b, :i, :j}])
iex> {:ok, {results, _continuation}} = Set.match(set, {:"$1", :b, :"$2", :_}, 2)
iex> results
[[:a, :c], [:e, :f]]

Specs

match!(any()) :: {[tuple()], any() | :end_of_table}

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

Specs

match!(t(), ETS.match_pattern()) :: [tuple()]

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

Link to this function

match!(set, pattern, limit)

View Source

Specs

match!(t(), ETS.match_pattern(), non_neg_integer()) ::
  {[tuple()], any() | :end_of_table}

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

Link to this function

match_delete(set, pattern)

View Source

Specs

match_delete(t(), ETS.match_pattern()) :: {:ok, t()} | {:error, any()}

Deletes all records that match the specified pattern.

Always returns :ok, regardless of whether anything was deleted or not.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.put!(set, [{:a, :b, :c, :d}, {:e, :b, :f, :g}, {:h, :i, :j, :k}])
iex> Set.match_delete(set, {:_, :b, :_, :_})
{:ok, set}
iex> Set.to_list!(set)
[{:h, :i, :j, :k}]
Link to this function

match_delete!(set, pattern)

View Source

Specs

match_delete!(t(), ETS.match_pattern()) :: t()

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

Link to this function

match_object(continuation)

View Source

Specs

match_object(any()) ::
  {:ok, {[tuple()], any() | :end_of_table}} | {:error, any()}

Matches next records from a match_object/3 or match_object/1 continuation.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.put!(set, [{:a, :b, :c}, {:d, :b, :e}, {:f, :b, :g}, {:h, :b, :i}])
iex> {:ok, {results, continuation}} = Set.match_object(set, {:"$1", :b, :_}, 2)
iex> results
[{:a, :b, :c}, {:d, :b, :e}]
iex> {:ok, {results2, continuation2}} = Set.match_object(continuation)
iex> results2
[{:f, :b, :g}, {:h, :b, :i}]
iex> {:ok, {[], :end_of_table}} = Set.match_object(continuation2)
Link to this function

match_object(set, pattern)

View Source

Specs

match_object(t(), ETS.match_pattern()) :: {:ok, [tuple()]} | {:error, any()}

Returns records in the specified Set that match the specified pattern.

For more information on the match pattern, see the erlang documentation

examples

Examples

iex> Set.new!(ordered: true)
iex> |> Set.put!([{:a, :b, :c, :d}, {:e, :c, :f, :g}, {:h, :b, :i, :j}])
iex> |> Set.match_object({:"$1", :b, :"$2", :_})
{:ok, [{:a, :b, :c, :d}, {:h, :b, :i, :j}]}
Link to this function

match_object(set, pattern, limit)

View Source

Specs

match_object(t(), ETS.match_pattern(), non_neg_integer()) ::
  {:ok, {[tuple()], any() | :end_of_table}} | {:error, any()}

Same as match_object/2 but limits number of results to the specified limit.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.put!(set, [{:a, :b, :c, :d}, {:e, :b, :f, :g}, {:h, :b, :i, :j}])
iex> {:ok, {results, _continuation}} = Set.match_object(set, {:"$1", :b, :"$2", :_}, 2)
iex> results
[{:a, :b, :c, :d}, {:e, :b, :f, :g}]
Link to this function

match_object!(continuation)

View Source

Specs

match_object!(any()) :: {[tuple()], any() | :end_of_table}

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

Link to this function

match_object!(set, pattern)

View Source

Specs

match_object!(t(), ETS.match_pattern()) :: [tuple()]

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

Link to this function

match_object!(set, pattern, limit)

View Source

Specs

match_object!(t(), ETS.match_pattern(), non_neg_integer()) ::
  {[tuple()], any() | :end_of_table}

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

Specs

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

Creates new set module with the specified options.

Note that the underlying :ets table will be attached to the process that calls new and will be destroyed if that process dies.

Possible options:

  • name: when specified, creates a named table with the specified name
  • ordered: when true, records in set are ordered (default false)
  • protection: :private, :protected, :public (default :protected)
  • heir: :none | {heir_pid, heir_data} (default :none)
  • keypos: integer (default 1)
  • read_concurrency: boolean (default false)
  • write_concurrency: boolean (default false)
  • compressed: boolean (default false)

examples

Examples

iex> {:ok, set} = Set.new(ordered: true, keypos: 3, read_concurrency: true, compressed: false)
iex> Set.info!(set)[:read_concurrency]
true

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

Specs

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

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

Specs

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

Returns the next key in the specified Set.

The given key does not need to exist in the set. The key returned will be the first key that exists in the set which is subsequent in term order to the key given.

Set must be ordered or error is returned.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.put!(set, {:key1, :val})
iex> Set.put!(set, {:key2, :val})
iex> Set.put!(set, {:key3, :val})
iex> Set.first(set)
{:ok, :key1}
iex> Set.next(set, :key1)
{:ok, :key2}
iex> Set.next(set, :key2)
{:ok, :key3}
iex> Set.next(set, :key3)
{:error, :end_of_table}
iex> Set.next(set, :a)
{:ok, :key1}
iex> Set.next(set, :z)
{:error, :end_of_table}

Specs

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

Same as next/1 but unwraps or raises on error

Specs

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

Returns the previous key in the specified Set.

The given key does not need to exist in the set. The key returned will be the first key that exists in the set which is previous in term order to the key given.

Set must be ordered or error is returned.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> Set.put!(set, {:key1, :val})
iex> Set.put!(set, {:key2, :val})
iex> Set.put!(set, {:key3, :val})
iex> Set.last(set)
{:ok, :key3}
iex> Set.previous(set, :key3)
{:ok, :key2}
iex> Set.previous(set, :key2)
{:ok, :key1}
iex> Set.previous(set, :key1)
{:error, :start_of_table}
iex> Set.previous(set, :a)
{:error, :start_of_table}
iex> Set.previous(set, :z)
{:ok, :key3}

Specs

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

Same as previous/1 but raises on :error

Returns previous key in table.

Specs

put(t(), tuple() | [tuple()]) :: {:ok, t()} | {:error, any()}

Puts tuple record or list of tuple records into table. Overwrites records for existing key(s).

Inserts multiple records in an atomic and isolated manner.

examples

Examples

iex> {:ok, set} = Set.new(ordered: true)
iex> {:ok, _} = Set.put(set, [{:a, :b, :c}, {:d, :e, :f}])
iex> {:ok, _} = Set.put(set, {:g, :h, :i})
iex> {:ok, _} = Set.put(set, {:d, :x, :y})
iex> Set.to_list(set)
{:ok, [{:a, :b, :c}, {:d, :x, :y}, {:g, :h, :i}]}
Link to this function

put!(set, record_or_records)

View Source

Specs

put!(t(), tuple() | [tuple()]) :: t()

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

Specs

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

Same as put/2 but doesn't put any records if one of the given keys already exists.

examples

Examples

iex> set = Set.new!(ordered: true)
iex> {:ok, _} = Set.put_new(set, [{:a, :b, :c}, {:d, :e, :f}])
iex> {:ok, _} = Set.put_new(set, [{:a, :x, :y}, {:g, :h, :i}]) # skips due to duplicate :a key
iex> {:ok, _} = Set.put_new(set, {:d, :z, :zz}) # skips due to duplicate :d key
iex> Set.to_list!(set)
[{:a, :b, :c}, {:d, :e, :f}]
Link to this function

put_new!(set, record_or_records)

View Source

Specs

put_new!(t(), tuple() | [tuple()]) :: t()

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

Specs

select(ETS.continuation()) ::
  {:ok, {[tuple()], ETS.continuation()} | ETS.end_of_table()} | {:error, any()}

Specs

select(t(), ETS.match_spec()) :: {:ok, [tuple()]} | {:error, any()}

Returns records in the specified Set that match the specified match specification.

For more information on the match specification, see the erlang documentation

examples

Examples

iex> Set.new!(ordered: true)
iex> |> Set.put!([{:a, :b, :c, :d}, {:e, :c, :f, :g}, {:h, :b, :i, :j}])
iex> |> Set.select([{{:"$1", :b, :"$2", :_},[],[:"$$"]}])
{:ok, [[:a, :c], [:h, :i]]}
Link to this function

select(set, spec, limit)

View Source

Specs

select(t(), ETS.match_spec(), limit :: integer()) ::
  {:ok, {[tuple()], ETS.continuation()} | ETS.end_of_table()} | {:error, any()}

Same as select/2 but limits the number of results returned.

Specs

Specs

select!(t(), ETS.match_spec()) :: [tuple()]

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

Link to this function

select!(set, spec, limit)

View Source

Specs

select!(t(), ETS.match_spec(), limit :: integer()) ::
  {[tuple()], ETS.continuation()} | ETS.end_of_table()

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

Link to this function

select_delete(set, spec)

View Source

Specs

select_delete(t(), ETS.match_spec()) ::
  {:ok, non_neg_integer()} | {:error, any()}

Deletes records in the specified Set that match the specified match specification.

For more information on the match specification, see the erlang documentation

examples

Examples

iex> set = Set.new!(ordered: true)
iex> set
iex> |> Set.put!([{:a, :b, :c, :d}, {:e, :c, :f, :g}, {:h, :b, :c, :h}])
iex> |> Set.select_delete([{{:"$1", :b, :"$2", :_},[{:"==", :"$2", :c}],[true]}])
{:ok, 2}
iex> Set.to_list!(set)
[{:e, :c, :f, :g}]
Link to this function

select_delete!(set, spec)

View Source

Specs

select_delete!(t(), ETS.match_spec()) :: non_neg_integer()

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

Specs

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

Returns contents of table as a list.

examples

Examples

iex> Set.new!(ordered: true)
iex> |> Set.put!({:a, :b, :c})
iex> |> Set.put!({:d, :e, :f})
iex> |> Set.put!({:d, :e, :f})
iex> |> Set.to_list()
{:ok, [{:a, :b, :c}, {:d, :e, :f}]}

Specs

to_list!(t()) :: [tuple()]

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

Link to this function

update_element(set, key, element_spec)

View Source

Specs

update_element(t(), any(), tuple() | [tuple()]) :: {:ok, t()} | {:error, any()}

Updates one or more elements within the record with the given key. The element_spec is a tuple (or list of tuples) of the form {position, value}, which will update the element at position (1-indexed) to have the given value. When a list is given, multiple elements can be updated within the same record. If the same position occurs more than once in the list, the last value in the list is written. If the list is empty or the function fails, no updates are done. The function is also atomic in the sense that other processes can never see any intermediate results.

Returns {:ok, set} if an object with the given key is found, otherwise returns {:error, :key_not_found}.

examples

Examples

iex> set = Set.new!()
iex> Set.put!(set, {:a, :b, :c})
iex> Set.update_element(set, :a, {2, :d})
{:ok, set}
iex> Set.to_list!(set)
[{:a, :d, :c}]
iex> Set.update_element(set, :a, [{2, :x}, {3, :y}])
{:ok, set}
iex> Set.to_list!(set)
[{:a, :x, :y}]
Link to this function

update_element!(set, key, element_spec)

View Source

Specs

update_element!(t(), any(), tuple() | [tuple()]) :: t()

Same as update_element/3 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 Set struct.

examples

Examples

iex> :ets.new(:my_ets_table, [:set, :named_table])
iex> {:ok, set} = Set.wrap_existing(:my_ets_table)
iex> Set.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.