@@ -17,11 +17,11 @@ Both modules may be the same if one backend implements both roles.
1717-export ([
1818 start /1 ,
1919 stop /1 ,
20- persist_events /3 ,
21- retrieve_and_fold_events /5 ,
20+ append /3 ,
21+ fold /5 ,
2222 retrieve_events /3 ,
23- save_snapshot /2 ,
24- retrieve_latest_snapshot /2 ,
23+ store /2 ,
24+ load_latest /2 ,
2525 new_snapshot /5 ,
2626 snapshot_stream_id /1 ,
2727 snapshot_domain /1 ,
@@ -82,13 +82,17 @@ stop({EventModule, SnapshotModule}) ->
8282 end .
8383
8484-doc """
85- Persists a list of events to the event store using the specified store module.
85+ Appends a list of events to the event store using the specified store module.
86+
87+ This is the primary mechanism for persisting domain events. All events in the list
88+ must target the same stream and have unique identifiers. The store backend ensures
89+ atomic persistence and maintains sequence ordering.
8690""" .
87- -spec persist_events (StoreContext , StreamId , Events ) -> ok when
91+ -spec append (StoreContext , StreamId , Events ) -> ok when
8892 StoreContext :: store_context (),
8993 StreamId :: stream_id (),
9094 Events :: [event ()].
91- persist_events ({EventModule , _ }, StreamId , Events ) when is_list (Events ) ->
95+ append ({EventModule , _ }, StreamId , Events ) when is_list (Events ) ->
9296 % % Validate that all events target the same StreamId
9397 ok = lists :foreach (
9498 fun (Event ) ->
@@ -111,12 +115,15 @@ persist_events({EventModule, _}, StreamId, Events) when is_list(Events) ->
111115 erlang :error (duplicate_event )
112116 end ,
113117
114- EventModule :persist_events (StreamId , Events ).
118+ EventModule :append (StreamId , Events ).
115119
116120-doc """
117- Retrieves and folds events from the event store using the specified persistence module.
121+ Folds events from the event store into an accumulator using the specified persistence module.
122+
123+ This is the core operation for event replay and state reconstruction. The backend
124+ retrieves events matching the given criteria and applies the fold function in sequence order.
118125""" .
119- -spec retrieve_and_fold_events (StoreContext , StreamId , Options , Fun , Acc0 ) -> Acc1 when
126+ -spec fold (StoreContext , StreamId , Options , Fun , Acc0 ) -> Acc1 when
120127 StoreContext :: store_context (),
121128 StreamId :: stream_id (),
122129 Options :: fold_events_opts (),
@@ -125,19 +132,21 @@ Retrieves and folds events from the event store using the specified persistence
125132 Acc1 :: term (),
126133 AccIn :: term (),
127134 AccOut :: term ().
128- retrieve_and_fold_events ({EventModule , _ }, StreamId , Options , Fun , InitialResult ) ->
129- EventModule :retrieve_and_fold_events (StreamId , Options , Fun , InitialResult ).
135+ fold ({EventModule , _ }, StreamId , Options , Fun , InitialResult ) ->
136+ EventModule :fold (StreamId , Options , Fun , InitialResult ).
130137
131138-doc """
132139Retrieves events for a given stream using the specified store module and options.
140+
141+ This is a convenience wrapper around fold/5 that collects all events into a list.
133142""" .
134143-spec retrieve_events (StoreContext , StreamId , Options ) -> Result when
135144 StoreContext :: store_context (),
136145 StreamId :: stream_id (),
137146 Options :: fold_events_opts (),
138147 Result :: [event ()].
139148retrieve_events (StoreContext , StreamId , Options ) ->
140- retrieve_and_fold_events (
149+ fold (
141150 StoreContext ,
142151 StreamId ,
143152 Options ,
@@ -304,36 +313,39 @@ new_snapshot(Domain, StreamId, Sequence, Timestamp, State) ->
304313 }.
305314
306315-doc """
307- Saves a snapshot using the specified store module.
316+ Stores a snapshot using the specified store module.
308317
309- This function delegates snapshot saving to the store module implementation.
310- The snapshot captures the aggregate state at a specific point in time, allowing
311- for faster aggregate rehydration by avoiding full event replay.
318+ This function delegates snapshot storage to the backend implementation. The snapshot
319+ captures aggregate state at a specific sequence number, enabling faster rehydration
320+ by avoiding full event replay from the stream's beginning .
312321
313322The snapshot record contains all necessary fields (domain, stream_id, sequence,
314- timestamp, state), making the API consistent with event persistence where events
315- are passed as complete records .
323+ timestamp, state), consistent with event persistence where complete records are
324+ passed rather than individual fields .
316325
317- Returns ` ok ` on success, or ` {warning, Reason} ` if persistence fails.
326+ Returns ` ok ` on success, or ` {warning, Reason} ` if persistence fails. Warnings are
327+ preferred over exceptions since snapshots are optimizations, not requirements.
318328""" .
319- -spec save_snapshot (StoreContext , Snapshot ) -> ok | {warning , Reason } when
329+ -spec store (StoreContext , Snapshot ) -> ok | {warning , Reason } when
320330 StoreContext :: store_context (),
321331 Snapshot :: snapshot (),
322332 Reason :: term ().
323- save_snapshot ({_ , SnapshotModule }, Snapshot ) ->
324- SnapshotModule :save_snapshot (Snapshot ).
333+ store ({_ , SnapshotModule }, Snapshot ) ->
334+ SnapshotModule :store (Snapshot ).
325335
326336-doc """
327- Retrieves the latest snapshot for a stream using the specified store module.
337+ Loads the latest snapshot for a stream using the specified store module.
328338
329- Returns ` {ok, Snapshot} ` if found, ` {error, not_found} ` otherwise.
339+ Returns ` {ok, Snapshot} ` if found, ` {error, not_found} ` otherwise. This enables
340+ fast aggregate rehydration by restoring state from the snapshot and replaying only
341+ events that occurred after the snapshot's sequence number.
330342""" .
331- -spec retrieve_latest_snapshot (StoreContext , StreamId ) -> {ok , Snapshot } | {error , not_found } when
343+ -spec load_latest (StoreContext , StreamId ) -> {ok , Snapshot } | {error , not_found } when
332344 StoreContext :: store_context (),
333345 StreamId :: stream_id (),
334346 Snapshot :: snapshot ().
335- retrieve_latest_snapshot ({_ , SnapshotModule }, StreamId ) ->
336- SnapshotModule :retrieve_latest_snapshot (StreamId ).
347+ load_latest ({_ , SnapshotModule }, StreamId ) ->
348+ SnapshotModule :load_latest (StreamId ).
337349
338350-doc """
339351Returns the stream identifier of the snapshot.
0 commit comments