wasm_redb_proto (wasm_redb v0.1.0)

View Source

Encodes requests to the redb guest and decodes its responses.

This is the Erlang half of engines/redb/src/abi.rs; the layouts are described there and in docs/abi.md. Each request function returns {Op, Payload} for wasm_storage_guest:call/4; decode/2 turns the guest's answer to that op into a term.

Transaction id 0 asks the guest for an implicit transaction: a fresh read transaction for a read, a fresh write transaction committed with the database's default durability for a write.

call/2 encodes a request in the caller's process, everything but the transaction id, which only the database process knows; in_txn/2 adds it there. A bad argument therefore fails in the caller, with {badarg, {What, Value}}, and never reaches the database process.

Summary

Functions

Opts: durability, two_phase_commit, quick_repair.

Encode a request that runs in a transaction, all but the transaction id. F names one of the functions below that take a transaction first, and Args are its other arguments. Raises {badarg, {What, Value}}.

Decode the body of a successful response to Op.

Whether the operation Op ends the transaction it runs in.

Whether an error Reason leaves the guest in a state that cannot be trusted: the database replaces the guest and reopens the file.

A request call/2 encoded, in transaction Txn.

Merge writes that run in one transaction into as few guest calls as their layout allows: a run of puts and batches becomes one batch, which opens each table once for all of them; any other write keeps a call of its own. Each step is the number of writes it stands for, and its call, as call/2 encodes it.

Opts: backend (host for device 0, memory for redb's in-guest memory), cache_size in bytes, durability (immediate or none, the default of implicit and explicit write transactions), two_phase_commit and quick_repair (booleans), and mode: exclusive (the default), single_writer (a writer that read-only guests may share the file with, through the host's locks) or read_only (one of those readers).

A chunk of a range. Opts: reverse (boolean), max_items (0 for no limit), max_bytes (at least one entry is always returned).

The atom for a guest error code (engines/redb/src/engine.rs, Code). Keep the two in step: the codes are numbered by hand on both sides.

Whether Call is a request call/2 could have returned.

Types

bound()

-type bound() :: unbounded | {inclusive, binary()} | {exclusive, binary()}.

durability()

-type durability() :: immediate | none | default.

op()

-type op() :: {put, binary(), binary(), binary()} | {delete, binary(), binary()}.

Functions

abort(Txn)

batch(Txn, Ops)

-spec batch(non_neg_integer(), [op()]) -> {non_neg_integer(), iodata()}.

begin_read()

begin_write(Opts)

Opts: durability, two_phase_commit, quick_repair.

call(F, Args)

-spec call(atom(), list()) -> {atom(), binary()}.

Encode a request that runs in a transaction, all but the transaction id. F names one of the functions below that take a transaction first, and Args are its other arguments. Raises {badarg, {What, Value}}.

check_integrity()

close()

commit(Txn)

compact(Maximum)

create_table(Txn, Table)

db_stats(Txn)

decode/2

Decode the body of a successful response to Op.

delete(Txn, Table, Key)

delete_range(Txn, Table, Lower, Upper)

delete_table(Txn, Table)

ends_transaction(Op)

-spec ends_transaction(non_neg_integer()) -> boolean().

Whether the operation Op ends the transaction it runs in.

fatal(Reason)

-spec fatal(atom()) -> boolean().

Whether an error Reason leaves the guest in a state that cannot be trusted: the database replaces the guest and reopens the file.

get(Txn, Table, Key)

in_txn/2

-spec in_txn({atom(), binary()}, non_neg_integer()) -> {non_neg_integer(), iodata()}.

A request call/2 encoded, in transaction Txn.

info()

list_tables(Txn)

merge_writes(Calls)

-spec merge_writes([{atom(), binary()}]) -> [{pos_integer(), {atom(), binary()}}].

Merge writes that run in one transaction into as few guest calls as their layout allows: a run of puts and batches becomes one batch, which opens each table once for all of them; any other write keeps a call of its own. Each step is the number of writes it stands for, and its call, as call/2 encodes it.

multi_get(Txn, Table, Keys)

open(Opts)

Opts: backend (host for device 0, memory for redb's in-guest memory), cache_size in bytes, durability (immediate or none, the default of implicit and explicit write transactions), two_phase_commit and quick_repair (booleans), and mode: exclusive (the default), single_writer (a writer that read-only guests may share the file with, through the host's locks) or read_only (one of those readers).

put(Txn, Table, Key, Value)

range(Txn, Table, Lower, Upper, Opts)

-spec range(non_neg_integer(), binary(), bound(), bound(), map()) -> {non_neg_integer(), iodata()}.

A chunk of a range. Opts: reverse (boolean), max_items (0 for no limit), max_bytes (at least one entry is always returned).

reason/1

The atom for a guest error code (engines/redb/src/engine.rs, Code). Keep the two in step: the codes are numbered by hand on both sides.

table_len(Txn, Table)

table_stats(Txn, Table)

valid/1

-spec valid(term()) -> boolean().

Whether Call is a request call/2 could have returned.