]> granicus.if.org Git - ejabberd/commitdiff
For pubsub items with max_item = 1 use order by in sql queries
authorPaweł Chmielowski <pchmielowski@process-one.net>
Wed, 31 Jul 2019 10:20:38 +0000 (12:20 +0200)
committerPaweł Chmielowski <pchmielowski@process-one.net>
Wed, 31 Jul 2019 11:06:44 +0000 (13:06 +0200)
src/gen_pubsub_node.erl
src/mod_pubsub.erl
src/node_flat.erl
src/node_flat_sql.erl
src/node_pep.erl
src/node_pep_sql.erl

index e4fe0e94864c6ab6b0d3a54ce8545e9c8eed6bc4..a483bd6eeb41ab2e5ab8a06c36e80d5021e54f55 100644 (file)
 -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(),
index b2e2fad60b0ae72ad5be34b79d1a7977870ba21a..f1f1dbd514964429e4a447e986d2c08104ab192a 100644 (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 ->
index 1bc2c5e6f368f84177efb1aeb5a8e384f55fef12..f0deeb7e264df54a7f48014cf9677574deae04b7 100644 (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)),
index 59e3c89b42cd333d1d170f15bbf77eb69131e38f..15d9fd770fca351ebb9c29bc928a1142f0ff3e41 100644 (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,"
index 3baafc10db29afbb20acb10a7e146e49da817618..334681578b7379e6a2d7b8a92d2b4c54c627a989 100644 (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).
 
index ac3ab2196963bfac5331f4a9887024bcb7852703..6c1e642b270834641afdd92c093bc91c539db9c4 100644 (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).