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

filter: Statementstmt 100% 91 / 91 Branchbran 76.6% 23 / 30 Conditioncond 87.5% 21 / 24 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 8 totaltotal 87.7% 164 / 187 CC 30 SCAR 13.6
CC
30
Cov
93%
claim_and_retrieve
9.0
disown_destination
5.2
remove
5.0
CRAP 3.9
« lib/POE/Component/MessageQueue/Logger.pm ↑ Summary lib/POE/Component/MessageQueue.pm »
1 #
2 # Copyright 2007-2010 David Snopek <dsnopek@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::Memory;
19 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 with qw(POE::Component::MessageQueue::Storage);
21  
22 # destination => @messages
23 1 ▶has 'messages' => (is => 'ro', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 sub store
26 {
27 201 ▶ my ($self, $msg, $callback) = @_;
Subroutine store: called CC=3 SCAR=11.0
Pod undocumented
28  
29 201 my $id = $msg->id;
30 201 my $destination = $msg->destination;
31  
32 # push onto our array
33 201 ▶ my $aref = ($self->messages->{$destination} ||= []);
Condition100%2/2
$self->messages->{$destination} ||= []
l!l
1965
MC/DC100%1/1
$self->messages->{$destination} ||= []
$self->messages->{$destination}
Truth table100%2/2
A
A= $self->messages->{$destination}
Aresult
11
00
34 201 push(@$aref, $msg);
35 201 $self->log(info => "Added $id");
36  
37 201 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 201 0 201
38 }
39  
40 sub _msg_foreach
41 {
42 8 ▶ my ($self, $action) = @_;
Subroutine _msg_foreach: called CC=3 SCAR=11.0
43 8 foreach my $messages_in_dest (values %{$self->messages})
44 {
45 28 foreach my $message (@$messages_in_dest)
46 {
47 1397 $action->($message);
48 }
49 }
50 }
51  
52 sub _msg_foreach_ids
53 {
54 3 ▶ my ($self, $ids, $action) = @_;
Subroutine _msg_foreach_ids: called CC=1 SCAR=0.0
55 3 my %id_hash = map { ($_, 1) } (@$ids);
56 $self->_msg_foreach(sub {
57 600 ▶ my $msg = $_[0];
Subroutine __ANON__: called CC=2 SCAR=6.9
58 600 ▶ $action->($msg) if (exists $id_hash{$msg->id});
Branch: 1 branch
BranchTrueFalseTotal
if exists $id_hash{$msg->id} 3 597 600
59 3 });
60 }
61  
62 sub get
63 {
64 2 ▶ my ($self, $ids, $callback) = @_;
Subroutine get: called CC=1 SCAR=0.0
65 2 my @messages;
66 2 ▶ $self->_msg_foreach_ids($ids, sub {push(@messages, $_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
67 2 @_ = (\@messages);
68 2 goto $callback;
69 }
70  
71 sub get_all
72 {
73 3 ▶ my ($self, $callback) = @_;
Subroutine get_all: called CC=1 SCAR=0.0
Pod undocumented
74 3 my @messages;
75 3 ▶ $self->_msg_foreach(sub {push(@messages, $_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
76 3 @_ = (\@messages);
77 3 goto $callback;
78 }
79  
80 sub claim_and_retrieve
81 {
82 411 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine claim_and_retrieve: called CC=9 SCAR=22.0
Pod undocumented
83 411 my $oldest;
84 411 ▶ my $aref = $self->messages->{$destination} || [];
Condition50%1/2
$self->messages->{$destination} || []
l!l
4110
MC/DC0%0/1
$self->messages->{$destination} || []
$self->messages->{$destination}
Truth table50%1/2
A
A= $self->messages->{$destination}
Aresult
11
00
85 411 my $current_time = time();
86 411 foreach my $msg (@$aref)
87 {
88 20403 ▶ unless ($msg->claimed || ($msg->has_delay and $current_time < $msg->deliver_at) ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg->claimed or $msg->has_delay and $current_time < $msg->deliver_at or $oldest and $oldest->timestamp < $msg->timestamp) 1313 19090 20403
Condition100%12/12
$msg->claimed or $msg->has_delay and $current_time < $msg->deliver_at or $oldest and $oldest->timestamp < $msg->timestamp
l!l&&r!l&&!r
1020291671034
$msg->claimed or $msg->has_delay and $current_time < $msg->deliver_at
l!l&&r!l&&!r
10200210201
$msg->has_delay and $current_time < $msg->deliver_at
!ll&&!rl&&r
1020012
$oldest and $oldest->timestamp < $msg->timestamp
!ll&&!rl&&r
4019128888
MC/DC60%3/5
$msg->claimed or $msg->has_delay and $current_time < $msg->deliver_at or $oldest and $oldest->timestamp < $msg->timestamp
$msg->claimed $msg->has_delay $current_time < $msg->deliver_at $oldest $oldest->timestamp < $msg->timestamp
Truth table50%4/8
A or B and C or D and E
A= $msg->claimedB= $msg->has_delayC= $current_time < $msg->deliver_atD= $oldestE= $oldest->timestamp < $msg->timestamp
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
89 ($oldest && $oldest->timestamp < $msg->timestamp))
90 {
91 1313 $oldest = $msg;
92 }
93 }
94 411 ▶ $self->_claim_it_yo($oldest, $client_id) if $oldest;
Branch: 1 branch
BranchTrueFalseTotal
if $oldest 401 10 411
95 411 @_ = ($oldest);
96 411 goto $callback;
97 }
98  
99 sub get_oldest
100 {
101 1 ▶ my ($self, $callback) = @_;
Subroutine get_oldest: called CC=1 SCAR=0.0
Pod undocumented
102 1 my $oldest;
103 $self->_msg_foreach(sub {
104 200 ▶ my $msg = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
105 200 ▶ $oldest = $msg unless ($oldest && ($oldest->timestamp < $msg->timestamp));
Branch: 1 branch
BranchTrueFalseTotal
unless $oldest and $oldest->timestamp < $msg->timestamp 12 188 200
Condition100%3/3
$oldest and $oldest->timestamp < $msg->timestamp
!ll&&!rl&&r
111188
MC/DC100%2/2
$oldest and $oldest->timestamp < $msg->timestamp
$oldest $oldest->timestamp < $msg->timestamp
Truth table100%3/3
A and B
A= $oldestB= $oldest->timestamp < $msg->timestamp
ABresult
0X0
100
111
106 1 });
107 1 @_ = ($oldest);
108 1 goto $callback;
109 }
110  
111 sub remove
112 {
113 1 ▶ my ($self, $message_ids, $callback) = @_;
Subroutine remove: called CC=5 SCAR=16.1
114 # Stuff IDs into a hash so we can quickly check if a message is on the list
115 1 my %id_hash = map { ($_, 1) } (@$message_ids);
116  
117 1 foreach my $messages (values %{$self->messages})
118 {
119 4 my $max = scalar @{$messages};
120  
121 4 ▶ for ( my $i = 0; $i < $max; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 200 4 204
122 {
123 200 my $message = $messages->[$i];
124 # Check if this messages is in the "remove" list
125 200 ▶ next unless exists $id_hash{$message->id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $id_hash{$message->id} 197 3 200
126 3 splice @$messages, $i, 1;
127 3 $i--; $max--;
128 }
129 }
130  
131 1 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 0 1
132 }
133  
134 sub empty
135 {
136 1 ▶ my ($self, $callback) = @_;
Subroutine empty: called CC=2 SCAR=7.0
Pod undocumented
137 1 %{$self->messages} = ();
138 1 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 0 1
139 }
140  
141 sub _claim_it_yo
142 {
143 402 ▶ my ($self, $msg, $client_id) = @_;;
Subroutine _claim_it_yo: called CC=1 SCAR=0.0
144 402 $msg->claim($client_id);
145 402 $self->log('info', sprintf('Message %s claimed by client %s',
146 $msg->id, $client_id));
147 }
148  
149 sub claim
150 {
151 1 ▶ my ($self, $ids, $client_id, $callback) = @_;
Subroutine claim: called CC=2 SCAR=7.0
152  
153 $self->_msg_foreach_ids($ids, sub {
154 1 ▶ $self->_claim_it_yo($_[0], $client_id);
Subroutine __ANON__: called CC=1 SCAR=0.0
155 1 });
156  
157 1 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 0 1
158 }
159  
160 sub disown_destination
161 {
162 400 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine disown_destination: called CC=5 SCAR=16.4
Pod undocumented
163 400 ▶ my $aref = $self->messages->{$destination} || [];
Condition50%1/2
$self->messages->{$destination} || []
l!l
4000
MC/DC0%0/1
$self->messages->{$destination} || []
$self->messages->{$destination}
Truth table50%1/2
A
A= $self->messages->{$destination}
Aresult
11
00
164 400 ▶ $_->disown foreach grep {$_->claimed && $_->claimant eq $client_id} @$aref;
Branch: 1 branch
BranchTrueFalseTotal
$_->claimed and $_->claimant eq $client_id 10200 9800 20000
165  
166 400 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 400 0 400
167 }
168  
169 sub disown_all
170 {
171 1 ▶ my ($self, $client_id, $callback) = @_;
Subroutine disown_all: called CC=2 SCAR=7.0
Pod undocumented
172 $self->_msg_foreach(sub {
173 200 ▶ my $m = $_[0];
Subroutine __ANON__: called CC=3 SCAR=11.1
174 200 ▶ $m->disown() if $m->claimed && $m->claimant eq $client_id;
Branch: 1 branch
BranchTrueFalseTotal
if $m->claimed and $m->claimant eq $client_id 1 199 200
Condition66%2/3
$m->claimed and $m->claimant eq $client_id
!ll&&!rl&&r
19901
MC/DC50%1/2
$m->claimed and $m->claimant eq $client_id
$m->claimed $m->claimant eq $client_id
Truth table66%2/3
A and B
A= $m->claimedB= $m->claimant eq $client_id
ABresult
0X0
100
111
175 1 });
176 1 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 0 1
177 }
178  
179 sub storage_shutdown
180 {
181 1 ▶ my ($self, $callback) = @_;
Subroutine storage_shutdown: called CC=2 SCAR=7.2
Pod undocumented
182 1 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 1 0 1
183 }
184  
185 12 1;
186  
187 __END__
188  
189 =pod
190  
191 =head1 NAME
192  
193 POE::Component::MessageQueue::Storage::Memory -- In memory storage engine.
194  
195 =head1 SYNOPSIS
196  
197 use POE;
198 use POE::Component::MessageQueue;
199 use POE::Component::MessageQueue::Storage::Memory;
200 use strict;
201  
202 POE::Component::MessageQueue->new({
203 storage => POE::Component::MessageQueue::Storage::Memory->new()
204 });
205  
206 POE::Kernel->run();
207 exit;
208  
209 =head1 DESCRIPTION
210  
211 A storage engine that keeps all the messages in memory. Provides no persistence
212 what-so-ever.
213  
214 For an alternative in-memory storage engine optimized for a large number of
215 messages, please see L<POE::Component::MessageQueue::Storage::BigMemory>.
216  
217 I wouldn't suggest using this as your main storage engine because if messages
218 aren't removed by consumers, it will continue to consume more memory until it
219 explodes. Check-out L<POE::Component::MessageQueue::Storage::Complex> which
220 can use this module internally to keep messages in memory for a period of
221 time before moving them into persistent storage.
222  
223 =head1 CONSTRUCTOR PARAMETERS
224  
225 None to speak of!
226  
227 =head1 SUPPORTED STOMP HEADERS
228  
229 =over 4
230  
231 =item B<persistent>
232  
233 I<Ignored>. Nothing is persistent in this store.
234  
235 =item B<expire-after>
236  
237 I<Ignored>. All messages are kept until handled.
238  
239 =item B<deliver-after>
240  
241 I<Fully Supported>.
242  
243 =back
244  
245 =head1 SEE ALSO
246  
247 L<POE::Component::MessageQueue::Storage::BigMemory> -- Alternative memory-based storage engine.
248  
249 L<POE::Component::MessageQueue>,
250 L<POE::Component::MessageQueue::Storage>
251  
252 I<Other storage engines:>
253  
254 L<POE::Component::MessageQueue::Storage::BigMemory>,
255 L<POE::Component::MessageQueue::Storage::FileSystem>,
256 L<POE::Component::MessageQueue::Storage::DBI>,
257 L<POE::Component::MessageQueue::Storage::Generic>,
258 L<POE::Component::MessageQueue::Storage::Generic::DBI>,
259 L<POE::Component::MessageQueue::Storage::Throttled>,
260 L<POE::Component::MessageQueue::Storage::Complex>,
261 L<POE::Component::MessageQueue::Storage::Default>
262  
263 =cut
« lib/POE/Component/MessageQueue/Logger.pm ↑ Summary lib/POE/Component/MessageQueue.pm »