refactor(mgmt): convert fuzzy filter func to named func

This commit is contained in:
JianBo He 2022-11-22 15:33:42 +08:00 committed by Zaiming (Stone) Shi
parent 0122d3900f
commit 9786a6c267
8 changed files with 35 additions and 48 deletions

View File

@ -48,6 +48,7 @@
-export([ -export([
qs2ms/2, qs2ms/2,
run_fuzzy_filter/2,
format_user_info/1, format_user_info/1,
group_match_spec/1 group_match_spec/1
]). ]).
@ -281,12 +282,7 @@ qs2ms(_Tab, {QString, Fuzzy}) ->
fuzzy_filter_fun([]) -> fuzzy_filter_fun([]) ->
undefined; undefined;
fuzzy_filter_fun(Fuzzy) -> fuzzy_filter_fun(Fuzzy) ->
fun(MsRaws) when is_list(MsRaws) -> {fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
lists:filter(
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
MsRaws
)
end.
run_fuzzy_filter(_, []) -> run_fuzzy_filter(_, []) ->
true; true;

View File

@ -50,6 +50,7 @@
-export([ -export([
qs2ms/2, qs2ms/2,
run_fuzzy_filter/2,
format_user_info/1, format_user_info/1,
group_match_spec/1 group_match_spec/1
]). ]).
@ -307,12 +308,7 @@ qs2ms(_Tab, {QString, FuzzyQString}) ->
fuzzy_filter_fun([]) -> fuzzy_filter_fun([]) ->
undefined; undefined;
fuzzy_filter_fun(Fuzzy) -> fuzzy_filter_fun(Fuzzy) ->
fun(MsRaws) when is_list(MsRaws) -> {fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
lists:filter(
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
MsRaws
)
end.
run_fuzzy_filter(_, []) -> run_fuzzy_filter(_, []) ->
true; true;

View File

@ -51,6 +51,7 @@
-export([ -export([
query_username/2, query_username/2,
query_clientid/2, query_clientid/2,
run_fuzzy_filter/2,
format_result/1 format_result/1
]). ]).
@ -589,12 +590,7 @@ query_clientid(_Tab, {_QString, FuzzyQString}) ->
fuzzy_filter_fun([]) -> fuzzy_filter_fun([]) ->
undefined; undefined;
fuzzy_filter_fun(Fuzzy) -> fuzzy_filter_fun(Fuzzy) ->
fun(MsRaws) when is_list(MsRaws) -> {fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
lists:filter(
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
MsRaws
)
end.
run_fuzzy_filter(_, []) -> run_fuzzy_filter(_, []) ->
true; true;

View File

@ -56,6 +56,7 @@
%% internal exports (for client query) %% internal exports (for client query)
-export([ -export([
qs2ms/2, qs2ms/2,
run_fuzzy_filter/2,
format_channel_info/1, format_channel_info/1,
format_channel_info/2 format_channel_info/2
]). ]).
@ -327,12 +328,7 @@ ms(lifetime, X) ->
fuzzy_filter_fun([]) -> fuzzy_filter_fun([]) ->
undefined; undefined;
fuzzy_filter_fun(Fuzzy) -> fuzzy_filter_fun(Fuzzy) ->
fun(MsRaws) when is_list(MsRaws) -> {fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
lists:filter(
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
MsRaws
)
end.
run_fuzzy_filter(_, []) -> run_fuzzy_filter(_, []) ->
true; true;

View File

@ -128,7 +128,9 @@ limit(Params) ->
]. ].
-type query_to_match_spec_fun() :: -type query_to_match_spec_fun() ::
fun((list(), list()) -> {ets:match_spec(), fun()}). fun((list(), list()) -> {ets:match_spec(), fuzzy_filter_fun()}).
-type fuzzy_filter_fun() :: undefined | {fun(), list()}.
-type format_result_fun() :: -type format_result_fun() ::
fun((node(), term()) -> term()) fun((node(), term()) -> term())
@ -269,6 +271,15 @@ collect_total_from_tail_nodes(Nodes, QueryState, ResultAcc = #{total := TotalAcc
%% } %% }
init_query_state(Tab, QString, MsFun, _Meta = #{page := Page, limit := Limit}) -> init_query_state(Tab, QString, MsFun, _Meta = #{page := Page, limit := Limit}) ->
{Ms, FuzzyFun} = erlang:apply(MsFun, [Tab, QString]), {Ms, FuzzyFun} = erlang:apply(MsFun, [Tab, QString]),
%% assert FuzzyFun type
_ =
case FuzzyFun of
undefined ->
ok;
{NamedFun, Args} ->
true = is_list(Args),
{type, external} = erlang:fun_info(NamedFun, type)
end,
#{ #{
page => Page, page => Page,
limit => Limit, limit => Limit,
@ -323,9 +334,14 @@ do_select(
{[], QueryState#{continuation => ?FRESH_SELECT}}; {[], QueryState#{continuation => ?FRESH_SELECT}};
{Rows, NContinuation} -> {Rows, NContinuation} ->
NRows = NRows =
case is_function(FuzzyFun) of case FuzzyFun of
true -> FuzzyFun(Rows); undefined ->
false -> Rows Rows;
{FilterFun, Args0} when is_function(FilterFun), is_list(Args0) ->
lists:filter(
fun(E) -> erlang:apply(FilterFun, [E | Args0]) end,
Rows
)
end, end,
{NRows, QueryState#{continuation => NContinuation}} {NRows, QueryState#{continuation => NContinuation}}
end. end.
@ -361,7 +377,7 @@ counting_total_fun(_QueryState = #{mactch_spec := Ms, fuzzy_fun := undefined}) -
fun(Tab) -> fun(Tab) ->
ets:select_count(Tab, CountingMs) ets:select_count(Tab, CountingMs)
end; end;
counting_total_fun(_QueryState = #{fuzzy_fun := FuzzyFun}) when is_function(FuzzyFun) -> counting_total_fun(_QueryState = #{fuzzy_fun := FuzzyFun}) when FuzzyFun =/= undefined ->
%% XXX: Calculating the total number for a fuzzy searching is very very expensive %% XXX: Calculating the total number for a fuzzy searching is very very expensive
%% so it is not supported now %% so it is not supported now
false. false.

View File

@ -47,6 +47,7 @@
-export([ -export([
qs2ms/2, qs2ms/2,
run_fuzzy_filter/2,
format_channel_info/1, format_channel_info/1,
format_channel_info/2 format_channel_info/2
]). ]).
@ -841,12 +842,7 @@ ms(created_at, X) ->
fuzzy_filter_fun([]) -> fuzzy_filter_fun([]) ->
undefined; undefined;
fuzzy_filter_fun(Fuzzy) -> fuzzy_filter_fun(Fuzzy) ->
fun(MsRaws) when is_list(MsRaws) -> {fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
lists:filter(
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
MsRaws
)
end.
run_fuzzy_filter(_, []) -> run_fuzzy_filter(_, []) ->
true; true;

View File

@ -33,6 +33,7 @@
-export([ -export([
qs2ms/2, qs2ms/2,
run_fuzzy_filter/2,
format/2 format/2
]). ]).
@ -214,12 +215,7 @@ update_ms(qos, X, {{Pid, Topic}, Opts}) ->
fuzzy_filter_fun([]) -> fuzzy_filter_fun([]) ->
undefined; undefined;
fuzzy_filter_fun(Fuzzy) -> fuzzy_filter_fun(Fuzzy) ->
fun(MsRaws) when is_list(MsRaws) -> {fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
lists:filter(
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
MsRaws
)
end.
run_fuzzy_filter(_, []) -> run_fuzzy_filter(_, []) ->
true; true;

View File

@ -34,7 +34,7 @@
-export(['/rule_events'/2, '/rule_test'/2, '/rules'/2, '/rules/:id'/2, '/rules/:id/reset_metrics'/2]). -export(['/rule_events'/2, '/rule_test'/2, '/rules'/2, '/rules/:id'/2, '/rules/:id/reset_metrics'/2]).
%% query callback %% query callback
-export([qs2ms/2, format_rule_resp/1]). -export([qs2ms/2, run_fuzzy_match/2, format_rule_resp/1]).
-define(ERR_NO_RULE(ID), list_to_binary(io_lib:format("Rule ~ts Not Found", [(ID)]))). -define(ERR_NO_RULE(ID), list_to_binary(io_lib:format("Rule ~ts Not Found", [(ID)]))).
-define(ERR_BADARGS(REASON), begin -define(ERR_BADARGS(REASON), begin
@ -582,12 +582,7 @@ ms(enable, X) ->
fuzzy_match_fun([]) -> fuzzy_match_fun([]) ->
undefined; undefined;
fuzzy_match_fun(Fuzzy) -> fuzzy_match_fun(Fuzzy) ->
fun(MsRaws) when is_list(MsRaws) -> {fun ?MODULE:run_fuzzy_match/2, [Fuzzy]}.
lists:filter(
fun(E) -> run_fuzzy_match(E, Fuzzy) end,
MsRaws
)
end.
run_fuzzy_match(_, []) -> run_fuzzy_match(_, []) ->
true; true;