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

filter: Statementstmt 51.4% 73 / 142 Branchbran 26.0% 12 / 46 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 66.6% 16 / 24 Podpod 0.0% 0 / 8 totaltotal 41.5% 105 / 253 CC 35 SCAR 30.6
CC
35
Cov
43%
claim_and_retrieve
72.0
claim
56.0
disown_all
12.0
CRAP 21.4
« lib/POE/Component/MessageQueue/Storage/Memory.pm ↑ Summary lib/POE/Component/MessageQueue/IDGenerator/SimpleInt.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 8 ▶use base qw(Heap::Elem);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 sub new
22 {
23 200 ▶ my ($class, $message) = @_;
Subroutine new: called CC=1 SCAR=0.0
24 200 my $self = $class->SUPER::new;
25  
26 200 $self->val($message);
27 200 bless($self, $class);
28 }
29  
30 sub cmp
31 {
32 720 ▶ my ($self, $other) = @_;
Subroutine cmp: called CC=1 SCAR=0.0
33 720 return $self->val->timestamp <=> $other->val->timestamp;
34 }
35  
36 8 1;
37  
38 package POE::Component::MessageQueue::Storage::BigMemory::DelayedMessageElement;
39 8 ▶use base qw(Heap::Elem);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 sub new
42 {
43 0 ▶ my ($class, $message) = @_;
Subroutine new: not called CC=1 SCAR=6.9
44 0 my $self = $class->SUPER::new;
45  
46 0 $self->val($message);
47 0 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 8 1;
57  
58 package POE::Component::MessageQueue::Storage::BigMemory;
59 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 8 with qw(POE::Component::MessageQueue::Storage);
61  
62 8 ▶use Heap::Fibonacci;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 8 ▶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 8 has 'claimed' => empty_hashref;
68 # queue_name => heap of messages
69 8 has 'unclaimed' => empty_hashref;
70 # queue_name => heap of messages
71 8 has 'delayed' => empty_hashref;
72 # message_id => info hash
73 8 has 'messages' => empty_hashref;
74  
75 has 'message_heap' => (
76 is => 'rw',
77 2 ▶ default => sub { Heap::Fibonacci->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 8 );
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 200 ▶ 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 0 ▶ POE::Component::MessageQueue::Storage::BigMemory::DelayedMessageElement->new(@_);
Subroutine _make_delayed_heap_elem: not called CC=1 SCAR=6.9
96 }
97  
98 sub store
99 {
100 100 ▶ my ($self, $msg, $callback) = @_;
Subroutine store: called CC=7 SCAR=22.8
Pod undocumented
101  
102 100 my $main = _make_heap_elem($msg);
103 100 $self->message_heap->add($main);
104  
105 100 my $info = $self->messages->{$msg->id} = {
106 message => $msg,
107 main => $main,
108 };
109  
110 100 ▶ if ($msg->has_delay && $msg->deliver_at > time())
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->has_delay and $msg->deliver_at > time) { } 0 100 100
Condition33%1/3
$msg->has_delay and $msg->deliver_at > time
!ll&&!rl&&r
10000
MC/DC0%0/2
$msg->has_delay and $msg->deliver_at > time
$msg->has_delay $msg->deliver_at > time
Truth table33%1/3
A and B
A= $msg->has_delayB= $msg->deliver_at > time
ABresult
0X0
100
111
111 {
112 0 my $elem = _make_delayed_heap_elem($msg);
113 my $heap =
114 0 ▶ ($self->delayed->{$msg->destination} ||= Heap::Fibonacci->new);
Condition0%0/3
$self->delayed->{$msg->destination} ||= "Heap::Fibonacci"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->delayed->{$msg->destination} ||= "Heap::Fibonacci"->new
$self->delayed->{$msg->destination} "Heap::Fibonacci"->new
Truth table0%0/3
A ||= B
A= $self->delayed->{$msg->destination}B= "Heap::Fibonacci"->new
ABresult
1X1
011
000
115 0 $heap->add($elem);
116 0 $info->{delayed} = $elem;
117 }
118 else
119 {
120 100 my $elem = _make_heap_elem($msg);
121 100 ▶ if ($msg->claimed)
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->claimed) { } 0 100 100
122 {
123 0 $self->claimed->{$msg->claimant}->{$msg->destination} = $elem;
124 }
125 else
126 {
127 my $heap =
128 100 ▶ ($self->unclaimed->{$msg->destination} ||= Heap::Fibonacci->new);
Condition66%2/3
$self->unclaimed->{$msg->destination} ||= "Heap::Fibonacci"->new
l!l&&r!l&&!r
9820
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 100 $heap->add($elem);
130 100 $info->{unclaimed} = $elem;
131 }
132 }
133  
134 100 my $id = $msg->id;
135 100 $self->log(info => "Added $id.");
136 100 @_ = ();
137 100 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 66 34 100
138 }
139  
140 sub get
141 {
142 60 ▶ my ($self, $ids, $callback) = @_;
Subroutine get: called CC=1 SCAR=0.0
143 @_ = ([map $_->{message}, grep $_,
144 60 map $self->messages->{$_}, @$ids]);
145 60 goto $callback;
146 }
147  
148 sub get_all
149 {
150 3 ▶ my ($self, $callback) = @_;
Subroutine get_all: called CC=1 SCAR=0.0
Pod undocumented
151 3 @_ = ([map $_->{message}, values %{$self->messages}]);
152 3 goto $callback;
153 }
154  
155 sub get_oldest
156 {
157 0 ▶ my ($self, $callback) = @_;
Subroutine get_oldest: not called CC=2 SCAR=17.9
Pod undocumented
158 0 my $top = $self->message_heap->top;
159 0 ▶ @_ = ($top && $top->val);
Condition0%0/3
$top && $top->val
!ll&&!rl&&r
000
MC/DC0%0/2
$top && $top->val
$top $top->val
Truth table0%0/3
A && B
A= $topB= $top->val
ABresult
0X0
100
111
160 0 goto $callback;
161 }
162  
163 sub claim_and_retrieve
164 {
165 0 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine claim_and_retrieve: not called CC=8 SCAR=42.8
Pod undocumented
166
167 # move delayed messages to normal storage
168 0 ▶ if (my $delayed = $self->delayed->{$destination})
Branch: 1 branch
BranchTrueFalseTotal
if (my $delayed = $self->delayed->{$destination}) 0 0 0
169 {
170 0 my $time = time();
171  
172 0 ▶ while (my $elem = $delayed->top)
Branch: 1 branch
BranchTrueFalseTotal
if (my $elem = $delayed->top) 0 0 0
173 {
174 0 my $msg = $elem->val;
175 0 ▶ last unless ($msg->deliver_at <= $time);
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->deliver_at <= $time 0 0 0
176  
177 0 my $info = $self->messages->{$msg->id};
178  
179 # remove from the delayed heap
180 0 $delayed->delete($elem);
181 0 delete $info->{delayed};
182  
183 # make a normal heap element
184 0 $elem = _make_heap_elem($msg);
185  
186 # add to the unclaimed heap
187 my $unclaimed =
188 0 ▶ ($self->unclaimed->{$msg->destination} ||= Heap::Fibonacci->new);
Condition0%0/3
$self->unclaimed->{$msg->destination} ||= "Heap::Fibonacci"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->unclaimed->{$msg->destination} ||= "Heap::Fibonacci"->new
$self->unclaimed->{$msg->destination} "Heap::Fibonacci"->new
Truth table0%0/3
A ||= B
A= $self->unclaimed->{$msg->destination}B= "Heap::Fibonacci"->new
ABresult
1X1
011
000
189 0 $unclaimed->add($elem);
190 0 $info->{unclaimed} = $elem;
191 }
192 }
193  
194 0 my $message;
195 0 my $heap = $self->unclaimed->{$destination};
196 0 ▶ if ($heap)
Branch: 1 branch
BranchTrueFalseTotal
if ($heap) 0 0 0
197 {
198 0 my $top = $heap->top;
199 0 ▶ $message = $top->val if $top;
Branch: 1 branch
BranchTrueFalseTotal
if $top 0 0 0
200 0 ▶ $self->claim($message->id, $client_id) if $message;
Branch: 1 branch
BranchTrueFalseTotal
if $message 0 0 0
201 }
202 0 @_ = ($message);
203 0 goto $callback;
204 }
205  
206 sub remove
207 {
208 60 ▶ my ($self, $ids, $callback) = @_;
Subroutine remove: called CC=8 SCAR=24.0
209  
210 60 foreach my $id (@$ids)
211 {
212 60 my $info = delete $self->messages->{$id};
213 60 ▶ next unless $info && $info->{message};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"message"} 0 60 60
Condition33%1/3
$info and $info->{"message"}
!ll&&!rl&&r
0060
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 60 my $msg = $info->{message};
215  
216 60 $self->message_heap->delete($info->{main});
217 60 ▶ if ($msg->claimed)
Branch: 3 branches
BranchTrueFalseTotal
if ($msg->claimed) { } 0 60 60
elsif ($info->{'delayed'}) { } 0 60 60
elsif ($info->{'unclaimed'}) { } 60 0 60
218 {
219 0 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 60 $self->unclaimed->{$msg->destination}->delete($info->{unclaimed});
228 }
229 }
230  
231 60 @_ = ();
232 60 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 60 60
233 }
234  
235 sub empty
236 {
237 1 ▶ my ($self, $callback) = @_;
Subroutine empty: called CC=3 SCAR=11.0
Pod undocumented
238  
239 1 %{$self->$_} = () foreach qw(messages claimed unclaimed delayed);
240 1 $self->message_heap(Heap::Fibonacci->new);
241 1 @_ = ();
242 1 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 0 1
243 }
244  
245 sub claim
246 {
247 0 ▶ my ($self, $ids, $client_id, $callback) = @_;
Subroutine claim: not called CC=7 SCAR=40.3
248  
249 0 foreach my $id (@$ids)
250 {
251 0 ▶ my $info = $self->messages->{$id} || next;
Condition0%0/2
$self->messages->{$id} || (next)
l!l
00
MC/DC0%0/1
$self->messages->{$id} || (next)
$self->messages->{$id}
Truth table0%0/2
A
A= $self->messages->{$id}
Aresult
11
00
252 0 my $message = $info->{message};
253 0 my $destination = $message->destination;
254
255 0 ▶ if ($message->claimed)
Branch: 3 branches
BranchTrueFalseTotal
if ($message->claimed) { } 0 0 0
elsif ($info->{'delayed'}) { } 0 0 0
elsif ($info->{'unclaimed'}) { } 0 0 0
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 0 delete $self->messages->{$message->id}->{unclaimed};
271 0 $self->unclaimed->{$destination}->delete($elem);
272 }
273 0 $message->claim($client_id);
274 0 $self->log(info => "Message $id claimed by client $client_id");
275 }
276 0 @_ = ();
277 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
278 }
279  
280 sub disown_all
281 {
282 0 ▶ my ($self, $client_id, $callback) = @_;
Subroutine disown_all: not called CC=3 SCAR=24.8
Pod undocumented
283 # We just happen to know that disown_destination is synchronous, so we can
284 # ignore the usual callback dance
285 0 foreach my $dest (keys %{$self->claimed->{$client_id}}) {
286 0 $self->disown_destination($dest, $client_id)
287 }
288 0 @_ = ();
289 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
290 }
291  
292 sub disown_destination
293 {
294 0 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine disown_destination: not called CC=3 SCAR=24.8
Pod undocumented
295 0 my $elem = delete $self->claimed->{$client_id}->{$destination};
296 0 ▶ if ($elem)
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 0 0 0
297 {
298 0 my $message = $elem->val;
299 0 $message->disown();
300 0 $self->unclaimed->{$destination}->add($elem);
301 0 $self->messages->{$message->id}->{unclaimed} = $elem;
302 }
303 0 @_ = ();
304 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
305 }
306  
307 # We don't persist anything, so just call our complete handler.
308 sub storage_shutdown
309 {
310 2 ▶ my ($self, $callback) = @_;
Subroutine storage_shutdown: called CC=2 SCAR=6.9
Pod undocumented
311 2 @_ = ();
312 2 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 1 2
313 }
314  
315 8 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
« lib/POE/Component/MessageQueue/Storage/Memory.pm ↑ Summary lib/POE/Component/MessageQueue/IDGenerator/SimpleInt.pm »