lib/POE/Component/MessageQueue/Storage/BigMemory.pm

filter: Statementstmt 95.7% 136 / 142 Branchbran 82.6% 38 / 46 Conditioncond 50.0% 10 / 20 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 95.8% 23 / 24 Podpod 0.0% 0 / 8 totaltotal 82.6% 209 / 253 CC 35 SCAR 16.2
CC
35
Cov
88%
remove
8.9
claim
8.1
claim_and_retrieve
8.0
CRAP 5.0
« script/mq.pl ↑ Summary lib/POE/Component/MessageQueue/Logger.pm »
1 #
2 # Copyright 2007, 2008 Paul Driver <frodwith@gmail.com>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17  
18 package POE::Component::MessageQueue::Storage::BigMemory::MessageElement;
19 18 ▶use base qw(Heap::Elem);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub new
22 {
23 1416 ▶ my ($class, $message) = @_;
Subroutine new: called CC=1 SCAR=0.0
24 1416 my $self = $class->SUPER::new;
25  
26 1416 $self->val($message);
27 1416 bless($self, $class);
28 }
29  
30 sub cmp
31 {
32 9083 ▶ my ($self, $other) = @_;
Subroutine cmp: called CC=1 SCAR=0.0
33 9083 return $self->val->timestamp <=> $other->val->timestamp;
34 }
35  
36 18 1;
37  
38 package POE::Component::MessageQueue::Storage::BigMemory::DelayedMessageElement;
39 18 ▶use base qw(Heap::Elem);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 sub new
42 {
43 2 ▶ my ($class, $message) = @_;
Subroutine new: called CC=1 SCAR=0.0
44 2 my $self = $class->SUPER::new;
45  
46 2 $self->val($message);
47 2 bless($self, $class);
48 }
49  
50 sub cmp
51 {
52 0 ▶ my ($self, $other) = @_;
Subroutine cmp: not called CC=1 SCAR=6.9
53 0 return $self->val->deliver_at <=> $other->val->deliver_at;
54 }
55  
56 18 1;
57  
58 package POE::Component::MessageQueue::Storage::BigMemory;
59 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 18 with qw(POE::Component::MessageQueue::Storage);
61  
62 18 ▶use Heap::Fibonacci;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 18 ▶use constant empty_hashref => (is => 'ro', default => sub { {} });
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 # claimer_id => heap element
67 18 has 'claimed' => empty_hashref;
68 # queue_name => heap of messages
69 18 has 'unclaimed' => empty_hashref;
70 # queue_name => heap of messages
71 18 has 'delayed' => empty_hashref;
72 # message_id => info hash
73 18 has 'messages' => empty_hashref;
74  
75 has 'message_heap' => (
76 is => 'rw',
77 9 ▶ default => sub { Heap::Fibonacci->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 18 );
79  
80 # Where messages are stored:
81 # -- A heap of all unclaimed messages sorted by timestamp
82 # -- Per destination heaps for unclaimed messages
83 # -- Per destination heaps for delayed messages
84 # -- A hash of claimant => messages.
85 #
86 # There is also a hash of ids to info about heap elements and such.
87  
88 sub _make_heap_elem
89 {
90 1416 ▶ POE::Component::MessageQueue::Storage::BigMemory::MessageElement->new(@_);
Subroutine _make_heap_elem: called CC=1 SCAR=0.0
91 }
92  
93 sub _make_delayed_heap_elem
94 {
95 2 ▶ POE::Component::MessageQueue::Storage::BigMemory::DelayedMessageElement->new(@_);
Subroutine _make_delayed_heap_elem: called CC=1 SCAR=0.0
96 }
97  
98 sub store
99 {
100 708 ▶ my ($self, $msg, $callback) = @_;
Subroutine store: called CC=7 SCAR=19.6
Pod undocumented
101  
102 708 my $main = _make_heap_elem($msg);
103 708 $self->message_heap->add($main);
104  
105 708 my $info = $self->messages->{$msg->id} = {
106 message => $msg,
107 main => $main,
108 };
109  
110 708 ▶ if ($msg->has_delay && $msg->deliver_at > time())
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->has_delay and $msg->deliver_at > time) { } 2 706 708
Condition66%2/3
$msg->has_delay and $msg->deliver_at > time
!ll&&!rl&&r
70602
MC/DC50%1/2
$msg->has_delay and $msg->deliver_at > time
$msg->has_delay $msg->deliver_at > time
Truth table66%2/3
A and B
A= $msg->has_delayB= $msg->deliver_at > time
ABresult
0X0
100
111
111 {
112 2 my $elem = _make_delayed_heap_elem($msg);
113 my $heap =
114 2 ▶ ($self->delayed->{$msg->destination} ||= Heap::Fibonacci->new);
Condition33%1/3
$self->delayed->{$msg->destination} ||= "Heap::Fibonacci"->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->delayed->{$msg->destination} ||= "Heap::Fibonacci"->new
$self->delayed->{$msg->destination} "Heap::Fibonacci"->new
Truth table33%1/3
A ||= B
A= $self->delayed->{$msg->destination}B= "Heap::Fibonacci"->new
ABresult
1X1
011
000
115 2 $heap->add($elem);
116 2 $info->{delayed} = $elem;
117 }
118 else
119 {
120 706 my $elem = _make_heap_elem($msg);
121 706 ▶ if ($msg->claimed)
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->claimed) { } 2 704 706
122 {
123 2 $self->claimed->{$msg->claimant}->{$msg->destination} = $elem;
124 }
125 else
126 {
127 my $heap =
128 704 ▶ ($self->unclaimed->{$msg->destination} ||= Heap::Fibonacci->new);
Condition66%2/3
$self->unclaimed->{$msg->destination} ||= "Heap::Fibonacci"->new
l!l&&r!l&&!r
689150
MC/DC0%0/2
$self->unclaimed->{$msg->destination} ||= "Heap::Fibonacci"->new
$self->unclaimed->{$msg->destination} "Heap::Fibonacci"->new
Truth table66%2/3
A ||= B
A= $self->unclaimed->{$msg->destination}B= "Heap::Fibonacci"->new
ABresult
1X1
011
000
129 704 $heap->add($elem);
130 704 $info->{unclaimed} = $elem;
131 }
132 }
133  
134 708 my $id = $msg->id;
135 708 $self->log(info => "Added $id.");
136 708 @_ = ();
137 708 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 486 222 708
138 }
139  
140 sub get
141 {
142 254 ▶ my ($self, $ids, $callback) = @_;
Subroutine get: called CC=1 SCAR=0.0
143 @_ = ([map $_->{message}, grep $_,
144 254 map $self->messages->{$_}, @$ids]);
145 254 goto $callback;
146 }
147  
148 sub get_all
149 {
150 17 ▶ my ($self, $callback) = @_;
Subroutine get_all: called CC=1 SCAR=0.0
Pod undocumented
151 17 @_ = ([map $_->{message}, values %{$self->messages}]);
152 17 goto $callback;
153 }
154  
155 sub get_oldest
156 {
157 393 ▶ my ($self, $callback) = @_;
Subroutine get_oldest: called CC=2 SCAR=7.0
Pod undocumented
158 393 my $top = $self->message_heap->top;
159 393 ▶ @_ = ($top && $top->val);
Condition66%2/3
$top && $top->val
!ll&&!rl&&r
2050188
MC/DC50%1/2
$top && $top->val
$top $top->val
Truth table66%2/3
A && B
A= $topB= $top->val
ABresult
0X0
100
111
160 393 goto $callback;
161 }
162  
163 sub claim_and_retrieve
164 {
165 1648 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine claim_and_retrieve: called CC=8 SCAR=20.8
Pod undocumented
166
167 # move delayed messages to normal storage
168 1648 ▶ if (my $delayed = $self->delayed->{$destination})
Branch: 1 branch
BranchTrueFalseTotal
if (my $delayed = $self->delayed->{$destination}) 6 1642 1648
169 {
170 6 my $time = time();
171  
172 6 ▶ while (my $elem = $delayed->top)
Branch: 1 branch
BranchTrueFalseTotal
if (my $elem = $delayed->top) 6 2 8
173 {
174 6 my $msg = $elem->val;
175 6 ▶ last unless ($msg->deliver_at <= $time);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->deliver_at <= $time 4 2 6
176  
177 2 my $info = $self->messages->{$msg->id};
178  
179 # remove from the delayed heap
180 2 $delayed->delete($elem);
181 2 delete $info->{delayed};
182  
183 # make a normal heap element
184 2 $elem = _make_heap_elem($msg);
185  
186 # add to the unclaimed heap
187 my $unclaimed =
188 2 ▶ ($self->unclaimed->{$msg->destination} ||= Heap::Fibonacci->new);
Condition33%1/3
$self->unclaimed->{$msg->destination} ||= "Heap::Fibonacci"->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->unclaimed->{$msg->destination} ||= "Heap::Fibonacci"->new
$self->unclaimed->{$msg->destination} "Heap::Fibonacci"->new
Truth table33%1/3
A ||= B
A= $self->unclaimed->{$msg->destination}B= "Heap::Fibonacci"->new
ABresult
1X1
011
000
189 2 $unclaimed->add($elem);
190 2 $info->{unclaimed} = $elem;
191 }
192 }
193  
194 1648 my $message;
195 1648 my $heap = $self->unclaimed->{$destination};
196 1648 ▶ if ($heap)
Branch: 1 branch
BranchTrueFalseTotal
if ($heap) 1223 425 1648
197 {
198 1223 my $top = $heap->top;
199 1223 ▶ $message = $top->val if $top;
Branch: 1 branch
BranchTrueFalseTotal
if $top 540 683 1223
200 1223 ▶ $self->claim($message->id, $client_id) if $message;
Branch: 1 branch
BranchTrueFalseTotal
if $message 540 683 1223
201 }
202 1648 @_ = ($message);
203 1648 goto $callback;
204 }
205  
206 sub remove
207 {
208 455 ▶ my ($self, $ids, $callback) = @_;
Subroutine remove: called CC=8 SCAR=21.8
209  
210 455 foreach my $id (@$ids)
211 {
212 458 my $info = delete $self->messages->{$id};
213 458 ▶ next unless $info && $info->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"message"} 0 458 458
Condition33%1/3
$info and $info->{"message"}
!ll&&!rl&&r
00458
MC/DC0%0/2
$info and $info->{"message"}
$info $info->{"message"}
Truth table33%1/3
A and B
A= $infoB= $info->{"message"}
ABresult
0X0
100
111
214 458 my $msg = $info->{message};
215  
216 458 $self->message_heap->delete($info->{main});
217 458 ▶ if ($msg->claimed)
Branch: 3 branches
BranchTrueFalseTotal
if ($msg->claimed) { } 37 421 458
elsif ($info->{'delayed'}) { } 0 421 421
elsif ($info->{'unclaimed'}) { } 421 0 421
218 {
219 37 delete $self->claimed->{$msg->claimant}->{$msg->destination};
220 }
221 elsif ($info->{delayed})
222 {
223 0 $self->delayed->{$msg->destination}->delete($info->{delayed});
224 }
225 elsif ($info->{unclaimed})
226 {
227 421 $self->unclaimed->{$msg->destination}->delete($info->{unclaimed});
228 }
229 }
230  
231 455 @_ = ();
232 455 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 208 247 455
233 }
234  
235 sub empty
236 {
237 7 ▶ my ($self, $callback) = @_;
Subroutine empty: called CC=3 SCAR=11.0
Pod undocumented
238  
239 7 %{$self->$_} = () foreach qw(messages claimed unclaimed delayed);
240 7 $self->message_heap(Heap::Fibonacci->new);
241 7 @_ = ();
242 7 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 7 0 7
243 }
244  
245 sub claim
246 {
247 541 ▶ my ($self, $ids, $client_id, $callback) = @_;
Subroutine claim: called CC=7 SCAR=20.9
248  
249 541 foreach my $id (@$ids)
250 {
251 541 ▶ my $info = $self->messages->{$id} || next;
Condition50%1/2
$self->messages->{$id} || (next)
l!l
5410
MC/DC0%0/1
$self->messages->{$id} || (next)
$self->messages->{$id}
Truth table50%1/2
A
A= $self->messages->{$id}
Aresult
11
00
252 541 my $message = $info->{message};
253 541 my $destination = $message->destination;
254
255 541 ▶ if ($message->claimed)
Branch: 3 branches
BranchTrueFalseTotal
if ($message->claimed) { } 0 541 541
elsif ($info->{'delayed'}) { } 0 541 541
elsif ($info->{'unclaimed'}) { } 541 0 541
256 {
257 # According to the docs, we just Do What We're Told.
258 $self->claimed->{$client_id}->{$destination} =
259 0 delete $self->claimed->{$message->claimant}->{$destination}
260 }
261 elsif ($info->{delayed})
262 {
263 my $elem = $self->claimed->{$client_id}->{$destination} =
264 0 delete $self->messages->{$message->id}->{delayed};
265 0 $self->delayed->{$destination}->delete($elem);
266 }
267 elsif ($info->{unclaimed})
268 {
269 my $elem = $self->claimed->{$client_id}->{$destination} =
270 541 delete $self->messages->{$message->id}->{unclaimed};
271 541 $self->unclaimed->{$destination}->delete($elem);
272 }
273 541 $message->claim($client_id);
274 541 $self->log(info => "Message $id claimed by client $client_id");
275 }
276 541 @_ = ();
277 541 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 540 541
278 }
279  
280 sub disown_all
281 {
282 16 ▶ my ($self, $client_id, $callback) = @_;
Subroutine disown_all: called CC=3 SCAR=11.0
Pod undocumented
283 # We just happen to know that disown_destination is synchronous, so we can
284 # ignore the usual callback dance
285 16 foreach my $dest (keys %{$self->claimed->{$client_id}}) {
286 1 $self->disown_destination($dest, $client_id)
287 }
288 16 @_ = ();
289 16 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 16 0 16
290 }
291  
292 sub disown_destination
293 {
294 1601 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine disown_destination: called CC=3 SCAR=11.0
Pod undocumented
295 1601 my $elem = delete $self->claimed->{$client_id}->{$destination};
296 1601 ▶ if ($elem)
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 504 1097 1601
297 {
298 504 my $message = $elem->val;
299 504 $message->disown();
300 504 $self->unclaimed->{$destination}->add($elem);
301 504 $self->messages->{$message->id}->{unclaimed} = $elem;
302 }
303 1601 @_ = ();
304 1601 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1600 1 1601
305 }
306  
307 # We don't persist anything, so just call our complete handler.
308 sub storage_shutdown
309 {
310 9 ▶ my ($self, $callback) = @_;
Subroutine storage_shutdown: called CC=2 SCAR=6.9
Pod undocumented
311 9 @_ = ();
312 9 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 8 1 9
313 }
314  
315 18 1;
316  
317 __END__
318  
319 =pod
320  
321 =head1 NAME
322  
323 POE::Component::MessageQueue::Storage::BigMemory -- In-memory storage engine
324 optimized for a large number of messages.
325  
326 =head1 SYNOPSIS
327  
328 use POE;
329 use POE::Component::MessageQueue;
330 use POE::Component::MessageQueue::Storage::BigMemory;
331 use strict;
332  
333 POE::Component::MessageQueue->new({
334 storage => POE::Component::MessageQueue::Storage::BigMemory->new()
335 });
336  
337 POE::Kernel->run();
338 exit;
339  
340 =head1 DESCRIPTION
341  
342 An in-memory storage engine that is optimised for a large number of messages.
343 Its an alternative to L<POE::Componenent::MessageQueue::Storage::Memory>, which
344 stores everything in a Perl ARARY, which can slow the MQ to a CRAWL when the
345 number of messsages in this store gets big.
346  
347 store() is a little bit slower per message in this module and it uses
348 more memory per message. Everything else should be considerably more efficient,
349 though, especially when the number of messages starts to climb. Many operations
350 in Storage::Memory are O(n*n). Most operations in this module are O(1)!
351  
352 I wouldn't suggest using this as your main storage engine because if messages
353 aren't removed by consumers, it will continue to consume more memory until it
354 explodes. Check-out L<POE::Component::MessageQueue::Storage::Complex> which
355 can use this module internally to keep messages in memory for a period of
356 time before moving them into persistent storage.
357  
358 =head1 CONSTRUCTOR PARAMETERS
359  
360 None to speak of!
361  
362 =head1 SUPPORTED STOMP HEADERS
363  
364 =over 4
365  
366 =item B<persistent>
367  
368 I<Ignored>. Nothing is persistent in this store.
369  
370 =item B<expire-after>
371  
372 I<Ignored>. All messages are kept until handled.
373  
374 =item B<deliver-after>
375  
376 I<Fully Supported>.
377  
378 =back
379  
380 =head1 SEE ALSO
381  
382 L<POE::Component::MessageQueue>,
383 L<POE::Component::MessageQueue::Storage>
384  
385 I<Other storage engines:>
386  
387 L<POE::Component::MessageQueue::Storage::Memory>,
388 L<POE::Component::MessageQueue::Storage::FileSystem>,
389 L<POE::Component::MessageQueue::Storage::DBI>,
390 L<POE::Component::MessageQueue::Storage::Generic>,
391 L<POE::Component::MessageQueue::Storage::Generic::DBI>,
392 L<POE::Component::MessageQueue::Storage::Throttled>,
393 L<POE::Component::MessageQueue::Storage::Complex>,
394 L<POE::Component::MessageQueue::Storage::Default>
395  
396 =cut
« script/mq.pl ↑ Summary lib/POE/Component/MessageQueue/Logger.pm »