add mnesia

This commit is contained in:
anlicheng 2025-06-04 10:57:35 +08:00
parent 6618ba5511
commit 1fbe970509
4 changed files with 136 additions and 0 deletions

View File

@ -40,6 +40,12 @@
pool_size = 10 :: integer()
}).
%% id生成器
-record(id_generator, {
id,
value = 1
}).
-record(endpoint, {
id :: integer(),
%%

View File

@ -10,9 +10,43 @@
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
io:setopts([{encoding, unicode}]),
%% mnesia数据库
start_mnesia(),
%%
erlang:system_flag(fullsweep_after, 16),
endpoint_sup:start_link().
stop(_State) ->
ok.
%% internal functions
%%
start_mnesia() ->
%%
ensure_mnesia_schema(),
ok = mnesia:start(),
Tables = mnesia:system_info(tables),
lager:debug("[endpoint_app] tables: ~p", [Tables]),
%%
not lists:member(id_generator, Tables) andalso id_generator_model:create_table(),
not lists:member(endpoint, Tables) andalso endpoint_model:create_table(),
ok.
-spec ensure_mnesia_schema() -> any().
ensure_mnesia_schema() ->
case mnesia:system_info(use_dir) of
true ->
lager:debug("[endpoint_app] mnesia schema exists"),
ok;
false ->
mnesia:stop(),
case mnesia:create_schema([node()]) of
ok -> ok;
{error, {_, {already_exists, _}}} -> ok;
Error ->
lager:debug("[endpoint_app] create mnesia schema failed with error: ~p", [Error]),
throw({init_schema, Error})
end
end.

View File

@ -0,0 +1,70 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 04. 7 2023 12:31
%%%-------------------------------------------------------------------
-module(endpoint_model).
-author("aresei").
-include("endpoint.hrl").
-include_lib("stdlib/include/qlc.hrl").
-define(TAB, endpoint).
%% API
-export([create_table/0]).
-export([insert/1, insert/1, get_endpoint/1, get_all_endpoints/0, display_endpoints/0]).
create_table() ->
%% id生成器
{atomic, ok} = mnesia:create_table(endpoint, [
{attributes, record_info(fields, endpoint)},
{record_name, endpoint},
{disc_copies, [node()]},
{type, ordered_set}
]).
insert(Endpoint = #endpoint{}) ->
case mnesia:transaction(fun() -> mnesia:write(?TAB, Endpoint, write) end) of
{'atomic', Res} ->
Res;
{'aborted', Reason} ->
{error, Reason}
end.
-spec get_endpoint(Id :: integer()) -> error | {ok, Endpoint :: #endpoint{}}.
get_endpoint(Id) when is_integer(Id) ->
case mnesia:dirty_read(?TAB, Id) of
[] ->
error;
[Endpoint] ->
{ok, Endpoint}
end.
-spec get_all_endpoints() -> [#endpoint{}].
get_all_endpoints() ->
Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(?TAB)]),
qlc:e(Q)
end,
case mnesia:transaction(Fun) of
{'atomic', Res} ->
Res;
{'aborted', _} ->
[]
end.
display_endpoints() ->
F = fun() ->
Q = qlc:q([E || E <- mnesia:table(?TAB)]),
qlc:e(Q)
end,
case mnesia:transaction(F) of
{atomic, Services} ->
{ok, Services};
{aborted, Error} ->
{error, Error}
end.

View File

@ -0,0 +1,26 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 06. 5 2025 10:32
%%%-------------------------------------------------------------------
-module(id_generator_model).
-author("anlicheng").
-include("endpoint.hrl").
%% API
-export([create_table/0, next_id/1]).
create_table() ->
%% id生成器
{atomic, ok} = mnesia:create_table(id_generator, [
{attributes, record_info(fields, id_generator)},
{record_name, id_generator},
{disc_copies, [node()]},
{type, ordered_set}
]).
next_id(Tab) when is_atom(Tab) ->
mnesia:dirty_update_counter(id_generator, Tab, 1).