esdb_aggregate_nif (reckon_db v1.6.0)
View SourceOptimized event aggregation operations for reckon-db.
This module provides high-performance event aggregation implementations:
- aggregate_events: Bulk fold with tagged value semantics
- sum_field: Vectorized sum accumulation for numeric fields
- count_where: Count events matching a condition
- merge_tagged_batch: Batch map merge with tagged values
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.
Tagged Value Semantics
Tagged values control how fields are aggregated:
{sum, N}: Add N to the current value (numeric accumulation){overwrite, V}: Replace current value with V- Plain value: Replace current value (default behavior)
Usage
%% Aggregate events with tagged value semantics
Events = [#{amount => {sum, 100}}, #{amount => {sum, 50}}],
Result = esdb_aggregate_nif:aggregate_events(Events, #{}, true),
#{amount := 150} = Result.
%% Sum a specific field across all events
Total = esdb_aggregate_nif:sum_field(Events, amount).
%% Check which mode is active
nif = esdb_aggregate_nif:implementation(). %% Enterprise
erlang = esdb_aggregate_nif:implementation(). %% Community
Summary
Functions
Aggregate a list of events with tagged value semantics.
Get statistics about event aggregation.
Count events matching a simple field condition.
Finalize a tagged map by unwrapping all tagged values.
Get the current implementation mode.
Check if the NIF is loaded (Enterprise mode).
Merge a batch of key-value pairs into a state map.
Sum a specific field across all events.
Functions
Aggregate a list of events with tagged value semantics.
Processes events in order, applying tagged value rules: - {sum, N} adds N to the current value - {overwrite, V} replaces the current value with V - Plain values replace the current value
If Finalize is true, the result will have tagged values unwrapped.
Get statistics about event aggregation.
Returns counts and basic metrics about the event list.
-spec count_where(Events :: [map()], Field :: atom() | binary(), Value :: term()) -> non_neg_integer().
Count events matching a simple field condition.
Returns the number of events where the specified field equals the expected value.
Finalize a tagged map by unwrapping all tagged values.
Converts {sum, N} to N and {overwrite, V} to V.
-spec implementation() -> nif | erlang.
Get the current implementation mode.
-spec is_nif_loaded() -> boolean().
Check if the NIF is loaded (Enterprise mode).
Merge a batch of key-value pairs into a state map.
Applies tagged value semantics to each pair.
Sum a specific field across all events.
Efficiently accumulates numeric values from a named field. Handles tagged values ({sum, N}) and plain numeric values.