Registry.count_match
count_match
, go back to Registry module for more information.
Specs
count_match(registry(), key(), match_pattern(), guards()) :: non_neg_integer()
Returns the number of {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.
Zero 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.CountMatchTest)
iex> {:ok, _} = Registry.register(Registry.CountMatchTest, "hello", {1, :atom, 1})
iex> {:ok, _} = Registry.register(Registry.CountMatchTest, "hello", {2, :atom, 2})
iex> Registry.count_match(Registry.CountMatchTest, "hello", {1, :_, :_})
1
iex> Registry.count_match(Registry.CountMatchTest, "hello", {2, :_, :_})
1
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :atom, :_})
2
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:"$1", :_, :"$1"})
2
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :_, :"$1"}, [{:>, :"$1", 1}])
1
iex> Registry.count_match(Registry.CountMatchTest, "hello", {:_, :"$1", :_}, [{:is_atom, :"$1"}])
2