From 24b29bac217042bbd0c8239b6f9611a3f1e91699 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 17 Jan 2025 12:12:22 +0800 Subject: [PATCH] add mnesia counter --- apps/iot/include/iot_tables.hrl | 15 +++++++ apps/iot/src/iot.app.src | 2 +- apps/iot/src/iot_app.erl | 57 +++++++++++++------------- apps/iot/src/mnesia/mnesia_counter.erl | 40 ++++++++++++++++++ 4 files changed, 84 insertions(+), 30 deletions(-) create mode 100644 apps/iot/include/iot_tables.hrl create mode 100644 apps/iot/src/mnesia/mnesia_counter.erl diff --git a/apps/iot/include/iot_tables.hrl b/apps/iot/include/iot_tables.hrl new file mode 100644 index 0000000..52f4820 --- /dev/null +++ b/apps/iot/include/iot_tables.hrl @@ -0,0 +1,15 @@ +%%%------------------------------------------------------------------- +%%% @author licheng5 +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 14. 2月 2023 19:48 +%%%------------------------------------------------------------------- +-author("licheng5"). + +%% 统计累加器 +-record(counter, { + key, + count = 0 :: integer() +}). \ No newline at end of file diff --git a/apps/iot/src/iot.app.src b/apps/iot/src/iot.app.src index a3483db..68e206f 100644 --- a/apps/iot/src/iot.app.src +++ b/apps/iot/src/iot.app.src @@ -16,7 +16,7 @@ poolboy, mysql, esockd, - % mnesia, + mnesia, crypto, public_key, ssl, diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index 5319cc4..92296fe 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -21,7 +21,7 @@ start(_StartType, _StartArgs) -> ok = iot_ignore_devices:new(), %% 启动数据库 - % start_mnesia(), + start_mnesia(), {ok, SupPid} = iot_sup:start_link(), %% 启动udp服务 @@ -74,31 +74,30 @@ start_udp_server() -> lager:debug("[iot_app] the udp server start at: ~p", [Port]). %% 启动内存数据库 -%start_mnesia() -> -% %% 启动数据库 -% ok = mnesia:start(), -% Tables = mnesia:system_info(tables), -% -% LoadTables = [totalizator], -% case lists:all(fun(Tab) -> lists:member(Tab, Tables) end, LoadTables) of -% true -> -% lager:debug("[iot_app] waiting for mnesia start: ~p", [LoadTables]), -% %% 加载必须等待的数据库表 -% mnesia:wait_for_tables(LoadTables, infinity), -% lager:debug("[iot_app] waiting for mnesia end"); -% false -> -% lager:warning("[iot_app] tables: ~p not exists, recreate mnesia schema", [LoadTables]), -% %% 清理掉以前的schema -% mnesia:stop(), -% mnesia:delete_schema([node()]), -% -% %% 创建schema -% ok = mnesia:create_schema([node()]), -% ok = mnesia:start(), -% -% %% 创建数据库表 -% %% id生成器 -% %% mnesia_id_generator:create_table(), -% %% 大数据统计表 -% mnesia_totalizator:create_table() -% end. \ No newline at end of file +start_mnesia() -> + %% 启动数据库 + ok = mnesia:start(), + Tables = mnesia:system_info(tables), + + LoadTables = [counter], + case lists:all(fun(Tab) -> lists:member(Tab, Tables) end, LoadTables) of + true -> + lager:debug("[iot_app] waiting for mnesia start: ~p", [LoadTables]), + %% 加载必须等待的数据库表 + mnesia:wait_for_tables(LoadTables, infinity), + lager:debug("[iot_app] waiting for mnesia end"); + false -> + lager:warning("[iot_app] tables: ~p not exists, recreate mnesia schema", [LoadTables]), + %% 清理掉以前的schema + mnesia:stop(), + mnesia:delete_schema([node()]), + + %% 创建schema + ok = mnesia:create_schema([node()]), + ok = mnesia:start(), + + %% 创建数据库表 + + %% 大数据统计表 + mnesia_counter:create_table() + end. \ No newline at end of file diff --git a/apps/iot/src/mnesia/mnesia_counter.erl b/apps/iot/src/mnesia/mnesia_counter.erl new file mode 100644 index 0000000..def2255 --- /dev/null +++ b/apps/iot/src/mnesia/mnesia_counter.erl @@ -0,0 +1,40 @@ +%%%------------------------------------------------------------------- +%%% @author aresei +%%% @copyright (C) 2023, +%%% @doc +%%% +%%% @end +%%% Created : 04. 7月 2023 12:31 +%%%------------------------------------------------------------------- +-module(mnesia_counter). +-author("aresei"). +-include("iot_tables.hrl"). + +%% API +-export([inc/1, inc/2, create_table/0, get_count/1]). + +create_table() -> + %% id生成器 + mnesia:create_table(counter, [ + {attributes, record_info(fields, counter)}, + {record_name, counter}, + {disc_copies, [node()]}, + {type, ordered_set} + ]). + +-spec inc(Key :: any()) -> integer(). +inc(Key) -> + inc(Key, 1). + +-spec inc(Key :: any(), Val :: integer()) -> integer(). +inc(Key, Val) when is_integer(Val), Val > 0 -> + mnesia:dirty_update_counter(counter, Key, Val). + +-spec get_count(Key :: any()) -> integer(). +get_count(Key) -> + case mnesia:dirty_read(counter, Key) of + [] -> + 0; + [#counter{count = Count} | _] -> + Count + end. \ No newline at end of file