%%%------------------------------------------------------------------- %%% @author anlicheng %%% @copyright (C) 2025, %%% @doc %%% %%% @end %%% Created : 12. 8月 2025 15:12 %%%------------------------------------------------------------------- -module(endpoint_mnesia). -author("aresei"). -include("endpoint.hrl"). -include_lib("stdlib/include/qlc.hrl"). -define(TAB, endpoint). %% API -export([create_table/0]). -export([insert/1, delete/1, check_name/1]). -export([get_endpoint/1]). -export([as_map/1]). create_table() -> %% id生成器 mnesia:create_table(endpoint, [ {attributes, record_info(fields, endpoint)}, {record_name, endpoint}, {disc_copies, [node()]}, {type, ordered_set} ]). -spec check_name(Name :: binary()) -> boolean() | {error, Reason :: any()}. check_name(Name) when is_binary(Name) -> Fun = fun() -> Q = qlc:q([E || E <- mnesia:table(?TAB), E#endpoint.name =:= Name]), case qlc:e(Q) of [] -> false; [_|_] -> true end end, case mnesia:transaction(Fun) 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 insert(Endpoint :: #endpoint{}) -> ok | {error, Reason :: term()}. insert(Endpoint = #endpoint{}) -> case mnesia:transaction(fun() -> mnesia:write(?TAB, Endpoint, write) end) of {'atomic', ok} -> ok; {'aborted', Reason} -> {error, Reason} end. -spec delete(Id :: integer()) -> ok | {error, Reason :: any()}. delete(Id) when is_integer(Id) -> case mnesia:transaction(fun() -> mnesia:delete(?TAB, Id, write) end) of {'atomic', ok} -> ok; {'aborted', Reason} -> {error, Reason} end. -spec as_map(Endpoint :: #endpoint{}) -> map(). as_map(#endpoint{id = Id, name = Name, title = Title, config = Config, updated_at = UpdateTs, created_at = CreateTs}) -> {ConfigKey, ConfigMap} = case Config of #http_endpoint{url = Url, pool_size = PoolSize} -> {<<"http">>, #{<<"url">> => Url, <<"pool_size">> => PoolSize}}; #mqtt_endpoint{host = Host, port = Port, client_id = ClientId, username = Username, password = Password, topic = Topic, qos = Qos} -> {<<"mqtt">>, #{<<"host">> => Host, <<"port">> => Port, <<"client_id">> => ClientId, <<"username">> => Username, <<"password">> => Password, <<"topic">> => Topic, <<"qos">> => Qos}}; #kafka_endpoint{username = Username, password = Password, bootstrap_servers = BootstrapServers, topic = Topic} -> {<<"kafka">>, #{<<"username">> => Username, <<"password">> => Password, <<"bootstrap_servers">> => BootstrapServers, <<"topic">> => Topic}}; #mysql_endpoint{host = Host, port = Port, username = Username, password = Password, database = Database, table_name = TableName} -> {<<"mysql">>, #{<<"host">> => Host, <<"port">> => Port, <<"username">> => Username, <<"password">> => Password, <<"database">> => Database, <<"table_name">> => TableName}} end, Map = #{ <<"id">> => Id, <<"name">> => Name, <<"title">> => Title, <<"update_ts">> => UpdateTs, <<"create_ts">> => CreateTs }, Map#{ConfigKey => ConfigMap}.