25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Optimize mod_privacy_sql:set_list

Previously we always did delete everything and set all entries back, now
we check if we need to delete anything and if not insert only missing data.
This commit is contained in:
Paweł Chmielowski 2023-04-20 10:55:21 +02:00
parent bb28265261
commit 5b8ebed81b

View File

@ -107,16 +107,21 @@ set_lists(#privacy{us = {LUser, LServer},
set_list(LUser, LServer, Name, List) -> set_list(LUser, LServer, Name, List) ->
RItems = lists:map(fun item_to_raw/1, List), RItems = lists:map(fun item_to_raw/1, List),
F = fun () -> F = fun() ->
ID = case get_privacy_list_id_t(LUser, LServer, Name) of {ID, New} = case get_privacy_list_id_t(LUser, LServer, Name) of
{selected, []} -> {selected, []} ->
add_privacy_list(LUser, LServer, Name), add_privacy_list(LUser, LServer, Name),
{selected, [{I}]} = {selected, [{I}]} =
get_privacy_list_id_t(LUser, LServer, Name), get_privacy_list_id_t(LUser, LServer, Name),
I; {I, true};
{selected, [{I}]} -> I {selected, [{I}]} -> {I, false}
end, end,
set_privacy_list(ID, RItems) case New of
false ->
set_privacy_list(ID, RItems);
_ ->
set_privacy_list_new(ID, RItems)
end
end, end,
transaction(LServer, F). transaction(LServer, F).
@ -402,22 +407,63 @@ add_privacy_list(LUser, LServer, Name) ->
"server_host=%(LServer)s", "server_host=%(LServer)s",
"name=%(Name)s"])). "name=%(Name)s"])).
set_privacy_list(ID, RItems) -> set_privacy_list_new(ID, RItems) ->
ejabberd_sql:sql_query_t(
?SQL("delete from privacy_list_data where id=%(ID)d")),
lists:foreach( lists:foreach(
fun({SType, SValue, SAction, Order, MatchAll, MatchIQ, fun({SType, SValue, SAction, Order, MatchAll, MatchIQ,
MatchMessage, MatchPresenceIn, MatchPresenceOut}) -> MatchMessage, MatchPresenceIn, MatchPresenceOut}) ->
ejabberd_sql:sql_query_t( ejabberd_sql:sql_query_t(
?SQL("insert into privacy_list_data(id, t, " ?SQL("insert into privacy_list_data(id, t, "
"value, action, ord, match_all, match_iq, " "value, action, ord, match_all, match_iq, "
"match_message, match_presence_in, match_presence_out) " "match_message, match_presence_in, match_presence_out) "
"values (%(ID)d, %(SType)s, %(SValue)s, %(SAction)s," "values (%(ID)d, %(SType)s, %(SValue)s, %(SAction)s,"
" %(Order)d, %(MatchAll)b, %(MatchIQ)b," " %(Order)d, %(MatchAll)b, %(MatchIQ)b,"
" %(MatchMessage)b, %(MatchPresenceIn)b," " %(MatchMessage)b, %(MatchPresenceIn)b,"
" %(MatchPresenceOut)b)")) " %(MatchPresenceOut)b)"))
end, end,
RItems). RItems).
set_privacy_list(ID, RItems) ->
case ejabberd_sql:sql_query_t(
?SQL("select @(t)s, @(value)s, @(action)s, @(ord)d, @(match_all)b, "
"@(match_iq)b, @(match_message)b, @(match_presence_in)b, "
"@(match_presence_out)b from privacy_list_data "
"where id=%(ID)d")) of
{selected, ExistingItems} ->
{ToAdd2, ToDelete2} =
lists:foldr(
fun(Value, {ToAdd, ToDelete}) ->
case lists:splitwith(fun(E) -> E /= Value end, ToAdd) of
{_S, []} ->
{ToAdd, [Value | ToDelete]};
{Pfx, [_ | Rest]} ->
{Pfx ++ Rest, ToDelete}
end
end, {RItems, []}, ExistingItems),
ToAdd3 =
if
ToDelete2 /= [] ->
ejabberd_sql:sql_query_t(
?SQL("delete from privacy_list_data where id=%(ID)d")),
RItems;
true ->
ToAdd2
end,
lists:foreach(
fun({SType, SValue, SAction, Order, MatchAll, MatchIQ,
MatchMessage, MatchPresenceIn, MatchPresenceOut}) ->
ejabberd_sql:sql_query_t(
?SQL("insert into privacy_list_data(id, t, "
"value, action, ord, match_all, match_iq, "
"match_message, match_presence_in, match_presence_out) "
"values (%(ID)d, %(SType)s, %(SValue)s, %(SAction)s,"
" %(Order)d, %(MatchAll)b, %(MatchIQ)b,"
" %(MatchMessage)b, %(MatchPresenceIn)b,"
" %(MatchPresenceOut)b)"))
end,
ToAdd3);
Err ->
Err
end.
del_privacy_lists(LUser, LServer) -> del_privacy_lists(LUser, LServer) ->
case ejabberd_sql:sql_query( case ejabberd_sql:sql_query(