Definitions

The example stores a query definition and executes it with different variables. It then changes the query ordering and updates the people that the query selects.

Example

Run stored definitions by path

c1.2 kB
stardust_db_source promote = {.kind = STARDUST_SOURCE_PATH, .path = {(const uint8_t *)"people/promote", 14}};
stardust_db_source by_role = {.kind = STARDUST_SOURCE_PATH, .path = {(const uint8_t *)"people/by_role", 14}};
stardust_db_mutation *mutation = NULL;
stardust_result *committed = NULL;
stardust_db_query *query = NULL;
stardust_db_rows *rows = NULL;
stardust_result *page = NULL;
stardust_document *result = NULL;
int64_t status = stardust_db_mutation_open(db, &promote, &mutation);
if (status == STARDUST_OK) status = stardust_db_mutation_run(mutation, NULL, NULL, NULL, &committed);
if (status == STARDUST_OK) status = stardust_db_query_open(db, &by_role, &query);
if (status == STARDUST_OK) status = stardust_db_query_run(query, NULL, NULL, NULL, &rows);
if (status == STARDUST_OK) status = stardust_db_rows_next(rows, &page);
if (status == STARDUST_OK) status = stardust_result_take_document(page, &result);
stardust_result_release(page);
stardust_db_rows_release(rows);
stardust_db_query_release(query);
stardust_result_release(committed);
stardust_db_mutation_release(mutation);
if (status != STARDUST_OK) {stardust_document_release(result); return status;}
Result / 33 B
[{name Ada} {name Bob} {name Cy}]

Definition versions

A transaction records each committed change to a stored definition.

  • An open definition handle identifies its stored path, so later calls can select the applicable version.
  • To read a specific historical version, use the as_of rules below.

Stored query and mutation plans

The plan operation below reads and controls the accepted plan for a stored query or mutation. It works without a planner plugin. Mutation selections use the query planner and report selection feedback. Background trials of a mutation execute its selection only. They do not apply its patch.

The adaptive planner can retain separate plans for exact parameter values. Values without a retained plan use the shared fallback plan. Measured repeated-use savings must justify the lookup, trial and storage costs within the database memory budget. Optional anchor probes can improve estimates when their measured cost justifies the work. An incomplete probe supplies a lower bound and cannot prove that a query is empty.

Use explain for the current accepted state and its paged revision history. Use status for the current state alone, or history for a history page. Each history entry includes the plan version, source and reason for the change. Trial reasons report the candidates and their measured outcomes.

The pin action prevents automatic replacement for every parameter value. The unpin action permits learning again. The replan action rebuilds the fallback plan and clears specialized parameter plans. It preserves the global pin. The revert action restores a complete historical state, including its pin and parameter plans. Revert requires the exact plan transaction and the same authored definition revision.

Explicit changes compare both authored and accepted plan revisions inside the commit. A concurrent definition or plan change reports conflict. Every returned plan document owns its buffers and remains readable after the database closes.

Operations

OperationReturnsSummary
stardust_db_definition_getStatus. document through outputdatabase.definition(kind, path, as_of) returns an owned authored definition document.
stardust_db_planStatus. document through outputdatabase.plan(kind, path, action, argument, page_size, max_bytes) reads or explicitly controls a stored query or mutation plan.
stardust_db_definition_listStatus. document through outputdatabase.definitions(kind, pattern) returns an owned array document of sorted matching definition paths.
stardust_db_definition_patchStatus. commit through outputdatabase.patch_definition(kind, path, patch) validates and stores a definition patch and returns commit information.

stardust_db_definition_get

c141 B
int64_t stardust_db_definition_get(stardust_db *db, stardust_bytes kind, stardust_bytes path, int64_t transaction, stardust_result **output);
c91 B
int64_t stardust_result_take_document(stardust_result *result, stardust_document **output);
c95 B
int64_t stardust_result_error(const stardust_result *result, stardust_diagnostic_view *output);
c54 B
void stardust_result_release(stardust_result *result);

Parameters

stardust_db_definition_get

ParameterTypeOwnershipDefaultMeaning
dbstardust_db *Borrowed handleRequiredKeep the handle alive until the call returns.
kindstardust_bytesValueRequiredThe definition folder kind.
pathstardust_bytesValueRequiredThe stored definition path.
transactionint64_tValueRequiredValue passed to the call.
outputstardust_result **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_result_take_document

ParameterTypeOwnershipDefaultMeaning
resultstardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_document **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_result_error

ParameterTypeOwnershipDefaultMeaning
resultconst stardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_diagnostic_view *Caller-owned outputRequiredReceives a record. Its pointer fields follow the record's ownership contract.

stardust_result_release

ParameterTypeOwnershipDefaultMeaning
resultstardust_result *Consumed handleNULLEnds ownership. Do not use the handle after this call.

Errors

StatusWhen
STARDUST_NOT_FOUNDno definition is stored at the path
STARDUST_CLOSEDthe database is closed
STARDUST_OUT_OF_MEMORYnative memory is exhausted
STARDUST_INVALID_ARGUMENTa required input or output pointer is NULL, or an input owner is not ready

Examples

Read a stored query definition

c444 B
stardust_bytes kind = {(const uint8_t *)"queries", 7};
stardust_bytes path = {(const uint8_t *)"people/by_role", 14};
stardust_result *read = NULL;
stardust_document *result = NULL;
int64_t status = stardust_db_definition_get(db, kind, path, 0, &read);
if (status == STARDUST_OK) status = stardust_result_take_document(read, &result);
stardust_result_release(read);
if (status != STARDUST_OK) {stardust_document_release(result); return status;}
Result / 128 B
{find       [?name]
 orderBy    [?name]
 parameters {?role engineer}
 where      [[?e role ?role]
             [?e name ?name]]}

Also calls: stardust_result_take_document, stardust_result_error, stardust_result_release

stardust_db_plan

c189 B
int64_t stardust_db_plan(stardust_db *db, stardust_bytes kind, stardust_bytes path, stardust_bytes action, int64_t argument, int64_t page_size, int64_t max_bytes, stardust_result **output);
c91 B
int64_t stardust_result_take_document(stardust_result *result, stardust_document **output);
c95 B
int64_t stardust_result_error(const stardust_result *result, stardust_diagnostic_view *output);
c54 B
void stardust_result_release(stardust_result *result);

Parameters

stardust_db_plan

ParameterTypeOwnershipDefaultMeaning
dbstardust_db *Borrowed handleRequiredKeep the handle alive until the call returns.
kindstardust_bytesValueRequiredqueries or mutations.
pathstardust_bytesValueRequiredThe stored definition path.
actionstardust_bytesValueRequiredexplain, status, history, pin, unpin, replan or revert. Pin prevents every automatic replacement. Replan chooses a statistics plan and preserves a pin. Unpin permits learning. Revert restores an exact earlier plan transaction only if its authored revision still matches; it preserves that revision's pin.
argumentint64_tValueRequiredFor history or explain, the exclusive lower transaction bound, or zero to start. For revert, the exact historical plan transaction. Other actions require zero.
page_sizeint64_tValueRequiredFor history or explain, the positive maximum revision count. Other actions require zero. History remains durable; this count only bounds one response.
max_bytesint64_tValueRequiredFor history or explain, the positive diagnostic buffer capacity in bytes. The database may grant less. Exhaustion reports out_of_memory with no partial response. Other actions require zero. Returned documents own independent buffers after the call.
outputstardust_result **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_result_take_document

ParameterTypeOwnershipDefaultMeaning
resultstardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_document **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_result_error

ParameterTypeOwnershipDefaultMeaning
resultconst stardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_diagnostic_view *Caller-owned outputRequiredReceives a record. Its pointer fields follow the record's ownership contract.

stardust_result_release

ParameterTypeOwnershipDefaultMeaning
resultstardust_result *Consumed handleNULLEnds ownership. Do not use the handle after this call.

Errors

StatusWhen
STARDUST_INVALID_ARGUMENTthe kind, action, revision or page limits are invalid, or the selected plan cannot run the current authored definition
STARDUST_NOT_FOUNDthe definition or exact historical plan revision does not exist
STARDUST_CONFLICTthe authored definition or accepted plan changed before the explicit control committed
STARDUST_CLOSEDthe database is closed
STARDUST_OUT_OF_MEMORYnative memory is exhausted

Examples

Pin a plan and explain its revision history

c735 B
stardust_bytes kind = {(const uint8_t *)"queries", 7};
stardust_bytes path = {(const uint8_t *)"people/by_role", 14};
stardust_bytes pin = {(const uint8_t *)"pin", 3};
stardust_bytes history = {(const uint8_t *)"explain", 7};
stardust_result *outcome = NULL;
int64_t status = stardust_db_plan(db, kind, path, pin, 0, 0, 0, &outcome);
stardust_result_release(outcome);
if (status != STARDUST_OK) return status;
outcome = NULL;
stardust_document *result = NULL;
status = stardust_db_plan(db, kind, path, history, 0, 16, 1 << 20, &outcome);
if (status == STARDUST_OK) status = stardust_result_take_document(outcome, &result);
stardust_result_release(outcome);
if (status != STARDUST_OK) {stardust_document_release(result); return status;}
Result / 939 B
{transaction 12
 plan        {format     2
              hash       {1 0}
              order      [0 1]
              parameters ""
              pinned     true
              policy     0000000000000000
              reason     explicit pin
              revision   7e7de32f33f508a5
              settled    false
              source     Planned
              version    1}
 history     [{transaction 12
               plan        {format     2
                            hash       {1 0}
                            order      [0 1]
                            parameters ""
                            pinned     true
                            policy     0000000000000000
                            reason     explicit pin
                            revision   7e7de32f33f508a5
                            settled    false
                            source     Planned
                            version    1}}]
 nextAfter   0}

Also calls: stardust_result_take_document, stardust_result_error, stardust_result_release

stardust_db_definition_list

c124 B
int64_t stardust_db_definition_list(stardust_db *db, stardust_bytes kind, stardust_bytes pattern, stardust_result **output);
c91 B
int64_t stardust_result_take_document(stardust_result *result, stardust_document **output);
c95 B
int64_t stardust_result_error(const stardust_result *result, stardust_diagnostic_view *output);
c54 B
void stardust_result_release(stardust_result *result);

Parameters

stardust_db_definition_list

ParameterTypeOwnershipDefaultMeaning
dbstardust_db *Borrowed handleRequiredKeep the handle alive until the call returns.
kindstardust_bytesValueRequiredThe definition folder kind.
patternstardust_bytesValueRequiredThe pattern for definition paths. An absent pattern selects all paths in the requested folder.
outputstardust_result **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_result_take_document

ParameterTypeOwnershipDefaultMeaning
resultstardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_document **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_result_error

ParameterTypeOwnershipDefaultMeaning
resultconst stardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_diagnostic_view *Caller-owned outputRequiredReceives a record. Its pointer fields follow the record's ownership contract.

stardust_result_release

ParameterTypeOwnershipDefaultMeaning
resultstardust_result *Consumed handleNULLEnds ownership. Do not use the handle after this call.

Errors

StatusWhen
STARDUST_INVALID_ARGUMENTthe kind or the pattern is invalid
STARDUST_CLOSEDthe database is closed
STARDUST_OUT_OF_MEMORYnative memory is exhausted

Examples

database/definitions

c465 B
stardust_result *listed = NULL;
stardust_bytes kind = {(const uint8_t *)"queries", 7};
stardust_bytes pattern = {(const uint8_t *)"people/.*", 9};
int64_t status = stardust_db_definition_list(db, kind, pattern, &listed);
if (status != STARDUST_OK) { stardust_result_release(listed); return status; }
stardust_document *result = NULL;
status = stardust_result_take_document(listed, &result);
stardust_result_release(listed);
if (status != STARDUST_OK) return status;
Result / 16 B
[people/by_role]

database/definitions

c465 B
stardust_result *listed = NULL;
stardust_bytes kind = {(const uint8_t *)"queries", 7};
stardust_bytes pattern = {(const uint8_t *)"people/.*", 9};
int64_t status = stardust_db_definition_list(db, kind, pattern, &listed);
if (status != STARDUST_OK) { stardust_result_release(listed); return status; }
stardust_document *result = NULL;
status = stardust_result_take_document(listed, &result);
stardust_result_release(listed);
if (status != STARDUST_OK) return status;
Result / 2 B
[]

database/definitions

c465 B
stardust_result *listed = NULL;
stardust_bytes kind = {(const uint8_t *)"queries", 7};
stardust_bytes pattern = {(const uint8_t *)"people/.*", 9};
int64_t status = stardust_db_definition_list(db, kind, pattern, &listed);
if (status != STARDUST_OK) { stardust_result_release(listed); return status; }
stardust_document *result = NULL;
status = stardust_result_take_document(listed, &result);
stardust_result_release(listed);
if (status != STARDUST_OK) return status;
Result / 16 B
[people/by_role]

database/definitions

c465 B
stardust_result *listed = NULL;
stardust_bytes kind = {(const uint8_t *)"queries", 7};
stardust_bytes pattern = {(const uint8_t *)"people/.*", 9};
int64_t status = stardust_db_definition_list(db, kind, pattern, &listed);
if (status != STARDUST_OK) { stardust_result_release(listed); return status; }
stardust_document *result = NULL;
status = stardust_result_take_document(listed, &result);
stardust_result_release(listed);
if (status != STARDUST_OK) return status;
Result / 2 B
[]

Also calls: stardust_result_take_document, stardust_result_error, stardust_result_release

stardust_db_definition_patch

c160 B
int64_t stardust_db_definition_patch(stardust_db *db, stardust_bytes kind, stardust_bytes path, const stardust_document *patch_input, stardust_result **output);
c101 B
int64_t stardust_db_result_commit_info(const stardust_result *result, stardust_db_commit_info *info);
c95 B
int64_t stardust_result_error(const stardust_result *result, stardust_diagnostic_view *output);
c54 B
void stardust_result_release(stardust_result *result);

Parameters

stardust_db_definition_patch

ParameterTypeOwnershipDefaultMeaning
dbstardust_db *Borrowed handleRequiredKeep the handle alive until the call returns.
kindstardust_bytesValueRequiredThe definition folder kind.
pathstardust_bytesValueRequiredThe stored definition path.
patch_inputconst stardust_document *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_result **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_db_result_commit_info

ParameterTypeOwnershipDefaultMeaning
resultconst stardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
infostardust_db_commit_info *Caller-owned outputRequiredReceives a record. Its pointer fields follow the record's ownership contract.

stardust_result_error

ParameterTypeOwnershipDefaultMeaning
resultconst stardust_result *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_diagnostic_view *Caller-owned outputRequiredReceives a record. Its pointer fields follow the record's ownership contract.

stardust_result_release

ParameterTypeOwnershipDefaultMeaning
resultstardust_result *Consumed handleNULLEnds ownership. Do not use the handle after this call.

Errors

StatusWhen
STARDUST_INVALID_DEFINITIONthe patched definition is invalid
STARDUST_REJECTEDa validator of an enabled plugin refused the definition, such as an event definition outside a mode or with a schema that does not compile
STARDUST_FAILEDa reactor fails; the definition and all derived changes are rolled back
STARDUST_CLOSEDthe database is closed
STARDUST_OUT_OF_MEMORYnative memory is exhausted
STARDUST_INVALID_ARGUMENTa required input or output pointer is NULL, or an input owner is not ready

Examples

database/patch_definition

definition_text holds this document:

dust123 B
parameters {?role engineer}
find       [?name]
where      [[?e role ?role]
            [?e name ?name]]
orderBy    [?name]
c484 B
stardust_document *definition = NULL;
stardust_result *result = NULL;
stardust_bytes kind = {(const uint8_t *)"queries", 7};
stardust_bytes path = {(const uint8_t *)"people/by_role", 14};
int64_t status = stardust_parse(definition_text, STARDUST_DUST, &definition);
if (status == STARDUST_OK) status = stardust_db_definition_patch(db, kind, path, definition, &result);
stardust_document_release(definition);
if (status != STARDUST_OK) {stardust_result_release(result); return status;}
Result / 105 B
{status       committed
 transaction  3
 facts        0
 committed_at 17928498462077535
 ids          {}}

Also calls: stardust_db_result_commit_info, stardust_result_error, stardust_result_release