barrel_design (barrel_design v0.1.0)

View Source

Versioned designs with automatically maintained views.

A design packages named views for a database. Each view selects documents with a BQL predicate and builds ordered key/value rows, either with a native projection or with a Wasm map run under fresh state, finite fuel, a deadline and memory limits.

Summary

Functions

Install a named, versioned design in an open database. Modules maps each referenced module digest (SHA-256 of its .wasm bytes) to the module: its bytes, or {wat, Text}, which is encoded to its canonical bytes first (see module/1). Modules already stored need not be passed again. Everything is checked before anything is written: the manifest, the digests, and each module against the views that use it.

install/3, or with #{publish => true} a signed publication: the design is validated as install/3 would, signed with this node's publisher key when may_sign allows its name for this database, and written as a replicated statement with its modules attached. Receivers whose policy trusts the key install it from there; this node does too. Succeeds once this node's reconcile reports the statement accepted. A retry signs the same payload to the same document and completes whatever a failed attempt left undone.

Installed designs by name.

The bytes a design stores for a module and the digest it names the module by. {wat, Text} is encoded with erlang_wasm's canonical encoder, which writes no names: comments, layout and $identifiers leave the digest unchanged. {wasm, Bytes} or plain bytes are kept as they are, so a compiler's output keeps its own digest.

Run a view definition over sample documents without installing anything. Source is {wat, Text} or {wasm, Bytes}, used as the view's map module (its map omits module). Returns, per document, its rows, its error or that the selector skipped it. See preview/4.

Preview any view as install/3 would run it: a native map, or a Wasm map, with a built-in or a Wasm reducer in the same or another module. Modules is install/3's #{Digest => Bytes | {wat, Text}} (or a {wat, _} | {wasm, _} source for the map, as in preview/3). Inputs, the manifest and each module in its role are checked as at install; nothing is stored or loaded into the node's module cache.

The publication report of reconcile/1.

Read a view. Selection: key, key_prefix (whole leading components), start_key and end_key (with inclusive_end, default true). limit rows (default 100, at most 10000) and a continuation from the previous page. reduce (default: the view has a reducer) totals with _sum or _count, per group of the first group_level key components (0, the default, for one total; exact for whole keys); max_scan bounds the rows one call folds.

Build the view again from every document into a new generation; the current one serves reads until the new one has caught up.

Apply the database's publication statements now, as the database's process does when one arrives, and report every name: its state (active, withdrawn, version_conflict, refused_by_policy, waiting_for_modules, local_install), its origin, accepted mark and provenance, and each statement's state.

Catch up with the database's last write as of this call; see refresh/3.

Catch up with the database's last write as of this call. The target is captured once, so later writes cannot move it; concurrent refreshes share the indexer's batches. Returns the checkpoint reached (it may include later writes) or, after timeout_ms (default 1000), {error, {timeout, Reached}} while indexing continues: the checkpoint reached, building, or unknown when the indexer did not answer. A view already current answers at once without running any map.

Release a cursor before its time-to-live: the read it served is over. A token already expired or released is fine.

Generation, build status, checkpoint, error count and the node's guest-call counters for a view.

Remove a design, its views and their rows.

uninstall/2, or with #{withdraw => true} a signed withdrawal of the published version this node runs, written like a publication; it succeeds once this node's record shows it withdrawn.

Types

checkpoint/0

-type checkpoint() :: #{instance_id := binary(), last_seq := binary()}.

view_ref/0

-type view_ref() :: {Design :: binary(), View :: binary()}.

Functions

install(Db, Design, Modules0)

-spec install(binary(), map(), #{binary() => binary() | {wat, binary()} | {wasm, binary()}}) ->
                 {ok, map()} | {error, term()}.

Install a named, versioned design in an open database. Modules maps each referenced module digest (SHA-256 of its .wasm bytes) to the module: its bytes, or {wat, Text}, which is encoded to its canonical bytes first (see module/1). Modules already stored need not be passed again. Everything is checked before anything is written: the manifest, the digests, and each module against the views that use it.

Returns once the design is stored and its views are scheduled to build. Installing the same version again changes nothing; a higher version keeps the rows of every view whose signature is unchanged and rebuilds the others in a new generation, the old one serving reads until the new one has caught up.

Inputs are bounded before any of them is encoded or hashed: at most 16 modules, each WAT text or binary at most 4 MiB, 16 MiB in all, and a manifest of at most 256 KiB ({error, {input_too_large, What, Size, Max}}).

install(Db, Design, Modules, Opts)

-spec install(binary(), map(), map(), map()) -> {ok, map()} | {error, term()}.

install/3, or with #{publish => true} a signed publication: the design is validated as install/3 would, signed with this node's publisher key when may_sign allows its name for this database, and written as a replicated statement with its modules attached. Receivers whose policy trusts the key install it from there; this node does too. Succeeds once this node's reconcile reports the statement accepted. A retry signs the same payload to the same document and completes whatever a failed attempt left undone.

list(Db)

-spec list(binary()) -> {ok, #{binary() => map()}} | {error, term()}.

Installed designs by name.

module(Bytes)

-spec module({wat, binary() | string()} | {wasm, binary()} | binary()) ->
                {ok, Digest :: binary(), Bytes :: binary()} | {error, term()}.

The bytes a design stores for a module and the digest it names the module by. {wat, Text} is encoded with erlang_wasm's canonical encoder, which writes no names: comments, layout and $identifiers leave the digest unchanged. {wasm, Bytes} or plain bytes are kept as they are, so a compiler's output keeps its own digest.

preview(Source, View, Docs)

-spec preview({wat, binary() | string()} | {wasm, binary()} | map(), map(), [map()]) ->
                 {ok, [map()]} | {error, term()}.

Run a view definition over sample documents without installing anything. Source is {wat, Text} or {wasm, Bytes}, used as the view's map module (its map omits module). Returns, per document, its rows, its error or that the selector skipped it. See preview/4.

preview(Source, View, Docs, Opts)

-spec preview(map() | tuple(), map(), [map()], map()) -> {ok, map()} | {error, term()}.

Preview any view as install/3 would run it: a native map, or a Wasm map, with a built-in or a Wasm reducer in the same or another module. Modules is install/3's #{Digest => Bytes | {wat, Text}} (or a {wat, _} | {wasm, _} source for the map, as in preview/3). Inputs, the manifest and each module in its role are checked as at install; nothing is stored or loaded into the node's module cache.

At most max_preview_docs documents (default 1000). Maps run in the indexer's batches, and every batch and reducer call takes a slot of the node's guest budget before the deadline: timeout_ms (default the view's read_timeout_ms).

Returns #{docs => PerDocument, groups => Groups}, the groups at group_level (default exact). A Wasm reducer's groups are computed under leaf batches of 1, 7 and max_reduce_values values and rereduce fan-ins of 2 and max_reduce_values; different results answer {error, {inconsistent_reduce, Group, Results}}. This catches a reducer that depends on batch boundaries or on how partials combine. It does not, and need not, catch one that depends on value order: values always reach a reducer in index order.

published(Db)

-spec published(binary()) -> {ok, map()} | {error, term()}.

The publication report of reconcile/1.

query(Db, _, Opts0)

-spec query(binary(), view_ref(), map()) -> {ok, [map()], map()} | {error, term()}.

Read a view. Selection: key, key_prefix (whole leading components), start_key and end_key (with inclusive_end, default true). limit rows (default 100, at most 10000) and a continuation from the previous page. reduce (default: the view has a reducer) totals with _sum or _count, per group of the first group_level key components (0, the default, for one total; exact for whole keys); max_scan bounds the rows one call folds.

A view with build => on_first_read is not built at install: its first query answers {error, {building, Checkpoint}} and starts it, and a refresh or a wait_for read starts it and waits.

freshness: stale (default) answers from the last complete generation at once; {wait_for, Checkpoint} first waits, up to timeout_ms (default 1000), until the view covers that database write. authorize, a fun of the database name returning ok, must allow the caller to read the whole database: a view exposes data from every document it indexes.

Returns rows and #{generation, checkpoint, status, continuation}. Before the first build completes: {error, {building, Checkpoint}}.

A page followed by more pins its snapshot in a cursor: later pages, read with its continuation, see the same checkpoint and generation even while the view moves on or switches generation. A continuation resumes only the read it came from (same view, instance and selecting options; timing and budget options may differ) on the node that issued it, else {error, invalid_continuation}. A cursor unused for cursor_ttl_ms (default 30000) or evicted beyond max_cursors per database (default 256) answers {error, stale_cursor}. pin => true keeps the cursor even for a last page and adds page_start, a token for the first row of the page; release_cursor/2 frees a cursor early.

rebuild(Db, _)

-spec rebuild(binary(), view_ref()) -> {ok, non_neg_integer()} | {error, term()}.

Build the view again from every document into a new generation; the current one serves reads until the new one has caught up.

reconcile(Db)

-spec reconcile(binary()) -> {ok, map()} | {error, term()}.

Apply the database's publication statements now, as the database's process does when one arrives, and report every name: its state (active, withdrawn, version_conflict, refused_by_policy, waiting_for_modules, local_install), its origin, accepted mark and provenance, and each statement's state.

refresh(Db, View)

-spec refresh(binary(), view_ref()) -> {ok, checkpoint()} | {error, term()}.

Catch up with the database's last write as of this call; see refresh/3.

refresh(Db, _, Opts)

-spec refresh(binary(), view_ref(), map()) -> {ok, checkpoint()} | {error, term()}.

Catch up with the database's last write as of this call. The target is captured once, so later writes cannot move it; concurrent refreshes share the indexer's batches. Returns the checkpoint reached (it may include later writes) or, after timeout_ms (default 1000), {error, {timeout, Reached}} while indexing continues: the checkpoint reached, building, or unknown when the indexer did not answer. A view already current answers at once without running any map.

release_cursor(Db, Token)

-spec release_cursor(binary(), binary()) -> ok.

Release a cursor before its time-to-live: the read it served is over. A token already expired or released is fine.

status(Db, _)

-spec status(binary(), view_ref()) -> {ok, map()} | {error, term()}.

Generation, build status, checkpoint, error count and the node's guest-call counters for a view.

uninstall(Db, Name)

-spec uninstall(binary(), binary()) -> ok | {error, term()}.

Remove a design, its views and their rows.

uninstall(Db, Name, Opts)

-spec uninstall(binary(), binary(), map()) -> ok | {error, term()}.

uninstall/2, or with #{withdraw => true} a signed withdrawal of the published version this node runs, written like a publication; it succeeds once this node's record shows it withdrawn.