Q/NaiveFIFO/Redis.pm

filter: Statementstmt 40.0% 30 / 75 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 47.3% 9 / 19 Podpod 100% 7 / 7 totaltotal 34.5% 46 / 133 CC 15 SCAR 27.0
CC
15
Cov
30%
new
42.0
claim_items
30.0
_deserialize
12.0
CRAP 14.9
« Q/ClaimFIFO/Redis.pm ↑ Summary Q/ReliableFIFO/Item.pm »
1 package Queue::Q::NaiveFIFO::Redis;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Queue::Q::NaiveFIFO;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use parent 'Queue::Q::NaiveFIFO';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Sereal::Encoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Sereal::Decoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $SerealEncoder;
14 3 our $SerealDecoder;
15  
16 use Class::XSAccessor {
17 3 getters => [qw(server port queue_name db _redis_conn)],
18 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub new {
21 0 ▶ my ($class, %params) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
22 0 for (qw(server port queue_name)) {
23 croak("Need '$_' parameter")
24 0 ▶ if not defined $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{$_} 0 0 0
25 }
26  
27 my $self = bless({
28 0 ▶ (map {$_ => $params{$_}} qw(server port queue_name) ),
Condition0%0/2
$params{'db'} || 0
l!l
00
MC/DC0%0/1
$params{'db'} || 0
$params{'db'}
Truth table0%0/2
A
A= $params{'db'}
Aresult
11
00
29 0 db => $params{db} || 0,
30 _redis_conn => undef,
31 } => $class);
32  
33 $self->{_redis_conn} = Redis->new(
34 0 ▶ %{$params{redis_options} || {}},
Branch: 1 branch
BranchTrueFalseTotal
$params{"redis_options"} or {} 0 0 0
35 encoding => undef, # force undef for binary data
36 server => join(":", $self->server, $self->port),
37 );
38  
39 0 ▶ $self->_redis_conn->select($self->db) if $self->db;
Branch: 1 branch
BranchTrueFalseTotal
if $self->db 0 0 0
40  
41 0 return $self;
42 }
43  
44 sub enqueue_item {
45 0 ▶ my $self = shift;
Subroutine enqueue_item: not called CC=2 SCAR=17.9
Pod documented
46 0 ▶ croak("Need exactly one item to enqeue")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
47 if not @_ == 1;
48 0 my ($blob) = $self->_serialize($_[0]);
49 0 $self->_redis_conn->lpush($self->queue_name, $blob);
50 }
51  
52 sub enqueue_items {
53 0 ▶ my $self = shift;
Subroutine enqueue_items: not called CC=2 SCAR=17.9
Pod documented
54 0 ▶ return if not @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
55 0 my $qn = $self->queue_name;
56 0 my $conn = $self->_redis_conn;
57 0 my @blobs = $self->_serialize(@_);
58 0 $conn->lpush($qn, @blobs);
59 }
60  
61 sub claim_item {
62 0 ▶ my ($self) = @_;
Subroutine claim_item: not called CC=1 SCAR=6.9
Pod documented
63 0 my ($rv) = $self->_deserialize( $self->_redis_conn->rpop($self->queue_name) );
64 0 return $rv;
65 }
66  
67 sub claim_items {
68 0 ▶ my ($self, $n) = @_;
Subroutine claim_items: not called CC=5 SCAR=34.0
Pod documented
69 0 ▶ $n ||= 1;
Condition0%0/2
$n ||= 1
l!l
00
MC/DC0%0/1
$n ||= 1
$n
Truth table0%0/2
A
A= $n
Aresult
11
00
70 0 my $conn = $self->_redis_conn;
71 0 my $qn = $self->queue_name;
72 0 ▶ if ($n > 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 100) 0 0 0
73 0 my ($l) = $self->_redis_conn->llen($qn);
74 0 ▶ $n = $l if $l < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $l < $n 0 0 0
75 }
76 0 my @elem;
77 0 ▶ $conn->rpop($qn, sub {push @elem, $_[0]}) for 1..$n;
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 $conn->wait_all_responses;
79 0 return $self->_deserialize( grep defined, @elem );
80 }
81  
82 sub flush_queue {
83 0 ▶ my $self = shift;
Subroutine flush_queue: not called CC=1 SCAR=6.9
Pod documented
84 0 $self->_redis_conn->del($self->queue_name);
85 }
86  
87 sub queue_length {
88 0 ▶ my $self = shift;
Subroutine queue_length: not called CC=1 SCAR=6.9
Pod documented
89 0 my ($len) = $self->_redis_conn->llen($self->queue_name);
90 0 return $len;
91 }
92  
93 sub _serialize {
94 0 ▶ my $self = shift;
Subroutine _serialize: not called CC=2 SCAR=17.9
95 0 ▶ $SerealEncoder ||= Sereal::Encoder->new({stringify_undef => 1, warn_undef => 1});
Condition0%0/3
$Queue::Q::NaiveFIFO::Redis::SerealEncoder ||= "Sereal::Encoder"->new({"stringify_undef", 1, "warn_undef", 1})
l!l&&r!l&&!r
000
MC/DC0%0/2
$Queue::Q::NaiveFIFO::Redis::SerealEncoder ||= "Sereal::Encoder"->new({"stringify_undef", 1, "warn_undef", 1})
$Queue::Q::NaiveFIFO::Redis::SerealEncoder "Sereal::Encoder"->new({"stringify_undef", 1, "warn_undef", 1})
Truth table0%0/3
A ||= B
A= $Queue::Q::NaiveFIFO::Redis::SerealEncoderB= "Sereal::Encoder"->new({"stringify_undef", 1, "warn_undef", 1})
ABresult
1X1
011
000
96 0 return map $SerealEncoder->encode($_), @_;
97 }
98  
99 sub _deserialize {
100 0 ▶ my $self = shift;
Subroutine _deserialize: not called CC=3 SCAR=24.8
101 0 ▶ $SerealDecoder ||= Sereal::Decoder->new();
Condition0%0/3
$Queue::Q::NaiveFIFO::Redis::SerealDecoder ||= "Sereal::Decoder"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Queue::Q::NaiveFIFO::Redis::SerealDecoder ||= "Sereal::Decoder"->new
$Queue::Q::NaiveFIFO::Redis::SerealDecoder "Sereal::Decoder"->new
Truth table0%0/3
A ||= B
A= $Queue::Q::NaiveFIFO::Redis::SerealDecoderB= "Sereal::Decoder"->new
ABresult
1X1
011
000
102 0 ▶ return map defined($_) ? $SerealDecoder->decode($_) : $_, @_;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
103 }
104  
105 3 1;
106  
107 __END__
« Q/ClaimFIFO/Redis.pm ↑ Summary Q/ReliableFIFO/Item.pm »