From 8e64992f47103f51785a95aee65f3907aff92064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Tue, 2 May 2023 11:43:18 +0200 Subject: [PATCH] Use more efficient way to calculate changes in set_privacy_list --- src/mod_privacy_sql.erl | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/mod_privacy_sql.erl b/src/mod_privacy_sql.erl index fc5ae6e0f..33d70a71c 100644 --- a/src/mod_privacy_sql.erl +++ b/src/mod_privacy_sql.erl @@ -422,6 +422,12 @@ set_privacy_list_new(ID, RItems) -> end, RItems). +calculate_difference(List1, List2) -> + Set1 = gb_sets:from_list(List1), + Set2 = gb_sets:from_list(List2), + {gb_sets:subtract(Set1, Set2), + gb_sets:subtract(Set2, Set1)}. + set_privacy_list(ID, RItems) -> case ejabberd_sql:sql_query_t( ?SQL("select @(t)s, @(value)s, @(action)s, @(ord)d, @(match_all)b, " @@ -429,25 +435,15 @@ set_privacy_list(ID, RItems) -> "@(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, + {ToAdd2, ToDelete2} = calculate_difference(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}) ->