Current section
Files
Jump to
Current section
Files
src/kv_sessions@ets_adapter.erl
-module(kv_sessions@ets_adapter).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([new_table/1, new/1]).
-spec new_table(binary()) -> carpenter@table:set(binary(), kv_sessions@session:session()).
new_table(Table_name) ->
_assert_subject = begin
_pipe = carpenter@table:build(Table_name),
_pipe@1 = carpenter@table:privacy(_pipe, private),
_pipe@2 = carpenter@table:write_concurrency(
_pipe@1,
auto_write_concurrency
),
_pipe@3 = carpenter@table:read_concurrency(_pipe@2, true),
_pipe@4 = carpenter@table:decentralized_counters(_pipe@3, true),
_pipe@5 = carpenter@table:compression(_pipe@4, false),
carpenter@table:set(_pipe@5)
end,
{ok, Table} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"kv_sessions/ets_adapter"/utf8>>,
function => <<"new_table"/utf8>>,
line => 18})
end,
Table.
-spec get_session(carpenter@table:set(binary(), UCO)) -> fun((kv_sessions@session:session_id()) -> {ok,
gleam@option:option(UCO)} |
{error, any()}).
get_session(Db) ->
fun(Session_id) ->
Res = begin
_pipe = Db,
_pipe@1 = carpenter@table:lookup(
_pipe,
kv_sessions@session:id_to_string(Session_id)
),
gleam@list:first(_pipe@1)
end,
case Res of
{ok, Tup} ->
{ok, {some, erlang:element(2, Tup)}};
{error, _} ->
{ok, none}
end
end.
-spec save_session(carpenter@table:set(binary(), kv_sessions@session:session())) -> fun((kv_sessions@session:session()) -> {ok,
kv_sessions@session:session()} |
{error, any()}).
save_session(Db) ->
fun(New_session) ->
_pipe = Db,
carpenter@table:insert(
_pipe,
[{kv_sessions@session:id_to_string(erlang:element(2, New_session)),
New_session}]
),
{ok, New_session}
end.
-spec delete_session(carpenter@table:set(binary(), any())) -> fun((kv_sessions@session:session_id()) -> {ok,
nil} |
{error, any()}).
delete_session(Db) ->
fun(Session_id) ->
_pipe = Db,
carpenter@table:delete(
_pipe,
kv_sessions@session:id_to_string(Session_id)
),
{ok, nil}
end.
-spec new(binary()) -> kv_sessions@session_config:session_store().
new(Table_name) ->
Db = new_table(Table_name),
{session_store, get_session(Db), save_session(Db), delete_session(Db)}.