11-module (event_sourcing_core_aggregate ).
22
3- -include_lib (" event_sourcing_core.hrl" ).
3+ -include_lib (" event_sourcing_core/include/event_sourcing_core .hrl" ).
44-include_lib (" kernel/include/logger.hrl" ).
55
66-behaviour (gen_server ).
3434Starts an aggregate process with a given timeout.
3535
3636- Aggregate is the aggregate module implementing the behavior.
37- - Store is a ` {EventStore, SnapshotStore} ` tuple.
37+ - StoreContext is a ` {EventStore, SnapshotStore} ` tuple.
3838- Id is the unique identifier for the aggregate instance.
3939- Timeout is the inactivity timeout in milliseconds before passivation.
4040
4141Function returns ` {ok, Pid} ` if successful, ` {error, Reason} ` otherwise.
4242""" .
43- -spec start_link (Aggregate , Store , Id , Opts ) -> gen_server :start_ret () when
43+ -spec start_link (Aggregate , StoreContext , Id , Opts ) -> gen_server :start_ret () when
4444 Aggregate :: module (),
45- Store :: event_sourcing_core_store :store (),
45+ StoreContext :: event_sourcing_core_store :store_context (),
4646 Id :: stream_id (),
4747 Opts ::
4848 #{
@@ -52,22 +52,24 @@ Function returns `{ok, Pid}` if successful, `{error, Reason}` otherwise.
5252 now_fun => fun (() -> timestamp ()),
5353 snapshot_interval => non_neg_integer ()
5454 }.
55- start_link (Aggregate , Store , Id , Opts ) ->
56- gen_server :start_link (? MODULE , {Aggregate , Store , Id , Opts }, []).
55+ start_link (Aggregate , StoreContext , Id , Opts ) ->
56+ gen_server :start_link (? MODULE , {Aggregate , StoreContext , Id , Opts }, []).
5757
5858-doc """
5959Starts a new aggregate process.
6060
6161- Aggregate is the aggregate module to start.
62- - Store must be provided as ` {EventStore, SnapshotStore} ` .
62+ - StoreContext must be provided as ` {EventStore, SnapshotStore} ` .
6363- Id is the unique identifier for the aggregate instance.
6464""" .
6565-spec start_link (
66- Aggregate :: module (), Store :: event_sourcing_core_store :store (), Id :: stream_id ()
66+ Aggregate :: module (),
67+ StoreContext :: event_sourcing_core_store :store_context (),
68+ Id :: stream_id ()
6769) ->
6870 gen_server :start_ret ().
69- start_link (Aggregate , Store , Id ) ->
70- start_link (Aggregate , Store , Id , #{}).
71+ start_link (Aggregate , StoreContext , Id ) ->
72+ start_link (Aggregate , StoreContext , Id , #{}).
7173
7274-spec dispatch (Pid , Command ) -> {ok , Result } | {error , Reason } when
7375 Pid :: pid (),
@@ -79,7 +81,7 @@ dispatch(Pid, Command) ->
7981
8082-record (state , {
8183 aggregate :: module (),
82- store :: event_sourcing_core_store :store (),
84+ store :: event_sourcing_core_store :store_context (),
8385 id :: stream_id (),
8486 state :: aggregate_state (),
8587 sequence = ? SEQUENCE_ZERO :: non_neg_integer (),
@@ -100,7 +102,7 @@ Retrieves all events for the aggregate from the persistence layer and applies th
100102sequentially to rehydrate the aggregate's state.
101103
102104- Aggregate is the aggregate module implementing the domain logic.
103- - Store is the persistence module (store) implementing event retrieval .
105+ - StoreContext is a ` {EventStore, SnapshotStore} ` tuple used for event and snapshot persistence .
104106- Id is the unique identifier for the aggregate.
105107- Opts is a map of options including:
106108 - ` timeout ` : Inactivity timeout in milliseconds.
@@ -112,7 +114,7 @@ sequentially to rehydrate the aggregate's state.
112114Function returns {ok, state()} on success, and returns {stop, Reason} on failure.
113115""" .
114116-spec init (
115- {module (), event_sourcing_core_store :store (), stream_id (), #{
117+ {module (), event_sourcing_core_store :store_context (), stream_id (), #{
116118 timeout => timeout (),
117119 sequence_zero => fun (() -> sequence ()),
118120 sequence_next => fun ((sequence ()) -> sequence ()),
@@ -121,14 +123,14 @@ Function returns {ok, state()} on success, and returns {stop, Reason} on failure
121123 }}
122124) ->
123125 {ok , state ()}.
124- init ({Aggregate , Store , Id , Opts }) ->
126+ init ({Aggregate , StoreContext , Id , Opts }) ->
125127 State0 = Aggregate :init (),
126128 SequenceZero = maps :get (sequence_zero , Opts , fun () -> ? SEQUENCE_ZERO end ),
127129 SequenceNext = maps :get (sequence_next , Opts , fun (Sequence ) -> Sequence + 1 end ),
128130
129131 % % Try to load the latest snapshot
130132 {StateFromSnapshot , SequenceFromSnapshot } =
131- case event_sourcing_core_store :retrieve_latest_snapshot (Store , Id ) of
133+ case event_sourcing_core_store :retrieve_latest_snapshot (StoreContext , Id ) of
132134 {ok , Snapshot } ->
133135 SnapshotState = event_sourcing_core_store :snapshot_state (Snapshot ),
134136 SnapshotSeq = event_sourcing_core_store :snapshot_sequence (Snapshot ),
@@ -149,7 +151,7 @@ init({Aggregate, Store, Id, Opts}) ->
149151 end ,
150152 {State1 , Sequence1 } =
151153 event_sourcing_core_store :retrieve_and_fold_events (
152- Store ,
154+ StoreContext ,
153155 Id ,
154156 #{from => SequenceFromSnapshot + 1 },
155157 FoldFun ,
@@ -160,7 +162,7 @@ init({Aggregate, Store, Id, Opts}) ->
160162 TimerRef = install_passivation (Timeout , undefined ),
161163 {ok , # state {
162164 aggregate = Aggregate ,
163- store = Store ,
165+ store = StoreContext ,
164166 id = Id ,
165167 state = State1 ,
166168 sequence = Sequence1 ,
@@ -269,7 +271,7 @@ Function returns the new state and sequence of the aggregate after the command i
269271process_command (
270272 # state {
271273 aggregate = Aggregate ,
272- store = Store ,
274+ store = StoreContext ,
273275 id = Id ,
274276 state = State0 ,
275277 sequence = Sequence0 ,
@@ -284,7 +286,7 @@ process_command(
284286 {ok , {State0 , Sequence0 }};
285287 {ok , PayloadEvents } when is_list (PayloadEvents ) ->
286288 ok = persist_events (
287- PayloadEvents , {Aggregate , Store , Id , Sequence0 , SequenceNext , NowFun }
289+ PayloadEvents , {Aggregate , StoreContext , Id , Sequence0 , SequenceNext , NowFun }
288290 ),
289291 {State1 , Sequence1 } =
290292 apply_events (PayloadEvents , {Aggregate , State0 , Sequence0 , SequenceNext }),
@@ -318,14 +320,14 @@ apply_events(PayloadEvents, {Aggregate, State0, Sequence0, SequenceNext}) ->
318320 PayloadEvents :: [event_payload ()],
319321 {
320322 Aggregate :: module (),
321- Store :: event_sourcing_core_store :store (),
323+ StoreContext :: event_sourcing_core_store :store_context (),
322324 Id :: stream_id (),
323325 Sequence0 :: sequence (),
324326 SequenceNext :: fun ((sequence ()) -> sequence ()),
325327 NowFun :: fun (() -> timestamp ())
326328 }
327329) -> ok .
328- persist_events (PayloadEvents , {Aggregate , Store , Id , Sequence0 , SequenceNext , NowFun }) ->
330+ persist_events (PayloadEvents , {Aggregate , StoreContext , Id , Sequence0 , SequenceNext , NowFun }) ->
329331 {Events , _ } =
330332 lists :foldl (
331333 fun (PayloadEvent , {Events , SequenceN }) ->
@@ -352,7 +354,7 @@ persist_events(PayloadEvents, {Aggregate, Store, Id, Sequence0, SequenceNext, No
352354 end ,
353355 Events
354356 ),
355- event_sourcing_core_store :persist_events (Store , Id , Events ).
357+ event_sourcing_core_store :persist_events (StoreContext , Id , Events ).
356358
357359-doc """
358360Saves a snapshot if the snapshot interval is configured and the current
@@ -367,7 +369,7 @@ maybe_save_snapshot(
367369 # state {
368370 snapshot_interval = Interval ,
369371 sequence = Sequence ,
370- store = Store ,
372+ store = StoreContext ,
371373 aggregate = Aggregate ,
372374 id = Id ,
373375 state = AggState ,
@@ -377,7 +379,7 @@ maybe_save_snapshot(
377379 Timestamp = NowFun (),
378380 logger :info (" Saving snapshot for ~p at sequence ~p " , [Id , Sequence ]),
379381 Snapshot = event_sourcing_core_store :new_snapshot (Aggregate , Id , Sequence , Timestamp , AggState ),
380- case event_sourcing_core_store :save_snapshot (Store , Snapshot ) of
382+ case event_sourcing_core_store :save_snapshot (StoreContext , Snapshot ) of
381383 ok ->
382384 ok ;
383385 {warning , Reason } ->
0 commit comments