|
18 | 18 |
|
19 | 19 | -export([ |
20 | 20 | get/1, |
21 | | - get_many/1, |
22 | 21 | get_targets/1, |
23 | 22 | get_all/0, |
24 | 23 | get_all/1, |
@@ -474,106 +473,59 @@ internal_delete_in_mnesia(QueueName, OnlyDurable, Reason) -> |
474 | 473 | %% get_targets(). |
475 | 474 | %% ------------------------------------------------------------------- |
476 | 475 |
|
477 | | -%% Queue target optimisation is only available in Khepri. |
478 | | -%% Mnesia falls back looking up the full amqqueue record. |
479 | 476 | -spec get_targets(rabbit_exchange:route_return()) -> |
480 | | - [amqqueue:target() | amqqueue:amqqueue() | |
481 | | - {amqqueue:target() | amqqueue:amqqueue(), rabbit_exchange:route_infos()}]. |
| 477 | + [amqqueue:target() | {amqqueue:target(), rabbit_exchange:route_infos()}]. |
482 | 478 | get_targets(Names) -> |
483 | 479 | rabbit_khepri:handle_fallback( |
484 | | - #{mnesia => fun() -> get_many_in_ets(?MNESIA_TABLE, Names) end, |
485 | | - khepri => fun() -> lookup_targets(Names) end |
| 480 | + #{mnesia => fun() -> lookup_targets(mnesia, Names) end, |
| 481 | + khepri => fun() -> lookup_targets(khepri, Names) end |
486 | 482 | }). |
487 | 483 |
|
488 | | -lookup_targets(Names) -> |
| 484 | +lookup_targets(Store, Names) -> |
489 | 485 | lists:filtermap(fun({Name, RouteInfos}) |
490 | 486 | when is_map(RouteInfos) -> |
491 | | - case lookup_target(Name) of |
| 487 | + case lookup_target(Store, Name) of |
492 | 488 | not_found -> false; |
493 | 489 | Target -> {true, {Target, RouteInfos}} |
494 | 490 | end; |
495 | 491 | (Name) -> |
496 | | - case lookup_target(Name) of |
| 492 | + case lookup_target(Store, Name) of |
497 | 493 | not_found -> false; |
498 | 494 | Target -> {true, Target} |
499 | 495 | end |
500 | 496 | end, Names). |
501 | 497 |
|
502 | | -lookup_target(#resource{name = NameBin} = Name) -> |
| 498 | +lookup_target(Store, #resource{name = NameBin} = Name) -> |
503 | 499 | case rabbit_volatile_queue:is(NameBin) of |
504 | 500 | true -> |
505 | | - %% This queue is not stored in the database. |
506 | | - %% We create it on the fly. |
507 | | - case rabbit_volatile_queue:new(Name) of |
| 501 | + %% This queue is not stored in the database. We create it on the fly. |
| 502 | + case rabbit_volatile_queue:new_target(Name) of |
508 | 503 | error -> not_found; |
509 | | - Q -> Q |
| 504 | + Target -> Target |
510 | 505 | end; |
511 | 506 | false -> |
512 | | - try |
513 | | - ets:lookup_element(?KHEPRI_TARGET_PROJECTION, Name, 2, not_found) of |
514 | | - not_found -> |
515 | | - not_found; |
516 | | - Target -> |
517 | | - amqqueue:new_target(Name, Target) |
518 | | - catch |
519 | | - error:badarg -> |
520 | | - not_found |
521 | | - end |
| 507 | + lookup_target0(Store, Name) |
522 | 508 | end. |
523 | 509 |
|
524 | | -%% ------------------------------------------------------------------- |
525 | | -%% get_many(). |
526 | | -%% ------------------------------------------------------------------- |
527 | | - |
528 | | --spec get_many(rabbit_exchange:route_return()) -> |
529 | | - [amqqueue:amqqueue() | {amqqueue:amqqueue(), rabbit_exchange:route_infos()}]. |
530 | | -get_many(Names) when is_list(Names) -> |
531 | | - rabbit_khepri:handle_fallback( |
532 | | - #{mnesia => fun() -> get_many_in_ets(?MNESIA_TABLE, Names) end, |
533 | | - khepri => fun() -> get_many_in_khepri(Names) end |
534 | | - }). |
535 | | - |
536 | | -get_many_in_khepri(Names) -> |
537 | | - try |
538 | | - get_many_in_ets(?KHEPRI_PROJECTION, Names) |
| 510 | +lookup_target0(khepri, Name) -> |
| 511 | + try ets:lookup_element(?KHEPRI_TARGET_PROJECTION, Name, 2, not_found) of |
| 512 | + not_found -> |
| 513 | + not_found; |
| 514 | + Target -> |
| 515 | + amqqueue:new_target(Name, Target) |
539 | 516 | catch |
540 | 517 | error:badarg -> |
541 | | - [] |
542 | | - end. |
543 | | - |
544 | | -get_many_in_ets(Table, [{Name, RouteInfos}]) |
545 | | - when is_map(RouteInfos) -> |
546 | | - case ets_lookup(Table, Name) of |
547 | | - [] -> []; |
548 | | - [Q] -> [{Q, RouteInfos}] |
| 518 | + not_found |
549 | 519 | end; |
550 | | -get_many_in_ets(Table, [Name]) -> |
551 | | - ets_lookup(Table, Name); |
552 | | -get_many_in_ets(Table, Names) when is_list(Names) -> |
553 | | - lists:filtermap(fun({Name, RouteInfos}) |
554 | | - when is_map(RouteInfos) -> |
555 | | - case ets_lookup(Table, Name) of |
556 | | - [] -> false; |
557 | | - [Q] -> {true, {Q, RouteInfos}} |
558 | | - end; |
559 | | - (Name) -> |
560 | | - case ets_lookup(Table, Name) of |
561 | | - [] -> false; |
562 | | - [Q] -> {true, Q} |
563 | | - end |
564 | | - end, Names). |
565 | | - |
566 | | -ets_lookup(Table, QName = #resource{name = QNameBin}) -> |
567 | | - case rabbit_volatile_queue:is(QNameBin) of |
568 | | - true -> |
569 | | - %% This queue record is not stored in the database. |
570 | | - %% We create it on the fly. |
571 | | - case rabbit_volatile_queue:new(QName) of |
572 | | - error -> []; |
573 | | - Q -> [Q] |
574 | | - end; |
575 | | - false -> |
576 | | - ets:lookup(Table, QName) |
| 520 | +lookup_target0(mnesia, Name) -> |
| 521 | + case ets:lookup(?MNESIA_TABLE, Name) of |
| 522 | + [] -> |
| 523 | + not_found; |
| 524 | + [Q] -> |
| 525 | + Type = amqqueue:get_type(Q), |
| 526 | + Pid = amqqueue:get_pid(Q), |
| 527 | + ExtraBcc = amqqueue:get_extra_bcc(Q), |
| 528 | + amqqueue:new_target(Name, {Type, Pid, ExtraBcc}) |
577 | 529 | end. |
578 | 530 |
|
579 | 531 | %% ------------------------------------------------------------------- |
@@ -663,6 +615,14 @@ get_many_durable_in_khepri(Names) -> |
663 | 615 | [] |
664 | 616 | end. |
665 | 617 |
|
| 618 | +get_many_in_ets(Table, Names) -> |
| 619 | + lists:filtermap(fun(Name) -> |
| 620 | + case ets:lookup(Table, Name) of |
| 621 | + [] -> false; |
| 622 | + [Q] -> {true, Q} |
| 623 | + end |
| 624 | + end, Names). |
| 625 | + |
666 | 626 | %% ------------------------------------------------------------------- |
667 | 627 | %% update(). |
668 | 628 | %% ------------------------------------------------------------------- |
|
0 commit comments