add mnesia counter
This commit is contained in:
parent
e6c7771ce1
commit
24b29bac21
15
apps/iot/include/iot_tables.hrl
Normal file
15
apps/iot/include/iot_tables.hrl
Normal 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()
|
||||
}).
|
||||
@ -16,7 +16,7 @@
|
||||
poolboy,
|
||||
mysql,
|
||||
esockd,
|
||||
% mnesia,
|
||||
mnesia,
|
||||
crypto,
|
||||
public_key,
|
||||
ssl,
|
||||
|
||||
@ -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.
|
||||
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.
|
||||
40
apps/iot/src/mnesia/mnesia_counter.erl
Normal file
40
apps/iot/src/mnesia/mnesia_counter.erl
Normal 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.
|
||||
Loading…
x
Reference in New Issue
Block a user