Registry.match
match
, go back to Registry module for more information.
Specs
Returns {pid, value}
pairs under the given key
in registry
that match pattern
.
Pattern must be an atom or a tuple that will match the structure of the
value stored in the registry. The atom :_
can be used to ignore a given
value or tuple element, while the atom :"$1"
can be used to temporarily assign part
of pattern to a variable for a subsequent comparison.
Optionally, it is possible to pass a list of guard conditions for more precise matching.
Each guard is a tuple, which describes checks that should be passed by assigned part of pattern.
For example the $1 > 1
guard condition would be expressed as the {:>, :"$1", 1}
tuple.
Please note that guard conditions will work only for assigned
variables like :"$1"
, :"$2"
, and so forth.
Avoid usage of special match variables :"$_"
and :"$$"
, because it might not work as expected.
An empty list will be returned if there is no match.
For unique registries, a single partition lookup is necessary. For duplicate registries, all partitions must be looked up.
Examples
In the example below we register the current process under the same key in a duplicate registry but with different values:
iex> Registry.start_link(keys: :duplicate, name: Registry.MatchTest)
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {1, :atom, 1})
iex> {:ok, _} = Registry.register(Registry.MatchTest, "hello", {2, :atom, 2})
iex> Registry.match(Registry.MatchTest, "hello", {1, :_, :_})
[{self(), {1, :atom, 1}}]
iex> Registry.match(Registry.MatchTest, "hello", {2, :_, :_})
[{self(), {2, :atom, 2}}]
iex> Registry.match(Registry.MatchTest, "hello", {:_, :atom, :_}) |> Enum.sort()
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]
iex> Registry.match(Registry.MatchTest, "hello", {:"$1", :_, :"$1"}) |> Enum.sort()
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]
iex> guards = [{:>, :"$1", 1}]
iex> Registry.match(Registry.MatchTest, "hello", {:_, :_, :"$1"}, guards)
[{self(), {2, :atom, 2}}]
iex> guards = [{:is_atom, :"$1"}]
iex> Registry.match(Registry.MatchTest, "hello", {:_, :"$1", :_}, guards) |> Enum.sort()
[{self(), {1, :atom, 1}}, {self(), {2, :atom, 2}}]