1
0
Fork 0
mirror of https://github.com/processone/ejabberd synced 2025-10-04 18:29:20 +02:00

Optimize room_unused_* commands

Previously to check if hibernated room was old enough we had to fetch info
about all rooms from database. Now we repurpose created_at field in sql
to store that info, that allow us to have more efficient query just for it.
This commit is contained in:
Paweł Chmielowski 2022-02-18 14:01:22 +01:00
parent f20e9e9b66
commit f86055378d
11 changed files with 99 additions and 42 deletions

View file

@ -38,6 +38,7 @@
register_online_user/4, unregister_online_user/4,
count_online_rooms_by_user/3, get_online_rooms_by_user/3,
get_subscribed_rooms/3, get_rooms_without_subscribers/2,
get_hibernated_rooms_older_than/3,
find_online_room_by_pid/2, remove_user/2]).
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]).
@ -64,13 +65,19 @@ store_room(LServer, Host, Name, Opts, ChangesHints) ->
_ -> {[], Opts}
end,
SOpts = misc:term_to_expr(Opts2),
Timestamp = case lists:keyfind(hibernation_time, 1, Opts) of
false -> <<"1900-01-01 00:00:00">>;
{_, undefined} -> <<"1900-01-01 00:00:00">>;
{_, Time} -> usec_to_sql_timestamp(Time)
end,
F = fun () ->
?SQL_UPSERT_T(
"muc_room",
["!name=%(Name)s",
"!host=%(Host)s",
"server_host=%(LServer)s",
"opts=%(SOpts)s"]),
"opts=%(SOpts)s",
"created_at=%(Timestamp)s"]),
case ChangesHints of
Changes when is_list(Changes) ->
[change_room(Host, Name, Change) || Change <- Changes];
@ -179,6 +186,23 @@ get_rooms_without_subscribers(LServer, Host) ->
[]
end.
get_hibernated_rooms_older_than(LServer, Host, Timestamp) ->
TimestampS = usec_to_sql_timestamp(Timestamp),
case catch ejabberd_sql:sql_query(
LServer,
?SQL("select @(name)s, @(opts)s from muc_room"
" where host=%(Host)s and created_at < %(TimestampS)s and created_at > '1900-01-01 00:00:00'")) of
{selected, RoomOpts} ->
lists:map(
fun({Room, Opts}) ->
OptsD = ejabberd_sql:decode_term(Opts),
#muc_room{name_host = {Room, Host},
opts = mod_muc:opts_to_binary(OptsD)}
end, RoomOpts);
_Err ->
[]
end.
get_rooms(LServer, Host) ->
case catch ejabberd_sql:sql_query(
LServer,
@ -497,3 +521,11 @@ clean_tables(ServerHost) ->
?ERROR_MSG("Failed to clean 'muc_online_users' table: ~p", [Err2]),
Err2
end.
usec_to_sql_timestamp(Timestamp) ->
case calendar:system_time_to_universal_time(Timestamp, microsecond) of
{{Year, Month, Day}, {Hour, Minute, Second}} ->
list_to_binary(io_lib:format("~4..0B-~2..0B-~2..0B "
"~2..0B:~2..0B:~2..0B",
[Year, Month, Day, Hour, Minute, Second]))
end.