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

filter: Statementstmt 82.6% 86 / 104 Branchbran 71.4% 20 / 28 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 87.8% 29 / 33 Podpod 0.0% 0 / 7 totaltotal 75.0% 144 / 192 CC 24 SCAR 10.3
CC
24
Cov
78%
__ANON__
5.7
_doboth
5.6
__ANON__
4.1
CRAP 2.8
« lib/POE/Component/MessageQueue/Topic.pm ↑ Summary lib/POE/Component/MessageQueue/Subscription.pm »
1 #
2 # Copyright 2007 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::Double;
19 16 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use MooseX::MultiInitArg;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # These guys just call a method on both front and back stores and have a
23 # simple no-arg completion callback. No reason to write them all!
24 16 foreach my $method (qw(empty disown_destination disown_all)) {
25 __PACKAGE__->meta->add_method($method, sub {
26 0 ▶ my $self = shift;
Subroutine disown_destination: not called CC=2 SCAR=11.0
Subroutine empty: called CC=2 SCAR=11.0
27 0 my $last = pop;
28 0 ▶ if(ref $last eq 'CODE')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $last eq 'CODE') { } 0 0 0
if (ref $last eq 'CODE') { } 1214 0 1214
29 {
30 0 my @args = @_;
31 $self->front->$method(@args, sub {
32 1214 ▶ $self->back->$method(@args, $last);
Subroutine __ANON__: called CC=1 SCAR=0.0
33 0 });
34 }
35 else
36 {
37 0 $self->front->$method(@_, $last);
38 0 $self->back->$method(@_, $last);
39 }
40 48 });
41 }
42  
43 16 with qw(POE::Component::MessageQueue::Storage);
44 16 ▶use POE::Component::MessageQueue::Storage::BigMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 has front => (
47 metaclass => 'MultiInitArg',
48 init_args => ['front_store'],
49 is => 'ro',
50 does => qw(POE::Component::MessageQueue::Storage),
51 3 ▶ default => sub {POE::Component::MessageQueue::Storage::BigMemory->new()},
Subroutine __ANON__: called CC=1 SCAR=0.0
52 16 required => 1,
53 );
54  
55 16 has back => (
56 is => 'ro',
57 metaclass => 'MultiInitArg',
58 init_args => [qw(back_store storage)],
59 does => qw(POE::Component::MessageQueue::Storage),
60 required => 1,
61 );
62  
63 # Any true value for a given ID means the message is in the front store.
64 # (value may be useful data, like message size)
65 has front_info => (
66 is => 'ro',
67 isa => 'HashRef',
68 6 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
69 16 traits => ['Hash'],
70 handles => {
71 'in_front' => 'exists',
72 'get_front' => 'get',
73 'set_front' => 'set',
74 'clear_front' => 'clear',
75 'delete_front' => 'delete',
76 },
77 );
78  
79 after 'set_logger' => sub {
80 3 ▶ my ($self, $logger) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
81 3 $self->front->set_logger($logger);
82 3 $self->back->set_logger($logger);
83 16 };
84  
85 sub in_back
86 {
87 450 ▶ my ($self, $id) = @_;
Subroutine in_back: called CC=2 SCAR=6.9
Pod undocumented
88 450 ▶ return 1 unless $self->in_front($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_front($id) 81 369 450
89 369 return $self->get_front($id)->{persisted};
90 }
91  
92 sub _split_ids
93 {
94 71 ▶ my ($self, $ids) = @_;
Subroutine _split_ids: called CC=4 SCAR=13.9
95 71 my (@fids, @bids);
96 71 foreach my $id (@$ids)
97 {
98 76 ▶ push (@fids, $id) if $self->in_front($id);
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_front($id) 2 74 76
99 76 ▶ push (@bids, $id) if $self->in_back($id);
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_back($id) 74 2 76
100 }
101 71 return (\@fids, \@bids);
102 }
103  
104 sub _doboth
105 {
106 66 ▶ my ($self, $ids, $do_front, $do_back, $callback) = @_;
Subroutine _doboth: called CC=5 SCAR=17.3
107 66 my ($fids, $bids) = $self->_split_ids($ids);
108  
109 66 ▶ if (@$fids && @$bids)
Branch: 3 branches
BranchTrueFalseTotal
if (@$fids and @$bids) { } 1 65 66
elsif (@$fids) { } 0 65 65
elsif (@$bids) { } 65 0 65
Condition66%2/3
@$fids and @$bids
!ll&&!rl&&r
6501
MC/DC50%1/2
@$fids and @$bids
@$fids @$bids
Truth table66%2/3
A and B
A= @$fidsB= @$bids
ABresult
0X0
100
111
110 {
111 1 ▶ $do_front->($fids, sub {$do_back->($bids, $callback)});
Subroutine __ANON__: called CC=1 SCAR=0.0
112 }
113 elsif(@$fids)
114 {
115 0 $do_front->($fids, $callback);
116 }
117 elsif(@$bids)
118 {
119 65 $do_back->($bids, $callback);
120 }
121 else
122 {
123 0 goto $callback;
124 }
125 }
126  
127 sub remove
128 {
129 63 ▶ my ($self, $aref, $callback) = @_;
Subroutine remove: called CC=1 SCAR=0.0
Pod undocumented
130 $self->_doboth(
131 $aref,
132 sub {
133 1 ▶ my ($ids, $callback) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
134 1 $self->delete_front($ids);
135 1 $self->front->remove($ids, $callback);
136 },
137 sub {
138 63 ▶ my ($ids, $callback) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
139 63 $self->back->remove($ids, $callback);
140 },
141 63 $callback,
142 );
143 }
144  
145 sub claim
146 {
147 3 ▶ my ($self, $aref, $client, $callback) = @_;
Subroutine claim: called CC=1 SCAR=0.2
Pod undocumented
148 $self->_doboth(
149 $aref,
150 0 ▶ sub {$self->front->claim($_[0], $client, $_[1])},
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 3 ▶ sub {$self->back ->claim($_[0], $client, $_[1])},
Subroutine __ANON__: called CC=1 SCAR=0.0
152 3 $callback,
153 );
154 }
155  
156 sub get
157 {
158 5 ▶ my ($self, $ids, $callback) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
159 5 my ($fids, $bids) = $self->_split_ids($ids);
160 $self->front->get($fids, sub {
161 5 ▶ goto $callback unless @$bids; # Avoid backstore call
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless @$bids 1 4 5
162 4 my $got_front = $_[0];
163 $self->back->get($bids, sub {
164 4 ▶ my $got_back = $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
165 4 push(@$got_back, @$got_front);
166 4 goto $callback;
167 4 });
168 5 });
169 }
170  
171 sub get_all
172 {
173 9 ▶ my ($self, $callback) = @_;
Subroutine get_all: called CC=1 SCAR=0.0
Pod undocumented
174 9 my %messages; # store in a hash to ensure uniqueness
175 $self->front->get_all(sub {
176 9 ▶ $messages{$_->id} = $_ foreach @{$_[0]};
Subroutine __ANON__: called CC=2 SCAR=6.9
177 $self->back->get_all(sub {
178 9 ▶ $messages{$_->id} = $_ foreach @{$_[0]};
Subroutine __ANON__: called CC=2 SCAR=6.9
179 9 @_ = ([values %messages]);
180 9 goto $callback;
181 9 });
182 9 });
183 }
184  
185 sub get_oldest
186 {
187 3 ▶ my ($self, $callback) = @_;
Subroutine get_oldest: called CC=1 SCAR=0.1
Pod undocumented
188 $self->front->get_oldest(sub {
189 3 ▶ my $f = $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.2
190 $self->back->get_oldest(sub {
191 3 ▶ my $b = $_[0];
Subroutine __ANON__: called CC=5 SCAR=17.5
192 3 ▶ @_ = (
Branch: 2 branches
BranchTrueFalseTotal
$f && $b ? : 1 2 3
$f->timestamp < $b->timestamp ? : 0 1 1
Condition50%3/6
$f and $b
!ll&&!rl&&r
201
$f or $b
l!l&&r!l&&!r
020
MC/DC50%1/2
$f and $b
$f $b
MC/DC0%0/2
$f or $b
$f $b
Truth table66%2/3
A and B
A= $fB= $b
ABresult
0X0
100
111
Truth table33%1/3
A or B
A= $fB= $b
ABresult
1X1
011
000
193 ($f && $b) ?
194 ($f->timestamp < $b->timestamp ? $f : $b) :
195 ($f || $b)
196 );
197 3 goto $callback;
198 3 });
199 3 });
200 }
201  
202 sub claim_and_retrieve
203 {
204 1215 ▶ my ($self, $destination, $client_id, $callback) = @_;
Subroutine claim_and_retrieve: called CC=1 SCAR=0.7
Pod undocumented
205  
206 $self->front->claim_and_retrieve($destination, $client_id, sub {
207 1215 ▶ if (my $msg = $_[0])
Subroutine __ANON__: called CC=3 SCAR=13.4
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $_[0]) { } 121 1094 1215
208 {
209 # We don't need to claim unless it's in the backstore already
210 121 ▶ goto $callback unless ($self->in_back($msg->id));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_back($msg->id) 121 0 121
211 $self->back->claim($msg->id, $client_id, sub {
212 0 ▶ @_ = ($msg);
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 goto $callback;
214 0 });
215 }
216 else
217 {
218 $self->back->claim_and_retrieve($destination, $client_id, sub {
219 1094 ▶ my $msg = $_[0];
Subroutine __ANON__: called CC=3 SCAR=14.2
220 1094 ▶ goto $callback
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg and $self->in_front($msg->id) 1094 0 1094
Condition66%2/3
defined $msg and $self->in_front($msg->id)
!ll&&!rl&&r
3710570
MC/DC0%0/2
defined $msg and $self->in_front($msg->id)
defined $msg $self->in_front($msg->id)
Truth table66%2/3
A and B
A= defined $msgB= $self->in_front($msg->id)
ABresult
0X0
100
111
221 if (not defined $msg or not $self->in_front($msg->id));
222  
223 $self->front->claim($msg->id, $client_id, sub {
224 0 ▶ @_ = ($msg);
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 0 goto $callback;
226 0 });
227 1094 });
228 }
229 1215 });
230 }
231  
232 16 1;
233  
234 __END__
235  
236 =pod
237  
238 =head1 NAME
239  
240 POE::Component::MessageQueue::Storage::Double -- Stores composed of two other
241 stores.
242
243 =head1 DESCRIPTION
244  
245 Refactor mercilessly, as they say. They also say don't repeat yourself. This
246 module contains functionality for any store that is a composition of two
247 stores. At least Throttled and Complex share this trait, and it doesn't make
248 any sense to duplicate code between them.
249  
250 =head1 CONSTRUCTOR PARAMETERS
251  
252 =over 2
253  
254 =item front => SCALAR
255  
256 =item back => SCALAR
257  
258 Takes a reference to a storage engine to use as the front store / back store.
259  
260 =back
261  
262 =head1 Unimplemented Methods
263  
264 =over 2
265  
266 =item store
267  
268 This isn't implemented because Complex and Throttled differ here. Perhaps
269 your storage differs here as well. This is essentially where you specify
270 policy about what goes in which store. Be sure you update the front_info hash
271 when you store something!
272  
273 =item storage_shutdown
274  
275 And this is where you specify policy about what happens when you die. You
276 lucky person, you.
277  
278 =back
279  
280 =cut
« lib/POE/Component/MessageQueue/Topic.pm ↑ Summary lib/POE/Component/MessageQueue/Subscription.pm »