05 Database requests
A client submits request Documents to an open database and polls response
Documents. Each request has a UUID message-id, a string method and a params
map. Each response has its own message-id, a correlation-id and a
causation-id that identifies its request. The result of a failed request is
an #err value of code, message and the failure's other members. The
namespace read methods and query read;
namespaces/save and transact write:
| Method | Parameters | Result |
|---|---|---|
transact |
code mutation map, optional bindings, session and dry-run |
tx or null and the ids of temporary names; with dry-run true, also dry-run and changes |
code is a native mutation value, {patch {...}} with optional if and
query members, as Transactions describes.
A string in code is never parsed. For example, against a new database:
{message-id #uuid "00000000-0000-4000-8000-000000000001" method "transact"
params {code {patch {_paul {name "Paul" spice 100}
_chani {name "Chani" partner _paul}}}}}
The response names the entity of each temporary name:
{message-id #uuid "10000000-0000-4000-8000-000000000001" correlation-id #uuid "00000000-0000-4000-8000-000000000001" causation-id #uuid "00000000-0000-4000-8000-000000000001" result {tx 6 ids {_chani #link 1 _paul #link 2}}}
ids maps each temporary name to its entity link; with a query, to the list
of its entities in row order, one per row whose body for the name holds a fact.
A transaction that changes nothing returns tx null and ids {}. A failed if
condition or two differing proposals for one field report
#err {code <conflict> …} and write nothing. A form the mutation compiler
refuses, or an expression that fails to compute, reports its position in
the request as an evaluation error does.
With dry-run true, transact does a dry run. The dry run does all the
steps of a real run in the write transaction: selection, expressions,
proposals, conditions and schema checks. Then it rolls back the transaction
and does not commit it. A dry run gives the same errors as a real run. It
publishes no transaction, sends no subscription event, and does not change the
ID sequence or the field catalog. dry-run false is a real run.
The result of a dry run has tx null, the ids that the real run would
allocate, dry-run true and a changes list. The list has one map for each
entity that the run would change, in ID order. entity is the entity link.
created is true for a new entity. name is the temporary name of the
entity, when it has one. fields maps each changed field to its action,
"assert", "retract" or "merge". before and after are the values of
the field before and after the transaction. A value is not in the map when
the field has no value. A merge is a map at a field that merges into the
entity that the field links to. When the link does not change, before and
after are the same link. For example:
{message-id #uuid "00000000-0000-4000-8000-000000000009" method "transact" params {dry-run true code {patch {2 {spice 60} _leto {name "Leto"}}}}}
Response:
{message-id #uuid "10000000-0000-4000-8000-000000000009" correlation-id #uuid "00000000-0000-4000-8000-000000000009" causation-id #uuid "00000000-0000-4000-8000-000000000009" result {tx null ids {_leto #link 24} dry-run true changes [{entity #link 2 created false fields {spice {action "assert" before 80 after 60}}} {entity #link 24 created true name "_leto" fields {name {action "assert" after "Leto"}}}]}}
A real run of the same request, with no other write between the two runs, allocates the same IDs.
Transactions and namespace saves share one writer: requests in one batch
publish in submission order, and each request is one transaction. Requests
with as-of, and sessions opened with as-of, are historical contexts and report
#err {code <read only> …}. A transaction runs under the request's CPU and
memory limits; a request cancelled or out of budget before its commit writes
nothing, and one cancelled after its commit still returns its result. If a
storage failure reports #err {code <storage failed> outcome "unknown"},
reopen the database and read the data before deciding whether to retry.
DUST evaluations attached to the database can also call named mutations; see
Transactions for literal, query-driven and saved-function examples.
Each call is its own transaction; an eval request is not an atomic batch of
all its calls. An error after a committed call reports outcome "committed"
and its transaction result. Preparation can run alongside queries; publication
uses the same single writer as the request methods above.
5.1. Schema checks and signatures
schemas/valid? requires value and exactly one of schema or name.
A schema is a native constraint value or an owned definition snapshot.
A literal string schema requires that exact string; it is never a namespace name.
A name is a qualified name such as "cargo/Shipment", resolved using the
optional session and its loaded namespace aliases. as-of selects the usual
historical context. Missing names and invalid schemas are request errors.
This complete request checks an integer:
[{message-id #uuid "00000000-0000-4000-8000-000000000001" method "schemas/valid?" params {schema #builtin "int?" value 80}}]
Its response is:
{id 1 result <valid>}
A mismatch returns {valid false diagnostics [...]}. Each diagnostic is the
same data that explain supplies: path points into the schema, in points
into the candidate, and schema, value and message describe the mismatch.
Both paths are native JSON Pointers. Checking never inserts defaults or changes
the candidate.
programs/signature requires code as native source forms. Optional bindings,
session and forms use the request interface's existing value conventions.
With forms true, code is a list and the response contains one signature
record per form. Source strings remain strings; the method does not parse them.
It analyzes definitions without evaluating the program or invoking its macros.
References to supplied bindings, namespaces or captured values stay symbolic
when the body alone does not establish their behavior.
[{message-id #uuid "00000000-0000-4000-8000-000000000008" method "programs/signature" params {code (fn [x] x)}}]
The complete response preserves the identity relation:
{id 8
result {<complete>
functions [{<complete>
clauses [{<!declared exact !variadic>
after []
contract ""
effect-inputs []
effect-outputs []
effects []
guaranteed {<possible>
constraints []
relations [{argument 0
count 0
function 0
kind "Aliases"
left {term {input -1
path []}}
operation "None"
right {term {input 0
path []}}}]}
inputs 1
possible {<possible>
constraints []
relations [{argument 0
count 0
function 0
kind "Aliases"
left {term {input -1
path []}}
operation "None"
right {term {input 0
path []}}}]}
required 1
result {<known>
alias {input 0
path []}
kinds []}}]
definition (fn [x] x)
environment "symbolic"
id 0}]}}
Each function has its source definition, a numeric id and clauses.
A clause includes inputs, required and variadic arity information,
result, exact, guaranteed and possible success regions, effects,
effect-inputs, effect-outputs and mutation after states.
Regions retain constraints and relations, including overflow and alias relations.
An input number of -1 denotes the result; a path's strings and integers select
fields and list elements. A function reference names another function's id.
complete false marks unsupported analysis or an analysis limit, without
claiming a guarantee. environment "symbolic" means the definition excludes
captured runtime state; it does not restore a closure.
Both methods return immutable owned Documents. A result remains usable after its session and database are released. Rendering a returned definition runs no validators. Cancellation and resource limits remain request failures. Foreign clients use the existing submit, poll, encode and release calls; there are no additional C exports.
The CLI can submit the same request file:
stardust request check-request.dust --config arrakis.dust
stardust request signature-request.dust --config arrakis.dust
request prints response pages as DUST. It exits nonzero for a request error;
a successful schema mismatch remains a result with valid false.
The request file holds one list of request maps, also for one request. For
another file, request sends no request. It writes the file name and the
problem to standard error and exits 2:
stardust: single.dust: expected a list of requests, got a map; write one request as [{...}]
stardust: item.dust: index 0: expected a request map, got an integer
stardust: empty.dust: expected a list of requests, got an empty file
stardust: bad.dust:1:7: invalid request file: Unexpected_End
A request that the database refuses before it runs, such as one whose method
is not a string, stops the command with exit 1. The message gives the index of
that request in the list. The command prints the responses of the requests
that the database accepted before the refusal. The other requests do not run.