httk.serve.optimade.backend.memory_store

A generic in-memory store implementing the httk store/searcher protocols.

Rows are plain dicts keyed by backend record keys, and search expressions evaluate as predicates over those rows. It is the reference Store implementation: it backs the example demo server and is what adapter_from_providers() loads an EntryProvider’s records into.

Set operations evaluate exactly here — has_any/has_only are plain set predicates over the row’s list value, and ~ negates them directly. The SQL backend needs an aggregate rendering and a second (HAVING) evaluation position to say the same thing, but that is entirely its own business: the neutral protocol only ever hands a store one expression per searcher.add call.

String matching is literal here too (contains/startswith/endswith are plain str operations), which is exactly what the neutral protocol promises — no pattern language is involved at any point.

Iteration yields SearchResult values, one entry in values per MemorySearcher.output() call: a variable output yields the whole row dict, a field output the row’s value for that field.

Attributes

Classes

MemoryExpression

Represent a boolean predicate over an in-memory row.

MemoryField

Represent a named field in an in-memory row.

MemoryVariable

A query variable over one table of rows; attribute access yields fields.

MemorySearcher

Build and execute a query over in-memory row tables.

InMemoryStore

Provide a store over dictionary rows.

MemoryResultSet

Represent rows projected from an in-memory query.

Module Contents

httk.serve.optimade.backend.memory_store.Row
httk.serve.optimade.backend.memory_store.Predicate
class httk.serve.optimade.backend.memory_store.MemoryExpression(predicate)

Represent a boolean predicate over an in-memory row.

Parameters:

predicate (Predicate) – Function returning whether a row matches.

predicate
class httk.serve.optimade.backend.memory_store.MemoryField(name)

Represent a named field in an in-memory row.

Parameters:

name (str) – Row key addressed by the field.

name
startswith(other)

Match string values that start with other.

Parameters:

other (str) – Prefix to match.

Returns:

Matching predicate.

Return type:

MemoryExpression

endswith(other)

Match string values that end with other.

Parameters:

other (str) – Required string suffix.

Returns:

Matching predicate.

Return type:

MemoryExpression

contains(other)

Match string values containing other.

Parameters:

other (str) – Required substring.

Returns:

Matching predicate.

Return type:

MemoryExpression

is_in(*values)

Match a scalar field against supplied values.

Parameters:

*values (Any) – Candidate scalar values.

Returns:

Matching predicate.

Return type:

MemoryExpression

has(value)

Match list values containing value.

Parameters:

value (Any) – Required list member.

Returns:

Matching predicate.

Return type:

MemoryExpression

has_any(*values)

Match list values containing any supplied member.

Parameters:

*values (Any) – Candidate list members.

Returns:

Matching predicate.

Return type:

MemoryExpression

has_only(*values)

Match list values containing no members outside the supplied set.

Parameters:

*values (Any) – Allowed list members.

Returns:

Matching predicate.

Return type:

MemoryExpression

class httk.serve.optimade.backend.memory_store.MemoryVariable(target)

A query variable over one table of rows; attribute access yields fields.

always_true/always_false are real methods, declared before the catch-all __getattr__ so they win over it — they are reserved names that never resolve to a row key.

Parameters:

target (str) – Table name used by the searcher.

target
always_true()

Return a predicate that matches every row.

Returns:

Always-true predicate.

Return type:

MemoryExpression

always_false()

Return a predicate that matches no row.

Returns:

Always-false predicate.

Return type:

MemoryExpression

class httk.serve.optimade.backend.memory_store.MemorySearcher(tables)

Build and execute a query over in-memory row tables.

Parameters:

tables (dict[str, list[Row]]) – Row lists keyed by table name.

offset = 0
variable(target)

Select a table and return its query variable.

Parameters:

target (Any) – Table name to select.

Returns:

Variable addressing the selected table.

Return type:

MemoryVariable

output(variable, name)

Append a whole-row or field output.

Parameters:
Raises:

TypeError – If variable is neither a variable nor a field.

add(expression)

Add a predicate to the query.

Parameters:

expression (MemoryExpression) – Predicate to apply to each row.

add_sort(field, descending)

Append a sort key.

Parameters:
  • field (MemoryField) – Field used for ordering.

  • descending (bool) – Sort in descending order when true.

count()

Return the number of rows matching the current query.

Returns:

Number of matching rows before paging.

Return type:

int

set_limit(limit)

Set the maximum number of rows returned by this query.

Parameters:

limit (int) – Maximum result count; a negative value means unbounded.

add_offset(offset)

Advance the query offset.

Parameters:

offset (int) – Number of matching rows to skip.

results(**outputs)

Return a result set for selected outputs.

Parameters:

**outputs (Any) – Optional output names mapped to variables or fields.

Returns:

Materialized result set.

Raises:
  • TypeError – If an output is not a variable or field.

  • ValueError – If no outputs are selected.

Return type:

httk.store.query.ResultSetLike

class httk.serve.optimade.backend.memory_store.InMemoryStore(tables)

Provide a store over dictionary rows.

Parameters:

tables (dict[str, list[Row]]) – Row lists keyed by table name.

tables
searcher(*, as_of=None)

Create a searcher over this store’s tables.

Parameters:

as_of (object) – Optional historic timestamp cutoff; unsupported here.

Returns:

Fresh in-memory searcher.

Raises:

ValueError – If a historic cutoff is requested.

Return type:

MemorySearcher

class httk.serve.optimade.backend.memory_store.MemoryResultSet(rows, outputs)

Represent rows projected from an in-memory query.

Parameters:
first()

Return the first result, if present.

Returns:

First result or None.

Return type:

httk.store.query.ResultRowLike | None

one()

Return the only result.

Returns:

The sole result.

Raises:
Return type:

httk.store.query.ResultRowLike

scalars(name=None)

Iterate one named scalar output from each result.

Parameters:

name (str | None) – Output name, or None when exactly one output exists.

Returns:

Iterator over scalar values.

Raises:
  • KeyError – If name is not declared.

  • ValueError – If no name is given and multiple outputs exist.

Return type:

collections.abc.Iterator[Any]

abstractmethod column(name)

Reject SQL-style column access for in-memory results.

Parameters:

name (str) – Unsupported column name.

Raises:

NotImplementedError – In-memory results have no column proxy.

abstractmethod cursor()

Reject SQL-style cursor access for in-memory results.

Raises:

NotImplementedError – In-memory results have no cursor proxy.