I’m pleased to announce the release of Khepri 0.2.0, followed this morning by 0.2.1 with a couple bug fixes. Highlights are described in detail in the release notes (including breaking changes and how to update your code), but let me summarize the most important addition here.
Indeed, Khepri 0.2.0 introduces stored procedures and triggers!
Triggers are a mechanism to execute an anonymous function automatically following some events.
Currently supported events are changes made to the tree: nodes were created, updated or deleted. In the future, it could support Erlang process monitoring, node monitoring, and so on. Triggers are registered using an event filter which, as its name suggests, takes care of filtering the event which should execute the associated function.
The anonymous function behind a trigger is stored in the database as the payload of tree node. This function is called a stored procedure. Before it is stored, the function is extracted like transaction functions are. However, there are no restrictions on what it can do, unlike transaction functions.
Here is an example taken from the release notes:
-
Store an anonymous function in the tree:
StoredProcPath = [path, to, stored_procedure], Fun = fun(Props) -> #{path := Path}, on_action => Action} = Props end, khepri_machine:put( StoreId, StoredProcPath, #kpayload_sproc{sproc = Fun}))}.
-
Register a trigger using an event filter:
EventFilter = #kevf_tree{path = [stock, wood, <<"oak">>]}, ok = khepri_machine:register_trigger( StoreId, TriggerId, EventFilter, StoredProcPath))}.
In the example above, as soon as the [stock, wood, <<"oak">>]
node is created, updated or deleted, the anonymous function will be executed.
Stored procedure can be used (i.e. stored, replicated and executed) independently of triggers.
Under the hood, significant improvements were made to the khepri_fun
module which is responsible for extracting the code of these anonymous functions. It is also getting large and probably deserves to be an independent library at this point.
If you have any comments and feedback, or if you started to play with the library in one of your projects, please share! I’m looking forward to listen to your experience!