25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

PubSub: Optimize publishing on large nodes (SQL)

Avoid an unnecessary SQL query while publishing an item on a PubSub node
without 'max_items' limit.  The query in question can be expensive if
the node has a large number of items.

Thanks to Ammonit Measurement GmbH for sponsoring this work.
This commit is contained in:
Holger Weiss 2021-08-21 20:02:58 +02:00
parent 1b0e59bb13
commit 29751a6174

View File

@ -259,14 +259,14 @@ publish_item(Nidx, Publisher, PublishModel, MaxItems, ItemId, Payload,
{result, _} -> {result, _} ->
{error, xmpp:err_forbidden()}; {error, xmpp:err_forbidden()};
_ -> _ ->
Items = [ItemId | itemids(Nidx, GenKey)], OldIds = maybe_remove_extra_items(Nidx, MaxItems,
{result, {_NI, OI}} = remove_extra_items(Nidx, MaxItems, Items), GenKey, ItemId),
set_item(#pubsub_item{ set_item(#pubsub_item{
itemid = {ItemId, Nidx}, itemid = {ItemId, Nidx},
creation = {Now, GenKey}, creation = {Now, GenKey},
modification = {Now, SubKey}, modification = {Now, SubKey},
payload = Payload}), payload = Payload}),
{result, {default, broadcast, OI}} {result, {default, broadcast, OldIds}}
end; end;
true -> true ->
{result, {default, broadcast, []}} {result, {default, broadcast, []}}
@ -934,6 +934,16 @@ update_subscription(Nidx, JID, Subscription) ->
"-affiliation='n'" "-affiliation='n'"
]). ]).
-spec maybe_remove_extra_items(mod_pubsub:nodeIdx(),
non_neg_integer() | unlimited, ljid(),
mod_pubsub:itemId()) -> [mod_pubsub:itemId()].
maybe_remove_extra_items(_Nidx, unlimited, _GenKey, _ItemId) ->
[];
maybe_remove_extra_items(Nidx, MaxItems, GenKey, ItemId) ->
ItemIds = [ItemId | itemids(Nidx, GenKey)],
{result, {_NewIds, OldIds}} = remove_extra_items(Nidx, MaxItems, ItemIds),
OldIds.
-spec decode_jid(SJID :: binary()) -> ljid(). -spec decode_jid(SJID :: binary()) -> ljid().
decode_jid(SJID) -> decode_jid(SJID) ->
jid:tolower(jid:decode(SJID)). jid:tolower(jid:decode(SJID)).