3535 has_for_source_in_mnesia /1 ,
3636 has_for_source_in_khepri /1 ,
3737 match_source_and_destination_in_khepri_tx /2 ,
38- clear_in_khepri /0
38+ clear_in_khepri /0 ,
39+ khepri_ret_to_deletions /2
3940 ]).
4041
4142-export ([
@@ -201,6 +202,14 @@ create_in_khepri(#binding{source = SrcName,
201202 case ChecksFun (Src , Dst ) of
202203 ok ->
203204 RoutePath = khepri_route_path (Binding ),
205+ DstPath = case DstName of
206+ # resource {kind = queue } ->
207+ rabbit_db_queue :khepri_queue_path (DstName );
208+ # resource {kind = exchange } ->
209+ rabbit_db_exchange :khepri_exchange_path (DstName )
210+ end ,
211+ KeepWhile = #{DstPath => # if_node_exists {}},
212+ PutOptions = #{keep_while => KeepWhile },
204213 MaybeSerial = rabbit_exchange :serialise_events (Src ),
205214 Serial = rabbit_khepri :transaction (
206215 fun () ->
@@ -210,11 +219,17 @@ create_in_khepri(#binding{source = SrcName,
210219 true ->
211220 already_exists ;
212221 false ->
213- ok = khepri_tx :put (RoutePath , sets :add_element (Binding , Set )),
222+ ok = khepri_tx :put (
223+ RoutePath ,
224+ sets :add_element (Binding , Set ),
225+ PutOptions ),
214226 serial_in_khepri (MaybeSerial , Src )
215227 end ;
216228 _ ->
217- ok = khepri_tx :put (RoutePath , sets :add_element (Binding , sets :new ([{version , 2 }]))),
229+ ok = khepri_tx :put (
230+ RoutePath ,
231+ sets :add_element (Binding , sets :new ([{version , 2 }])),
232+ PutOptions ),
218233 serial_in_khepri (MaybeSerial , Src )
219234 end
220235 end , rw ),
@@ -906,6 +921,7 @@ delete_for_destination_in_khepri(#resource{virtual_host = VHost, kind = Kind, na
906921 Name ,
907922 ? KHEPRI_WILDCARD_STAR ), % % RoutingKey
908923 {ok , BindingsMap } = khepri_tx_adv :delete_many (Pattern ),
924+ % logger:alert("BindingsMap = ~p", [BindingsMap]),
909925 Bindings = maps :fold (
910926 fun (Path , Props , Acc ) ->
911927 case {Path , Props } of
@@ -920,6 +936,38 @@ delete_for_destination_in_khepri(#resource{virtual_host = VHost, kind = Kind, na
920936 rabbit_binding :group_bindings_fold (fun maybe_auto_delete_exchange_in_khepri /4 ,
921937 lists :keysort (# binding .source , Bindings ), OnlyDurable ).
922938
939+ khepri_ret_to_deletions (Deleted , OnlyDurable ) ->
940+ Bindings0 = maps :fold (
941+ fun (Path , Props , Acc ) ->
942+ case {Path , Props } of
943+ {? RABBITMQ_KHEPRI_ROUTE_PATH (
944+ _VHost , _SrcName , _Kind , _Name , _RoutingKey ),
945+ #{data := Set }} ->
946+ sets :to_list (Set ) ++ Acc ;
947+ {_ , _ } ->
948+ Acc
949+ end
950+ end , [], Deleted ),
951+ Bindings1 = lists :keysort (# binding .source , Bindings0 ),
952+ rabbit_binding :group_bindings_fold (
953+ fun (XName , Bindings , Deletions , _OnlyDurable ) ->
954+ ExchangePath = rabbit_db_exchange :khepri_exchange_path (XName ),
955+ case Deleted of
956+ #{ExchangePath := #{data := X }} ->
957+ rabbit_binding :add_deletion (
958+ XName , X , deleted , Bindings , Deletions );
959+ _ ->
960+ case rabbit_db_exchange :get (XName ) of
961+ {ok , X } ->
962+ rabbit_binding :add_deletion (
963+ XName , X , not_deleted , Bindings , Deletions );
964+ _ ->
965+ Deletions
966+ end
967+ end
968+ end ,
969+ Bindings1 , OnlyDurable ).
970+
923971% % -------------------------------------------------------------------
924972% % delete_transient_for_destination_in_mnesia().
925973% % -------------------------------------------------------------------
0 commit comments