Packages
InMemory event store for the Eventsourcing library
Retired package: Deprecated - This library has been added to the eventsourcing library as a module
Current section
Files
Jump to
Current section
Files
src/eventsourcing_inmemory.erl
-module(eventsourcing_inmemory).
-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]).
-opaque memory_store(SEW, SEX, SEY, SEZ) :: {memory_store,
gleam@erlang@process:subject(event_message(SEY)),
gleam@erlang@process:subject(snapshot_message(SEW))} |
{gleam_phantom, SEX, SEZ}.
-type event_message(SFA) :: {set_events,
binary(),
list(eventsourcing:event_envelop(SFA))} |
{get_events,
binary(),
gleam@erlang@process:subject({ok,
list(eventsourcing:event_envelop(SFA))} |
{error, nil})}.
-type snapshot_message(SFB) :: {set_snapshot,
binary(),
eventsourcing:snapshot(SFB)} |
{get_snapshot,
binary(),
gleam@erlang@process:subject({ok, eventsourcing:snapshot(SFB)} |
{error, nil})}.
-file("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 69).
-spec handle_events_message(
event_message(SFM),
gleam@dict:dict(binary(), list(eventsourcing:event_envelop(SFM)))
) -> gleam@otp@actor:next(any(), gleam@dict:dict(binary(), list(eventsourcing:event_envelop(SFM)))).
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("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 124).
-spec wrap_events(binary(), integer(), list(SGX), list({binary(), binary()})) -> list(eventsourcing:event_envelop(SGX)).
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("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 149).
-spec handle_snapshot_message(
snapshot_message(SHC),
gleam@dict:dict(binary(), eventsourcing:snapshot(SHC))
) -> gleam@otp@actor:next(any(), gleam@dict:dict(binary(), eventsourcing:snapshot(SHC))).
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("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 165).
-spec save_snapshot(
memory_store(SHG, any(), any(), SHJ),
eventsourcing:snapshot(SHG)
) -> {ok, nil} | {error, eventsourcing:event_sourcing_error(SHJ)}.
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("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 82).
-spec load_events(
memory_store(any(), any(), SFS, SFT),
any(),
binary(),
integer()
) -> {ok, list(eventsourcing:event_envelop(SFS))} |
{error, eventsourcing:event_sourcing_error(SFT)}.
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("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 108).
-spec commit_events(
memory_store(SGE, SGF, SGG, SGH),
eventsourcing:aggregate(SGE, SGF, SGG, SGH),
list(SGG),
list({binary(), binary()})
) -> {ok, {list(eventsourcing:event_envelop(SGG)), integer()}} |
{error, eventsourcing:event_sourcing_error(SGH)}.
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) ->
Events@1 = lists:append(Past_events, Wrapped_events),
gleam@otp@actor:send(
erlang:element(2, Memory_store),
{set_events, Aggregate_id, Events@1}
),
_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_inmemory"/utf8>>,
function => <<"commit_events"/utf8>>,
line => 119})
end,
{Wrapped_events, erlang:element(3, Last_event)}
end
).
-file("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 176).
-spec load_snapshot(memory_store(SHS, any(), any(), SHV), binary()) -> {ok,
gleam@option:option(eventsourcing:snapshot(SHS))} |
{error, eventsourcing:event_sourcing_error(SHV)}.
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("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 48).
-spec new() -> eventsourcing:event_store(memory_store(SQI, SQJ, SQK, SQL), SQI, SQJ, SQK, SQL, memory_store(SQI, SQJ, SQK, SQL)).
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_inmemory"/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_inmemory"/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}.