Wrapper around Mnesia Backup & Restore callback modules.
Khepri uses the same callback module as Mnesia to implement its import and export feature. The Mnesia Backup & Restore API is described in the Mnesia documentation.
Mnesia doesn't provide an Erlang behavior module to ease development. This module can be used as the behavior instead. For example:-module(my_export_module).
-behavior(khepri_import_export).
-export([open_write/1,
write/2,
commit_write/1,
abort_write/1,
open_read/1,
read/1,
close_read/1]).
There are two sets of callback functions to implement: one used during export (or "backup" in Mnesia terms), one used during import (or "restore" in Mnesia terms).
Module:open_write(Args)
This function is called at the beginning of an export. It is responsible
for any initialization and must return an {ok, ModulePriv}
tuple.
ModulePriv
is a private state passed to the following functions.
Module:write(ModulePriv, BackupItems)
This function is called for each subset of the items to export.
BackupItems
is a list of opaque Erlang terms. The callback module
can't depend on the structure of these Erlang terms. Only the fact that it
is a list is guarantied.
An empty list of BackupItems
means this is the last call to
Module:write/2
. Otherwise, the way all items to export are split into
several subsets and thus several calls to Module:write/2
is
undefined.
At the end of each call, the function must return {ok, NewModulePriv}
.
This new private state is passed to the subsequent calls.
Module:commit_write(ModulePriv)
This function is called after successful calls to Module:write/2
. It
is responsible for doing any cleanup.
This function can return ok
or {ok, Ret}
if it wants to return some
Erlang terms to the caller.
Module:abort_write(ModulePriv)
This function is called after failed call to Module:write/2
, or if
reading from the Khepri store fails. It is responsible for doing any
cleanup.
This function can return ok
or {ok, Ret}
if it wants to return some
Erlang terms to the caller.
Module:open_read(Args)
This function is called at the beginning of an import. It is responsible
for any initialization and must return an {ok, ModulePriv}
tuple.
ModulePriv
is a private state passed to the following functions.
Module:read(ModulePriv)
This function is one or more times until there is nothing left to import.
The function must return {ok, BackupItems, NewModulePriv}
. This new
private state is passed to the subsequent calls.
BackupItems
is a list of opaque Erlang terms. The callback module
can't depend on the structure of these Erlang terms. The backup items must
be returned exactly as they were at the time of the export and in the same
order.
An empty list of BackupItems
means this is the last batch.
Module:read/1
won't be called anymore. Module:close_read/1
will be
called next instead.
Module:close_read(ModulePriv)
This function is called after the last call to Module:read/1
,
successful or not, or if the actual import into the Khepri store fails. It
is responsible for doing any cleanup.
This function can return ok
or {ok, Ret}
if it wants to return some
Erlang terms to the caller.
khepri_export_erlang
. This module offers to
export Khepri tree nodes in a plain text file where backup items are
formatted as Erlang terms.
backup_item() = term()
An opaque term passed in a list to Module:write/2
and returned by
Module:read/1
.
module_priv() = any()
Private data passed to Module:open_write/1
or Module:open_read/1
initially.
Generated by EDoc