Module khepri_tx

Khepri API for transactional queries and updates.

Description

Khepri API for transactional queries and updates.

Transactions are anonymous functions which take no arguments, much like what Mnesia supports. However, unlike with Mnesia, transaction functions in Khepri are restricted:

The reason is that the transaction function must always have the exact same outcome given its inputs. Indeed, the transaction function is executed on every Ra cluster members participating in the consensus. The function must therefore modify the Khepri state (the database) identically on all Ra members. This is also true for Ra members joining the cluster later or catching up after a network partition.

To achieve that:
  1. The code of the transaction function is extracted from the its initial Erlang module. This way, the transaction function does not depend on the initial module availability and is not affected by a module reload. See khepri_fun)
  2. The code is verified to make sure it does not perform any denied operations.
  3. The extracted transaction function is stored as a Khepri state machine command in the Ra journal to be replicated on all Ra members.

Data Types

tx_abort()

tx_abort() = {aborted, any()}

tx_fun()

tx_fun() = fun(() -> tx_fun_result())

tx_fun_bindings()

tx_fun_bindings() = #{Name::atom() => Value::any()}

tx_fun_result()

tx_fun_result() = any() | no_return()

Function Index

put/2Creates or modifies a specific tree node in the tree structure.
put/3Creates or modifies a specific tree node in the tree structure.
create/2Creates a specific tree node in the tree structure only if it does not exist.
create/3Creates a specific tree node in the tree structure only if it does not exist.
update/2Updates a specific tree node in the tree structure only if it already exists.
update/3Updates a specific tree node in the tree structure only if it already exists.
compare_and_swap/3Updates a specific tree node in the tree structure only if it already exists and its data matches the given DataPattern.
compare_and_swap/4Updates a specific tree node in the tree structure only if it already exists and its data matches the given DataPattern.
clear_payload/1Clears the payload of a specific tree node in the tree structure.
clear_payload/2Clears the payload of a specific tree node in the tree structure.
delete/1Deletes all tree nodes matching the path pattern.
exists/1Returns true if the tree node pointed to by the given path exists, otherwise false.
exists/2Returns true if the tree node pointed to by the given path exists, otherwise false.
get/1Returns all tree nodes matching the path pattern.
get/2Returns all tree nodes matching the path pattern.
get_node_props/1Returns the tree node properties associated with the given node path.
get_node_props/2Returns the tree node properties associated with the given node path.
has_data/1Returns true if the tree node pointed to by the given path has data, otherwise false.
get_data/1Returns the data associated with the given node path.
get_data/2Returns the data associated with the given node path.
get_data_or/2Returns the data associated with the given node path, or Default if there is no data.
get_data_or/3Returns the data associated with the given node path, or Default if there is no data.
list/1Returns all direct child nodes under the given path.
list/2Returns all direct child nodes under the given path.
find/2Returns all tree nodes matching the path pattern.
find/3Finds tree nodes under PathPattern which match the given Condition.
abort/1Aborts the transaction.
is_transaction/0Indicates if the calling function runs in the context of a transaction function.

Function Details

put/2

put(PathPattern, Data) -> Result

Creates or modifies a specific tree node in the tree structure.

put/3

put(PathPattern, Data, Extra) -> Result

Creates or modifies a specific tree node in the tree structure.

create/2

create(PathPattern, Data) -> Result

Creates a specific tree node in the tree structure only if it does not exist.

create/3

create(PathPattern, Data, Extra) -> Result

Creates a specific tree node in the tree structure only if it does not exist.

update/2

update(PathPattern, Data) -> Result

Updates a specific tree node in the tree structure only if it already exists.

update/3

update(PathPattern, Data, Extra) -> Result

Updates a specific tree node in the tree structure only if it already exists.

compare_and_swap/3

compare_and_swap(PathPattern, DataPattern, Data) -> Result

Updates a specific tree node in the tree structure only if it already exists and its data matches the given DataPattern.

compare_and_swap/4

compare_and_swap(PathPattern, DataPattern, Data, Extra) -> Result

Updates a specific tree node in the tree structure only if it already exists and its data matches the given DataPattern.

clear_payload/1

clear_payload(PathPattern) -> Result

Clears the payload of a specific tree node in the tree structure.

clear_payload/2

clear_payload(PathPattern, Extra) -> Result

Clears the payload of a specific tree node in the tree structure.

delete/1

delete(PathPattern) -> Result

Deletes all tree nodes matching the path pattern.

exists/1

exists(PathPattern) -> Exists

Returns true if the tree node pointed to by the given path exists, otherwise false.

exists/2

exists(PathPattern, Options) -> Exists

Returns true if the tree node pointed to by the given path exists, otherwise false.

get/1

get(PathPattern) -> Result

Returns all tree nodes matching the path pattern.

get/2

get(PathPattern, Options) -> Result

Returns all tree nodes matching the path pattern.

get_node_props/1

get_node_props(PathPattern) -> NodeProps

Returns the tree node properties associated with the given node path.

get_node_props/2

get_node_props(PathPattern, Options) -> NodeProps

Returns the tree node properties associated with the given node path.

has_data/1

has_data(PathPattern) -> HasData

Returns true if the tree node pointed to by the given path has data, otherwise false.

get_data/1

get_data(PathPattern) -> Data

Returns the data associated with the given node path.

get_data/2

get_data(PathPattern, Options) -> Data

Returns the data associated with the given node path.

get_data_or/2

get_data_or(PathPattern, Default) -> Data

Returns the data associated with the given node path, or Default if there is no data.

get_data_or/3

get_data_or(PathPattern, Default, Options) -> Data

Returns the data associated with the given node path, or Default if there is no data.

list/1

list(PathPattern) -> Result

Returns all direct child nodes under the given path.

list/2

list(PathPattern, Options) -> Result

Returns all direct child nodes under the given path.

find/2

find(PathPattern, Condition) -> Result

Returns all tree nodes matching the path pattern.

find/3

find(PathPattern, Condition, Options) -> Result

Finds tree nodes under PathPattern which match the given Condition.

abort/1

abort(Reason) -> no_return()

Reason: term to return to caller of the transaction.

Aborts the transaction.

Any changes so far are not committed to the store.

khepri:transaction/1 and friends will return {atomic, Reason}.

is_transaction/0

is_transaction() -> boolean()

returns: true if the calling code runs inside a transaction function, false otherwise.

Indicates if the calling function runs in the context of a transaction function.


Generated by EDoc