wasm_storage_ns behaviour (wasm_storage v0.1.0)

View Source

Named storage: a root that devices are opened in by relative path.

A random-access device (wasm_storage) is enough for an engine that keeps one file, like redb. An engine that creates, deletes and renames files, like SQLite with its journal, also needs a namespace. A root is a capability: it grants a directory (or any other provider's notion of one) and nothing above it.

{ok, Root} = wasm_storage_ns:root({wasm_storage_dir, "/var/db/app"}, #{}),
{ok, H} = wasm_storage_ns:open(Root, <<"main.db">>, #{create => true}),
ok = wasm_storage_ns:delete(Root, <<"main.db-journal">>).

Paths are relative, /-separated binaries. This module refuses, before any provider sees them: an empty path, an absolute path, an empty, . or .. component, a backslash and a NUL byte. Providers refuse symbolic links.

Summary

Functions

Remove the entry at Path.

The identity of the entry at Path, for wasm_storage_owner.

What the provider says about the root.

Open the device at Path. Opts are those of the provider's device.

Rename From to To, replacing To. Atomic where the provider says so.

root(Spec, #{}).

Open a root. Spec is {Provider, Arg}.

What is at Path, without following a symbolic link.

Make the entries of a directory durable: creations, deletions and renames in it. <<>> is the root. A provider that cannot do it returns {error, enotsup}; it never returns ok without doing it.

The components of a valid relative path, or {error, einval}.

Types

root()

-opaque root()

stat()

-type stat() ::
          #{type := regular | directory | symlink | other,
            size := non_neg_integer(),
            identity := term()}.

Callbacks

delete/2

-callback delete(State :: term(), [binary()]) -> ok | {error, wasm_storage:reason()}.

info(State)

-callback info(State :: term()) -> map().

open/3

-callback open(State :: term(), [binary()], Opts :: map()) ->
                  {ok, wasm_storage:handle()} | {error, wasm_storage:reason()}.

rename/3

-callback rename(State :: term(), [binary()], [binary()]) -> ok | {error, wasm_storage:reason()}.

root(Arg, Opts)

-callback root(Arg :: term(), Opts :: map()) -> {ok, term()} | {error, wasm_storage:reason()}.

stat/2

-callback stat(State :: term(), [binary()]) -> {ok, stat()} | {error, wasm_storage:reason()}.

sync_directory/2

-callback sync_directory(State :: term(), [binary()]) -> ok | {error, wasm_storage:reason()}.

Functions

delete/2

-spec delete(root(), binary()) -> ok | {error, wasm_storage:reason()}.

Remove the entry at Path.

identity(Root, Path)

-spec identity(root(), binary()) -> {ok, term()} | {error, wasm_storage:reason()}.

The identity of the entry at Path, for wasm_storage_owner.

info(State)

-spec info(root()) -> map().

What the provider says about the root.

open/3

-spec open(root(), binary(), map()) -> {ok, wasm_storage:handle()} | {error, wasm_storage:reason()}.

Open the device at Path. Opts are those of the provider's device.

rename/3

-spec rename(root(), binary(), binary()) -> ok | {error, wasm_storage:reason()}.

Rename From to To, replacing To. Atomic where the provider says so.

root(Spec)

root(Spec, #{}).

root(Arg, Opts)

-spec root({module(), term()}, map()) -> {ok, root()} | {error, wasm_storage:reason()}.

Open a root. Spec is {Provider, Arg}.

stat/2

-spec stat(root(), binary()) -> {ok, stat()} | {error, wasm_storage:reason()}.

What is at Path, without following a symbolic link.

sync_directory/2

-spec sync_directory(root(), binary()) -> ok | {error, wasm_storage:reason()}.

Make the entries of a directory durable: creations, deletions and renames in it. <<>> is the root. A provider that cannot do it returns {error, enotsup}; it never returns ok without doing it.

validate/1

-spec validate(binary() | string()) -> {ok, [binary()]} | {error, einval}.

The components of a valid relative path, or {error, einval}.