Queries and mutations

The example stores the people in a database and queries their names. It collects query pages, updates Ada's age, and reads the changed age.

The optional adaptive query planner learns from runs and compares alternative plans for stored queries. Enable it through the database's plugins open option.

Example

The example compares these results:

  • a committed mutation
  • a repeated mutation
  • a query that matches no entity

Give Ada her birthday and read her age

birthday-text holds this document:

json186 B
{
  "query": {
    "find": [
      "?e"
    ],
    "where": [
      [
        "?e",
        "name",
        "Ada"
      ]
    ]
  },
  "patch": {
    "?e": {
      "age": 37
    }
  }
}

query-text holds this document:

json150 B
{
  "find": [
    "?age"
  ],
  "where": [
    [
      "?e",
      "name",
      "Ada"
    ],
    [
      "?e",
      "age",
      "?age"
    ]
  ]
}
clojure266 B
(with-open [source (sd/parse birthday-text :json)
            birthday (sd/mutation db source)]
  (sd/run birthday))
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query)]
  (sd/all-pages rows))
Result / 25 B
[
  {
    "age": 37
  }
]

Query pages

Each query page is an owned document.

  • You can keep a page after you release the rows handle, query, or database that produced it.
  • Release each page when you finish with its values. The document ownership rule applies.

Cached joins

The database can keep completed native fact joins as owned binding rows before aggregation. When you run the same query and plan again, the cache can supply those rows at the same database snapshot. Aggregates and output expressions still run for each call.

The cache uses the database working-memory budget and gives memory back when queries need it. The cache discards rows from joins that do not complete. After a write, new queries use a new snapshot. An active reader keeps the rows from its original snapshot.

Parameterized, temporal and overlay queries use their normal execution paths. Queries with predicates, derivations, member clauses or transaction terms also use those paths. Background planner trials and sampled foreground measurements execute joins directly.

Exact indexes

A query or mutation definition that compares a field with a value makes the database build that field's exact index. The comparison is a fact clause with a constant or a parameter value, or a =, <, <=, >, >= or hasPrefix predicate on a fact clause's value. With the index, a hasPrefix predicate reads only the texts that start with the prefix, and no value of another type.

  • Storing the definition starts the build. An inline query or mutation that compares a field the same way also starts one.
  • The database builds the index in the background, including the values committed before the definition. The call that started it does not wait.
  • Queries scan the field until its index is ready. The results are the same; only the time differs.
  • Use indexes_sync to wait until every index that earlier definitions and inline runs ask for is ready, for example before you measure a query.
  • An index that no definition uses any more stays until the database next opens.

Operations

OperationReturnsSummary
read-entitydocumentdatabase.read_entity(entity, max_depth, max_entities) atomically returns an owned {document links revisions truncated} document, expanding links up to max_depth hops.
queryqueryOpen an owned query from a path or an inline definition.
mutationmutationOpen an owned mutation from a path or an inline definition.
runrowsRun the query and return an owned row cursor.
closenoneRelease the query handle.
nextdocumentRead the next page as an owned document.
pagespagesrows.pages() iterates the non-empty pages of the cursor.
all-pagesdocumentrows.all_pages() reads every remaining page into one owned array document.
closenoneRelease the rows handle.
subscribesubscriptionquery.subscribe(parameters, options) opens an owned live subscription.
nexteventWait for the next live event.
eventsUnavailablesubscription.events() delivers events in order until the subscription is cancelled, closes or fails.
cancelnonesubscription.cancel() ends the subscription.
closenoneRelease the subscription handle.
runcommitRun the mutation and return commit information.
closenoneRelease the mutation handle.
merge-patchcommitdatabase.merge_patch(patch) applies an entity-keyed patch document in one transaction and returns commit information.
indexes-syncnonedatabase.indexes_sync(max_wait) waits until the database built every exact index that earlier definition commits and inline runs ask for.
bulk-beginnonedatabase.bulk_begin() starts a bulk build of data merge patches held by the database.
bulk-addnonedatabase.bulk_add(patch) copies and queues one merge-patch document in the bulk build.
bulk-finishdocumentdatabase.bulk_finish() publishes the bulk build with one commit, ends it and returns an owned summary document.
bulk-abortnonedatabase.bulk_abort() drops the bulk build in progress without writing it.

read-entity

clojure11 B
[db entity]
clojure34 B
[db entity max-depth max-entities]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
entityIntegerValueRequiredThe decimal user entity ID.
max-depthIntegerValueAbsentLink hops to expand along each path, 0 through 32. 0, the default, leaves every link a {#link id} reference.
max-entitiesIntegerValueAbsentThe most entity documents the result holds, counting the root and every expanded or absent target each time it is placed. 0 selects the default of 1000.

Errors

StatusWhen
:invalid-argumentthe entity ID is outside the user entity range, max_depth is negative or above 32, or max_entities is negative
:closedthe database is closed
:out-of-memorynative memory is exhausted

Notes

revisions maps each placed entity's decimal ID to its last-touch transaction for #_tx.stardust/checkAndSet. Empty object and array fields are omitted recursively. See entity reads for result fields, JSON Pointer links and expansion rules.

Examples

Read Bob with his mentor expanded

clojure25 B
(sd/read-entity db 2 1 0)
Result / 291 B
{
  "document": {
    "age": 41,
    "mentor": {
      "age": 36,
      "name": "Ada",
      "role": "engineer"
    },
    "name": "Bob",
    "role": "engineer"
  },
  "links": {
    "/mentor": {
      "#link": 1
    }
  },
  "revisions": {
    "1": 4,
    "2": 4
  },
  "truncated": false
}

C exports: stardust_db_read_entity, stardust_result_take_document, stardust_result_error, stardust_result_release

query

clojure11 B
[db source]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
sourceSourceValueRequiredAn inline native definition or a stored definition path.

Errors

StatusWhen
:not-foundno definition is stored at the path
:invalid-definitionthe definition is invalid
:closedthe database is closed
:out-of-memorynative memory is exhausted

Notes

Inline definitions are copied. Stored paths select their definition at run time.

Query documents.

Examples

Open an inline query and read its first page

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure144 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query)]
  (sd/next rows))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

Refuse an invalid query definition

definition-text holds this document:

json29 B
{
  "find": [
    "?x"
  ]
}
clojure84 B
(with-open [definition (sd/parse definition-text :json)]
  (sd/query db definition))
Result / 168 B
{
  "code": "unbound_variable",
  "message": "Unbound_Variable",
  "path": null,
  "field": null,
  "entity": null,
  "offset": null,
  "status": "invalid_definition"
}

Report a missing stored query

clojure30 B
(sd/query db "people/missing")
Result / 174 B
{
  "code": "not_found",
  "message": "no definition at this path",
  "path": "people/missing",
  "field": null,
  "entity": null,
  "offset": null,
  "status": "not_found"
}

C exports: stardust_db_query_open, stardust_db_query_error

mutation

clojure11 B
[db source]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
sourceSourceValueRequiredAn inline native definition or a stored definition path.

Errors

StatusWhen
:not-foundno definition is stored at the path
:invalid-definitionthe definition is invalid
:closedthe database is closed
:out-of-memorynative memory is exhausted

Notes

Inline definitions are copied. Stored paths select their definition at run time.

Mutation documents.

Examples

database/mutation

birthday-text holds this document:

json186 B
{
  "query": {
    "find": [
      "?e"
    ],
    "where": [
      [
        "?e",
        "name",
        "Ada"
      ]
    ]
  },
  "patch": {
    "?e": {
      "age": 37
    }
  }
}
clojure116 B
(with-open [source (sd/parse birthday-text :json)
            mutation (sd/mutation db source)]
  (sd/run mutation))
Result / 111 B
{
  "status": "committed",
  "transaction": 5,
  "facts": 1,
  "committed_at": 17923367749351308,
  "ids": {}
}

C exports: stardust_db_mutation_open, stardust_db_mutation_error

run

clojure8 B
[handle]
clojure19 B
[handle parameters]
clojure50 B
[handle parameters {:keys [max-cost] :as options}]

Parameters

ParameterTypeOwnershipDefaultMeaning
handleQueryBorrowed for the callRequiredThe query owner for this operation. Keep it alive during the call.
parametersDocumentBorrowed for the callAbsent means no supplied parametersAn optional object of parameter values that merge-patches the definition's declared parameters. A parameter matched in the entity position of a fact clause takes an entity reference {#link id}, never the bare ID number: a commit returns IDs as numbers, so wrap one as {#link id}, or as the typed Link value where the binding has one, before passing it. A query run fails with entity_reference_expected when a bare number reaches that position. Projected and found entities also read back as {#link id}, so a typed field that receives one uses Link rather than a plain integer.
optionsrun-optionsValueAbsent selects operation defaultsThe operation's options record. See its field defaults.

Errors

StatusWhen
:not-foundthe stored query definition was deleted
:invalid-definitionthe loaded query definition is invalid
:invalid-argumentthe parameters or run options are invalid
:evaluation-failedthe query fails or exceeds max_cost
:closedthe query is closed
:out-of-memorynative memory is exhausted

Notes

Query documents.

Examples

Open an inline query and read its first page

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure144 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query)]
  (sd/next rows))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

Capture the host duration boundary

boundary-text holds this document:

json20 B
9223372036854775807

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure357 B
(with-open [boundary (sd/parse boundary-text :json)
            definition (sd/parse query-text :json)
            query (sd/query db definition)]
  (let [max-cost (java.time.Duration/ofSeconds (sd/i64 boundary (sd/root boundary)))
        options {:page-size 2 :max-cost max-cost :as-of 0}]
    (with-open [rows (sd/run query nil options)]
      options)))
Result / 69 B
{
  "page_size": 2,
  "max_cost": 9223372036854775807,
  "as_of": 0
}

Truncate a positive sub-tick duration

boundary-text holds this document:

json3 B
99

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure355 B
(with-open [boundary (sd/parse boundary-text :json)
            definition (sd/parse query-text :json)
            query (sd/query db definition)]
  (let [max-cost (java.time.Duration/ofNanos (sd/i64 boundary (sd/root boundary)))
        options {:page-size 2 :max-cost max-cost :as-of 0}]
    (with-open [rows (sd/run query nil options)]
      options)))
Result / 51 B
{
  "page_size": 2,
  "max_cost": 0,
  "as_of": 0
}

Explain query execution

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure187 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            ^java.io.Closeable rows (sd/run query nil {:explain? true})]
  (sd/explain rows))
Result / 670 B
{
  "load_definition": {
    "cpu": 836,
    "wall": 892,
    "arena_bytes": 1680
  },
  "parameters": {
    "cpu": 7,
    "wall": 12,
    "arena_bytes": 0
  },
  "run": {
    "cpu": 12141,
    "wall": 12239,
    "arena_bytes": 7424
  },
  "total": {
    "cpu": 13019,
    "wall": 13177,
    "arena_bytes": 9104
  },
  "arena_capacity": 4194304,
  "work": 8593,
  "rows": 3,
  "compile": 413,
  "plan": 697,
  "execute": 0,
  "group": 0,
  "order": 0,
  "memory_bytes": 5995,
  "memory_refusals": 0,
  "budget_bytes": 10489856512,
  "budget_used_bytes": 10194,
  "budget_peak_bytes": 14267,
  "budget_refusals": 0,
  "retry": 0,
  "stages_completed": 3,
  "retries": 0
}

A negative sub-tick Java duration rounds toward negative infinity

clojure65 B
{:page-size 0 :max-cost (java.time.Duration/ofNanos -1) :as-of 0}
Result / 52 B
{
  "page_size": 0,
  "max_cost": -1,
  "as_of": 0
}

Run with typed query options

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure240 B
(let [options {:page-size 2 :max-cost (java.time.Duration/ofSeconds 1) :as-of 0}]
  (with-open [definition (sd/parse query-text :json)
              query (sd/query db definition)
              rows (sd/run query nil options)]
    options))
Result / 58 B
{
  "page_size": 2,
  "max_cost": 10000000,
  "as_of": 0
}

C exports: stardust_db_query_run, stardust_db_rows_error

close

clojure8 B
[handle]

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
handleQueryConsumed ownerRequiredThe query owner to release. Do not use it after this call.

Examples

Open an inline query and read its first page

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure144 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query)]
  (sd/next rows))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

C exports: stardust_db_query_release

next

clojure8 B
[handle]

Parameters

ParameterTypeOwnershipDefaultMeaning
handleRowsBorrowed for the callRequiredThe rows owner for this operation. Keep it alive during the call.

Errors

StatusWhen
:evaluation-failedthe page does not render
:closedthe rows is closed
:out-of-memorynative memory is exhausted

Notes

An empty array ends the cursor. Every page reads the snapshot selected by the run. Returned pages survive cursor and database closure.

Examples

Open an inline query and read its first page

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure144 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query)]
  (sd/next rows))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

C exports: stardust_db_rows_next, stardust_result_take_document, stardust_result_error, stardust_result_release

pages

clojure6 B
[rows]

Parameters

ParameterTypeOwnershipDefaultMeaning
rowsRowsBorrowed for the callRequiredThe rows owner for this operation. Keep it alive during the call.

Errors

StatusWhen
:evaluation-faileda page does not render
:closedthe rows is closed
:out-of-memorynative memory is exhausted

Notes

Pages are read lazily from the run's snapshot. Rust callers own each page. In other bindings, a page closes when iteration advances. Use next to retain an owned page. Close the cursor separately.

Examples

Iterate the pages of a query run

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure187 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query nil {:page-size 1})]
  (reduce (fn [n _page] (inc n)) 0 rows))
Result / 1 B
3

C exports: stardust_db_rows_next, stardust_result_take_document, stardust_result_error, stardust_result_release, stardust_document_root, stardust_document_read_many, stardust_document_release

all-pages

clojure6 B
[rows]

Parameters

ParameterTypeOwnershipDefaultMeaning
rowsRowsBorrowed for the callRequiredThe rows owner for this operation. Keep it alive during the call.

Errors

StatusWhen
:evaluation-faileda page does not render
:closedthe rows is closed
:out-of-memorynative memory is exhausted

Notes

The array contains rows, not page documents. Previously read pages are excluded. Memory grows with the remaining result. Use pages for bounded page-by-page processing. Close the cursor separately.

Examples

Read all remaining rows into one document

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure149 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query)]
  (sd/all-pages rows))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

C exports: stardust_db_rows_next, stardust_result_take_document, stardust_result_error, stardust_result_release, stardust_document_count, stardust_document_read_many, stardust_document_i64, stardust_document_f64, stardust_document_release, stardust_document_builder_create, stardust_document_builder_add_many, stardust_document_builder_seal, stardust_document_builder_release

close

clojure8 B
[handle]

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
handleRowsConsumed ownerRequiredThe rows owner to release. Do not use it after this call.

Examples

Open an inline query and read its first page

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure144 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            rows (sd/run query)]
  (sd/next rows))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

C exports: stardust_db_rows_release

subscribe

clojure7 B
[query]
clojure18 B
[query parameters]
clojure49 B
[query parameters {:keys [max-cost] :as options}]

Parameters

ParameterTypeOwnershipDefaultMeaning
queryQueryBorrowed for the callRequiredThe query owner for this operation. Keep it alive during the call.
parametersDocumentBorrowed for the callAbsent means no supplied parametersAn optional object of parameter values that merge-patches the definition's declared parameters. A parameter matched in the entity position of a fact clause takes an entity reference {#link id}, never the bare ID number: a commit returns IDs as numbers, so wrap one as {#link id}, or as the typed Link value where the binding has one, before passing it. A query run fails with entity_reference_expected when a bare number reaches that position. Projected and found entities also read back as {#link id}, so a typed field that receives one uses Link rather than a plain integer.
optionsrun-optionsValueAbsent selects operation defaultsThe operation's options record. See its field defaults.

Errors

StatusWhen
:invalid-definitionthe query or its callable definitions are invalid
:invalid-argumentthe parameters or subscription options are invalid
:closedthe query is closed
:out-of-memorynative memory is exhausted

Notes

The subscription copies its definition and parameters. Later definition edits do not change it. The page_size option is ignored.

Subscriptions register readiness callbacks automatically. Event waits use runtime notifications instead of blocking in the C ABI.

Query documents.

Examples

query/subscribe

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure305 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            subscription (sd/subscribe query)]
  (let [event (or (sd/next subscription (java.time.Duration/ofSeconds 5))
                  (throw (java.util.concurrent.TimeoutException.)))]
    (:result event)))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

Read the complete initial subscription event

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure259 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            subscription (sd/subscribe query)]
  (or (sd/next subscription (java.time.Duration/ofSeconds 5))
      (throw (java.util.concurrent.TimeoutException.))))
Result / 174 B
{
  "sequence": 1,
  "superseded": 0,
  "transaction": 4,
  "result": [
    {
      "name": "Ada"
    },
    {
      "name": "Bob"
    },
    {
      "name": "Cy"
    }
  ]
}

C exports: stardust_db_query_subscribe, stardust_db_subscription_set_notify, stardust_db_subscription_error

next

clojure23 B
[subscription max-wait]

Parameters

ParameterTypeOwnershipDefaultMeaning
subscriptionSubscriptionBorrowed for the callRequiredThe subscription owner for this operation. Keep it alive during the call.
max-waitjava.time.DurationValueRequiredThe maximum wait. Zero checks once.

Errors

StatusWhen
:invalid-argumentmax_wait is negative
:cancelledthe subscription is cancelled or closed
:evaluation-faileda rerun of the query failed. Every later call reports this failure
:closedthe subscription is closed
:out-of-memorynative memory is exhausted

Notes

Each event replaces the entire result. superseded counts skipped computed results, not commits. Cancel, close and database close wake blocked waits. Windows rounds deadlines to milliseconds.

Examples

subscription/next

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure305 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            subscription (sd/subscribe query)]
  (let [event (or (sd/next subscription (java.time.Duration/ofSeconds 5))
                  (throw (java.util.concurrent.TimeoutException.)))]
    (:result event)))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

C exports: stardust_db_subscription_next, stardust_db_subscription_error, stardust_document_release

events

A subscription is reducible. Use reduce or into to consume events.

C exports: stardust_db_subscription_next, stardust_db_subscription_error

cancel

clojure14 B
[subscription]

Parameters

ParameterTypeOwnershipDefaultMeaning
subscriptionSubscriptionBorrowed for the callRequiredThe subscription owner for this operation. Keep it alive during the call.

Notes

Repeated cancellation is safe. Close the subscription separately to release its handle.

Examples

subscription/cancel

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure334 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            subscription (sd/subscribe query)]
  (let [event (or (sd/next subscription (java.time.Duration/ofSeconds 5))
                  (throw (java.util.concurrent.TimeoutException.)))]
    (sd/cancel subscription)
    (:result event)))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

C exports: stardust_db_subscription_cancel

close

clojure8 B
[handle]

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
handleSubscriptionConsumed ownerRequiredThe subscription owner to release. Do not use it after this call.

Examples

subscription/close

query-text holds this document:

json134 B
{
  "find": [
    "?name"
  ],
  "where": [
    [
      "?e",
      "name",
      "?name"
    ]
  ],
  "orderBy": [
    "?name"
  ]
}
clojure305 B
(with-open [definition (sd/parse query-text :json)
            query (sd/query db definition)
            subscription (sd/subscribe query)]
  (let [event (or (sd/next subscription (java.time.Duration/ofSeconds 5))
                  (throw (java.util.concurrent.TimeoutException.)))]
    (:result event)))
Result / 82 B
[
  {
    "name": "Ada"
  },
  {
    "name": "Bob"
  },
  {
    "name": "Cy"
  }
]

C exports: stardust_db_subscription_release

run

clojure8 B
[handle]
clojure19 B
[handle parameters]
clojure50 B
[handle parameters {:keys [max-cost] :as options}]

Parameters

ParameterTypeOwnershipDefaultMeaning
handleMutationBorrowed for the callRequiredThe mutation owner for this operation. Keep it alive during the call.
parametersDocumentBorrowed for the callAbsent means no supplied parametersAn optional object of parameter values that merge-patches the definition's declared parameters. A parameter matched in the entity position of a fact clause takes an entity reference {#link id}, never the bare ID number: a commit returns IDs as numbers, so wrap one as {#link id}, or as the typed Link value where the binding has one, before passing it. A query run fails with entity_reference_expected when a bare number reaches that position. Projected and found entities also read back as {#link id}, so a typed field that receives one uses Link rather than a plain integer.
optionsrun-optionsValueAbsent selects operation defaultsThe operation's options record. See its field defaults.

Errors

StatusWhen
:invalid-definitionthe mutation selection or definition is invalid
:invalid-argumentthe parameters or run options are invalid
:schema-violationthe write breaks a schema
:conflictconcurrent writes prevent the commit
:rejecteda mutation target or write guard refuses the write
:unavailablethe patch sets stardust/history and no enabled plugin purges history
:evaluation-failedthe mutation fails
:closedthe mutation is closed
:out-of-memorynative memory is exhausted

Notes

Mutation documents.

Examples

mutation/run

birthday-text holds this document:

json186 B
{
  "query": {
    "find": [
      "?e"
    ],
    "where": [
      [
        "?e",
        "name",
        "Ada"
      ]
    ]
  },
  "patch": {
    "?e": {
      "age": 37
    }
  }
}
clojure116 B
(with-open [source (sd/parse birthday-text :json)
            mutation (sd/mutation db source)]
  (sd/run mutation))
Result / 111 B
{
  "status": "committed",
  "transaction": 5,
  "facts": 1,
  "committed_at": 17923367756028911,
  "ids": {}
}

Explain mutation execution

birthday-text holds this document:

json186 B
{
  "query": {
    "find": [
      "?e"
    ],
    "where": [
      [
        "?e",
        "name",
        "Ada"
      ]
    ]
  },
  "patch": {
    "?e": {
      "age": 37
    }
  }
}
clojure150 B
(with-open [source (sd/parse birthday-text :json)
            mutation (sd/mutation db source)]
  (sd/explain (sd/run mutation nil {:explain? true})))
Result / 389 B
{
  "load_definition": {
    "cpu": 785,
    "wall": 790,
    "arena_bytes": 2320
  },
  "parameters": {
    "cpu": 10,
    "wall": 12,
    "arena_bytes": 0
  },
  "run": {
    "cpu": 9039,
    "wall": 40514,
    "arena_bytes": 43856
  },
  "total": {
    "cpu": 9937,
    "wall": 41433,
    "arena_bytes": 46176
  },
  "arena_capacity": 4194304,
  "stages_completed": 3,
  "attempts": 1
}

C exports: stardust_db_mutation_run, stardust_db_result_commit_info, stardust_result_error, stardust_result_release

close

clojure8 B
[handle]

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
handleMutationConsumed ownerRequiredThe mutation owner to release. Do not use it after this call.

Examples

mutation/close

birthday-text holds this document:

json186 B
{
  "query": {
    "find": [
      "?e"
    ],
    "where": [
      [
        "?e",
        "name",
        "Ada"
      ]
    ]
  },
  "patch": {
    "?e": {
      "age": 37
    }
  }
}
clojure116 B
(with-open [source (sd/parse birthday-text :json)
            mutation (sd/mutation db source)]
  (sd/run mutation))
Result / 111 B
{
  "status": "committed",
  "transaction": 5,
  "facts": 1,
  "committed_at": 17923367755020813,
  "ids": {}
}

C exports: stardust_db_mutation_release

merge-patch

clojure10 B
[db patch]
clojure18 B
[db patch options]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
patchDocumentBorrowed for the callRequiredAn entity-keyed native patch. Temporary names use #_name. #_tx holds metadata and an optional stardust/checkAndSet map from entity IDs to expected transactions. Positive expectations match exact last-touch transactions; zero requires no current facts. Conditions are consumed and checked atomically, including for no-ops.
optionsmapValue{}Binding options map. The :library key selects the loaded native library. Omission uses the binding's default library. Optional explain output. This record contains explain output for merge_patch.

Errors

StatusWhen
:invalid-commit-timethe #_tx commit time is not later than the last commit
:conflictan entity revision check fails or a concurrent writer prevents the commit; a revision mismatch reports the entity and expected and actual revisions
:rejectedthe database refuses the patch; the code and diagnostic path name the reason and key
:unavailablethe patch sets stardust/history and no enabled plugin purges history
:invalid-argumentthe patch is not an entity-keyed object, or a link names an ID outside the user entity range
:closedthe database is closed
:out-of-memorynative memory is exhausted

Notes

Patch documents.

Examples

database/merge_patch

seed-text holds this document:

json333 B
{
  "#_ada": {
    "name": "Ada",
    "role": "engineer",
    "age": 36
  },
  "#_bob": {
    "name": "Bob",
    "role": "engineer",
    "age": 41,
    "mentor": {
      "#link": "#_ada"
    }
  },
  "#_cy": {
    "name": "Cy",
    "role": "designer",
    "age": 29
  },
  "#_tx": {
    "author": "smoke",
    "reason": "seed"
  }
}
clojure74 B
(with-open [patch (sd/parse seed-text :json)]
  (sd/merge-patch db patch))
Result / 161 B
{
  "status": "committed",
  "transaction": 4,
  "facts": 12,
  "committed_at": 17923367746573834,
  "ids": {
    "#_ada": 1,
    "#_bob": 2,
    "#_cy": 3
  }
}

Explain patch validation and commit

seed-text holds this document:

json333 B
{
  "#_ada": {
    "name": "Ada",
    "role": "engineer",
    "age": 36
  },
  "#_bob": {
    "name": "Bob",
    "role": "engineer",
    "age": 41,
    "mentor": {
      "#link": "#_ada"
    }
  },
  "#_cy": {
    "name": "Cy",
    "role": "designer",
    "age": 29
  },
  "#_tx": {
    "author": "smoke",
    "reason": "seed"
  }
}
clojure104 B
(with-open [patch (sd/parse seed-text :json)]
  (sd/explain (sd/merge-patch db patch {:explain? true})))
Result / 298 B
{
  "copy_patch": {
    "cpu": 12321,
    "wall": 12502,
    "arena_bytes": 3096
  },
  "commit": {
    "cpu": 766,
    "wall": 33672,
    "arena_bytes": 8008
  },
  "total": {
    "cpu": 13252,
    "wall": 46346,
    "arena_bytes": 11118
  },
  "arena_capacity": 4194304,
  "stages_completed": 2
}

Refuse a metadata-only transaction

patch-text holds this document:

json38 B
{
  "#_tx": {
    "author": "x"
  }
}
clojure75 B
(with-open [patch (sd/parse patch-text :json)]
  (sd/merge-patch db patch))
Result / 199 B
{
  "code": "transaction_only",
  "message": "a patch of only #_tx is refused: #_tx annotates a write",
  "path": "#_tx",
  "field": null,
  "entity": null,
  "offset": null,
  "status": "rejected"
}

Refuse a repeated commit time

future-text holds this document:

json120 B
{
  "#_d": {
    "x": 1
  },
  "#_tx": {
    "stardust/committedAt": {
      "#utc": "2200-01-01T00:00:00Z"
    }
  }
}

older-text holds this document:

json120 B
{
  "#_e": {
    "x": 2
  },
  "#_tx": {
    "stardust/committedAt": {
      "#utc": "2200-01-01T00:00:00Z"
    }
  }
}
clojure152 B
(with-open [future (sd/parse future-text :json)
            older (sd/parse older-text :json)]
  (sd/merge-patch db future)
  (sd/merge-patch db older))
Result / 227 B
{
  "code": "invalid_commit_time",
  "message": "stardust/committedAt must be strictly newer than the latest commit time",
  "path": null,
  "field": null,
  "entity": null,
  "offset": null,
  "status": "invalid_commit_time"
}

C exports: stardust_db_merge_patch, stardust_db_result_commit_info, stardust_result_error, stardust_result_release

indexes-sync

clojure13 B
[db max-wait]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
max-waitjava.time.DurationValueRequiredThe maximum wait. Zero checks once.

Errors

StatusWhen
:invalid-argumentmax_wait is negative
:timeoutmax_wait passed first
:closedthe database is closed

Notes

An index that cannot be built does not hold this wait.

Examples

Wait for the exact indexes of the stored queries

clojure94 B
(sd/indexes-sync db (java.time.Duration/ofSeconds 5))
(sd/definitions db :queries "people/.*")
Result / 22 B
[
  "people/by_role"
]

C exports: stardust_db_indexes_sync

bulk-begin

clojure4 B
[db]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.

Errors

StatusWhen
:invalid-argumenta bulk build is already in progress on the database
:closedthe database is closed
:out-of-memorynative memory is exhausted

Notes

On a database with existing facts, each added patch commits immediately and the summary reports fallback true. Closing the database drops an unfinished build.

Examples

Start a bulk build and drop it

clojure37 B
(sd/bulk-begin db)
(sd/bulk-abort db)

C exports: stardust_db_bulk_begin

bulk-add

clojure10 B
[db patch]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
patchDocumentBorrowed for the callRequiredOne borrowed entity-keyed merge-patch document, as merge_patch takes it. Temporary names use #_name and are scoped to this patch. Transaction metadata uses #_tx. The document is borrowed only until the call returns.

Errors

StatusWhen
:invalid-argumentno bulk build is in progress
:rejectedthe database refuses the patch; the build has failed and later adds report the same status
:closedthe database is closed
:out-of-memorynative memory is exhausted

Notes

Patches retain add order. Entity and field order within a patch does not matter. Temporary names resolve within one patch. The call waits for space within the build's memory budget. After failure, use finish for its diagnostic or abort for cleanup.

Patch documents.

Examples

Queue patches in commit order

clojure325 B
;; The first entity of an empty database is 1, so the second patch updates it.
(sd/bulk-begin db)
(with-open [patch (sd/parse "{\"#_ada\":{\"name\":\"Ada\"}}" :json)]
  (sd/bulk-add db patch))
(with-open [patch (sd/parse "{\"1\":{\"title\":\"Countess\"}}" :json)]
  (sd/bulk-add db patch))
(with-open [_ (sd/bulk-finish db)])

C exports: stardust_db_bulk_add

bulk-finish

clojure4 B
[db]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.

Errors

StatusWhen
:invalid-argumentno bulk build is in progress
:rejectedthe database refuses a patch; an initial build publishes nothing and fallback keeps earlier commits
:conflictanother write committed to the database during the build
:closedthe database is closed
:out-of-memorynative memory is exhausted

Notes

Finish ends the build even on failure. An initial build publishes all patches or none. Fallback keeps patches already committed.

The summary contains:

FieldMeaning
statuscommitted, or no_change when nothing was written
patches, transactions, facts, entitiesLoad counts
firstTransaction, lastTransactionAssigned transaction range
fallbackWhether patches committed individually
threadsWorker count
tempBytesTemporary run bytes
memoryBytes, memoryPeakBytesReserved memory share and its peak use
phasesparse, assign, rows, updates, spill, build and commit timings, each with wallNs and cpuNs

The memory budget covers build allocations. It excludes operating-system file cache and the process baseline.

Examples

Load patches with one commit and read the summary

clojure408 B
;; bulk-load does the same for a sequence of patches, aborting on failure.
(sd/bulk-begin db)
(with-open [patch (sd/parse "{\"#_ada\":{\"name\":\"Ada\",\"title\":\"Countess\"}}" :json)]
  (sd/bulk-add db patch))
;; Finish ends the build also when it fails.
(with-open [summary (sd/bulk-finish db)]
  ;; status, patches, transactions, facts, entities and phase timings
  (:transactions (sd/to-value summary)))

C exports: stardust_db_bulk_finish, stardust_result_take_document, stardust_result_error, stardust_result_release

bulk-abort

clojure4 B
[db]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.

Notes

Abort preserves patches already committed in fallback mode. It is safe without a build or after database closure.

Examples

Drop a bulk build without writing it

clojure190 B
(sd/bulk-begin db)
(with-open [patch (sd/parse "{\"#_ada\":{\"name\":\"Ada\"}}" :json)]
  (sd/bulk-add db patch))
;; Abort writes nothing; without a build it does nothing.
(sd/bulk-abort db)

C exports: stardust_db_bulk_abort

run_options

This record supplies options for query and mutation run. Zero selects each default. Mutations use DB_Write_Options, whose fields are a subset.

FieldTypeUnitRangeMeaning
page_sizeCountCountHost type rangeRows per page. 0 selects the default page size. Queries only.
max_costDurationHost duration (100 ns ticks at the ABI)Tick rangeThread CPU duration limit. 0 selects the engine’s dynamic budget.
as_ofIntegerNoneHost type rangeA positive transaction runs a stored definition as of that transaction.

commit

This record contains the result of mutation run, merge_patch and patch_definition.

FieldTypeUnitRangeAbsentMeaning
statusCommit-StatusNoneHost type range——
transactionIntegerNoneHost type range—Zero when nothing was written.
factsIntegerCountHost type range——
committed_atInstantUTC instant (100 ns ticks since the Unix epoch at the ABI)Tick rangenilRecorded commit time as a UTC instant. The call measures this value. Independent calls can differ.
idsId-MapNoneHost type range—Maps each #_name of a merge patch to its entity ID number. Wrap an ID as {#link id}, or as the typed Link value where the binding has one, to pass it as an entity reference.

event

This record contains the result of subscription next.

FieldTypeUnitMeaning
sequenceIntegerCountEvent number. Event 1 is the initial result.
supersededIntegerCountIntermediate computed results skipped by this subscription since its previous event. Zero for its first event. The delivered result replaces them in full. This value counts neither commits nor changed rows.
transactionIntegerNoneTransaction the result was computed at.
resultDocumentNoneThe full query result. The event owns it and the consumer closes it.