add mnesia counter

This commit is contained in:
anlicheng 2025-01-17 12:12:22 +08:00
parent e6c7771ce1
commit 24b29bac21
4 changed files with 84 additions and 30 deletions

View File

@ -0,0 +1,15 @@
%%%-------------------------------------------------------------------
%%% @author licheng5
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 14. 2 2023 19:48
%%%-------------------------------------------------------------------
-author("licheng5").
%%
-record(counter, {
key,
count = 0 :: integer()
}).

View File

@ -16,7 +16,7 @@
poolboy, poolboy,
mysql, mysql,
esockd, esockd,
% mnesia, mnesia,
crypto, crypto,
public_key, public_key,
ssl, ssl,

View File

@ -21,7 +21,7 @@ start(_StartType, _StartArgs) ->
ok = iot_ignore_devices:new(), ok = iot_ignore_devices:new(),
%% %%
% start_mnesia(), start_mnesia(),
{ok, SupPid} = iot_sup:start_link(), {ok, SupPid} = iot_sup:start_link(),
%% udp服务 %% udp服务
@ -74,31 +74,30 @@ start_udp_server() ->
lager:debug("[iot_app] the udp server start at: ~p", [Port]). lager:debug("[iot_app] the udp server start at: ~p", [Port]).
%% %%
%start_mnesia() -> start_mnesia() ->
% %% %%
% ok = mnesia:start(), ok = mnesia:start(),
% Tables = mnesia:system_info(tables), Tables = mnesia:system_info(tables),
%
% LoadTables = [totalizator], LoadTables = [counter],
% case lists:all(fun(Tab) -> lists:member(Tab, Tables) end, LoadTables) of case lists:all(fun(Tab) -> lists:member(Tab, Tables) end, LoadTables) of
% true -> true ->
% lager:debug("[iot_app] waiting for mnesia start: ~p", [LoadTables]), lager:debug("[iot_app] waiting for mnesia start: ~p", [LoadTables]),
% %% %%
% mnesia:wait_for_tables(LoadTables, infinity), mnesia:wait_for_tables(LoadTables, infinity),
% lager:debug("[iot_app] waiting for mnesia end"); lager:debug("[iot_app] waiting for mnesia end");
% false -> false ->
% lager:warning("[iot_app] tables: ~p not exists, recreate mnesia schema", [LoadTables]), lager:warning("[iot_app] tables: ~p not exists, recreate mnesia schema", [LoadTables]),
% %% schema %% schema
% mnesia:stop(), mnesia:stop(),
% mnesia:delete_schema([node()]), mnesia:delete_schema([node()]),
%
% %% schema %% schema
% ok = mnesia:create_schema([node()]), ok = mnesia:create_schema([node()]),
% ok = mnesia:start(), ok = mnesia:start(),
%
% %% %%
% %% id生成器
% %% mnesia_id_generator:create_table(), %%
% %% mnesia_counter:create_table()
% mnesia_totalizator:create_table() end.
% end.

View File

@ -0,0 +1,40 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @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.