| 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 | 3 | ▶ | use Class::XSAccessor { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | getters => [qw(server port queue_name db _redis_conn)], | |||||||||||||||||||
| 18 | 3 | }; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 21 | 0 | my ($class, %params) = @_; | |||||||||||||||||||
| 22 | 0 | for (qw(server port queue_name)) { | |||||||||||||||||||
| 23 | croak("Need '$_' parameter") | ||||||||||||||||||||
| 24 | 0 | ▶ | if not defined $params{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | my $self = bless({ | ||||||||||||||||||||
| 28 | 0 | ▶ | (map {$_ => $params{$_}} qw(server port queue_name) ), | ||||||||||||||||||
|
Condition0%0/2
$params{'db'} || 0
MC/DC0%0/1
$params{'db'} || 0
$params{'db'}
Truth table0%0/2
A
A= $params{'db'}
| |||||||||||||||||||||
| 29 | 0 | db => $params{db} || 0, | |||||||||||||||||||
| 30 | _redis_conn => undef, | ||||||||||||||||||||
| 31 | } => $class); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | $self->{_redis_conn} = Redis->new( | ||||||||||||||||||||
| 34 | 0 | ▶ | %{$params{redis_options} || {}}, | ||||||||||||||||||
|
Condition0%0/2
$params{'redis_options'} || {}
MC/DC0%0/1
$params{'redis_options'} || {}
$params{'redis_options'}
Truth table0%0/2
A
A= $params{'redis_options'}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | return $self; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 0 | ▶ | sub enqueue_item { | ||||||||||||||||||
|
Subroutine
enqueue_item: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 45 | 0 | my $self = shift; | |||||||||||||||||||
| 46 | 0 | ▶ | croak("Need exactly one item to enqeue") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | if not @_ == 1; | ||||||||||||||||||||
| 48 | 0 | my ($blob) = $self->_serialize($_[0]); | |||||||||||||||||||
| 49 | 0 | $self->_redis_conn->lpush($self->queue_name, $blob); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | sub enqueue_items { | ||||||||||||||||||
|
Subroutine
enqueue_items: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||
| 54 | 0 | ▶ | return if not @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub claim_item { | ||||||||||||||||||
|
Subroutine
claim_item: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 62 | 0 | my ($self) = @_; | |||||||||||||||||||
| 63 | 0 | my ($rv) = $self->_deserialize( $self->_redis_conn->rpop($self->queue_name) ); | |||||||||||||||||||
| 64 | 0 | return $rv; | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub claim_items { | ||||||||||||||||||
|
Subroutine
claim_items: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | my ($self, $n) = @_; | |||||||||||||||||||
| 69 | 0 | ▶ | $n ||= 1; | ||||||||||||||||||
|
Condition0%0/2
$n ||= 1
MC/DC0%0/1
$n ||= 1
$n
Truth table0%0/2
A
A= $n
| |||||||||||||||||||||
| 70 | 0 | my $conn = $self->_redis_conn; | |||||||||||||||||||
| 71 | 0 | my $qn = $self->queue_name; | |||||||||||||||||||
| 72 | 0 | ▶ | if ($n > 100) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | my ($l) = $self->_redis_conn->llen($qn); | |||||||||||||||||||
| 74 | 0 | ▶ | $n = $l if $l < $n; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub flush_queue { | ||||||||||||||||||
|
Subroutine
flush_queue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 83 | 0 | my $self = shift; | |||||||||||||||||||
| 84 | 0 | $self->_redis_conn->del($self->queue_name); | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 0 | ▶ | sub queue_length { | ||||||||||||||||||
|
Subroutine
queue_length: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 88 | 0 | my $self = shift; | |||||||||||||||||||
| 89 | 0 | my ($len) = $self->_redis_conn->llen($self->queue_name); | |||||||||||||||||||
| 90 | 0 | return $len; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | sub _serialize { | ||||||||||||||||||
|
Subroutine
_serialize: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 94 | 0 | my $self = shift; | |||||||||||||||||||
| 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})
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})
| |||||||||||||||||||||
| 96 | 0 | return map $SerealEncoder->encode($_), @_; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | ▶ | sub _deserialize { | ||||||||||||||||||
|
Subroutine
_deserialize: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 100 | 0 | my $self = shift; | |||||||||||||||||||
| 101 | 0 | ▶ | $SerealDecoder ||= Sereal::Decoder->new(); | ||||||||||||||||||
|
Condition0%0/3
$Queue::Q::NaiveFIFO::Redis::SerealDecoder ||= "Sereal::Decoder"->new
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
| |||||||||||||||||||||
| 102 | 0 | ▶ | return map defined($_) ? $SerealDecoder->decode($_) : $_, @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 3 | 1; | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||