Optimize the trie match
This commit is contained in:
parent
0c418581b2
commit
4230a3c535
|
@ -101,14 +101,20 @@ do_add_route(Topic, Dest) when is_binary(Topic) ->
|
|||
%% @doc Match routes
|
||||
-spec(match_routes(emqx_topic:topic()) -> [emqx_types:route()]).
|
||||
match_routes(Topic) when is_binary(Topic) ->
|
||||
case emqx_trie:empty() of
|
||||
true -> lookup_routes(Topic);
|
||||
false ->
|
||||
%% Optimize: routing table will be replicated to all router nodes.
|
||||
Matched = mnesia:ets(fun emqx_trie:match/1, [Topic]),
|
||||
case match_trie(Topic) of
|
||||
[] -> lookup_routes(Topic);
|
||||
Matched ->
|
||||
lists:append([lookup_routes(To) || To <- [Topic | Matched]])
|
||||
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()]).
|
||||
lookup_routes(Topic) ->
|
||||
ets:lookup(?ROUTE, Topic).
|
||||
|
|
Loading…
Reference in New Issue