wasm_storage behaviour (wasm_storage v0.1.0)

View Source

Random-access storage devices.

A device is a flat, zero-initialized range of bytes addressed by 64-bit offsets. It may be a file, memory, or a wrapper that injects faults; the databases above it do not know which. This module is the behaviour every provider implements and the functions callers use. The exact semantics of each call are in the storage contract; the short version:

  • pread/3 returns at most Len bytes. It returns fewer only when the range crosses the end of the device, and eof when it starts at or past the end.
  • pwrite/3 writes every byte or fails. After a failure the target range holds unspecified bytes. Writing past the end extends the device; any gap reads as zeros.
  • resize/2 truncates, or extends with zeros.
  • A completed write is visible to every later read through any handle of the device, in the order the writes were made.
  • sync/2 returns ok once every completed write is as durable as the device can make it. info/1 says how durable that is: on a durable device a synced write survives a crash of the process or the node, and reaches the disk as far as the OS's sync takes it (on macOS, a barrier that does not flush the drive cache); volatile devices survive nothing past their own lifetime. Nothing here is proven against a power loss.
  • A handle is used by one process at a time. Calls on a closed handle return {error, closed}.

Open a device with a spec, {Provider, Arg}:

{ok, H} = wasm_storage:open({wasm_storage_file, "data.redb"},
                            #{create => true}),
ok = wasm_storage:pwrite(H, 0, <<"hello">>),
{ok, <<"hello">>} = wasm_storage:pread(H, 0, 5),
ok = wasm_storage:sync(H, data),
ok = wasm_storage:close(H).

Summary

Functions

Close the handle. Every later call on it returns {error, closed}.

The identity of the resource a spec names, without opening it. Two specs that reach the same file have the same identity.

What the provider says about the device, persistence included.

open(Spec, #{}).

Open a device. The calling process owns the handle: some providers (files) only accept calls from it.

Read up to Len bytes at Offset. See the module doc for short reads.

The provider module of a handle.

Write Data at Offset, extending the device if needed.

Set the size of the device. New bytes read as zeros.

The current size of the device, in bytes.

Make completed writes durable. data covers the bytes and the size; full also covers the other metadata the provider keeps.

Types

handle()

-opaque handle()

info()

-type info() :: #{provider := module(), persistence := durable | volatile, atom() => term()}.

reason()

-type reason() ::
          closed | eof | einval | enospc | eio | enoent | eexist | eacces | enotsup | ebadf | erofs |
          efbig |
          atom() |
          {atom(), term()}.

spec()

-type spec() :: {module(), term()}.

sync_mode()

-type sync_mode() :: data | full.

Callbacks

close(State)

-callback close(State :: term()) -> ok | {error, reason()}.

identity(Arg, Opts)

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

info(State)

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

open(Arg, Opts)

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

pread/3

-callback pread(State :: term(), non_neg_integer(), non_neg_integer()) ->
                   {ok, binary()} | eof | {error, reason()}.

pwrite/3

-callback pwrite(State :: term(), non_neg_integer(), iodata()) -> ok | {error, reason()}.

resize/2

-callback resize(State :: term(), non_neg_integer()) -> ok | {error, reason()}.

size(State)

-callback size(State :: term()) -> {ok, non_neg_integer()} | {error, reason()}.

sync/2

-callback sync(State :: term(), sync_mode()) -> ok | {error, reason()}.

Functions

close(State)

-spec close(handle()) -> ok | {error, reason()}.

Close the handle. Every later call on it returns {error, closed}.

identity(Arg, Opts)

-spec identity(spec(), map()) -> {ok, term()} | {error, reason()}.

The identity of the resource a spec names, without opening it. Two specs that reach the same file have the same identity.

info(State)

-spec info(handle()) -> info().

What the provider says about the device, persistence included.

open(Spec)

-spec open(spec()) -> {ok, handle()} | {error, reason()}.

open(Spec, #{}).

open(Arg, Opts)

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

Open a device. The calling process owns the handle: some providers (files) only accept calls from it.

pread/3

-spec pread(handle(), integer(), integer()) -> {ok, binary()} | eof | {error, reason()}.

Read up to Len bytes at Offset. See the module doc for short reads.

provider/1

-spec provider(handle()) -> module().

The provider module of a handle.

pwrite/3

-spec pwrite(handle(), integer(), iodata()) -> ok | {error, reason()}.

Write Data at Offset, extending the device if needed.

resize/2

-spec resize(handle(), integer()) -> ok | {error, reason()}.

Set the size of the device. New bytes read as zeros.

size(State)

-spec size(handle()) -> {ok, non_neg_integer()} | {error, reason()}.

The current size of the device, in bytes.

sync/2

-spec sync(handle(), sync_mode() | atom()) -> ok | {error, reason()}.

Make completed writes durable. data covers the bytes and the size; full also covers the other metadata the provider keeps.

A failed sync says nothing about writes made before it: they may or may not be durable. The caller decides what to do; nothing is retried here.