wasm_storage behaviour (wasm_storage v0.1.0)
View SourceRandom-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/3returns at mostLenbytes. It returns fewer only when the range crosses the end of the device, andeofwhen it starts at or past the end.pwrite/3writes 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/2truncates, 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/2returnsokonce every completed write is as durable as the device can make it.info/1says how durable that is: on adurabledevice 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);volatiledevices 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
-opaque handle()
-type sync_mode() :: data | full.
Callbacks
-callback pread(State :: term(), non_neg_integer(), non_neg_integer()) -> {ok, binary()} | eof | {error, reason()}.
-callback pwrite(State :: term(), non_neg_integer(), iodata()) -> ok | {error, reason()}.
-callback resize(State :: term(), non_neg_integer()) -> ok | {error, reason()}.
-callback size(State :: term()) -> {ok, non_neg_integer()} | {error, reason()}.
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.
-spec size(handle()) -> {ok, non_neg_integer()} | {error, reason()}.
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.
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.