Optimize the trie match

This commit is contained in:
Feng Lee 2019-01-04 17:49:55 +08:00 committed by Feng Lee
parent 0c418581b2
commit 4230a3c535
1 changed files with 11 additions and 5 deletions

View File

@ -101,14 +101,20 @@ do_add_route(Topic, Dest) when is_binary(Topic) ->
%% @doc Match routes %% @doc Match routes
-spec(match_routes(emqx_topic:topic()) -> [emqx_types:route()]). -spec(match_routes(emqx_topic:topic()) -> [emqx_types:route()]).
match_routes(Topic) when is_binary(Topic) -> match_routes(Topic) when is_binary(Topic) ->
case emqx_trie:empty() of case match_trie(Topic) of
true -> lookup_routes(Topic); [] -> lookup_routes(Topic);
false -> Matched ->
%% Optimize: routing table will be replicated to all router nodes.
Matched = mnesia:ets(fun emqx_trie:match/1, [Topic]),
lists:append([lookup_routes(To) || To <- [Topic | Matched]]) lists:append([lookup_routes(To) || To <- [Topic | Matched]])
end. end.
%% @private
%% Optimize: routing table will be replicated to all router nodes.
match_trie(Topic) ->
case emqx_trie:empty() of
true -> [];
false -> mnesia:ets(fun emqx_trie:match/1, [Topic])
end.
-spec(lookup_routes(emqx_topic:topic()) -> [emqx_types:route()]). -spec(lookup_routes(emqx_topic:topic()) -> [emqx_types:route()]).
lookup_routes(Topic) -> lookup_routes(Topic) ->
ets:lookup(?ROUTE, Topic). ets:lookup(?ROUTE, Topic).