refactor(mgmt_api): `select_table_with_count`.
This commit is contained in:
parent
ea7aa5bc41
commit
3960204ce3
|
@ -196,14 +196,14 @@ extra_sub_props(Props) ->
|
||||||
|
|
||||||
query(Tab, {Qs, []}, Continuation, Limit) ->
|
query(Tab, {Qs, []}, Continuation, Limit) ->
|
||||||
Ms = qs2ms(Qs),
|
Ms = qs2ms(Qs),
|
||||||
emqx_mgmt_api:select_table(Tab, Ms, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(Tab, Ms, Continuation, Limit,
|
||||||
fun format_channel_info/1);
|
fun format_channel_info/1);
|
||||||
|
|
||||||
query(Tab, {Qs, Fuzzy}, Continuation, Limit) ->
|
query(Tab, {Qs, Fuzzy}, Continuation, Limit) ->
|
||||||
Ms = qs2ms(Qs),
|
Ms = qs2ms(Qs),
|
||||||
FuzzyFilterFun = fuzzy_filter_fun(Fuzzy),
|
FuzzyFilterFun = fuzzy_filter_fun(Fuzzy),
|
||||||
emqx_mgmt_api:select_table(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit,
|
||||||
fun format_channel_info/1).
|
fun format_channel_info/1).
|
||||||
|
|
||||||
qs2ms(Qs) ->
|
qs2ms(Qs) ->
|
||||||
{MtchHead, Conds} = qs2ms(Qs, 2, {#{}, []}),
|
{MtchHead, Conds} = qs2ms(Qs, 2, {#{}, []}),
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
%% first_next query APIs
|
%% first_next query APIs
|
||||||
-export([ node_query/5
|
-export([ node_query/5
|
||||||
, cluster_query/4
|
, cluster_query/4
|
||||||
, select_table/5
|
, select_table_with_count/5
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([do_query/6]).
|
-export([do_query/6]).
|
||||||
|
@ -196,36 +196,38 @@ rpc_call(Node, M, F, A, T) ->
|
||||||
%% Table Select
|
%% Table Select
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
select_table(Tab, {Ms, FuzzyFilterFun}, ?FRESH_SELECT, Limit, FmtFun)
|
select_table_with_count(Tab, {Ms, FuzzyFilterFun}, ?FRESH_SELECT, Limit, FmtFun)
|
||||||
when is_function(FuzzyFilterFun) andalso Limit > 0 ->
|
when is_function(FuzzyFilterFun) andalso Limit > 0 ->
|
||||||
case ets:select(Tab, Ms, Limit) of
|
case ets:select(Tab, Ms, Limit) of
|
||||||
'$end_of_table' ->
|
'$end_of_table' ->
|
||||||
{[], ?FRESH_SELECT};
|
{0, [], ?FRESH_SELECT};
|
||||||
{RawResult, NContinuation} ->
|
{RawResult, NContinuation} ->
|
||||||
{lists:map(FmtFun, lists:reverse(FuzzyFilterFun(RawResult))), NContinuation}
|
Rows = FuzzyFilterFun(RawResult),
|
||||||
|
{length(Rows), lists:map(FmtFun, Rows), NContinuation}
|
||||||
end;
|
end;
|
||||||
select_table(_Tab, {_Ms, FuzzyFilterFun}, Continuation, _Limit, FmtFun)
|
select_table_with_count(_Tab, {_Ms, FuzzyFilterFun}, Continuation, _Limit, FmtFun)
|
||||||
when is_function(FuzzyFilterFun) ->
|
when is_function(FuzzyFilterFun) ->
|
||||||
case ets:select(Continuation) of
|
case ets:select(Continuation) of
|
||||||
'$end_of_table' ->
|
'$end_of_table' ->
|
||||||
{[], ?FRESH_SELECT};
|
{0, [], ?FRESH_SELECT};
|
||||||
{RawResult, NContinuation} ->
|
{RawResult, NContinuation} ->
|
||||||
{lists:map(FmtFun, lists:reverse(FuzzyFilterFun(RawResult))), NContinuation}
|
Rows = FuzzyFilterFun(RawResult),
|
||||||
|
{length(Rows), lists:map(FmtFun, Rows), NContinuation}
|
||||||
end;
|
end;
|
||||||
select_table(Tab, Ms, ?FRESH_SELECT, Limit, FmtFun)
|
select_table_with_count(Tab, Ms, ?FRESH_SELECT, Limit, FmtFun)
|
||||||
when Limit > 0 ->
|
when Limit > 0 ->
|
||||||
case ets:select(Tab, Ms, Limit) of
|
case ets:select(Tab, Ms, Limit) of
|
||||||
'$end_of_table' ->
|
'$end_of_table' ->
|
||||||
{[], ?FRESH_SELECT};
|
{0, [], ?FRESH_SELECT};
|
||||||
{RawResult, NContinuation} ->
|
{RawResult, NContinuation} ->
|
||||||
{lists:map(FmtFun, lists:reverse(RawResult)), NContinuation}
|
{length(RawResult), lists:map(FmtFun, RawResult), NContinuation}
|
||||||
end;
|
end;
|
||||||
select_table(_Tab, _Ms, Continuation, _Limit, FmtFun) ->
|
select_table_with_count(_Tab, _Ms, Continuation, _Limit, FmtFun) ->
|
||||||
case ets:select(Continuation) of
|
case ets:select(Continuation) of
|
||||||
'$end_of_table' ->
|
'$end_of_table' ->
|
||||||
{[], ?FRESH_SELECT};
|
{0, [], ?FRESH_SELECT};
|
||||||
{RawResult, NContinuation} ->
|
{RawResult, NContinuation} ->
|
||||||
{lists:map(FmtFun, lists:reverse(RawResult)), NContinuation}
|
{length(RawResult), lists:map(FmtFun, RawResult), NContinuation}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
|
@ -90,7 +90,7 @@ alarms(delete, _Params) ->
|
||||||
|
|
||||||
query(Table, _QsSpec, Continuation, Limit) ->
|
query(Table, _QsSpec, Continuation, Limit) ->
|
||||||
Ms = [{'$1',[],['$1']}],
|
Ms = [{'$1',[],['$1']}],
|
||||||
emqx_mgmt_api:select_table(Table, Ms, Continuation, Limit, fun format_alarm/1).
|
emqx_mgmt_api:select_table_with_count(Table, Ms, Continuation, Limit, fun format_alarm/1).
|
||||||
|
|
||||||
format_alarm(Alarms) when is_list(Alarms) ->
|
format_alarm(Alarms) when is_list(Alarms) ->
|
||||||
[emqx_alarm:format(Alarm) || Alarm <- Alarms];
|
[emqx_alarm:format(Alarm) || Alarm <- Alarms];
|
||||||
|
|
|
@ -557,14 +557,14 @@ generate_qs(Qs) ->
|
||||||
|
|
||||||
query(Tab, {Qs, []}, Continuation, Limit) ->
|
query(Tab, {Qs, []}, Continuation, Limit) ->
|
||||||
Ms = qs2ms(Qs),
|
Ms = qs2ms(Qs),
|
||||||
emqx_mgmt_api:select_table(Tab, Ms, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(Tab, Ms, Continuation, Limit,
|
||||||
fun format_channel_info/1);
|
fun format_channel_info/1);
|
||||||
|
|
||||||
query(Tab, {Qs, Fuzzy}, Continuation, Limit) ->
|
query(Tab, {Qs, Fuzzy}, Continuation, Limit) ->
|
||||||
Ms = qs2ms(Qs),
|
Ms = qs2ms(Qs),
|
||||||
FuzzyFilterFun = fuzzy_filter_fun(Fuzzy),
|
FuzzyFilterFun = fuzzy_filter_fun(Fuzzy),
|
||||||
emqx_mgmt_api:select_table(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit,
|
||||||
fun format_channel_info/1).
|
fun format_channel_info/1).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% QueryString to Match Spec
|
%% QueryString to Match Spec
|
||||||
|
|
|
@ -107,7 +107,7 @@ generate_topic(Params) -> Params.
|
||||||
|
|
||||||
query(Tab, {Qs, _}, Continuation, Limit) ->
|
query(Tab, {Qs, _}, Continuation, Limit) ->
|
||||||
Ms = qs2ms(Qs, [{{route, '_', '_'}, [], ['$_']}]),
|
Ms = qs2ms(Qs, [{{route, '_', '_'}, [], ['$_']}]),
|
||||||
emqx_mgmt_api:select_table(Tab, Ms, Continuation, Limit, fun format/1).
|
emqx_mgmt_api:select_table_with_count(Tab, Ms, Continuation, Limit, fun format/1).
|
||||||
|
|
||||||
qs2ms([], Res) -> Res;
|
qs2ms([], Res) -> Res;
|
||||||
qs2ms([{topic,'=:=', T} | Qs], [{{route, _, N}, [], ['$_']}]) ->
|
qs2ms([{topic,'=:=', T} | Qs], [{{route, _, N}, [], ['$_']}]) ->
|
||||||
|
|
|
@ -150,12 +150,12 @@ format({_Subscriber, Topic, Options}) ->
|
||||||
|
|
||||||
query(Tab, {Qs, []}, Continuation, Limit) ->
|
query(Tab, {Qs, []}, Continuation, Limit) ->
|
||||||
Ms = qs2ms(Qs),
|
Ms = qs2ms(Qs),
|
||||||
emqx_mgmt_api:select_table(Tab, Ms, Continuation, Limit, fun format/1);
|
emqx_mgmt_api:select_table_with_count(Tab, Ms, Continuation, Limit, fun format/1);
|
||||||
|
|
||||||
query(Tab, {Qs, Fuzzy}, Continuation, Limit) ->
|
query(Tab, {Qs, Fuzzy}, Continuation, Limit) ->
|
||||||
Ms = qs2ms(Qs),
|
Ms = qs2ms(Qs),
|
||||||
FuzzyFilterFun = fuzzy_filter_fun(Fuzzy),
|
FuzzyFilterFun = fuzzy_filter_fun(Fuzzy),
|
||||||
emqx_mgmt_api:select_table(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit, fun format/1).
|
emqx_mgmt_api:select_table_with_count(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit, fun format/1).
|
||||||
|
|
||||||
fuzzy_filter_fun(Fuzzy) ->
|
fuzzy_filter_fun(Fuzzy) ->
|
||||||
fun(MsRaws) when is_list(MsRaws) ->
|
fun(MsRaws) when is_list(MsRaws) ->
|
||||||
|
|
Loading…
Reference in New Issue