fix issue #68 - /# topics will not match #, +/#

This commit is contained in:
Feng 2015-10-11 15:27:56 +08:00
parent 9571143e41
commit 8eb54cc2c3
2 changed files with 12 additions and 5 deletions

View File

@ -223,7 +223,7 @@ dispatch(Topic, #mqtt_message{qos = Qos} = Msg ) when is_binary(Topic) ->
-spec match(Topic :: binary()) -> [mqtt_topic()].
match(Topic) when is_binary(Topic) ->
MatchedTopics = mnesia:async_dirty(fun emqttd_trie:find/1, [Topic]),
MatchedTopics = mnesia:async_dirty(fun emqttd_trie:match/1, [Topic]),
lists:append([mnesia:dirty_read(topic, Name) || Name <- MatchedTopics]).
%%%=============================================================================

View File

@ -37,7 +37,7 @@
-copy_mnesia({mnesia, [copy]}).
%% Trie API
-export([insert/1, find/1, delete/1]).
-export([insert/1, match/1, delete/1]).
-type node_id() :: binary() | atom().
@ -111,9 +111,9 @@ insert(Topic) when is_binary(Topic) ->
%% @doc Find trie nodes that match topic
%% @end
%%------------------------------------------------------------------------------
-spec find(Topic :: binary()) -> list(MatchedTopic :: binary()).
find(Topic) when is_binary(Topic) ->
TrieNodes = match_node(root, emqttd_topic:words(Topic), []),
-spec match(Topic :: binary()) -> list(MatchedTopic :: binary()).
match(Topic) when is_binary(Topic) ->
TrieNodes = match_node(root, emqttd_topic:words(Topic)),
[Name || #trie_node{topic=Name} <- TrieNodes, Name=/= undefined].
%%------------------------------------------------------------------------------
@ -166,6 +166,13 @@ add_path({Node, Word, Child}) ->
%%
%% @end
%%------------------------------------------------------------------------------
match_node(root, [<<"$SYS">>|Words]) ->
match_node(<<"$SYS">>, Words, []);
match_node(NodeId, Words) ->
match_node(NodeId, Words, []).
match_node(NodeId, [], ResAcc) ->
mnesia:read(trie_node, NodeId) ++ 'match_#'(NodeId, ResAcc);