View Source ActiveMemory.Operations (ActiveMemory v0.7.1)

The shared implementation of the table operations and store setup used by ActiveMemory.Store and ActiveMemory.ActiveRepo.

Every function takes the table module explicitly and dispatches to that table's configured adapter, applying the common validation, uuid handling, seeding and before_init logic. This keeps the single-table Store and the multi-table Repo sharing one implementation rather than duplicating it.

Link to this section Summary

Functions

Run the before_init methods for a store.

Delete the record provided.

Delete every record in table whose expires_at is at or before now (milliseconds). Used by the Store/ActiveRepo sweep to reclaim memory; reads already hide expired records, so this is only about freeing them.

Get one record matching an attributes map or a match query. An expired record is treated as {:error, :not_found}.

Evaluate seed_file and write its records to table.

Get all records matching an attributes map or a match query.

Get one record matching the query, delete it, and return it. An expired record is treated as {:error, :not_found}.

Write a record to table.

Link to this section Functions

@spec all(atom()) :: [map()]
Link to this function

before_init(methods, module)

View Source
@spec before_init(:default | tuple() | list(), module()) :: {:ok, atom()}

Run the before_init methods for a store.

spec is :default, a single {method, args} tuple, or a list of such tuples. module is the module the methods are defined on (the Store or Repo).

@spec create_table(atom()) :: {:ok, :created | :recovered} | {:error, any()}
@spec delete(any(), atom()) :: :ok | {:error, any()}

Delete the record provided.

Returns :ok for a struct matching table or for nil, and {:error, :bad_schema} when the struct does not match table.

@spec delete_all(atom()) :: :ok | {:error, any()}
Link to this function

delete_expired(table, now)

View Source
@spec delete_expired(atom(), integer()) :: :ok

Delete every record in table whose expires_at is at or before now (milliseconds). Used by the Store/ActiveRepo sweep to reclaim memory; reads already hide expired records, so this is only about freeing them.

@spec one(map() | tuple(), atom()) :: {:ok, map()} | {:error, any()}

Get one record matching an attributes map or a match query. An expired record is treated as {:error, :not_found}.

@spec seed(binary() | nil, atom()) :: {:ok, :seed_success} | {:error, any()}

Evaluate seed_file and write its records to table.

A nil seed_file is a no-op. Returns {:ok, :seed_success} or {:error, reason}.

@spec select(map() | tuple(), atom()) :: {:ok, [map()]} | {:error, any()}

Get all records matching an attributes map or a match query.

Returns {:error, :bad_select_query} for any other query shape.

@spec withdraw(map() | tuple(), atom()) :: {:ok, map()} | {:error, any()}

Get one record matching the query, delete it, and return it. An expired record is treated as {:error, :not_found}.

@spec write(map(), atom()) :: {:ok, map()} | {:error, any()}

Write a record to table.

When the schema has a uuid field a value is generated if one is absent. When the table has a ttl the record's expires_at is stamped from the current time. Returns {:error, :bad_schema} when the struct does not match table.