1.% common queue operations
In (x, 0, {queue, [_] = In, [], 1})-> {queue, [x], in, 2 };
In (x, 0, {queue, in, out, Len}) When is_list (in), is_list (out)-> {queue, [x | in], out, len + 1 };
Priority queue operations:
In (x, priority,Q = {queue, [], [], 0})-> in (x, priority, {pqueue, []});
In (x, priority, q = {queue,,,})-> In (x, priority, {pqueue, [{0, Q}]});
Main Operations:
in(X, Priority, {pqueue, Queues}) ->P = maybe_negate_priority(Priority),{pqueue, case lists:keysearch(P, 1, Queues) of {value, {_, Q}} -> lists:keyreplace(P, 1, Queues, {P, in(X, Q)}); false when P == infinity -> [{P, {queue, [X], [], 1}} | Queues]; false -> case Queues of [{infinity, InfQueue} | Queues1] -> [{infinity, InfQueue} | lists:keysort(1, [{P, {queue, [X], [], 1}} | Queues1])]; _ -> lists:keysort(1, [{P, {queue, [X], [], 1}} | Queues]) end end}.
Sort by priority of the priority queue. If the priority of the queue is infinity, it is placed in the header. Other queues are sorted by priority.
2.% actions on the columns. If the columns are empty after the queue is out, the r2f (in, Len-1) is taken out of the queue to the queue.
Out ({queue, [], [], 0} = q)-> {empty, Q };
Out ({queue, [v], [], 1})-> {value, V}, {queue, [], [], 0 }};
Out ({queue, [Y | in], [], Len})->
[V|Out] = lists:reverse(In, []),{{value, V}, {queue, [Y], Out}, Len - 1};
Out ({queue, In, [v], Len}) When is_list (in)-> {value, V}, r2f (in, len-1 )};
Out ({queue, In, [v | out], Len}) When is_list (in)-> {value, V}, {queue, in, out, l-EN1 }};
Key steps for outbound queue:
out({pqueue, [{P, Q} | Queues]}) ->{R, Q1} = out(Q),NewQ = case is_empty(Q1) of true -> case Queues of [] -> {queue, [], [], 0}; [{0, OnlyQ}] -> OnlyQ; [_|_] -> {pqueue, Queues} end; false -> {pqueue, [{P, Q1} | Queues]} end,{R, NewQ}.
For a queue with a priority, if the Q queue is empty after the priority is retrieved, determine the next queue queues. Otherwise, {pqueue, [{P, Q1} | queues]} is returned.
Conclusion: The number formats returned by the outbound queue operations are different, similar to {value, V}, {queue, in, out, l-EN1 }}
Priority queue operations in rabbitmq