merge zenmq_trie, zenmq_mnesia, zenmq_boot
This commit is contained in:
parent
bd1e618cc8
commit
3c53cf2b09
|
@ -18,6 +18,10 @@
|
||||||
|
|
||||||
-export([apply_module_attributes/1, all_module_attributes/1]).
|
-export([apply_module_attributes/1, all_module_attributes/1]).
|
||||||
|
|
||||||
|
-ifdef(TEST).
|
||||||
|
-compile(export_all).
|
||||||
|
-endif.
|
||||||
|
|
||||||
%% only {F, Args}...
|
%% only {F, Args}...
|
||||||
apply_module_attributes(Name) ->
|
apply_module_attributes(Name) ->
|
||||||
[{Module, [apply(Module, F, Args) || {F, Args} <- Attrs]} ||
|
[{Module, [apply(Module, F, Args) || {F, Args} <- Attrs]} ||
|
||||||
|
@ -43,7 +47,7 @@ all_module_attributes(Name) ->
|
||||||
%% Copy from rabbit_misc.erl
|
%% Copy from rabbit_misc.erl
|
||||||
module_attributes(Module) ->
|
module_attributes(Module) ->
|
||||||
case catch Module:module_info(attributes) of
|
case catch Module:module_info(attributes) of
|
||||||
{'EXIT', {undef, [{Module, module_info, _} | _]}} ->
|
{'EXIT', {undef, [{Module, module_info, [attributes], []} | _]}} ->
|
||||||
[];
|
[];
|
||||||
{'EXIT', Reason} ->
|
{'EXIT', Reason} ->
|
||||||
exit(Reason);
|
exit(Reason);
|
||||||
|
|
|
@ -30,6 +30,9 @@
|
||||||
%% Schema and tables
|
%% Schema and tables
|
||||||
-export([copy_schema/1, delete_schema/0, del_schema_copy/1,
|
-export([copy_schema/1, delete_schema/0, del_schema_copy/1,
|
||||||
create_table/2, copy_table/1, copy_table/2]).
|
create_table/2, copy_table/1, copy_table/2]).
|
||||||
|
-ifdef(TEST).
|
||||||
|
-compile(export_all).
|
||||||
|
-endif.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Start and init mnesia
|
%% Start and init mnesia
|
||||||
|
@ -187,8 +190,8 @@ remove_from_cluster(Node) when Node =/= node() ->
|
||||||
ensure_ok(del_schema_copy(Node)),
|
ensure_ok(del_schema_copy(Node)),
|
||||||
ensure_ok(rpc:call(Node, ?MODULE, delete_schema, []));
|
ensure_ok(rpc:call(Node, ?MODULE, delete_schema, []));
|
||||||
{true, false} ->
|
{true, false} ->
|
||||||
ensure_ok(zenmq_mnesia:del_schema_copy(Node)),
|
ensure_ok(del_schema_copy(Node)),
|
||||||
ensure_ok(rpc:call(Node, ?MODULE, delete_schema, [Node]));
|
ensure_ok(rpc:call(Node, ?MODULE, delete_schema, []));
|
||||||
{false, _} ->
|
{false, _} ->
|
||||||
{error, node_not_in_cluster}
|
{error, node_not_in_cluster}
|
||||||
end.
|
end.
|
||||||
|
@ -228,14 +231,14 @@ ensure_tab({aborted, {already_exists, _Name}}) -> ok;
|
||||||
ensure_tab({aborted, {already_exists, _Name, _Node}})-> ok;
|
ensure_tab({aborted, {already_exists, _Name, _Node}})-> ok;
|
||||||
ensure_tab({aborted, Error}) -> Error.
|
ensure_tab({aborted, Error}) -> Error.
|
||||||
|
|
||||||
%% @doc Wait for mnesia to start, stop or copy tables.
|
%% @doc Wait for mnesia to start, stop or tables ready.
|
||||||
-spec wait_for(start | stop | tables) -> ok | {error, Reason :: atom()}.
|
-spec wait_for(start | stop | tables) -> ok | {error, Reason :: atom()}.
|
||||||
wait_for(start) ->
|
wait_for(start) ->
|
||||||
case mnesia:system_info(is_running) of
|
case mnesia:system_info(is_running) of
|
||||||
yes -> ok;
|
yes -> ok;
|
||||||
no -> {error, mnesia_unexpectedly_stopped};
|
no -> {error, mnesia_unexpectedly_stopped};
|
||||||
stopping -> {error, mnesia_unexpectedly_stopping};
|
stopping -> {error, mnesia_unexpectedly_stopping};
|
||||||
starting -> timer:sleep(500), wait_for(start)
|
starting -> timer:sleep(1000), wait_for(start)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
wait_for(stop) ->
|
wait_for(stop) ->
|
||||||
|
@ -243,7 +246,7 @@ wait_for(stop) ->
|
||||||
no -> ok;
|
no -> ok;
|
||||||
yes -> {error, mnesia_unexpectedly_running};
|
yes -> {error, mnesia_unexpectedly_running};
|
||||||
starting -> {error, mnesia_unexpectedly_starting};
|
starting -> {error, mnesia_unexpectedly_starting};
|
||||||
stopping -> timer:sleep(500), wait_for(stop)
|
stopping -> timer:sleep(1000), wait_for(stop)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
wait_for(tables) ->
|
wait_for(tables) ->
|
||||||
|
|
|
@ -14,9 +14,13 @@
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
%% @doc MQTT Topic Trie: [Trie](http://en.wikipedia.org/wiki/Trie)
|
%% @doc MQTT Topic Trie:
|
||||||
|
%% [Trie](http://en.wikipedia.org/wiki/Trie)
|
||||||
|
%% @end
|
||||||
-module(emqttd_trie).
|
-module(emqttd_trie).
|
||||||
|
|
||||||
|
-include("emqttd_trie.hrl").
|
||||||
|
|
||||||
%% Mnesia Callbacks
|
%% Mnesia Callbacks
|
||||||
-export([mnesia/1]).
|
-export([mnesia/1]).
|
||||||
|
|
||||||
|
@ -26,24 +30,6 @@
|
||||||
%% Trie API
|
%% Trie API
|
||||||
-export([insert/1, match/1, delete/1, lookup/1]).
|
-export([insert/1, match/1, delete/1, lookup/1]).
|
||||||
|
|
||||||
-type node_id() :: binary() | atom().
|
|
||||||
|
|
||||||
-record(trie_node, {
|
|
||||||
node_id :: node_id(),
|
|
||||||
edge_count = 0 :: non_neg_integer(),
|
|
||||||
topic :: binary() | undefined
|
|
||||||
}).
|
|
||||||
|
|
||||||
-record(trie_edge, {
|
|
||||||
node_id :: node_id(),
|
|
||||||
word :: binary() | atom()
|
|
||||||
}).
|
|
||||||
|
|
||||||
-record(trie, {
|
|
||||||
edge :: #trie_edge{},
|
|
||||||
node_id :: node_id()
|
|
||||||
}).
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Mnesia Callbacks
|
%% Mnesia Callbacks
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
@ -72,7 +58,7 @@ mnesia(copy) ->
|
||||||
%% Trie API
|
%% Trie API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
%% @doc Insert topic to trie tree.
|
%% @doc Insert topic to trie
|
||||||
-spec insert(Topic :: binary()) -> ok.
|
-spec insert(Topic :: binary()) -> ok.
|
||||||
insert(Topic) when is_binary(Topic) ->
|
insert(Topic) when is_binary(Topic) ->
|
||||||
case mnesia:read(trie_node, Topic) of
|
case mnesia:read(trie_node, Topic) of
|
||||||
|
@ -112,7 +98,7 @@ delete(Topic) when is_binary(Topic) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal Functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
|
|
|
@ -16,15 +16,74 @@
|
||||||
|
|
||||||
-module(emqttd_trie_tests).
|
-module(emqttd_trie_tests).
|
||||||
|
|
||||||
|
-ifdef(TEST).
|
||||||
|
|
||||||
-include("emqttd.hrl").
|
-include("emqttd.hrl").
|
||||||
|
|
||||||
-ifdef(TEST).
|
-include("emqttd_trie.hrl").
|
||||||
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
match_test() ->
|
-define(TRIE, emqttd_trie).
|
||||||
mnesia:start(),
|
|
||||||
emqttd_trie:mnesia(boot),
|
trie_test_() ->
|
||||||
|
{setup, fun setup/0, fun teardown/1,
|
||||||
|
[{foreach, fun() -> ok end, fun(_) -> clear() end,
|
||||||
|
[?_test(t_insert()),
|
||||||
|
?_test(t_match()),
|
||||||
|
?_test(t_match2()),
|
||||||
|
?_test(t_match3()),
|
||||||
|
?_test(t_delete()),
|
||||||
|
?_test(t_delete2()),
|
||||||
|
?_test(t_delete3())]
|
||||||
|
}]}.
|
||||||
|
|
||||||
|
setup() ->
|
||||||
|
emqttd_mnesia:ensure_started(),
|
||||||
|
?TRIE:mnesia(boot),
|
||||||
|
?TRIE:mnesia(copy).
|
||||||
|
|
||||||
|
teardown(_) ->
|
||||||
|
emqttd_mnesia:ensure_stopped(),
|
||||||
|
emqttd_mnesia:delete_schema().
|
||||||
|
|
||||||
|
t_insert() ->
|
||||||
|
TN = #trie_node{node_id = <<"sensor">>,
|
||||||
|
edge_count = 3,
|
||||||
|
topic = <<"sensor">>,
|
||||||
|
flags = undefined},
|
||||||
|
?assertEqual({atomic, [TN]}, mnesia:transaction(
|
||||||
|
fun() ->
|
||||||
|
?TRIE:insert(<<"sensor/1/metric/2">>),
|
||||||
|
?TRIE:insert(<<"sensor/+/#">>),
|
||||||
|
?TRIE:insert(<<"sensor/#">>),
|
||||||
|
?TRIE:insert(<<"sensor">>),
|
||||||
|
?TRIE:insert(<<"sensor">>),
|
||||||
|
?TRIE:lookup(<<"sensor">>)
|
||||||
|
end)).
|
||||||
|
|
||||||
|
t_match() ->
|
||||||
|
Machted = [<<"sensor/+/#">>, <<"sensor/#">>],
|
||||||
|
?assertEqual({atomic, Machted}, mnesia:transaction(
|
||||||
|
fun() ->
|
||||||
|
?TRIE:insert(<<"sensor/1/metric/2">>),
|
||||||
|
?TRIE:insert(<<"sensor/+/#">>),
|
||||||
|
?TRIE:insert(<<"sensor/#">>),
|
||||||
|
?TRIE:match(<<"sensor/1">>)
|
||||||
|
end)).
|
||||||
|
|
||||||
|
t_match2() ->
|
||||||
|
Matched = {[<<"+/+/#">>, <<"+/#">>, <<"#">>], []},
|
||||||
|
?assertEqual({atomic, Matched}, mnesia:transaction(
|
||||||
|
fun() ->
|
||||||
|
?TRIE:insert(<<"#">>),
|
||||||
|
?TRIE:insert(<<"+/#">>),
|
||||||
|
?TRIE:insert(<<"+/+/#">>),
|
||||||
|
{?TRIE:match(<<"a/b/c">>),
|
||||||
|
?TRIE:match(<<"$SYS/broker/zenmq">>)}
|
||||||
|
end)).
|
||||||
|
|
||||||
|
t_match3() ->
|
||||||
Topics = [<<"d/#">>, <<"a/b/c">>, <<"a/b/+">>, <<"a/#">>, <<"#">>, <<"$SYS/#">>],
|
Topics = [<<"d/#">>, <<"a/b/c">>, <<"a/b/+">>, <<"a/#">>, <<"#">>, <<"$SYS/#">>],
|
||||||
mnesia:transaction(fun() -> [emqttd_trie:insert(Topic) || Topic <- Topics] end),
|
mnesia:transaction(fun() -> [emqttd_trie:insert(Topic) || Topic <- Topics] end),
|
||||||
Matched = mnesia:async_dirty(fun emqttd_trie:match/1, [<<"a/b/c">>]),
|
Matched = mnesia:async_dirty(fun emqttd_trie:match/1, [<<"a/b/c">>]),
|
||||||
|
@ -32,4 +91,51 @@ match_test() ->
|
||||||
SysMatched = mnesia:async_dirty(fun emqttd_trie:match/1, [<<"$SYS/a/b/c">>]),
|
SysMatched = mnesia:async_dirty(fun emqttd_trie:match/1, [<<"$SYS/a/b/c">>]),
|
||||||
?assertEqual([<<"$SYS/#">>], SysMatched).
|
?assertEqual([<<"$SYS/#">>], SysMatched).
|
||||||
|
|
||||||
|
t_delete() ->
|
||||||
|
TN = #trie_node{node_id = <<"sensor/1">>,
|
||||||
|
edge_count = 2,
|
||||||
|
topic = undefined,
|
||||||
|
flags = undefined},
|
||||||
|
?assertEqual({atomic, [TN]}, mnesia:transaction(
|
||||||
|
fun() ->
|
||||||
|
?TRIE:insert(<<"sensor/1/#">>),
|
||||||
|
?TRIE:insert(<<"sensor/1/metric/2">>),
|
||||||
|
?TRIE:insert(<<"sensor/1/metric/3">>),
|
||||||
|
?TRIE:delete(<<"sensor/1/metric/2">>),
|
||||||
|
?TRIE:delete(<<"sensor/1/metric">>),
|
||||||
|
?TRIE:delete(<<"sensor/1/metric">>),
|
||||||
|
?TRIE:lookup(<<"sensor/1">>)
|
||||||
|
end)).
|
||||||
|
|
||||||
|
t_delete2() ->
|
||||||
|
?assertEqual({atomic, {[], []}}, mnesia:transaction(
|
||||||
|
fun() ->
|
||||||
|
?TRIE:insert(<<"sensor">>),
|
||||||
|
?TRIE:insert(<<"sensor/1/metric/2">>),
|
||||||
|
?TRIE:insert(<<"sensor/1/metric/3">>),
|
||||||
|
?TRIE:delete(<<"sensor">>),
|
||||||
|
?TRIE:delete(<<"sensor/1/metric/2">>),
|
||||||
|
?TRIE:delete(<<"sensor/1/metric/3">>),
|
||||||
|
{?TRIE:lookup(<<"sensor">>),
|
||||||
|
?TRIE:lookup(<<"sensor/1">>)}
|
||||||
|
end)).
|
||||||
|
|
||||||
|
t_delete3() ->
|
||||||
|
?assertEqual({atomic, {[], []}}, mnesia:transaction(
|
||||||
|
fun() ->
|
||||||
|
?TRIE:insert(<<"sensor/+">>),
|
||||||
|
?TRIE:insert(<<"sensor/+/metric/2">>),
|
||||||
|
?TRIE:insert(<<"sensor/+/metric/3">>),
|
||||||
|
?TRIE:delete(<<"sensor/+/metric/2">>),
|
||||||
|
?TRIE:delete(<<"sensor/+/metric/3">>),
|
||||||
|
?TRIE:delete(<<"sensor">>),
|
||||||
|
?TRIE:delete(<<"sensor/+">>),
|
||||||
|
?TRIE:delete(<<"sensor/+/unknown">>),
|
||||||
|
{?TRIE:lookup(<<"sensor">>), ?TRIE:lookup(<<"sensor/+">>)}
|
||||||
|
end)).
|
||||||
|
|
||||||
|
clear() ->
|
||||||
|
mnesia:clear_table(trie),
|
||||||
|
mnesia:clear_table(trie_node).
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
Loading…
Reference in New Issue