aboutsummaryrefslogtreecommitdiff
path: root/src/node_flat.erl
diff options
context:
space:
mode:
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>2018-09-02 00:37:07 +0300
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>2018-09-02 00:37:07 +0300
commit5c931d7004202f285cb0d5bea286adb8fc60e8b1 (patch)
tree0ba8514f9e531a078828584475fa84e93ee3a5ba /src/node_flat.erl
parentGet stacktrace out of lager context (diff)
Correctly handle empty result with RSM
Fixes #2588
Diffstat (limited to 'src/node_flat.erl')
-rw-r--r--src/node_flat.erl72
1 files changed, 39 insertions, 33 deletions
diff --git a/src/node_flat.erl b/src/node_flat.erl
index 2538a09a5..11e0785c0 100644
--- a/src/node_flat.erl
+++ b/src/node_flat.erl
@@ -738,39 +738,43 @@ get_items(Nidx, _From, undefined) ->
get_items(Nidx, _From, #rsm_set{max = Max, index = IncIndex,
'after' = After, before = Before}) ->
- RItems = lists:keysort(#pubsub_item.creation,
- mnesia:index_read(pubsub_item, Nidx, #pubsub_item.nodeidx)),
- Count = length(RItems),
- Limit = case Max of
- undefined -> ?MAXITEMS;
- _ -> Max
- end,
- {Offset, ItemsPage} =
- case {IncIndex, Before, After} of
- {I, undefined, undefined} ->
- SubList = lists:nthtail(I, RItems),
- {I, lists:sublist(SubList, Limit)};
- {_, <<>>, undefined} ->
- %% 2.5 Requesting the Last Page in a Result Set
- SubList = lists:reverse(RItems),
- {0, lists:sublist(SubList, Limit)};
- {_, Stamp, undefined} ->
- BeforeNow = encode_stamp(Stamp),
- SubList = lists:dropwhile(
- fun(#pubsub_item{creation = {Now, _}}) ->
- Now >= BeforeNow
- end, lists:reverse(RItems)),
- {0, lists:sublist(SubList, Limit)};
- {_, undefined, Stamp} ->
- AfterNow = encode_stamp(Stamp),
- SubList = lists:dropwhile(
- fun(#pubsub_item{creation = {Now, _}}) ->
- Now =< AfterNow
- end, RItems),
- {0, lists:sublist(SubList, Limit)}
- end,
- Rsm = rsm_page(Count, IncIndex, Offset, ItemsPage),
- {result, {ItemsPage, Rsm}}.
+ case lists:keysort(#pubsub_item.creation,
+ mnesia:index_read(pubsub_item, Nidx, #pubsub_item.nodeidx)) of
+ [] ->
+ {result, {[], #rsm_set{count = 0}}};
+ RItems ->
+ Count = length(RItems),
+ Limit = case Max of
+ undefined -> ?MAXITEMS;
+ _ -> Max
+ end,
+ {Offset, ItemsPage} =
+ case {IncIndex, Before, After} of
+ {I, undefined, undefined} ->
+ SubList = lists:nthtail(I, RItems),
+ {I, lists:sublist(SubList, Limit)};
+ {_, <<>>, undefined} ->
+ %% 2.5 Requesting the Last Page in a Result Set
+ SubList = lists:reverse(RItems),
+ {0, lists:sublist(SubList, Limit)};
+ {_, Stamp, undefined} ->
+ BeforeNow = encode_stamp(Stamp),
+ SubList = lists:dropwhile(
+ fun(#pubsub_item{creation = {Now, _}}) ->
+ Now >= BeforeNow
+ end, lists:reverse(RItems)),
+ {0, lists:sublist(SubList, Limit)};
+ {_, undefined, Stamp} ->
+ AfterNow = encode_stamp(Stamp),
+ SubList = lists:dropwhile(
+ fun(#pubsub_item{creation = {Now, _}}) ->
+ Now =< AfterNow
+ end, RItems),
+ {0, lists:sublist(SubList, Limit)}
+ end,
+ Rsm = rsm_page(Count, IncIndex, Offset, ItemsPage),
+ {result, {ItemsPage, Rsm}}
+ end.
get_items(Nidx, JID, AccessModel, PresenceSubscription, RosterGroup, _SubId, RSM) ->
SubKey = jid:tolower(JID),
@@ -920,6 +924,8 @@ first_in_list(Pred, [H | T]) ->
_ -> first_in_list(Pred, T)
end.
+rsm_page(Count, _, _, []) ->
+ #rsm_set{count = Count};
rsm_page(Count, Index, Offset, Items) ->
FirstItem = hd(Items),
LastItem = lists:last(Items),