refactor(mgmt): convert fuzzy filter func to named func
This commit is contained in:
parent
0122d3900f
commit
9786a6c267
|
@ -48,6 +48,7 @@
|
|||
|
||||
-export([
|
||||
qs2ms/2,
|
||||
run_fuzzy_filter/2,
|
||||
format_user_info/1,
|
||||
group_match_spec/1
|
||||
]).
|
||||
|
@ -281,12 +282,7 @@ qs2ms(_Tab, {QString, Fuzzy}) ->
|
|||
fuzzy_filter_fun([]) ->
|
||||
undefined;
|
||||
fuzzy_filter_fun(Fuzzy) ->
|
||||
fun(MsRaws) when is_list(MsRaws) ->
|
||||
lists:filter(
|
||||
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||
MsRaws
|
||||
)
|
||||
end.
|
||||
{fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
|
||||
|
||||
run_fuzzy_filter(_, []) ->
|
||||
true;
|
||||
|
|
|
@ -50,6 +50,7 @@
|
|||
|
||||
-export([
|
||||
qs2ms/2,
|
||||
run_fuzzy_filter/2,
|
||||
format_user_info/1,
|
||||
group_match_spec/1
|
||||
]).
|
||||
|
@ -307,12 +308,7 @@ qs2ms(_Tab, {QString, FuzzyQString}) ->
|
|||
fuzzy_filter_fun([]) ->
|
||||
undefined;
|
||||
fuzzy_filter_fun(Fuzzy) ->
|
||||
fun(MsRaws) when is_list(MsRaws) ->
|
||||
lists:filter(
|
||||
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||
MsRaws
|
||||
)
|
||||
end.
|
||||
{fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
|
||||
|
||||
run_fuzzy_filter(_, []) ->
|
||||
true;
|
||||
|
|
|
@ -51,6 +51,7 @@
|
|||
-export([
|
||||
query_username/2,
|
||||
query_clientid/2,
|
||||
run_fuzzy_filter/2,
|
||||
format_result/1
|
||||
]).
|
||||
|
||||
|
@ -589,12 +590,7 @@ query_clientid(_Tab, {_QString, FuzzyQString}) ->
|
|||
fuzzy_filter_fun([]) ->
|
||||
undefined;
|
||||
fuzzy_filter_fun(Fuzzy) ->
|
||||
fun(MsRaws) when is_list(MsRaws) ->
|
||||
lists:filter(
|
||||
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||
MsRaws
|
||||
)
|
||||
end.
|
||||
{fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
|
||||
|
||||
run_fuzzy_filter(_, []) ->
|
||||
true;
|
||||
|
|
|
@ -56,6 +56,7 @@
|
|||
%% internal exports (for client query)
|
||||
-export([
|
||||
qs2ms/2,
|
||||
run_fuzzy_filter/2,
|
||||
format_channel_info/1,
|
||||
format_channel_info/2
|
||||
]).
|
||||
|
@ -327,12 +328,7 @@ ms(lifetime, X) ->
|
|||
fuzzy_filter_fun([]) ->
|
||||
undefined;
|
||||
fuzzy_filter_fun(Fuzzy) ->
|
||||
fun(MsRaws) when is_list(MsRaws) ->
|
||||
lists:filter(
|
||||
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||
MsRaws
|
||||
)
|
||||
end.
|
||||
{fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
|
||||
|
||||
run_fuzzy_filter(_, []) ->
|
||||
true;
|
||||
|
|
|
@ -128,7 +128,9 @@ limit(Params) ->
|
|||
].
|
||||
|
||||
-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() ::
|
||||
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}) ->
|
||||
{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,
|
||||
limit => Limit,
|
||||
|
@ -323,9 +334,14 @@ do_select(
|
|||
{[], QueryState#{continuation => ?FRESH_SELECT}};
|
||||
{Rows, NContinuation} ->
|
||||
NRows =
|
||||
case is_function(FuzzyFun) of
|
||||
true -> FuzzyFun(Rows);
|
||||
false -> Rows
|
||||
case FuzzyFun of
|
||||
undefined ->
|
||||
Rows;
|
||||
{FilterFun, Args0} when is_function(FilterFun), is_list(Args0) ->
|
||||
lists:filter(
|
||||
fun(E) -> erlang:apply(FilterFun, [E | Args0]) end,
|
||||
Rows
|
||||
)
|
||||
end,
|
||||
{NRows, QueryState#{continuation => NContinuation}}
|
||||
end.
|
||||
|
@ -361,7 +377,7 @@ counting_total_fun(_QueryState = #{mactch_spec := Ms, fuzzy_fun := undefined}) -
|
|||
fun(Tab) ->
|
||||
ets:select_count(Tab, CountingMs)
|
||||
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
|
||||
%% so it is not supported now
|
||||
false.
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
|
||||
-export([
|
||||
qs2ms/2,
|
||||
run_fuzzy_filter/2,
|
||||
format_channel_info/1,
|
||||
format_channel_info/2
|
||||
]).
|
||||
|
@ -841,12 +842,7 @@ ms(created_at, X) ->
|
|||
fuzzy_filter_fun([]) ->
|
||||
undefined;
|
||||
fuzzy_filter_fun(Fuzzy) ->
|
||||
fun(MsRaws) when is_list(MsRaws) ->
|
||||
lists:filter(
|
||||
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||
MsRaws
|
||||
)
|
||||
end.
|
||||
{fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
|
||||
|
||||
run_fuzzy_filter(_, []) ->
|
||||
true;
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
|
||||
-export([
|
||||
qs2ms/2,
|
||||
run_fuzzy_filter/2,
|
||||
format/2
|
||||
]).
|
||||
|
||||
|
@ -214,12 +215,7 @@ update_ms(qos, X, {{Pid, Topic}, Opts}) ->
|
|||
fuzzy_filter_fun([]) ->
|
||||
undefined;
|
||||
fuzzy_filter_fun(Fuzzy) ->
|
||||
fun(MsRaws) when is_list(MsRaws) ->
|
||||
lists:filter(
|
||||
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||
MsRaws
|
||||
)
|
||||
end.
|
||||
{fun ?MODULE:run_fuzzy_filter/2, [Fuzzy]}.
|
||||
|
||||
run_fuzzy_filter(_, []) ->
|
||||
true;
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
-export(['/rule_events'/2, '/rule_test'/2, '/rules'/2, '/rules/:id'/2, '/rules/:id/reset_metrics'/2]).
|
||||
|
||||
%% 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_BADARGS(REASON), begin
|
||||
|
@ -582,12 +582,7 @@ ms(enable, X) ->
|
|||
fuzzy_match_fun([]) ->
|
||||
undefined;
|
||||
fuzzy_match_fun(Fuzzy) ->
|
||||
fun(MsRaws) when is_list(MsRaws) ->
|
||||
lists:filter(
|
||||
fun(E) -> run_fuzzy_match(E, Fuzzy) end,
|
||||
MsRaws
|
||||
)
|
||||
end.
|
||||
{fun ?MODULE:run_fuzzy_match/2, [Fuzzy]}.
|
||||
|
||||
run_fuzzy_match(_, []) ->
|
||||
true;
|
||||
|
|
Loading…
Reference in New Issue