emqx_pqueue.erl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. %% The contents of this file are subject to the Mozilla Public License
  2. %% Version 1.1 (the "License"); you may not use this file except in
  3. %% compliance with the License. You may obtain a copy of the License
  4. %% at http://www.mozilla.org/MPL/
  5. %%
  6. %% Software distributed under the License is distributed on an "AS IS"
  7. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  8. %% the License for the specific language governing rights and
  9. %% limitations under the License.
  10. %%
  11. %% The Original Code is RabbitMQ.
  12. %%
  13. %% The Initial Developer of the Original Code is GoPivotal, Inc.
  14. %% Copyright (c) 2007-2015 Pivotal Software, Inc. All rights reserved.
  15. %%
  16. %% Priority queues have essentially the same interface as ordinary
  17. %% queues, except that a) there is an in/3 that takes a priority, and
  18. %% b) we have only implemented the core API we need.
  19. %%
  20. %% Priorities should be integers - the higher the value the higher the
  21. %% priority - but we don't actually check that.
  22. %%
  23. %% in/2 inserts items with priority 0.
  24. %%
  25. %% We optimise the case where a priority queue is being used just like
  26. %% an ordinary queue. When that is the case we represent the priority
  27. %% queue as an ordinary queue. We could just call into the 'queue'
  28. %% module for that, but for efficiency we implement the relevant
  29. %% functions directly in here, thus saving on inter-module calls and
  30. %% eliminating a level of boxing.
  31. %%
  32. %% When the queue contains items with non-zero priorities, it is
  33. %% represented as a sorted kv list with the inverted Priority as the
  34. %% key and an ordinary queue as the value. Here again we use our own
  35. %% ordinary queue implemention for efficiency, often making recursive
  36. %% calls into the same function knowing that ordinary queues represent
  37. %% a base case.
  38. -module(emqx_pqueue).
  39. -export([ new/0
  40. , is_queue/1
  41. , is_empty/1
  42. , len/1
  43. , plen/2
  44. , to_list/1
  45. , from_list/1
  46. , in/2
  47. , in/3
  48. , out/1
  49. , out/2
  50. , out_p/1
  51. , join/2
  52. , filter/2
  53. , fold/3
  54. , highest/1
  55. , shift/1
  56. ]).
  57. -export_type([q/0]).
  58. %%----------------------------------------------------------------------------
  59. -type(priority() :: integer() | 'infinity').
  60. -type(squeue() :: {queue, [any()], [any()], non_neg_integer()}).
  61. -type(pqueue() :: squeue() | {pqueue, [{priority(), squeue()}]}).
  62. -type(q() :: pqueue()).
  63. %%----------------------------------------------------------------------------
  64. -spec(new() -> pqueue()).
  65. new() ->
  66. {queue, [], [], 0}.
  67. -spec(is_queue(any()) -> boolean()).
  68. is_queue({queue, R, F, L}) when is_list(R), is_list(F), is_integer(L) ->
  69. true;
  70. is_queue({pqueue, Queues}) when is_list(Queues) ->
  71. lists:all(fun ({infinity, Q}) -> is_queue(Q);
  72. ({P, Q}) -> is_integer(P) andalso is_queue(Q)
  73. end, Queues);
  74. is_queue(_) ->
  75. false.
  76. -spec(is_empty(pqueue()) -> boolean()).
  77. is_empty({queue, [], [], 0}) ->
  78. true;
  79. is_empty(_) ->
  80. false.
  81. -spec(len(pqueue()) -> non_neg_integer()).
  82. len({queue, _R, _F, L}) ->
  83. L;
  84. len({pqueue, Queues}) ->
  85. lists:sum([len(Q) || {_, Q} <- Queues]).
  86. -spec(plen(priority(), pqueue()) -> non_neg_integer()).
  87. plen(0, {queue, _R, _F, L}) ->
  88. L;
  89. plen(_, {queue, _R, _F, _}) ->
  90. 0;
  91. plen(P, {pqueue, Queues}) ->
  92. case lists:keysearch(maybe_negate_priority(P), 1, Queues) of
  93. {value, {_, Q}} -> len(Q);
  94. false -> 0
  95. end.
  96. -spec(to_list(pqueue()) -> [{priority(), any()}]).
  97. to_list({queue, In, Out, _Len}) when is_list(In), is_list(Out) ->
  98. [{0, V} || V <- Out ++ lists:reverse(In, [])];
  99. to_list({pqueue, Queues}) ->
  100. [{maybe_negate_priority(P), V} || {P, Q} <- Queues,
  101. {0, V} <- to_list(Q)].
  102. -spec(from_list([{priority(), any()}]) -> pqueue()).
  103. from_list(L) ->
  104. lists:foldl(fun ({P, E}, Q) -> in(E, P, Q) end, new(), L).
  105. -spec(in(any(), pqueue()) -> pqueue()).
  106. in(Item, Q) ->
  107. in(Item, 0, Q).
  108. -spec(in(any(), priority(), pqueue()) -> pqueue()).
  109. in(X, 0, {queue, [_] = In, [], 1}) ->
  110. {queue, [X], In, 2};
  111. in(X, 0, {queue, In, Out, Len}) when is_list(In), is_list(Out) ->
  112. {queue, [X|In], Out, Len + 1};
  113. in(X, Priority, _Q = {queue, [], [], 0}) ->
  114. in(X, Priority, {pqueue, []});
  115. in(X, Priority, Q = {queue, _, _, _}) ->
  116. in(X, Priority, {pqueue, [{0, Q}]});
  117. in(X, Priority, {pqueue, Queues}) ->
  118. P = maybe_negate_priority(Priority),
  119. {pqueue, case lists:keysearch(P, 1, Queues) of
  120. {value, {_, Q}} ->
  121. lists:keyreplace(P, 1, Queues, {P, in(X, Q)});
  122. false when P == infinity ->
  123. [{P, {queue, [X], [], 1}} | Queues];
  124. false ->
  125. case Queues of
  126. [{infinity, InfQueue} | Queues1] ->
  127. [{infinity, InfQueue} |
  128. lists:keysort(1, [{P, {queue, [X], [], 1}} | Queues1])];
  129. _ ->
  130. lists:keysort(1, [{P, {queue, [X], [], 1}} | Queues])
  131. end
  132. end}.
  133. -spec(out(pqueue()) -> {empty | {value, any()}, pqueue()}).
  134. out({queue, [], [], 0} = Q) ->
  135. {empty, Q};
  136. out({queue, [V], [], 1}) ->
  137. {{value, V}, {queue, [], [], 0}};
  138. out({queue, [Y|In], [], Len}) ->
  139. [V|Out] = lists:reverse(In, []),
  140. {{value, V}, {queue, [Y], Out, Len - 1}};
  141. out({queue, In, [V], Len}) when is_list(In) ->
  142. {{value,V}, r2f(In, Len - 1)};
  143. out({queue, In,[V|Out], Len}) when is_list(In) ->
  144. {{value, V}, {queue, In, Out, Len - 1}};
  145. out({pqueue, [{P, Q} | Queues]}) ->
  146. {R, Q1} = out(Q),
  147. NewQ = case is_empty(Q1) of
  148. true -> case Queues of
  149. [] -> {queue, [], [], 0};
  150. [{0, OnlyQ}] -> OnlyQ;
  151. [_|_] -> {pqueue, Queues}
  152. end;
  153. false -> {pqueue, [{P, Q1} | Queues]}
  154. end,
  155. {R, NewQ}.
  156. -spec(shift(pqueue()) -> pqueue()).
  157. shift(Q = {queue, _, _, _}) ->
  158. Q;
  159. shift({pqueue, []}) ->
  160. {pqueue, []}; %% Shouldn't happen?
  161. shift({pqueue, [Hd|Rest]}) ->
  162. {pqueue, Rest ++ [Hd]}. %% Let's hope there are not many priorities.
  163. -spec(out_p(pqueue()) -> {empty | {value, any(), priority()}, pqueue()}).
  164. out_p({queue, _, _, _} = Q) -> add_p(out(Q), 0);
  165. out_p({pqueue, [{P, _} | _]} = Q) -> add_p(out(Q), maybe_negate_priority(P)).
  166. out(0, {queue, _, _, _} = Q) ->
  167. out(Q);
  168. out(Priority, {queue, _, _, _}) ->
  169. erlang:error(badarg, [Priority]);
  170. out(Priority, {pqueue, Queues}) ->
  171. P = maybe_negate_priority(Priority),
  172. case lists:keysearch(P, 1, Queues) of
  173. {value, {_, Q}} ->
  174. {R, Q1} = out(Q),
  175. Queues1 = case is_empty(Q1) of
  176. true -> lists:keydelete(P, 1, Queues);
  177. false -> lists:keyreplace(P, 1, Queues, {P, Q1})
  178. end,
  179. {R, case Queues1 of
  180. [] -> {queue, [], [], 0};
  181. [{0, OnlyQ}] -> OnlyQ;
  182. [_|_] -> {pqueue, Queues1}
  183. end};
  184. false ->
  185. {empty, {pqueue, Queues}}
  186. end.
  187. add_p(R, P) -> case R of
  188. {empty, Q} -> {empty, Q};
  189. {{value, V}, Q} -> {{value, V, P}, Q}
  190. end.
  191. -spec(join(pqueue(), pqueue()) -> pqueue()).
  192. join(A, {queue, [], [], 0}) ->
  193. A;
  194. join({queue, [], [], 0}, B) ->
  195. B;
  196. join({queue, AIn, AOut, ALen}, {queue, BIn, BOut, BLen}) ->
  197. {queue, BIn, AOut ++ lists:reverse(AIn, BOut), ALen + BLen};
  198. join(A = {queue, _, _, _}, {pqueue, BPQ}) ->
  199. {Pre, Post} =
  200. lists:splitwith(fun ({P, _}) -> P < 0 orelse P == infinity end, BPQ),
  201. Post1 = case Post of
  202. [] -> [ {0, A} ];
  203. [ {0, ZeroQueue} | Rest ] -> [ {0, join(A, ZeroQueue)} | Rest ];
  204. _ -> [ {0, A} | Post ]
  205. end,
  206. {pqueue, Pre ++ Post1};
  207. join({pqueue, APQ}, B = {queue, _, _, _}) ->
  208. {Pre, Post} =
  209. lists:splitwith(fun ({P, _}) -> P < 0 orelse P == infinity end, APQ),
  210. Post1 = case Post of
  211. [] -> [ {0, B} ];
  212. [ {0, ZeroQueue} | Rest ] -> [ {0, join(ZeroQueue, B)} | Rest ];
  213. _ -> [ {0, B} | Post ]
  214. end,
  215. {pqueue, Pre ++ Post1};
  216. join({pqueue, APQ}, {pqueue, BPQ}) ->
  217. {pqueue, merge(APQ, BPQ, [])}.
  218. merge([], BPQ, Acc) ->
  219. lists:reverse(Acc, BPQ);
  220. merge(APQ, [], Acc) ->
  221. lists:reverse(Acc, APQ);
  222. merge([{P, A}|As], [{P, B}|Bs], Acc) ->
  223. merge(As, Bs, [ {P, join(A, B)} | Acc ]);
  224. merge([{PA, A}|As], Bs = [{PB, _}|_], Acc) when PA < PB orelse PA == infinity ->
  225. merge(As, Bs, [ {PA, A} | Acc ]);
  226. merge(As = [{_, _}|_], [{PB, B}|Bs], Acc) ->
  227. merge(As, Bs, [ {PB, B} | Acc ]).
  228. -spec(filter(fun ((any()) -> boolean()), pqueue()) -> pqueue()).
  229. filter(Pred, Q) -> fold(fun(V, P, Acc) ->
  230. case Pred(V) of
  231. true -> in(V, P, Acc);
  232. false -> Acc
  233. end
  234. end, new(), Q).
  235. -spec(fold(fun ((any(), priority(), A) -> A), A, pqueue()) -> A).
  236. fold(Fun, Init, Q) -> case out_p(Q) of
  237. {empty, _Q} -> Init;
  238. {{value, V, P}, Q1} -> fold(Fun, Fun(V, P, Init), Q1)
  239. end.
  240. -spec(highest(pqueue()) -> priority() | 'empty').
  241. highest({queue, [], [], 0}) -> empty;
  242. highest({queue, _, _, _}) -> 0;
  243. highest({pqueue, [{P, _} | _]}) -> maybe_negate_priority(P).
  244. r2f([], 0) -> {queue, [], [], 0};
  245. r2f([_] = R, 1) -> {queue, [], R, 1};
  246. r2f([X,Y], 2) -> {queue, [X], [Y], 2};
  247. r2f([X,Y|R], L) -> {queue, [X,Y], lists:reverse(R, []), L}.
  248. maybe_negate_priority(infinity) -> infinity;
  249. maybe_negate_priority(P) -> -P.