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¶
Represent a boolean predicate over an in-memory row. |
|
Represent a named field in an in-memory row. |
|
A query variable over one table of rows; attribute access yields fields. |
|
Build and execute a query over in-memory row tables. |
|
Provide a store over dictionary rows. |
|
Represent rows projected from an in-memory query. |
Module Contents¶
- 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.
- 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.
- startswith(other)[source]¶
Match string values that start with
other.- Parameters:
other (str) – Prefix to match.
- Returns:
Matching predicate.
- Return type:
- endswith(other)[source]¶
Match string values that end with
other.- Parameters:
other (str) – Required string suffix.
- Returns:
Matching predicate.
- Return type:
- contains(other)[source]¶
Match string values containing
other.- Parameters:
other (str) – Required substring.
- Returns:
Matching predicate.
- Return type:
- is_in(*values)[source]¶
Match a scalar field against supplied values.
- Parameters:
*values (Any) – Candidate scalar values.
- Returns:
Matching predicate.
- Return type:
- has(value)[source]¶
Match list values containing
value.- Parameters:
value (Any) – Required list member.
- Returns:
Matching predicate.
- Return type:
- has_any(*values)[source]¶
Match list values containing any supplied member.
- Parameters:
*values (Any) – Candidate list members.
- Returns:
Matching predicate.
- Return type:
- 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_falseare 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.
- always_true()[source]¶
Return a predicate that matches every row.
- Returns:
Always-true predicate.
- Return type:
- class httk.serve.optimade.backend.memory_store.MemorySearcher(tables)[source]¶
Build and execute a query over in-memory row tables.
- 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:
- output(variable, name)[source]¶
Append a whole-row or field output.
- Parameters:
variable (MemoryVariable | MemoryField) – Variable for a whole row or field for one value.
name (str) – Output name.
- Raises:
TypeError – If
variableis 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:
- 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:
- class httk.serve.optimade.backend.memory_store.InMemoryStore(tables)[source]¶
Provide a store over dictionary rows.
- 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:
- class httk.serve.optimade.backend.memory_store.MemoryResultSet(rows, outputs)[source]¶
Represent rows projected from an in-memory query.
- Parameters:
outputs (list[tuple[str, MemoryVariable | MemoryField]]) – Named variables or fields projected from each row.
- one()[source]¶
Return the only result.
- Returns:
The sole result.
- Raises:
httk.store.NoResultError – If the result set is empty.
httk.store.MultipleResultsError – If it has more than one result.
- Return type:
- scalars(name=None)[source]¶
Iterate one named scalar output from each result.
- Parameters:
name (str | None) – Output name, or
Nonewhen exactly one output exists.- Returns:
Iterator over scalar values.
- Raises:
KeyError – If
nameis not declared.ValueError – If no name is given and multiple outputs exist.
- Return type:
- 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.