Q/ReliableFIFO/Item.pm

filter: Statementstmt 37.2% 16 / 43 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 28.5% 4 / 14 Podpod 100% 8 / 8 totaltotal 31.4% 28 / 89 CC 11 SCAR 23.3
CC
11
Cov
25%
new
30.0
_deserialize
12.0
_get
6.0
CRAP 10.2
« Q/NaiveFIFO/Redis.pm ↑ Summary Q/DistFIFO.pm »
1 package Queue::Q::ReliableFIFO::Item;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Class::XSAccessor {getters => ['_serialized']};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # for reasons of debugging, JSON is easier, while Sereal::* is
8 # faster (about 7%) and delivers smaller serialized blobs.
9 1 ▶use JSON::XS; # use the real stuff, no fall back on pure Perl JSON please
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 my $serializer = JSON::XS->new->utf8->pretty(0);
11 1 my $deserializer = JSON::XS->new->utf8->pretty(0);
12  
13 #use Sereal::Encoder;
14 #use Sereal::Decoder;
15 #my $serializer = Sereal::Encoder->new();
16 #my $deserializer = Sereal::Decoder->new();
17  
18 1 my @item_info = (
19 't', # time the item was created
20 'rc', # requeue counter (how often the item is requeued)
21 'fc', # fail counter (how often the item reached max-requeue)
22 'error', # last error message
23 );
24  
25 sub new {
26 0 ▶ my $class = shift;
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
27 0 my $self = bless { @_ }, $class;
28  
29 die "'data' or '_serialized' named parameters required for constructor"
30 0 ▶ if not exists $self->{data} and not exists $self->{_serialized};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"} or exists $self->{"_serialized"} 0 0 0
Condition0%0/3
exists $self->{"data"} or exists $self->{"_serialized"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $self->{"data"} or exists $self->{"_serialized"}
exists $self->{"data"} exists $self->{"_serialized"}
Truth table0%0/3
A or B
A= exists $self->{"data"}B= exists $self->{"_serialized"}
ABresult
1X1
011
000
31  
32 0 ▶ if (not exists $self->{_serialized}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_serialized"}) 0 0 0
33 $self->{_serialized} ||=
34 $serializer->encode({
35 t => time(),
36 b => $self->{data}
37 0 ▶ });
Condition0%0/3
$self->{"_serialized"} ||= $serializer->encode({"t", time, "b", $self->{"data"}})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_serialized"} ||= $serializer->encode({"t", time, "b", $self->{"data"}})
$self->{"_serialized"} $serializer->encode({"t", time, "b", $self->{"data"}})
Truth table0%0/3
A ||= B
A= $self->{"_serialized"}B= $serializer->encode({"t", time, "b", $self->{"data"}})
ABresult
1X1
011
000
38 }
39 0 return $self;
40 }
41  
42 sub _get {
43 0 ▶ my ($self, $elem) = @_;
Subroutine _get: not called CC=2 SCAR=17.9
44 $self->_deserialize
45 0 ▶ if not exists $self->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"} 0 0 0
46 0 return $self->{$elem};
47 }
48  
49 0 ▶sub time_created { $_[0]->_get('t_created') || $_[0]->_get('t') }
Subroutine time_created: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_get("t_created") or $_[0]->_get("t") 0 0 0
50 0 ▶sub time_queued { $_[0]->_get('t') }
Subroutine time_queued: not called CC=1 SCAR=6.9
Pod documented
51 0 ▶sub data { $_[0]->_get('data') }
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
52 0 ▶sub requeue_count { $_[0]->_get('rc') || 0 }
Subroutine requeue_count: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_get("rc") or 0 0 0 0
53 0 ▶sub fail_count { $_[0]->_get('fc') || 0 }
Subroutine fail_count: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_get("fc") or 0 0 0 0
54 0 ▶sub last_error { $_[0]->_get('error') }
Subroutine last_error: not called CC=1 SCAR=6.9
Pod documented
55  
56 sub inc_nr_requeues {
57 0 ▶ my $self = shift;
Subroutine inc_nr_requeues: not called CC=1 SCAR=6.9
Pod documented
58 0 my $plain = $deserializer->decode($self->{_serialized});
59 0 $self->{rc} = ++$plain->{rc};
60 0 $self->{_serialized} = $serializer->encode($plain);
61 0 return $self->{rc};
62 }
63  
64 sub _deserialize {
65 0 ▶ my $self = shift;
Subroutine _deserialize: not called CC=3 SCAR=24.8
66 0 my $plain = $deserializer->decode($self->{_serialized});
67 0 $self->{data} = $plain->{b};
68 0 for my $k (@item_info) {
69 0 ▶ if (exists $plain->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $plain->{$k}) { } 0 0 0
70 0 $self->{$k} = $plain->{$k};
71 }
72 else {
73 0 delete $self->{$k};
74 }
75 }
76 }
77 1 1;
78 __END__
« Q/NaiveFIFO/Redis.pm ↑ Summary Q/DistFIFO.pm »