Query engine¶
Memoized queries with red-green verification, early cutoff, persistence, and a log of every step.
queries
¶
A demand-driven query engine with red-green invalidation and early cutoff.
A compilation is a set of queries: functions of a key (parse of a
module, check of a module, interface of a module, ...) that read
inputs (source text) and other queries. The engine memoizes every query
result together with the queries and inputs it read, its fingerprint (a
digest of the result), and two revisions: when it was last verified and when
it last changed.
Setting an input to a value with a new fingerprint starts a new revision. Asking for a query then walks its recorded dependencies in the order it read them: if none changed after the result was last verified, the result is green and is reused without running the query. Otherwise the query runs again, and if its new result has the old fingerprint the result is backdated (early cutoff): its change revision stays where it was, so the queries that read it stay green. This is the scheme of Salsa and of rustc's incremental compilation, a constructive-trace build system in the terminology of Mokhov, Mitchell, and Peyton Jones (2018).
A query declared with a QueryCodec
is also persisted in an ArtifactStore:
its result and the fingerprints of its dependencies survive the process, and
a later engine reuses the stored result when every dependency still has the
recorded fingerprint, without running the query.
Every step is recorded in QueryEngine.log,
which is how a caller (or a test) sees exactly what was recomputed.
QueryAction
¶
What the engine did for one query key:
input: an input took a value with a new fingerprint;execute: a query ran and its result changed (or was new);cutoff: a query ran and its result kept its fingerprint, so it was backdated and its dependents stay valid;green: a query was verified without running;load: a query's result was reused from the persistent store after its recorded dependencies were verified, without running.
QueryError
¶
Bases: RuntimeError
A query cannot be answered: an unset input or a dependency cycle.
QueryEvent
didactic-model
¶
QueryEvent(*, revision: int, action: QueryAction, query: str, key: str)
Bases: Model
One step of the engine.
| PARAMETER | DESCRIPTION |
|---|---|
revision
|
The engine revision the step happened at.
TYPE:
|
action
|
What the engine did.
TYPE:
|
query
|
The query or input name.
TYPE:
|
key
|
The key within it.
TYPE:
|
QueryCodec
¶
How a query's results are written to and read from the store.
| PARAMETER | DESCRIPTION |
|---|---|
encode
|
Serialize a result.
TYPE:
|
decode
|
Read a serialized result back.
TYPE:
|
version
|
The codec's format version. It is part of every stored record's address, so a change of format never reads an old record.
TYPE:
|
Source code in src/quivers/incremental/queries.py
QueryTable
¶
QueryTable(engine: QueryEngine, name: str)
Bases: ABC
An input or query table of an engine.
| PARAMETER | DESCRIPTION |
|---|---|
engine
|
The owning engine.
TYPE:
|
name
|
The table's name, unique in the engine.
TYPE:
|
Source code in src/quivers/incremental/queries.py
refresh
abstractmethod
¶
Bring one key up to date.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, str]
|
The revision its value last changed at and its fingerprint. |
Source code in src/quivers/incremental/queries.py
Input
¶
Input(engine: QueryEngine, name: str, fingerprint: Callable[[V], str])
Bases: QueryTable
A table of values set from outside the engine.
| PARAMETER | DESCRIPTION |
|---|---|
engine
|
The owning engine.
TYPE:
|
name
|
The input's name.
TYPE:
|
fingerprint
|
The digest of a value; a value with the old digest is no change.
TYPE:
|
Source code in src/quivers/incremental/queries.py
set
¶
Set one key's value.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key.
TYPE:
|
value
|
The new value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the value changed: a value with the old fingerprint starts no new revision and invalidates nothing. |
Source code in src/quivers/incremental/queries.py
keys
¶
Every key that has a value.
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...]
|
The keys, sorted. |
get
¶
Read one key, recording the read as a dependency.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
V
|
The value. |
| RAISES | DESCRIPTION |
|---|---|
QueryError
|
If the key has never been set. |
Source code in src/quivers/incremental/queries.py
refresh
¶
An input is always current.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, str]
|
The revision the value was set at and its fingerprint. |
| RAISES | DESCRIPTION |
|---|---|
QueryError
|
If the key has never been set. |
Source code in src/quivers/incremental/queries.py
Query
¶
Query(engine: QueryEngine, name: str, compute: Callable[[str], V], fingerprint: Callable[[V], str], codec: QueryCodec[V] | None)
Bases: QueryTable
A memoized function of a key.
| PARAMETER | DESCRIPTION |
|---|---|
engine
|
The owning engine.
TYPE:
|
name
|
The query's name.
TYPE:
|
compute
|
The function. It reads inputs and queries through their
TYPE:
|
fingerprint
|
The digest of a result. Two results with one fingerprint are interchangeable for every reader, so the fingerprint must cover everything a reader of the result can observe.
TYPE:
|
codec
|
How results persist in the engine's store, or None to keep them in memory only.
TYPE:
|
Source code in src/quivers/incremental/queries.py
get
¶
Answer the query, recording the read as a dependency.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
V
|
The current result. |
| RAISES | DESCRIPTION |
|---|---|
QueryError
|
If answering it needs its own answer, or reads an unset input. |
Source code in src/quivers/incremental/queries.py
fingerprint
¶
The fingerprint of the current result.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The digest, computing the result if needed (without recording a dependency). |
Source code in src/quivers/incremental/queries.py
refresh
¶
Bring one key up to date.
| PARAMETER | DESCRIPTION |
|---|---|
key
|
The key.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[int, str]
|
The revision its result last changed at and its fingerprint. |
Source code in src/quivers/incremental/queries.py
QueryEngine
¶
QueryEngine(*, store: ArtifactStore | None = None, namespace: str = 'qvr')
The tables, revision, active frames, and log of one incremental build.
| PARAMETER | DESCRIPTION |
|---|---|
store
|
Where queries with a codec persist their results, or None to keep everything in memory.
TYPE:
|
namespace
|
Separates this engine's persisted records from those of engines built with other query definitions (another engine version, say).
TYPE:
|
Source code in src/quivers/incremental/queries.py
log
instance-attribute
¶
log: list[QueryEvent] = []
Every step the engine has taken, in order: each input set, each
query run, cut off, or reused, and each result loaded from the
store, with its key (see QueryEvent).
input
¶
input(name: str, fingerprint: Callable[[V], str]) -> Input[V]
Declare an input table.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Its name.
TYPE:
|
fingerprint
|
The digest of a value.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Input[V]
|
The table. |
| RAISES | DESCRIPTION |
|---|---|
QueryError
|
If the name is taken. |
Source code in src/quivers/incremental/queries.py
query
¶
query(name: str, compute: Callable[[str], V], fingerprint: Callable[[V], str], *, codec: QueryCodec[V] | None = None) -> Query[V]
Declare a query.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Its name.
TYPE:
|
compute
|
The function of a key.
TYPE:
|
fingerprint
|
The digest of a result.
TYPE:
|
codec
|
How results persist, or None for memory only.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Query[V]
|
The query. |
| RAISES | DESCRIPTION |
|---|---|
QueryError
|
If the name is taken. |
Source code in src/quivers/incremental/queries.py
table
¶
table(name: str) -> QueryTable
A table by name.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
QueryTable
|
The table. |
| RAISES | DESCRIPTION |
|---|---|
QueryError
|
If there is no such table, which happens when a persisted record names a query this engine does not define. |
Source code in src/quivers/incremental/queries.py
advance
¶
record
¶
record(action: QueryAction, query: str, key: str) -> None
Append a step to the log.
| PARAMETER | DESCRIPTION |
|---|---|
action
|
What happened.
TYPE:
|
query
|
The table.
TYPE:
|
key
|
The key.
TYPE:
|
Source code in src/quivers/incremental/queries.py
read
¶
read(table: QueryTable, key: str) -> None
Record a read as a dependency of the running query, if any.
| PARAMETER | DESCRIPTION |
|---|---|
table
|
The table read.
TYPE:
|
key
|
The key read.
TYPE:
|
Source code in src/quivers/incremental/queries.py
run
¶
run(table: QueryTable, key: str, compute: Callable[[], V]) -> tuple[V, tuple[tuple[QueryTable, str], ...]]
Run a query body, collecting what it reads.
| PARAMETER | DESCRIPTION |
|---|---|
table
|
The query.
TYPE:
|
key
|
Its key.
TYPE:
|
compute
|
The body.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[V, tuple[tuple[QueryTable, str], ...]]
|
The result and its dependencies in reading order. |
| RAISES | DESCRIPTION |
|---|---|
QueryError
|
If the query is already running, which is a dependency cycle. |
Source code in src/quivers/incremental/queries.py
events
¶
events(action: QueryAction | None = None, *, since: int = 0) -> tuple[QueryEvent, ...]
The logged steps, optionally of one action and from one point.
| PARAMETER | DESCRIPTION |
|---|---|
action
|
Only steps of this action, or all when None.
TYPE:
|
since
|
Skip the first
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[QueryEvent, ...]
|
The steps in order. |
Source code in src/quivers/incremental/queries.py
bytes_fingerprint
¶
The fingerprint of a byte string.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The bytes.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Their BLAKE3 digest. |