From 1fbe970509d078ac66a07e1ac2c87208b33f9ab7 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 4 Jun 2025 10:57:35 +0800 Subject: [PATCH] add mnesia --- apps/endpoint/include/endpoint.hrl | 6 ++ apps/endpoint/src/endpoint_app.erl | 34 +++++++++ apps/endpoint/src/models/endpoint_model.erl | 70 +++++++++++++++++++ .../src/models/id_generator_model.erl | 26 +++++++ 4 files changed, 136 insertions(+) create mode 100644 apps/endpoint/src/models/endpoint_model.erl create mode 100644 apps/endpoint/src/models/id_generator_model.erl diff --git a/apps/endpoint/include/endpoint.hrl b/apps/endpoint/include/endpoint.hrl index 0789fbd..bd70d64 100644 --- a/apps/endpoint/include/endpoint.hrl +++ b/apps/endpoint/include/endpoint.hrl @@ -40,6 +40,12 @@ pool_size = 10 :: integer() }). +%% id生成器 +-record(id_generator, { + id, + value = 1 +}). + -record(endpoint, { id :: integer(), %% 标题描述 diff --git a/apps/endpoint/src/endpoint_app.erl b/apps/endpoint/src/endpoint_app.erl index 4ec2d1e..8b7194c 100644 --- a/apps/endpoint/src/endpoint_app.erl +++ b/apps/endpoint/src/endpoint_app.erl @@ -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. \ No newline at end of file diff --git a/apps/endpoint/src/models/endpoint_model.erl b/apps/endpoint/src/models/endpoint_model.erl new file mode 100644 index 0000000..842b580 --- /dev/null +++ b/apps/endpoint/src/models/endpoint_model.erl @@ -0,0 +1,70 @@ +%%%------------------------------------------------------------------- +%%% @author aresei +%%% @copyright (C) 2023, +%%% @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. \ No newline at end of file diff --git a/apps/endpoint/src/models/id_generator_model.erl b/apps/endpoint/src/models/id_generator_model.erl new file mode 100644 index 0000000..e102e64 --- /dev/null +++ b/apps/endpoint/src/models/id_generator_model.erl @@ -0,0 +1,26 @@ +%%%------------------------------------------------------------------- +%%% @author anlicheng +%%% @copyright (C) 2025, +%%% @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). \ No newline at end of file