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[source]
httk.serve.optimade.backend.memory_store.Predicate[source]
class httk.serve.optimade.backend.memory_store.MemoryExpression(predicate)[source]

Represent a boolean predicate over an in-memory row.

Parameters:

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

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

Represent a named field in an in-memory row.

Parameters:

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

name[source]
startswith(other)[source]

Match string values that start with other.

Parameters:

other (str) – Prefix to match.

Returns:

Matching predicate.

Return type:

MemoryExpression

endswith(other)[source]

Match string values that end with other.

Parameters:

other (str) – Required string suffix.

Returns:

Matching predicate.

Return type:

MemoryExpression

contains(other)[source]

Match string values containing other.

Parameters:

other (str) – Required substring.

Returns:

Matching predicate.

Return type:

MemoryExpression

is_in(*values)[source]

Match a scalar field against supplied values.

Parameters:

*values (Any) – Candidate scalar values.

Returns:

Matching predicate.

Return type:

MemoryExpression

has(value)[source]

Match list values containing value.

Parameters:

value (Any) – Required list member.

Returns:

Matching predicate.

Return type:

MemoryExpression

has_any(*values)[source]

Match list values containing any supplied member.

Parameters:

*values (Any) – Candidate list members.

Returns:

Matching predicate.

Return type:

MemoryExpression

has_only(*values)[source]

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)[source]

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[source]
always_true()[source]

Return a predicate that matches every row.

Returns:

Always-true predicate.

Return type:

MemoryExpression

always_false()[source]

Return a predicate that matches no row.

Returns:

Always-false predicate.

Return type:

MemoryExpression

class httk.serve.optimade.backend.memory_store.MemorySearcher(tables)[source]

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

Parameters:

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

offset = 0[source]
variable(target)[source]

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)[source]

Append a whole-row or field output.

Parameters:
Raises:

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

add(expression)[source]

Add a predicate to the query.

Parameters:

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

add_sort(field, descending)[source]

Append a sort key.

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

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

count()[source]

Return the number of rows matching the current query.

Returns:

Number of matching rows before paging.

Return type:

int

set_limit(limit)[source]

Set the maximum number of rows returned by this query.

Parameters:

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

add_offset(offset)[source]

Advance the query offset.

Parameters:

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

results(**outputs)[source]

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)[source]

Provide a store over dictionary rows.

Parameters:

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

tables[source]
searcher(*, as_of=None)[source]

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)[source]

Represent rows projected from an in-memory query.

Parameters:
first()[source]

Return the first result, if present.

Returns:

First result or None.

Return type:

httk.store.query.ResultRowLike | None

one()[source]

Return the only result.

Returns:

The sole result.

Raises:
Return type:

httk.store.query.ResultRowLike

scalars(name=None)[source]

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)[source]

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()[source]

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

Raises:

NotImplementedError – In-memory results have no cursor proxy.