SuperCache.Storage.MatchSpec (SuperCache v1.7.0)

Copy Markdown View Source

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?

Summary

Functions

Create a match spec to get all records.

compile(spec) deprecated

Create a match spec for deletion.

Recursively extract capture variables (:"$1", :"$2", ...) from a match pattern, deduplicate them, and sort them numerically by variable index.

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

match_spec()

@type match_spec() :: [{term(), [term()], [term()]}]

Functions

all_records()

@spec all_records() :: match_spec()

Create a match spec to get all records.

compile(spec)

This function is deprecated. Broken on OTP 28+ (:ets.select/2 rejects compiled refs); pass the plain spec to select/2 instead.
@spec compile(match_spec()) :: :ets.comp_match_spec() | match_spec()

delete_match(pattern)

@spec delete_match(tuple() | atom()) :: match_spec()

Create a match spec for deletion.

extract_bindings(pattern)

@spec extract_bindings(term()) :: [atom()]

Recursively extract capture variables (:"$1", :"$2", ...) from a match pattern, deduplicate them, and sort them numerically by variable index.

Supports capture variables at any nesting depth inside tuples and lists, so patterns like {{:kv, :"$1"}, :"$2"} or {:rec, :"$2", :"$1"} work correctly. The returned list is sorted by variable number so the output binding list is always [b1, b2, ...] in numerical order.

match(pattern, bindings)

@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).

match_object(pattern)

@spec match_object(tuple() | atom()) :: match_spec()

Create a match spec for :ets.match_object/2 style queries.

select(table, spec)

@spec select(atom() | :ets.tid(), :ets.comp_match_spec() | match_spec()) :: [term()]

Run a match spec against a table.

select_delete(table, spec)

@spec select_delete(atom() | :ets.tid(), :ets.comp_match_spec() | match_spec()) ::
  non_neg_integer()

Run a match spec for deletion.

with_guard(pattern, guards, body)

@spec with_guard(tuple() | atom(), [tuple()], [term()]) :: match_spec()

Create a match spec with a guard condition.