Module khepri_import_export

Wrapper around Mnesia Backup & Restore callback modules.

Description

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).

Export callback API

  1. 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.

  2. 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.

  3. 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.

  4. 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.

Import callback API

  1. 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.

  2. 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.

  3. 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.

Available callback modules

Khepri comes with khepri_export_erlang. This module offers to export Khepri tree nodes in a plain text file where backup items are formatted as Erlang terms.

Data Types

backup_item()

backup_item() = term()

An opaque term passed in a list to Module:write/2 and returned by Module:read/1.

The callback module must not depend on the content of this term.

module_priv()

module_priv() = any()

Private data passed to Module:open_write/1 or Module:open_read/1 initially.

The actual term is specific to the callback module implementation. The callback can use this term however it wants. It is returned from most callback functions and passed to the next one.


Generated by EDoc