%%%-------------------------------------------------------------------
|
%%% @author pansen
|
%%% @copyright (C) 2018, <COMPANY>
|
%%% @doc
|
%%%
|
%%% @end
|
%%% Created : 01. 八月 2018 16:01
|
%%%-------------------------------------------------------------------
|
-module(syncDB_mnesia).
|
-author("pansen").
|
|
-compile(export_all).
|
|
%%-include("DbTool.hrl").
|
-include("nodeTool.hrl").
|
|
test() ->
|
io:format("start test 12333 ~n"),
|
mnesia:start(),
|
lager:start(),
|
ok.
|
|
setCNode(CNode) ->
|
Ret = mochiglobal:put('cNodeName', CNode),
|
case Ret of
|
ok ->
|
Res = [{atomic, ok}];
|
_ ->
|
Res = [{aborted, notok}]
|
end,
|
Res.
|
|
% setCNode(CNode) ->
|
% io:format("setCNode name ~p ",[CNode]),
|
|
% % BerPid = mochiglobal:get('sendMessToCNodePID'),
|
% % case BerPid of
|
% % undefined ->
|
% % io:format("BerPid is ~p ",[BerPid]),
|
% % Pid = spawn(fun() ->
|
% % callCNode:loop(CNode)
|
% % end),
|
% % mochiglobal:put('sendMessToCNodePID',Pid);
|
% % _->
|
% % io:format("BerPid is ~p ",[BerPid]),
|
% % BerPid ! stop,
|
% % Pid = spawn(fun() ->
|
% % callCNode:loop(CNode)
|
% % end),
|
% % mochiglobal:put('sendMessToCNodePID',Pid)
|
% % end,
|
% Pid = spawn(fun() ->
|
% callCNode:loop(CNode)
|
% end),
|
% case whereis(sendMessToCNodePID) of
|
% undefined ->
|
% ok;
|
% _->
|
% unregister(sendMessToCNodePID)
|
% end,
|
% Ret = register(sendMessToCNodePID, Pid),
|
% case Ret of
|
% true ->
|
% io:format("register staus ~p, Pid is ~p",[Ret,Pid]),
|
% Res =[{atomic, ok}];
|
% _->
|
% io:format("register staus ~p, Pid is ~p",[Ret,Pid]),
|
% Res=[{aborted,notok}]
|
% end,
|
% Res. %%注册进程名字
|
% %% mochiglobal:put('cNPid', Pid).
|
|
sendMessage() ->
|
Nodes = mnesia:system_info(running_db_nodes),
|
io:format("sendMessage ~p", [Nodes]),
|
Res = syncTool:rpcSendMessToCNode(Nodes, {call, 'update', 1}),
|
case Res of
|
null ->
|
io:format("send ok"),
|
Ret = [{atomic, ok}];
|
_ ->
|
io:format("send error"),
|
Ret = [{aborted, notok}]
|
end,
|
Ret.
|
|
% sendMessage()->
|
% Nodes=mnesia:system_info(running_db_nodes),
|
% io:format("sendMessage ~p",[Nodes]),
|
% Res = syncTool:rpcSendMessToCNode(Nodes, {call, 'update', 1}),
|
% case Res of
|
% null ->
|
% io:format("send ok"),
|
% Ret=[{atomic, ok}];
|
% _ ->
|
% io:format("send error"),
|
% Ret=[{aborted,notok}]
|
% end,
|
% Ret.
|
|
|
%%
|
%% 启动节点并自动连接到集群
|
%% 新节点自动进行初始化
|
%%
|
startNode(NodeName, FatherNodeName, DeviceId, ClusterId, ClusterName) ->
|
lager:start(),
|
io:format("start Node"),
|
%% setCNode('c1@127.0.0.1'),
|
mnesiaDbStart(1000),
|
TableList = mnesia:system_info(tables),
|
try
|
case TableList of
|
[schema] ->
|
if
|
FatherNodeName == ' ' ->
|
Ret = initLocalFathNodeDB(NodeName),
|
io:format("initLocalFathNodeDB is : ~p ~p~n", [FatherNodeName, Ret]),
|
%%%#todo add device_rec to db
|
clusterInfo:add_ClusterInfo(ClusterId, ClusterName),
|
device_info:add_deviceInfo(uuid:to_string(uuid:uuid5(uuid:uuid4(), "NodeName")), DeviceId, NodeName, ClusterId);
|
true ->
|
Ret = initLocalSubNodeDB(NodeName, FatherNodeName),
|
io:format("initLocalSubNodeDB is : ~p ~p~n", [FatherNodeName, Ret]),
|
%%%#todo add device_rec to db
|
device_info:add_deviceInfo(uuid:to_string(uuid:uuid5(uuid:uuid4(), "NodeName")), DeviceId, NodeName, ClusterId)
|
end,
|
mnesiaDbStart(10000)
|
end
|
catch
|
Ex:Type ->
|
{Ex, Type, erlang:get_stacktrace()},
|
io:format("table list is ~p,~p,~p ~n", [Ex, Type, TableList])
|
end,
|
|
io:format("mnesia info tables ~p~n", [mnesia:info()]),
|
%% try
|
VE = syncDB:findAllNode(),
|
if
|
is_tuple(VE) == true ->
|
RSRET = {faile, notok};
|
is_list(VE) == true ->
|
RSRET = {atomic, ok}
|
end,
|
RSRET.
|
|
modifyCluName(CluId, CluName) ->
|
Tmp = clusterInfo:add_ClusterInfo(CluId, CluName),
|
if
|
Tmp == ok ->
|
RET = [{atomic, ok}];
|
true ->
|
RET = [{aborted, message}]
|
end,
|
RET.
|
|
removeNode() ->
|
%% #todo 集群的最后一个节点退出时,删除集群信息
|
[Node | Nodes] = mnesia:system_info(running_db_nodes),
|
if
|
Node == node() ->
|
io:format("Node eq node() ~n"),
|
device_info:del_DevInfo(Node),
|
mnesia:stop(),
|
{atomic, ok};
|
true ->
|
rpc:call(Node, syncDB, rpcDeleteNode, [node()])
|
end.
|
|
findAllNode() ->
|
device_info:deviceOnlineList2Maps(device_info:findDeviceOnlineList('device_info', mnesia:system_info(running_db_nodes))).
|
|
%%????
|
getNodeOrTable(TableName, SyncType) ->
|
if
|
SyncType == '1' ->
|
NewTableName = TableName,
|
Nodes = mnesia:system_info(running_db_nodes);
|
true ->
|
%% #todo
|
NewTableName = "",
|
Nodes = [node()]
|
end,
|
{Nodes, NewTableName}.
|
|
|
%% 创建底库
|
%% 创建底库 表类型,表名,同步状态,黑白名单
|
createDatabase(TableType, TableName, SyncType, BwType, StartTime, EndTime) ->
|
io:format("createDatabase ~p ~p ~p ~p ~p ~p ~n", [TableType, TableName, SyncType, BwType, StartTime, EndTime]),
|
Uuid = "",
|
Tup = getNodeOrTable(TableName, SyncType),
|
if
|
TableType == 'person' ->
|
%% Ret = person_info:create_personInfo(element(1, Tup), element(2, Tup)),
|
%% Ret2 = person_info_feature:create_personFeature(element(1, Tup), element(2, Tup)),
|
Ret = person_info:create_personInfo(element(1, Tup), TableName),
|
PerFea = addFea2TableName(TableName),
|
Ret2 = person_info_feature:create_personFeature(element(1, Tup), PerFea),
|
io:format("Ret ~p ~n", [Ret]),
|
if
|
Ret == {atomic, ok} ->
|
tableType:add_tableType(Uuid, TableName, BwType),
|
%%todo time
|
sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
|
%% #todo send message
|
PerRet = [Ret | [Ret2]];
|
true ->
|
Z = element(1, element(2, Ret)),
|
io:format("Z ~p ~n", [Z]),
|
if
|
Z == already_exists ->
|
tableType:add_tableType(Uuid, TableName, BwType),
|
%%todo time
|
sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
|
%% #todo send message
|
PerRet = [{atomic, ok} | [{atomic, ok}]];
|
true ->
|
PerRet = [Ret | [Ret2]]
|
end
|
end;
|
TableType == 'car' ->
|
%% Ret = car_info:create_carInfo(element(1, Tup), element(2, Tup)),
|
Ret = car_info:create_carInfo(element(1, Tup), TableName),
|
tableType:add_tableType(Uuid, TableName, BwType),
|
%%todo time
|
sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
|
%% PerRet = [Ret],
|
io:format("Ret ~p ~n", [Ret]),
|
if
|
Ret == {atomic, ok} ->
|
PerRet = [Ret];
|
true ->
|
Z = element(1, element(2, Ret)),
|
io:format("Z ~p ~n", [Z]),
|
if
|
Z == already_exists ->
|
PerRet = [{atomic, ok} | [{atomic, ok}]];
|
true ->
|
PerRet = [Ret]
|
end
|
end;
|
true ->
|
PerRet = [{aborted, {'tableType_error', TableType}}],
|
io:format("Error ~p ~n", [TableType])
|
end,
|
%% tableType:add_tableType(element(2, Tup), BwType),
|
%% if
|
%% SyncType == '1' ->
|
%%
|
%% end,
|
io:format("PerRet ~p ~n", [PerRet]),
|
PerRet.
|
|
%% c底库
|
%% 创建底库 表类型,表名,同步状态,黑白名单
|
updateDatabase(Uuid, TableType, TableName, SyncType, BwType, StartTime, EndTime) ->
|
io:format("createDatabase ~p ~p ~p ~p ~p ~p ~n", [TableType, TableName, SyncType, BwType, StartTime, EndTime]),
|
Tup = getNodeOrTable(TableName, SyncType),
|
if
|
TableType == 'person' ->
|
tableType:add_tableType(Uuid, TableName, BwType),
|
%%todo time
|
sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
|
%% #todo send message
|
PerRet = [{atomic, ok}];
|
TableType == 'car' ->
|
%% Ret = car_info:create_carInfo(element(1, Tup), element(2, Tup)),
|
%% Ret = car_info:create_carInfo(element(1, Tup), TableName),
|
tableType:add_tableType(Uuid, TableName, BwType),
|
%%todo time
|
sys_o_tables:add_sys_table(Uuid, TableName, 'test', TableType, StartTime, EndTime),
|
%% #todo send message
|
PerRet = [{atomic, ok}];
|
true ->
|
PerRet = [{aborted, {'tableType_error', TableType}}],
|
io:format("Error ~p ~n", [TableType])
|
end,
|
io:format("PerRet ~p ~n", [PerRet]),
|
PerRet.
|
|
|
%% shanchu底库
|
%% shanchu底库 表类型,表名
|
deleteDatabase(TableType, TableName) ->
|
io:format("deleteDatabase ~p ~p ~n", [TableType, TableName]),
|
%% case of
|
%% ->;
|
%%end,
|
Ret = mnesia:clear_table(TableName),
|
if
|
TableType == 'person' ->
|
case Ret of
|
{atomic, ok} ->
|
PerFea = addFea2TableName(TableName),
|
Ret2 = mnesia:clear_table(PerFea),
|
case Ret2 of
|
{atomic, ok} ->
|
tableType:del_TableTypeInfo(TableName),
|
sys_o_tables:del_SysTabInfo(TableName),
|
mnesia:delete_table(TableName),
|
mnesia:delete_table(PerFea);
|
%% #todo send message
|
_ ->
|
io:format("")
|
end;
|
_ ->
|
Ret2 = {aborted, notok},
|
io:format("")
|
end,
|
PerRet = [Ret | [Ret2]];
|
TableType == 'car' ->
|
tableType:del_TableTypeInfo(TableName),
|
sys_o_tables:del_SysTabInfo(TableName),
|
mnesia:delete_table(TableName),
|
PerRet = [Ret];
|
true ->
|
PerRet = [{aborted, {'tableType_error', TableType}}],
|
tableType:del_TableTypeInfo(TableName),
|
sys_o_tables:del_SysTabInfo(TableName),
|
io:format("Error ~p ~n", [TableType])
|
end,
|
%% tableType:add_tableType(element(2, Tup), BwType),
|
io:format("PerRet ~p ~n", [PerRet]),
|
PerRet.
|
|
findAllDatabase() ->
|
sys_o_tables:sysTabformat2Mat(sys_o_tables:findAllSysTableInfo('sys_o_tables')).
|
|
findAllTypeInfo() ->
|
tableType:tableTypeformat2Mat(tableType:findAllTableTypeInfo('tableType')).
|
|
|
%% 插入人
|
%% TableName, ID, PersonName, Age, Sex, IdCard, PhoneNum
|
%% TableName, Feature
|
addPersonData(TableName, PersonName, Age, Sex, IdCard, PhoneNum, Img, UUID, Feature) ->
|
if
|
UUID == '' ->
|
io:format("UUID is ~p ~n", [UUID]),
|
ID = syncTool:getUUIDStr();
|
true ->
|
ID = UUID
|
end,
|
PerFea = addFea2TableName(TableName),
|
Uid = person_info_feature:add_personFea(ID, PerFea, Feature, Img, IdCard),
|
if
|
Uid == '' ->
|
Ret = {aborted, {add_error, Feature}};
|
true ->
|
Ret = person_info:add_personInfo(TableName, Uid, PersonName, Age, Sex, IdCard, PhoneNum)
|
% case Ret of
|
% '' ->
|
% ok;
|
% _ ->
|
% syncTool:rpcSendMessToCNode(mnesia:system_info(running_db_nodes), {call, 'bar', 65})
|
% end
|
end,
|
io:format("Ret ~p ~n", [Ret]),
|
Ret.
|
|
%% 删除人
|
%% TableName, UUID
|
deletePersonData(TableName, UUID) ->
|
PerFea = addFea2TableName(TableName),
|
DelRet = person_info_feature:del_personFea(PerFea, UUID),
|
if
|
DelRet == {atomic, ok} ->
|
Ret = [person_info:del_personInfo(TableName, UUID)];
|
true ->
|
Ret = {aborted, {del_error, {TableName, UUID}}}
|
end,
|
io:format("Ret ~p ~n", [Ret]),
|
Ret.
|
|
findSinglePersonInfo(TableName, UUID) ->
|
person_info:personInfoFormat2Mat(person_info:findPersonInfo(TableName, UUID)).
|
|
|
testFunc(PersonId, []) ->
|
[];
|
testFunc(PersonId, [TN | T]) ->
|
io:format("table is ~p ", [TN]),
|
TableName = element(1, TN),
|
Records = person_info:findPersonInfo(TableName, PersonId),
|
case Records of
|
[] ->
|
TemMaps = #{"tableName"=>TableName, "Uuid"=>"null"};
|
_ ->
|
[TRco | TRecords] = Records,
|
io:format("TRco is ~p ", [TRco]),
|
TemMaps = #{"tableName"=>TableName, "Uuid"=>element(1, TRco)}
|
end,
|
[maps:to_list(TemMaps) | testFunc(PersonId, T)].
|
|
%% 判断集群内某人是否存在
|
singlePersonIsExists(PersonId) ->
|
TableLists = sys_o_tables:findAllSysTableInfo('sys_o_tables'),
|
testFunc(PersonId, TableLists).
|
|
|
testFunc2(NewPerId, OldId, Idcard, PerPicUrl, []) ->
|
[];
|
testFunc2(NewPerId, OldId, Idcard, PerPicUrl, [TableRec | T]) ->
|
io:format("table is ~p ~n", [TableRec]),
|
TableName = element(1, TableRec),
|
NewRecords = person_info:findPersonInfo(TableName, NewPerId),
|
case NewRecords of
|
[] ->
|
io:format("~p have no newId ~p will search oldId ~n", [TableName, NewPerId]),
|
OldRecords = person_info:findPersonInfo(TableName, OldId),
|
case OldRecords of
|
[] ->
|
io:format("~p have no oldId ~p ~n", [TableName, OldId]),
|
ResRetMaps = #{"tableName"=>TableName, "result"=>"notexist"},
|
%% no action
|
ok;
|
_ ->
|
io:format("~p have oldId ~p will update record~n", [TableName, OldId]),
|
%% update oldRecord
|
Ret = {},
|
case Ret of
|
[] ->
|
%% ok?
|
ResRetMaps = #{"tableName"=>TableName, "result"=>"true"},
|
ok;
|
_ ->
|
%% not ok
|
ResRetMaps = #{"tableName"=>TableName, "result"=>"false"},
|
ok
|
end,
|
ok
|
end;
|
_ ->
|
io:format("~p have newId ~p will update newId ~n", [TableName, NewPerId]),
|
%% update newRecord
|
OldRecords = person_info:findPersonInfo(TableName, OldId),
|
case OldRecords of
|
[] ->
|
%% no action
|
io:format("~p have no oldId ~p ~n", [TableName, OldId]),
|
ok;
|
_ ->
|
%% delete oldRecord
|
io:format("~p have oldId ~p will delete record~n", [TableName, OldId]),
|
deletePersonData(TableName, OldId),
|
ok
|
end,
|
ResRetMaps = #{"tableName"=>TableName, "result"=>"true"},
|
ok
|
end,
|
[maps:to_list(ResRetMaps) | testFunc2(NewPerId, OldId, Idcard, PerPicUrl, T)].
|
|
singlePersonUpdate(NewPerId, OldId, Idcard, PerPicUrl) ->
|
TableLists = sys_o_tables:findAllSysTableInfo('sys_o_tables'),
|
testFunc2(NewPerId, OldId, Idcard, PerPicUrl, TableLists).
|
|
%% 插入车
|
addCarData() ->
|
ok.
|
|
%% 加载人脸数据库
|
loadFaceFeaData(TableName) ->
|
PerFea = addFea2TableName(TableName),
|
Results = person_info_feature:findAllPersonFea(PerFea),
|
person_info_feature:personFeaFormat2Mat(Results).
|
|
%% 加载人脸数据库2 no feature
|
loadFaceFeaData2(TableName) ->
|
PerFea = addFea2TableName(TableName),
|
Results = person_info_feature:findAllPersonFea(PerFea),
|
person_info_feature:personNoFeaFormat2Mat(Results).
|