Compiled match specifications for ETS queries.
This module provides helper functions to create and compile match specs for common ETS query patterns. Compiled match specs can be reused across multiple queries for better performance.
Why use match specs?
:ets.match/2and:ets.match_object/2parse the pattern every time:ets.select/2with a compiled match spec skips parsing- For repeated queries, compiled specs reduce CPU overhead
Example
# Instead of:
:ets.match(table, {:"$1", :"$2"})
# Use:
spec = MatchSpec.match([{:"$1", :"$2"}], [:"$1", :"$2"])
:ets.select(table, spec)
Summary
Functions
Create a match spec to get all records.
Compile a match spec via :ets.match_spec_compile/1.
Create a match spec for deletion.
Create a match spec for :ets.match/2 style queries.
Create a match spec for :ets.match_object/2 style queries.
Run a match spec against a table.
Run a match spec for deletion.
Create a match spec with a guard condition.
Types
Functions
@spec all_records() :: match_spec()
Create a match spec to get all records.
Note: :"$_" is only valid in the body of a match spec, not in the
pattern. A generic "match all" select spec that works for any tuple arity
does not exist — the pattern must match the stored tuple structure exactly.
Prefer :ets.tab2list/1 when you need all records from a table, or use
match_object/1 with an explicit pattern like {:"$1", :"$2"}.
@spec compile(match_spec()) :: :ets.comp_match_spec() | match_spec()
Compile a match spec via :ets.match_spec_compile/1.
Deprecated / broken on OTP 28+: as of stdlib 8.x, :ets.select/2
rejects compiled match-spec references ("not a valid match specification"),
so the returned value cannot be used on those releases. On older OTP
releases the compiled reference worked and could be more efficient when
the same query ran repeatedly.
Prefer passing the uncompiled spec from match/2 or match_object/1
directly to select/2 — this is what all SuperCache internals do.
@spec delete_match(tuple() | atom()) :: match_spec()
Create a match spec for deletion.
Returns a match spec that, when used with :ets.select_delete/2,
deletes matching records.
Parameters
pattern: The match pattern with:"$1",:"$2", etc.
Example
# Delete all records with :expired status
spec = MatchSpec.delete_match({:"$1", :"$2", :expired})
:ets.select_delete(table, spec)
@spec match(tuple() | atom(), [term()]) :: match_spec()
Create a match spec for :ets.match/2 style queries.
Returns a match spec that, when used with :ets.select/2, returns
a list of binding lists (like :ets.match/2).
Parameters
pattern: The match pattern with:"$1",:"$2", etc.bindings: List of bindings to return (e.g.,[:"$1", :"$2"])
Example
# Match all records and return the first two fields
spec = MatchSpec.match({:"$1", :"$2", :_}, [:"$1", :"$2"])
:ets.select(table, spec)
@spec match_object(tuple() | atom()) :: match_spec()
Create a match spec for :ets.match_object/2 style queries.
Returns a match spec that, when used with :ets.select/2, returns
full matching tuples (like :ets.match_object/2).
Parameters
pattern: The match pattern with:"$1",:"$2", etc.
Example
# Match all records with :admin as third element
spec = MatchSpec.match_object({:"$1", :"$2", :admin})
:ets.select(table, spec)
@spec select(atom() | :ets.tid(), :ets.comp_match_spec() | match_spec()) :: [term()]
Run a match spec against a table.
Accepts either a compiled match spec or a regular match spec list.
@spec select_delete(atom() | :ets.tid(), :ets.comp_match_spec() | match_spec()) :: non_neg_integer()
Run a match spec for deletion.
Accepts either a compiled match spec or a regular match spec list.
@spec with_guard(tuple() | atom(), [tuple()], [term()]) :: match_spec()
Create a match spec with a guard condition.
Parameters
pattern: The match patternguards: List of guard conditions (tuples)body: List of expressions to return
Example
# Match records where the second element is > 100
spec = MatchSpec.with_guard(
{:"$1", :"$2"},
[{:>, :"$2", 100}],
[:"$_"]
)