Skip to content

Commit 844e054

Browse files
committed
refactor(store): clarify meaning of store context
1 parent a6022c2 commit 844e054

8 files changed

Lines changed: 89 additions & 93 deletions

apps/event_sourcing_core/src/event_sourcing_core_aggregate.erl

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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).
@@ -34,15 +34,15 @@
3434
Starts 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

4141
Function 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 """
5959
Starts 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
100102
sequentially 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.
112114
Function 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
269271
process_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 """
358360
Saves 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} ->

apps/event_sourcing_core/src/event_sourcing_core_event_store.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Implementations must guarantee:
2020
Typical implementations include in-memory stores (ETS) and relational databases.
2121
""".
2222

23-
-include_lib("event_sourcing_core.hrl").
23+
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
2424

2525
-doc """
2626
Starts the event store, performing any necessary initialization.

apps/event_sourcing_core/src/event_sourcing_core_mgr_aggregate.erl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ monitoring them for crashes.
1010

1111
-behaviour(gen_server).
1212

13-
-include_lib("event_sourcing_core.hrl").
13+
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
1414

1515
-export([
1616
init/1,
@@ -28,7 +28,7 @@ monitoring them for crashes.
2828

2929
-record(state, {
3030
aggregate :: module(),
31-
store :: event_sourcing_core_store:store(),
31+
store :: event_sourcing_core_store:store_context(),
3232
router :: module(),
3333
opts ::
3434
#{
@@ -46,7 +46,7 @@ monitoring them for crashes.
4646
Starts the aggregate manager with custom options.
4747

4848
- Aggregate is he module implementing the aggregate logic.
49-
- Store is a `{EventStore, SnapshotStore}` tuple.
49+
- StoreContext is a `{EventStore, SnapshotStore}` tuple.
5050
- Router is the module extracting routing info from commands.
5151
- Opts is the configuration options:
5252
- `timeout`: Timeout for operations (default: `infinity`).
@@ -56,9 +56,9 @@ Starts the aggregate manager with custom options.
5656

5757
Function returns `{ok, Pid}` on success, or an error tuple if the server fails to start.
5858
""".
59-
-spec start_link(Aggregate, Store, Router, Opts) -> gen_server:start_ret() when
59+
-spec start_link(Aggregate, StoreContext, Router, Opts) -> gen_server:start_ret() when
6060
Aggregate :: module(),
61-
Store :: event_sourcing_core_store:store(),
61+
StoreContext :: event_sourcing_core_store:store_context(),
6262
Router :: module(),
6363
Opts ::
6464
#{
@@ -67,25 +67,25 @@ Function returns `{ok, Pid}` on success, or an error tuple if the server fails t
6767
sequence_next => fun((sequence()) -> sequence()),
6868
now_fun => fun(() -> timestamp())
6969
}.
70-
start_link(Aggregate, Store, Router, Opts) ->
71-
gen_server:start_link({local, ?MODULE}, ?MODULE, {Aggregate, Store, Router, Opts}, []).
70+
start_link(Aggregate, StoreContext, Router, Opts) ->
71+
gen_server:start_link({local, ?MODULE}, ?MODULE, {Aggregate, StoreContext, Router, Opts}, []).
7272

7373
-doc """
7474
Starts the aggregate manager with the given aggregate, store, and router modules,
7575
using default options.
7676

7777
- Aggregate is the module implementing the aggregate logic.
78-
- Store follows the same `{EventStore, SnapshotStore}` convention.
78+
- StoreContext follows the same `{EventStore, SnapshotStore}` convention.
7979
- Router is the module extracting routing info from commands.
8080

8181
Function returns `{ok, Pid}` on success, or an error tuple if the server fails to start.
8282
""".
83-
-spec start_link(Aggregate, Store, Router) -> gen_server:start_ret() when
83+
-spec start_link(Aggregate, StoreContext, Router) -> gen_server:start_ret() when
8484
Aggregate :: module(),
85-
Store :: event_sourcing_core_store:store(),
85+
StoreContext :: event_sourcing_core_store:store_context(),
8686
Router :: module().
87-
start_link(Aggregate, Store, Router) ->
88-
start_link(Aggregate, Store, Router, #{}).
87+
start_link(Aggregate, StoreContext, Router) ->
88+
start_link(Aggregate, StoreContext, Router, #{}).
8989

9090
-doc """
9191
Stops the aggregate manager.
@@ -124,9 +124,9 @@ Initializes the aggregate manager state.
124124

125125
Function returns `{ok, State}` with an initialized state record.
126126
""".
127-
-spec init({Aggregate, Store, Router, Opts}) -> {ok, State} when
127+
-spec init({Aggregate, StoreContext, Router, Opts}) -> {ok, State} when
128128
Aggregate :: module(),
129-
Store :: event_sourcing_core_store:store(),
129+
StoreContext :: event_sourcing_core_store:store_context(),
130130
Router :: module(),
131131
Opts ::
132132
#{
@@ -136,10 +136,10 @@ Function returns `{ok, State}` with an initialized state record.
136136
now_fun => fun(() -> timestamp())
137137
},
138138
State :: state().
139-
init({Aggregate, Store, Router, Opts}) ->
139+
init({Aggregate, StoreContext, Router, Opts}) ->
140140
{ok, #state{
141141
aggregate = Aggregate,
142-
store = Store,
142+
store = StoreContext,
143143
router = Router,
144144
opts = Opts,
145145
pids = #{}
@@ -217,15 +217,15 @@ ensure_and_dispatch(
217217
Id,
218218
Command,
219219
#state{
220-
store = Store,
220+
store = StoreContext,
221221
pids = Pids,
222222
opts = Opts
223223
} =
224224
State
225225
) ->
226226
case maps:get(Id, Pids, undefined) of
227227
undefined ->
228-
case start_aggregate(Aggregate, Store, Id, Opts) of
228+
case start_aggregate(Aggregate, StoreContext, Id, Opts) of
229229
{ok, Pid} ->
230230
Result = forward(Pid, Command),
231231
NewPids = maps:put(Id, Pid, Pids),
@@ -258,9 +258,9 @@ Monitors the new process and returns its pid.
258258

259259
Function returns `{ok, Pid}` on success, or `{error, Reason}` on failure.
260260
""".
261-
-spec start_aggregate(Aggregate, Store, Id, Opts) -> {ok, Result} | {error, Reason} when
261+
-spec start_aggregate(Aggregate, StoreContext, Id, Opts) -> {ok, Result} | {error, Reason} when
262262
Aggregate :: module(),
263-
Store :: event_sourcing_core_store:store(),
263+
StoreContext :: event_sourcing_core_store:store_context(),
264264
Id :: stream_id(),
265265
Opts ::
266266
#{
@@ -271,8 +271,8 @@ Function returns `{ok, Pid}` on success, or `{error, Reason}` on failure.
271271
},
272272
Result :: pid(),
273273
Reason :: term().
274-
start_aggregate(Aggregate, Store, Id, Opts) ->
275-
case event_sourcing_core_aggregate:start_link(Aggregate, Store, Id, Opts) of
274+
start_aggregate(Aggregate, StoreContext, Id, Opts) ->
275+
case event_sourcing_core_aggregate:start_link(Aggregate, StoreContext, Id, Opts) of
276276
{ok, Pid} ->
277277
erlang:monitor(process, Pid),
278278
{ok, Pid};

apps/event_sourcing_core/src/event_sourcing_core_mgr_behaviour.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ for commands.
99
Implementations of this behaviour are responsible for extracting routing information,
1010
enabling the aggregate manager to dispatch commands to the correct aggregate instance.
1111
""".
12-
-include_lib("event_sourcing_core.hrl").
12+
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
1313

1414
-doc """
1515
Extracts the routing information from the command.

apps/event_sourcing_core/src/event_sourcing_core_snapshot_store.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Common implementations include key–value stores, databases, or object storage
2020
systems (e.g., S3).
2121
""".
2222

23-
-include_lib("event_sourcing_core.hrl").
23+
-include_lib("event_sourcing_core/include/event_sourcing_core.hrl").
2424

2525
-doc """
2626
Save a snapshot for a stream.

0 commit comments

Comments
 (0)