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

For pubsub items with max_item = 1 use order by in sql queries

This commit is contained in:
Paweł Chmielowski 2019-07-31 12:20:38 +02:00
parent 48bed43902
commit 51bf552c3f
6 changed files with 59 additions and 5 deletions

View File

@ -186,6 +186,9 @@
-callback get_last_items(nodeIdx(), jid(), undefined | rsm_set()) ->
{result, [pubsubItem()]}.
-callback get_only_item(nodeIdx(), jid()) ->
{result, [pubsubItem()]}.
-callback get_item(NodeIdx :: nodeIdx(),
ItemId :: itemId(),
JID :: jid(),

View File

@ -2151,6 +2151,21 @@ get_last_items(Host, Type, Nidx, LJID, Count) when Count > 1 ->
get_last_items(_Host, _Type, _Nidx, _LJID, _Count) ->
[].
-spec get_only_item(host(), binary(), nodeIdx(), ljid()) -> [#pubsub_item{}].
get_only_item(Host, Type, Nidx, LJID) ->
case get_cached_item(Host, Nidx) of
undefined ->
case node_action(Host, Type, get_only_item, [Nidx, LJID]) of
{result, Items} when length(Items) < 2 ->
Items;
{result, Items} ->
[hd(lists:keysort(#pubsub_item.modification, Items))];
_ -> []
end;
LastItem ->
[LastItem]
end.
%% @doc <p>Return the list of affiliations as an XMPP response.</p>
-spec get_affiliations(host(), binary(), jid(), [binary()]) ->
{result, pubsub()} | {error, stanza_error()}.
@ -3031,7 +3046,13 @@ c2s_handle_info(C2SState, _) ->
send_items(Host, Node, Nidx, Type, Options, LJID, Number) ->
send_items(Host, Node, Nidx, Type, Options, Host, LJID, LJID, Number).
send_items(Host, Node, Nidx, Type, Options, Publisher, SubLJID, ToLJID, Number) ->
case get_last_items(Host, Type, Nidx, SubLJID, Number) of
Items = case max_items(Host, Options) of
1 ->
get_only_item(Host, Type, Nidx, SubLJID);
_ ->
get_last_items(Host, Type, Nidx, SubLJID, Number)
end,
case Items of
[] ->
ok;
Items ->

View File

@ -46,7 +46,7 @@
get_subscriptions/2, set_subscriptions/4,
get_pending_nodes/2, get_states/1, get_state/2,
set_state/1, get_items/7, get_items/3, get_item/7,
get_last_items/3,
get_last_items/3, get_only_item/2,
get_item/2, set_item/1, get_item_name/3, node_to_path/1,
path_to_node/1, can_fetch_item/2, is_subscribed/1, transform/1]).
@ -812,6 +812,9 @@ get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM
get_items(Nidx, JID, RSM)
end.
get_only_item(Nidx, From) ->
get_last_items(Nidx, From, 1).
get_last_items(Nidx, _From, Count) when Count > 0 ->
Items = mnesia:index_read(pubsub_item, Nidx, #pubsub_item.nodeidx),
LastItems = lists:reverse(lists:keysort(#pubsub_item.modification, Items)),

View File

@ -51,7 +51,8 @@
set_state/1, get_items/7, get_items/3, get_item/7,
get_item/2, set_item/1, get_item_name/3, node_to_path/1,
path_to_node/1,
get_entity_subscriptions_for_send_last/2, get_last_items/3]).
get_entity_subscriptions_for_send_last/2, get_last_items/3,
get_only_item/2]).
-export([decode_jid/1, encode_jid/1, encode_jid_like/1,
decode_affiliation/1, decode_subscriptions/1,
@ -741,6 +742,25 @@ get_last_items(Nidx, _From, Limit) ->
{result, []}
end.
get_only_item(Nidx, _From) ->
SNidx = misc:i2l(Nidx),
Query = fun(mssql, _) ->
ejabberd_sql:sql_query_t(
[<<"select itemid, publisher, creation, modification, payload",
" from pubsub_item where nodeid='", SNidx/binary, "'">>]);
(_, _) ->
ejabberd_sql:sql_query_t(
[<<"select itemid, publisher, creation, modification, payload",
" from pubsub_item where nodeid='", SNidx/binary, "'">>])
end,
case catch ejabberd_sql:sql_query_t(Query) of
{selected, [<<"itemid">>, <<"publisher">>, <<"creation">>,
<<"modification">>, <<"payload">>], RItems} ->
{result, [raw_to_item(Nidx, RItem) || RItem <- RItems]};
_ ->
{result, []}
end.
get_item(Nidx, ItemId) ->
case catch ejabberd_sql:sql_query_t(
?SQL("select @(itemid)s, @(publisher)s, @(creation)s,"

View File

@ -42,7 +42,7 @@
get_subscriptions/2, set_subscriptions/4,
get_pending_nodes/2, get_states/1, get_state/2,
set_state/1, get_items/7, get_items/3, get_item/7,
get_last_items/3,
get_last_items/3, get_only_item/2,
get_item/2, set_item/1, get_item_name/3, node_to_path/1,
path_to_node/1, depends/3]).
@ -231,6 +231,9 @@ get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, SubId, RSM)
get_last_items(Nidx, From, Count) ->
node_flat:get_last_items(Nidx, From, Count).
get_only_item(Nidx, From) ->
node_flat:get_only_item(Nidx, From).
get_item(Nidx, ItemId) ->
node_flat:get_item(Nidx, ItemId).

View File

@ -46,7 +46,8 @@
set_state/1, get_items/7, get_items/3, get_item/7,
get_item/2, set_item/1, get_item_name/3, node_to_path/1,
path_to_node/1, depends/3,
get_entity_subscriptions_for_send_last/2, get_last_items/3]).
get_entity_subscriptions_for_send_last/2, get_last_items/3,
get_only_item/2]).
depends(_Host, _ServerHost, _Opts) ->
[{mod_caps, hard}].
@ -218,6 +219,9 @@ get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, SubId, RSM)
get_last_items(Nidx, JID, Count) ->
node_flat_sql:get_last_items(Nidx, JID, Count).
get_only_item(Nidx, JID) ->
node_flat_sql:get_only_item(Nidx, JID).
get_item(Nidx, ItemId) ->
node_flat_sql:get_item(Nidx, ItemId).