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(RXL, RXM, RXN, RXO) :: {memory_store,
gleam@erlang@process:subject(event_message(RXN)),
gleam@erlang@process:subject(snapshot_message(RXL))} |
{gleam_phantom, RXM, RXO}.
-type event_message(RXP) :: {set_events,
binary(),
list(eventsourcing:event_envelop(RXP))} |
{get_events,
binary(),
gleam@erlang@process:subject({ok,
list(eventsourcing:event_envelop(RXP))} |
{error, nil})}.
-type snapshot_message(RXQ) :: {set_snapshot,
binary(),
eventsourcing:snapshot(RXQ)} |
{get_snapshot,
binary(),
gleam@erlang@process:subject({ok, eventsourcing:snapshot(RXQ)} |
{error, nil})}.
-file("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 65).
-spec handle_events_message(
event_message(RYB),
gleam@dict:dict(binary(), list(eventsourcing:event_envelop(RYB)))
) -> gleam@otp@actor:next(any(), gleam@dict:dict(binary(), list(eventsourcing:event_envelop(RYB)))).
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", 119).
-spec wrap_events(binary(), integer(), list(RZL), list({binary(), binary()})) -> list(eventsourcing:event_envelop(RZL)).
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", 144).
-spec handle_snapshot_message(
snapshot_message(RZQ),
gleam@dict:dict(binary(), eventsourcing:snapshot(RZQ))
) -> gleam@otp@actor:next(any(), gleam@dict:dict(binary(), eventsourcing:snapshot(RZQ))).
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", 160).
-spec save_snapshot(
memory_store(RZU, any(), any(), any()),
eventsourcing:snapshot(RZU)
) -> nil.
save_snapshot(Memory_store, Snapshot) ->
gleam@otp@actor:send(
erlang:element(3, Memory_store),
{set_snapshot, erlang:element(2, Snapshot), Snapshot}
).
-file("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 78).
-spec load_events(memory_store(any(), any(), RYH, RYI), binary(), integer()) -> {ok,
list(eventsourcing:event_envelop(RYH))} |
{error, eventsourcing:event_sourcing_error(RYI)}.
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", 103).
-spec commit(
memory_store(RYS, RYT, RYU, RYV),
eventsourcing:aggregate(RYS, RYT, RYU, RYV),
list(RYU),
list({binary(), binary()})
) -> {ok, {list(eventsourcing:event_envelop(RYU)), integer()}} |
{error, eventsourcing:event_sourcing_error(RYV)}.
commit(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, 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"/utf8>>,
line => 114})
end,
{Wrapped_events, erlang:element(3, Last_event)}
end
).
-file("/Users/renata-amutio/Projects/renatillas/eventsourcing_inmemory/src/eventsourcing_inmemory.gleam", 170).
-spec load_snapshot(memory_store(SAD, any(), any(), SAG), binary()) -> {ok,
gleam@option:option(eventsourcing:snapshot(SAD))} |
{error, eventsourcing:event_sourcing_error(SAG)}.
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(SIJ, SIK, SIL, SIM), SIJ, SIK, SIL, SIM).
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,
Memory_store,
fun load_events/3,
fun commit/4,
fun save_snapshot/2,
fun load_snapshot/2}.