From 4230a3c535a5b2fb13d11507699f25de7f306cc5 Mon Sep 17 00:00:00 2001 From: Feng Lee Date: Fri, 4 Jan 2019 17:49:55 +0800 Subject: [PATCH] Optimize the trie match --- src/emqx_router.erl | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/emqx_router.erl b/src/emqx_router.erl index 793735d55..f01db0d52 100644 --- a/src/emqx_router.erl +++ b/src/emqx_router.erl @@ -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).