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

filter: Statementstmt 6.5% 6 / 91 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 4.5% 1 / 22 Podpod 0.0% 0 / 8 totaltotal 3.7% 7 / 187 CC 30 SCAR 32.6
CC
30
Cov
4%
claim_and_retrieve
90.0
remove
30.0
disown_destination
30.0
CRAP 26.1
« lib/POE/Component/MessageQueue/Message.pm ↑ Summary lib/POE/Component/MessageQueue/Storage/BigMemory.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 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 with qw(POE::Component::MessageQueue::Storage);
21  
22 # destination => @messages
23 0 ▶has 'messages' => (is => 'ro', default => sub { {} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
24  
25 sub store
26 {
27 0 ▶ my ($self, $msg, $callback) = @_;
Subroutine store: not called CC=3 SCAR=24.8
Pod undocumented
28  
29 0 my $id = $msg->id;
30 0 my $destination = $msg->destination;
31  
32 # push onto our array
33 0 ▶ my $aref = ($self->messages->{$destination} ||= []);
Condition0%0/2
$self->messages->{$destination} ||= []
l!l
00
MC/DC0%0/1
$self->messages->{$destination} ||= []
$self->messages->{$destination}
Truth table0%0/2
A
A= $self->messages->{$destination}
Aresult
11
00
34 0 push(@$aref, $msg);
35 0 $self->log(info => "Added $id");
36  
37 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
38 }
39  
40 sub _msg_foreach
41 {
42 0 ▶ my ($self, $action) = @_;
Subroutine _msg_foreach: not called CC=3 SCAR=24.8
43 0 foreach my $messages_in_dest (values %{$self->messages})
44 {
45 0 foreach my $message (@$messages_in_dest)
46 {
47 0 $action->($message);
48 }
49 }
50 }
51  
52 sub _msg_foreach_ids
53 {
54 0 ▶ my ($self, $ids, $action) = @_;
Subroutine _msg_foreach_ids: not called CC=1 SCAR=6.9
55 0 my %id_hash = map { ($_, 1) } (@$ids);
56 $self->_msg_foreach(sub {
57 0 ▶ my $msg = $_[0];
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 ▶ $action->($msg) if (exists $id_hash{$msg->id});
Branch: 1 branch
BranchTrueFalseTotal
if exists $id_hash{$msg->id} 0 0 0
59 0 });
60 }
61  
62 sub get
63 {
64 0 ▶ my ($self, $ids, $callback) = @_;
Subroutine get: not called CC=1 SCAR=6.9
65 0 my @messages;
66 0 ▶ $self->_msg_foreach_ids($ids, sub {push(@messages, $_[0])});
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 @_ = (\@messages);
68 0 goto $callback;
69 }
70  
71 sub get_all
72 {
73 0 ▶ my ($self, $callback) = @_;
Subroutine get_all: not called CC=1 SCAR=6.9
Pod undocumented
74 0 my @messages;
75 0 ▶ $self->_msg_foreach(sub {push(@messages, $_[0])});
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 @_ = (\@messages);
77 0 goto $callback;
78 }
79  
80 sub claim_and_retrieve
81 {
82 0 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine claim_and_retrieve: not called CC=9 SCAR=45.0
Pod undocumented
83 0 my $oldest;
84 0 ▶ my $aref = $self->messages->{$destination} || [];
Condition0%0/2
$self->messages->{$destination} || []
l!l
00
MC/DC0%0/1
$self->messages->{$destination} || []
$self->messages->{$destination}
Truth table0%0/2
A
A= $self->messages->{$destination}
Aresult
11
00
85 0 my $current_time = time();
86 0 foreach my $msg (@$aref)
87 {
88 0 ▶ 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) 0 0 0
Condition0%0/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
000
$msg->claimed or $msg->has_delay and $current_time < $msg->deliver_at
l!l&&r!l&&!r
000
$msg->has_delay and $current_time < $msg->deliver_at
!ll&&!rl&&r
000
$oldest and $oldest->timestamp < $msg->timestamp
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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 0 $oldest = $msg;
92 }
93 }
94 0 ▶ $self->_claim_it_yo($oldest, $client_id) if $oldest;
Branch: 1 branch
BranchTrueFalseTotal
if $oldest 0 0 0
95 0 @_ = ($oldest);
96 0 goto $callback;
97 }
98  
99 sub get_oldest
100 {
101 0 ▶ my ($self, $callback) = @_;
Subroutine get_oldest: not called CC=1 SCAR=6.9
Pod undocumented
102 0 my $oldest;
103 $self->_msg_foreach(sub {
104 0 ▶ my $msg = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
105 0 ▶ $oldest = $msg unless ($oldest && ($oldest->timestamp < $msg->timestamp));
Branch: 1 branch
BranchTrueFalseTotal
unless $oldest and $oldest->timestamp < $msg->timestamp 0 0 0
Condition0%0/3
$oldest and $oldest->timestamp < $msg->timestamp
!ll&&!rl&&r
000
MC/DC0%0/2
$oldest and $oldest->timestamp < $msg->timestamp
$oldest $oldest->timestamp < $msg->timestamp
Truth table0%0/3
A and B
A= $oldestB= $oldest->timestamp < $msg->timestamp
ABresult
0X0
100
111
106 0 });
107 0 @_ = ($oldest);
108 0 goto $callback;
109 }
110  
111 sub remove
112 {
113 0 ▶ my ($self, $message_ids, $callback) = @_;
Subroutine remove: not called CC=5 SCAR=34.0
114 # Stuff IDs into a hash so we can quickly check if a message is on the list
115 0 my %id_hash = map { ($_, 1) } (@$message_ids);
116  
117 0 foreach my $messages (values %{$self->messages})
118 {
119 0 my $max = scalar @{$messages};
120  
121 0 ▶ for ( my $i = 0; $i < $max; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 0 0 0
122 {
123 0 my $message = $messages->[$i];
124 # Check if this messages is in the "remove" list
125 0 ▶ next unless exists $id_hash{$message->id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $id_hash{$message->id} 0 0 0
126 0 splice @$messages, $i, 1;
127 0 $i--; $max--;
128 }
129 }
130  
131 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
132 }
133  
134 sub empty
135 {
136 0 ▶ my ($self, $callback) = @_;
Subroutine empty: not called CC=2 SCAR=17.9
Pod undocumented
137 0 %{$self->messages} = ();
138 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
139 }
140  
141 sub _claim_it_yo
142 {
143 0 ▶ my ($self, $msg, $client_id) = @_;;
Subroutine _claim_it_yo: not called CC=1 SCAR=6.9
144 0 $msg->claim($client_id);
145 0 $self->log('info', sprintf('Message %s claimed by client %s',
146 $msg->id, $client_id));
147 }
148  
149 sub claim
150 {
151 0 ▶ my ($self, $ids, $client_id, $callback) = @_;
Subroutine claim: not called CC=2 SCAR=17.9
152  
153 $self->_msg_foreach_ids($ids, sub {
154 0 ▶ $self->_claim_it_yo($_[0], $client_id);
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 0 });
156  
157 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
158 }
159  
160 sub disown_destination
161 {
162 0 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine disown_destination: not called CC=5 SCAR=34.0
Pod undocumented
163 0 ▶ my $aref = $self->messages->{$destination} || [];
Condition0%0/2
$self->messages->{$destination} || []
l!l
00
MC/DC0%0/1
$self->messages->{$destination} || []
$self->messages->{$destination}
Truth table0%0/2
A
A= $self->messages->{$destination}
Aresult
11
00
164 0 ▶ $_->disown foreach grep {$_->claimed && $_->claimant eq $client_id} @$aref;
Branch: 1 branch
BranchTrueFalseTotal
$_->claimed and $_->claimant eq $client_id 0 0 0
165  
166 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
167 }
168  
169 sub disown_all
170 {
171 0 ▶ my ($self, $client_id, $callback) = @_;
Subroutine disown_all: not called CC=2 SCAR=17.9
Pod undocumented
172 $self->_msg_foreach(sub {
173 0 ▶ my $m = $_[0];
Subroutine __ANON__: not called CC=3 SCAR=24.8
174 0 ▶ $m->disown() if $m->claimed && $m->claimant eq $client_id;
Branch: 1 branch
BranchTrueFalseTotal
if $m->claimed and $m->claimant eq $client_id 0 0 0
Condition0%0/3
$m->claimed and $m->claimant eq $client_id
!ll&&!rl&&r
000
MC/DC0%0/2
$m->claimed and $m->claimant eq $client_id
$m->claimed $m->claimant eq $client_id
Truth table0%0/3
A and B
A= $m->claimedB= $m->claimant eq $client_id
ABresult
0X0
100
111
175 0 });
176 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
177 }
178  
179 sub storage_shutdown
180 {
181 0 ▶ my ($self, $callback) = @_;
Subroutine storage_shutdown: not called CC=2 SCAR=17.9
Pod undocumented
182 0 ▶ goto $callback if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
183 }
184  
185 7 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/Message.pm ↑ Summary lib/POE/Component/MessageQueue/Storage/BigMemory.pm »