Skip to content

Commit 864ec1e

Browse files
Introduce rabbit_{exchange,amqqueue}:lookup_many/1
rabbit_amqqueue:lookup/1 already supports lists of keys but it makes less sense for rabbit_exchange:lookup/1. This introduces a uniform API element that can be used to look up N entities by key while preserving the historically accumulated difference that stems from the common access patterns for each entity type. (cherry picked from commit 4216b2a) Conflicts: src/rabbit_amqqueue.erl src/rabbit_exchange.erl
1 parent f261041 commit 864ec1e

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/rabbit_amqqueue.erl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
forget_all_durable/1, delete_crashed/1, delete_crashed/2,
2323
delete_crashed_internal/2]).
2424
-export([pseudo_queue/2, immutable/1]).
25-
-export([lookup/1, not_found_or_absent/1, with/2, with/3, with_or_die/2,
25+
-export([lookup/1, lookup_many/1, not_found_or_absent/1, with/2, with/3, with_or_die/2,
2626
assert_equivalence/5,
2727
check_exclusive_access/2, with_exclusive_access_or_die/3,
2828
stat/1, deliver/2, requeue/3, ack/3, reject/4]).
@@ -94,13 +94,6 @@
9494
-spec update
9595
(name(), fun((rabbit_types:amqqueue()) -> rabbit_types:amqqueue())) ->
9696
'not_found' | rabbit_types:amqqueue().
97-
-spec lookup
98-
(name()) ->
99-
rabbit_types:ok(rabbit_types:amqqueue()) |
100-
rabbit_types:error('not_found');
101-
([name()]) ->
102-
[rabbit_types:amqqueue()].
103-
-spec not_found_or_absent(name()) -> not_found_or_absent().
10497
-spec with(name(), qfun(A)) ->
10598
A | rabbit_types:error(not_found_or_absent()).
10699
-spec with(name(), qfun(A), fun((not_found_or_absent()) -> B)) -> A | B.
@@ -436,6 +429,13 @@ add_default_binding(#amqqueue{name = QueueName}) ->
436429
args = []},
437430
?INTERNAL_USER).
438431

432+
-spec lookup
433+
(name()) ->
434+
rabbit_types:ok(rabbit_types:amqqueue()) |
435+
rabbit_types:error('not_found');
436+
([name()]) ->
437+
[rabbit_types:amqqueue()].
438+
439439
lookup([]) -> []; %% optimisation
440440
lookup([Name]) -> ets:lookup(rabbit_queue, Name); %% optimisation
441441
lookup(Names) when is_list(Names) ->
@@ -445,6 +445,14 @@ lookup(Names) when is_list(Names) ->
445445
lookup(Name) ->
446446
rabbit_misc:dirty_read({rabbit_queue, Name}).
447447

448+
449+
-spec lookup_many ([name()]) -> [amqqueue:amqqueue()].
450+
451+
lookup_many(Names) when is_list(Names) ->
452+
lookup(Names).
453+
454+
-spec not_found_or_absent(name()) -> not_found_or_absent().
455+
448456
not_found_or_absent(Name) ->
449457
%% NB: we assume that the caller has already performed a lookup on
450458
%% rabbit_queue and not found anything

src/rabbit_exchange.erl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
-export([recover/1, policy_changed/2, callback/4, declare/7,
2222
assert_equivalence/6, assert_args_equivalence/2, check_type/1,
23-
lookup/1, lookup_or_die/1, list/0, list/1, lookup_scratch/2,
23+
lookup/1, lookup_many/1, lookup_or_die/1, list/0, list/1, lookup_scratch/2,
2424
update_scratch/3, update_decorators/1, immutable/1,
2525
info_keys/0, info/1, info/2, info_all/1, info_all/2, info_all/4,
2626
route/2, delete/3, validate_binding/2]).
@@ -55,9 +55,6 @@
5555
-spec assert_args_equivalence
5656
(rabbit_types:exchange(), rabbit_framing:amqp_table())
5757
-> 'ok' | rabbit_types:connection_exit().
58-
-spec lookup_or_die
59-
(name()) -> rabbit_types:exchange() |
60-
rabbit_types:channel_exit().
6158
-spec list() -> [rabbit_types:exchange()].
6259
-spec list_names() -> [rabbit_exchange:name()].
6360
-spec list(rabbit_types:vhost()) -> [rabbit_types:exchange()].
@@ -252,19 +249,26 @@ assert_args_equivalence(#exchange{ name = Name, arguments = Args },
252249

253250
-spec lookup
254251
(name()) -> rabbit_types:ok(rabbit_types:exchange()) |
255-
rabbit_types:error('not_found');
256-
([name()]) ->
257-
[rabbit_types:exchange()].
252+
rabbit_types:error('not_found').
258253

259-
lookup([]) -> [];
260-
lookup([Name]) -> ets:lookup(rabbit_exchange, Name);
261-
lookup(Names) when is_list(Names) ->
262-
%% Normally we'd call mnesia:dirty_read/1 here, but that is quite
263-
%% expensive for reasons explained in rabbit_misc:dirty_read/1.
264-
lists:append([ets:lookup(rabbit_exchange, Name) || Name <- Names]);
265254
lookup(Name) ->
266255
rabbit_misc:dirty_read({rabbit_exchange, Name}).
267256

257+
258+
-spec lookup_many([name()]) -> [rabbit_types:exchange()].
259+
260+
lookup_many([]) -> [];
261+
lookup_many([Name]) -> ets:lookup(rabbit_exchange, Name);
262+
lookup_many(Names) when is_list(Names) ->
263+
%% Normally we'd call mnesia:dirty_read/1 here, but that is quite
264+
%% expensive for reasons explained in rabbit_misc:dirty_read/1.
265+
lists:append([ets:lookup(rabbit_exchange, Name) || Name <- Names]).
266+
267+
268+
-spec lookup_or_die
269+
(name()) -> rabbit_types:exchange() |
270+
rabbit_types:channel_exit().
271+
268272
lookup_or_die(Name) ->
269273
case lookup(Name) of
270274
{ok, X} -> X;

0 commit comments

Comments
 (0)