add mnesia
This commit is contained in:
parent
6618ba5511
commit
1fbe970509
@ -40,6 +40,12 @@
|
|||||||
pool_size = 10 :: integer()
|
pool_size = 10 :: integer()
|
||||||
}).
|
}).
|
||||||
|
|
||||||
|
%% id生成器
|
||||||
|
-record(id_generator, {
|
||||||
|
id,
|
||||||
|
value = 1
|
||||||
|
}).
|
||||||
|
|
||||||
-record(endpoint, {
|
-record(endpoint, {
|
||||||
id :: integer(),
|
id :: integer(),
|
||||||
%% 标题描述
|
%% 标题描述
|
||||||
|
|||||||
@ -10,9 +10,43 @@
|
|||||||
-export([start/2, stop/1]).
|
-export([start/2, stop/1]).
|
||||||
|
|
||||||
start(_StartType, _StartArgs) ->
|
start(_StartType, _StartArgs) ->
|
||||||
|
io:setopts([{encoding, unicode}]),
|
||||||
|
%% 启动mnesia数据库
|
||||||
|
start_mnesia(),
|
||||||
|
%% 加速内存的回收
|
||||||
|
erlang:system_flag(fullsweep_after, 16),
|
||||||
endpoint_sup:start_link().
|
endpoint_sup:start_link().
|
||||||
|
|
||||||
stop(_State) ->
|
stop(_State) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% internal functions
|
%% 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.
|
||||||
70
apps/endpoint/src/models/endpoint_model.erl
Normal file
70
apps/endpoint/src/models/endpoint_model.erl
Normal 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.
|
||||||
26
apps/endpoint/src/models/id_generator_model.erl
Normal file
26
apps/endpoint/src/models/id_generator_model.erl
Normal 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).
|
||||||
Loading…
x
Reference in New Issue
Block a user