Current section

Files

Jump to
eventsourcing src eventsourcing@memory_store.erl
Raw

src/eventsourcing@memory_store.erl

-module(eventsourcing@memory_store).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new/0]).
-export_type([memory_store/4, event_message/1, snapshot_message/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-opaque memory_store(SFQ, SFR, SFS, SFT) :: {memory_store,
gleam@erlang@process:subject(event_message(SFS)),
gleam@erlang@process:subject(snapshot_message(SFQ))} |
{gleam_phantom, SFR, SFT}.
-type event_message(SFU) :: {set_events,
binary(),
list(eventsourcing:event_envelop(SFU))} |
{get_events,
binary(),
gleam@erlang@process:subject({ok,
list(eventsourcing:event_envelop(SFU))} |
{error, nil})}.
-type snapshot_message(SFV) :: {set_snapshot,
binary(),
eventsourcing:snapshot(SFV)} |
{get_snapshot,
binary(),
gleam@erlang@process:subject({ok, eventsourcing:snapshot(SFV)} |
{error, nil})}.
-file("src/eventsourcing/memory_store.gleam", 69).
-spec handle_events_message(
event_message(SGG),
gleam@dict:dict(binary(), list(eventsourcing:event_envelop(SGG)))
) -> gleam@otp@actor:next(any(), gleam@dict:dict(binary(), list(eventsourcing:event_envelop(SGG)))).
handle_events_message(Message, State) ->
case Message of
{set_events, Key, Value} ->
_pipe = State,
_pipe@1 = gleam@dict:insert(_pipe, Key, Value),
gleam@otp@actor:continue(_pipe@1);
{get_events, Key@1, Response} ->
Value@1 = begin
_pipe@2 = State,
gleam_stdlib:map_get(_pipe@2, Key@1)
end,
gleam@otp@actor:send(Response, Value@1),
gleam@otp@actor:continue(State)
end.
-file("src/eventsourcing/memory_store.gleam", 124).
-spec wrap_events(binary(), integer(), list(SHR), list({binary(), binary()})) -> list(eventsourcing:event_envelop(SHR)).
wrap_events(Aggregate_id, Current_sequence, Events, Metadata) ->
_pipe = gleam@list:map_fold(
Events,
Current_sequence,
fun(Sequence, Event) ->
Next_sequence = Sequence + 1,
{Next_sequence,
{memory_store_event_envelop,
Aggregate_id,
Sequence + 1,
Event,
Metadata}}
end
),
gleam@pair:second(_pipe).
-file("src/eventsourcing/memory_store.gleam", 149).
-spec handle_snapshot_message(
snapshot_message(SHW),
gleam@dict:dict(binary(), eventsourcing:snapshot(SHW))
) -> gleam@otp@actor:next(any(), gleam@dict:dict(binary(), eventsourcing:snapshot(SHW))).
handle_snapshot_message(Message, State) ->
case Message of
{set_snapshot, Key, Value} ->
_pipe = State,
_pipe@1 = gleam@dict:insert(_pipe, Key, Value),
gleam@otp@actor:continue(_pipe@1);
{get_snapshot, Key@1, Response} ->
Value@1 = begin
_pipe@2 = State,
gleam_stdlib:map_get(_pipe@2, Key@1)
end,
gleam@otp@actor:send(Response, Value@1),
gleam@otp@actor:continue(State)
end.
-file("src/eventsourcing/memory_store.gleam", 165).
-spec save_snapshot(
memory_store(SIA, any(), any(), SID),
eventsourcing:snapshot(SIA)
) -> {ok, nil} | {error, eventsourcing:event_sourcing_error(SID)}.
save_snapshot(Memory_store, Snapshot) ->
_pipe = gleam@otp@actor:send(
erlang:element(3, Memory_store),
{set_snapshot, erlang:element(2, Snapshot), Snapshot}
),
{ok, _pipe}.
-file("src/eventsourcing/memory_store.gleam", 82).
-spec load_events(
memory_store(any(), any(), SGM, SGN),
any(),
binary(),
integer()
) -> {ok, list(eventsourcing:event_envelop(SGM))} |
{error, eventsourcing:event_sourcing_error(SGN)}.
load_events(Memory_store, _, Aggregate_id, Start_from) ->
_pipe = gleam@erlang@process:try_call(
erlang:element(2, Memory_store),
fun(_capture) -> {get_events, Aggregate_id, _capture} end,
1000
),
_pipe@1 = gleam@result:map_error(_pipe, fun(Error) -> case Error of
call_timeout ->
{event_store_error, <<"timeout"/utf8>>};
{callee_down, _} ->
{event_store_error,
<<"process responsible of getting events is down"/utf8>>}
end end),
gleam@result:'try'(_pipe@1, fun(Result) -> case Result of
{ok, Events} ->
{ok,
begin
_pipe@2 = Events,
gleam@list:drop(_pipe@2, Start_from)
end};
{error, nil} ->
{ok, []}
end end).
-file("src/eventsourcing/memory_store.gleam", 108).
-spec commit_events(
memory_store(SGY, SGZ, SHA, SHB),
eventsourcing:aggregate(SGY, SGZ, SHA, SHB),
list(SHA),
list({binary(), binary()})
) -> {ok, {list(eventsourcing:event_envelop(SHA)), integer()}} |
{error, eventsourcing:event_sourcing_error(SHB)}.
commit_events(Memory_store, Aggregate, Events, Metadata) ->
{aggregate, Aggregate_id, _, Sequence} = Aggregate,
Wrapped_events = wrap_events(Aggregate_id, Sequence, Events, Metadata),
gleam@result:map(
load_events(Memory_store, nil, Aggregate_id, 0),
fun(Past_events) ->
All_events = lists:append(Past_events, Wrapped_events),
gleam@otp@actor:send(
erlang:element(2, Memory_store),
{set_events, Aggregate_id, All_events}
),
_assert_subject = gleam@list:last(Wrapped_events),
{ok, Last_event} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"eventsourcing/memory_store"/utf8>>,
function => <<"commit_events"/utf8>>,
line => 119})
end,
{Wrapped_events, erlang:element(3, Last_event)}
end
).
-file("src/eventsourcing/memory_store.gleam", 176).
-spec load_snapshot(memory_store(SIM, any(), any(), SIP), binary()) -> {ok,
gleam@option:option(eventsourcing:snapshot(SIM))} |
{error, eventsourcing:event_sourcing_error(SIP)}.
load_snapshot(Memory_store, Aggregate_id) ->
_pipe = gleam@erlang@process:try_call(
erlang:element(3, Memory_store),
fun(_capture) -> {get_snapshot, Aggregate_id, _capture} end,
1000
),
_pipe@1 = gleam@result:map_error(_pipe, fun(Error) -> case Error of
call_timeout ->
{event_store_error, <<"timeout"/utf8>>};
{callee_down, _} ->
{event_store_error,
<<"process responsible for getting snapshots is down"/utf8>>}
end end),
gleam@result:'try'(_pipe@1, fun(Result) -> case Result of
{ok, Snapshot} ->
{ok, {some, Snapshot}};
{error, nil} ->
{ok, none}
end end).
-file("src/eventsourcing/memory_store.gleam", 48).
?DOC(" Create a new memory store record.\n").
-spec new() -> eventsourcing:event_store(memory_store(SRC, SRD, SRE, SRF), SRC, SRD, SRE, SRF, memory_store(SRC, SRD, SRE, SRF)).
new() ->
_assert_subject = gleam@otp@actor:start(
maps:new(),
fun handle_events_message/2
),
{ok, Event_actor} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"eventsourcing/memory_store"/utf8>>,
function => <<"new"/utf8>>,
line => 49})
end,
_assert_subject@1 = gleam@otp@actor:start(
maps:new(),
fun handle_snapshot_message/2
),
{ok, Snapshot_actor} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"eventsourcing/memory_store"/utf8>>,
function => <<"new"/utf8>>,
line => 50})
end,
Memory_store = {memory_store, Event_actor, Snapshot_actor},
{event_store,
fun(F) -> F(Memory_store) end,
fun(F@1) -> F@1(Memory_store) end,
fun(F@2) -> F@2(Memory_store) end,
fun(F@3) -> F@3(Memory_store) end,
fun commit_events/4,
fun load_events/4,
fun load_snapshot/2,
fun save_snapshot/2,
Memory_store}.