|
17 | 17 | prepare/2
|
18 | 18 | ]).
|
19 | 19 |
|
| 20 | +-export([init_from_stream/2]). |
| 21 | + |
20 | 22 | -import(rabbit_misc,
|
21 | 23 | [maps_put_truthy/3]).
|
22 | 24 |
|
|
99 | 101 |
|
100 | 102 | -export_type([state/0]).
|
101 | 103 |
|
| 104 | +%% API |
| 105 | + |
| 106 | +-spec init_from_stream(binary(), mc:annotations()) -> |
| 107 | + mc:state(). |
| 108 | +init_from_stream(Payload, #{} = Anns0) -> |
| 109 | + Sections = amqp10_framing:decode_bin(Payload, [server_mode]), |
| 110 | + Msg = msg_body_encoded(Sections, Payload, #msg_body_encoded{}), |
| 111 | + %% when initalising from stored stream data the recovered |
| 112 | + %% annotations take precendence over the ones provided |
| 113 | + Anns = maps:merge(Anns0, essential_properties(Msg, recover)), |
| 114 | + mc:init(?MODULE, Msg, Anns). |
| 115 | + |
| 116 | +%% CALLBACKS |
| 117 | + |
| 118 | +init(#msg_body_encoded{} = Msg) -> |
| 119 | + {Msg, #{}}; |
102 | 120 | init(Payload) ->
|
103 | 121 | Sections = amqp10_framing:decode_bin(Payload, [server_mode]),
|
104 | 122 | Msg = msg_body_encoded(Sections, Payload, #msg_body_encoded{}),
|
105 |
| - Anns = essential_properties(Msg), |
| 123 | + Anns = essential_properties(Msg, new), |
106 | 124 | {Msg, Anns}.
|
107 | 125 |
|
108 | 126 | convert_from(?MODULE, Sections, _Env) when is_list(Sections) ->
|
@@ -622,16 +640,44 @@ encode_deaths(Deaths) ->
|
622 | 640 | {map, Map}
|
623 | 641 | end, Deaths).
|
624 | 642 |
|
625 |
| -essential_properties(Msg) -> |
| 643 | +essential_properties(#msg_body_encoded{} = Msg, new) -> |
626 | 644 | Durable = get_property(durable, Msg),
|
627 | 645 | Priority = get_property(priority, Msg),
|
628 | 646 | Timestamp = get_property(timestamp, Msg),
|
629 | 647 | Ttl = get_property(ttl, Msg),
|
630 |
| - Anns = #{?ANN_DURABLE => Durable}, |
631 |
| - maps_put_truthy( |
632 |
| - ?ANN_PRIORITY, Priority, |
633 |
| - maps_put_truthy( |
634 |
| - ?ANN_TIMESTAMP, Timestamp, |
635 |
| - maps_put_truthy( |
636 |
| - ttl, Ttl, |
637 |
| - Anns))). |
| 648 | + Anns0 = #{?ANN_DURABLE => Durable}, |
| 649 | + Anns = maps_put_truthy( |
| 650 | + ?ANN_PRIORITY, Priority, |
| 651 | + maps_put_truthy( |
| 652 | + ?ANN_TIMESTAMP, Timestamp, |
| 653 | + maps_put_truthy( |
| 654 | + ttl, Ttl, |
| 655 | + Anns0))), |
| 656 | + Anns; |
| 657 | +essential_properties(#msg_body_encoded{message_annotations = MA} = Msg, recover) -> |
| 658 | + Anns = essential_properties(Msg, new), |
| 659 | + case MA of |
| 660 | + [] -> |
| 661 | + Anns; |
| 662 | + _ -> |
| 663 | + lists:foldl( |
| 664 | + fun ({{symbol, <<"x-routing-key">>}, |
| 665 | + {utf8, Key}}, Acc) -> |
| 666 | + maps:update_with(?ANN_ROUTING_KEYS, |
| 667 | + fun(L) -> [Key | L] end, |
| 668 | + [Key], |
| 669 | + Acc); |
| 670 | + ({{symbol, <<"x-cc">>}, |
| 671 | + {list, CCs0}}, Acc) -> |
| 672 | + CCs = [CC || {_T, CC} <- CCs0], |
| 673 | + maps:update_with(?ANN_ROUTING_KEYS, |
| 674 | + fun(L) -> L ++ CCs end, |
| 675 | + CCs, |
| 676 | + Acc); |
| 677 | + ({{symbol, <<"x-exchange">>}, |
| 678 | + {utf8, Exchange}}, Acc) -> |
| 679 | + Acc#{?ANN_EXCHANGE => Exchange}; |
| 680 | + (_, Acc) -> |
| 681 | + Acc |
| 682 | + end, Anns, MA) |
| 683 | + end. |
0 commit comments