esdb_graph_nif (reckon_db v1.1.0)
View SourceOptimized graph operations for causation analysis in reckon-db.
This module provides high-performance graph algorithms:
- Graph building: Build edges from event causation relationships
- Topological sort: Order events by causation (causes before effects)
- Path finding: Check paths, find ancestors/descendants
- DOT export: Generate Graphviz visualization
The mode is automatically detected at startup based on whether the NIF library is available. Community edition users (hex.pm) will always use the Erlang fallbacks, which provide identical functionality.
Usage
%% Build edges from events (event_id, causation_id pairs)
Nodes = [{<<"evt-1">>, undefined}, {<<"evt-2">>, <<"evt-1">>}],
Edges = esdb_graph_nif:build_edges(Nodes).
%% Topological sort
{ok, Sorted} = esdb_graph_nif:topo_sort([<<"evt-1">>, <<"evt-2">>], Edges).
%% Check which mode is active
nif = esdb_graph_nif:implementation(). %% Enterprise
erlang = esdb_graph_nif:implementation(). %% Community
Summary
Functions
Build edges from a list of event_id, causation_id tuples.
Find leaf nodes (nodes with no outgoing edges).
Find root nodes (nodes with no incoming edges).
Get all ancestors of a node (nodes that can reach it).
Get all descendants of a node (nodes reachable from it).
Get graph statistics.
Check if the graph contains cycles.
Check if there's a path between two nodes.
Get the current implementation mode.
Check if the NIF is loaded (Enterprise mode).
Generate DOT format for Graphviz visualization.
Generate DOT format with simplified labels (just type).
Perform topological sort on the graph.
Functions
-spec build_edges([{EventId :: binary(), CausationId :: binary() | undefined}]) -> [{binary(), binary()}].
Build edges from a list of event_id, causation_id tuples.
Causation ID can be undefined for root events. Returns list of from_id, to_id edges.
Find leaf nodes (nodes with no outgoing edges).
Find root nodes (nodes with no incoming edges).
-spec get_ancestors(Nodes :: [binary()], Edges :: [{binary(), binary()}], Target :: binary()) -> [binary()].
Get all ancestors of a node (nodes that can reach it).
-spec get_descendants(Nodes :: [binary()], Edges :: [{binary(), binary()}], Source :: binary()) -> [binary()].
Get all descendants of a node (nodes reachable from it).
Get graph statistics.
Returns a map with node_count, edge_count, root_count, leaf_count, max_depth.
Check if the graph contains cycles.
-spec has_path(Nodes :: [binary()], Edges :: [{binary(), binary()}], From :: binary(), To :: binary()) -> boolean().
Check if there's a path between two nodes.
-spec implementation() -> nif | erlang.
Get the current implementation mode.
-spec is_nif_loaded() -> boolean().
Check if the NIF is loaded (Enterprise mode).
-spec to_dot(Nodes :: [{binary(), binary(), binary()}], Edges :: [{binary(), binary()}]) -> binary().
Generate DOT format for Graphviz visualization.
Nodes are tuples of {event_id, event_type, label}.
Generate DOT format with simplified labels (just type).
Nodes are tuples of {event_id, event_type}.
-spec topo_sort(Nodes :: [binary()], Edges :: [{binary(), binary()}]) -> {ok, [binary()]} | {error, cycle_detected}.
Perform topological sort on the graph.
Returns nodes in dependency order (causes before effects).