| 1 | package Data::Queue::Batch; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | our $VERSION = "0.02"; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub new { | ||||||||||||||||||||||||||||
| 9 | 20 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 20 | ▶ | my $batch_size = delete($args{batch_size}) || 100; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{'batch_size'} || 100
MC/DC0%0/1
delete $args{'batch_size'} || 100
delete $args{'batch_size'}
Truth table50%1/2
A
A= delete $args{'batch_size'}
| |||||||||||||||||||||||||||||
| 11 | 20 | my $callback = delete($args{callback}); | |||||||||||||||||||||||||||
| 12 | 20 | return bless { | |||||||||||||||||||||||||||
| 13 | callback => $callback, | ||||||||||||||||||||||||||||
| 14 | batch_size => $batch_size,, | ||||||||||||||||||||||||||||
| 15 | %args, | ||||||||||||||||||||||||||||
| 16 | available => 0, | ||||||||||||||||||||||||||||
| 17 | _queue => [], | ||||||||||||||||||||||||||||
| 18 | }, $class; | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 95 | ▶ | sub size { scalar(@{ shift->{_queue} }) } | ||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub available { shift->{available} } | ||||||||||||||||||||||||||
|
Subroutine
available: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub batch_size { shift->{batch_size} } | ||||||||||||||||||||||||||
|
Subroutine
batch_size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 51 | ▶ | sub push { shift->enqueue(@_) } | ||||||||||||||||||||||||||
|
Subroutine
push: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | sub enqueue { | ||||||||||||||||||||||||||||
| 27 | 56 | ▶ | my ($self, @values) = @_; | ||||||||||||||||||||||||||
|
Subroutine
enqueue: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 56 | CORE::push(@{ $self->{_queue} }, @values); | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 56 | my $unmarked = $self->size - $self->{available}; | |||||||||||||||||||||||||||
| 31 | 56 | my $marking = $unmarked - ($unmarked % $self->{batch_size}); | |||||||||||||||||||||||||||
| 32 | 56 | $self->{available} += $marking; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 56 | ▶ | if ($self->{callback} && $self->{available}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"callback"} and $self->{"available"}
MC/DC100%2/2
$self->{"callback"} and $self->{"available"}
$self->{"callback"}
$self->{"available"}
Truth table100%3/3
A and B
A= $self->{"callback"}B= $self->{"available"}
| |||||||||||||||||||||||||||||
| 35 | 19 | $self->{callback}->($self->_take($self->{available})); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 56 | return; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 23 | ▶ | sub shift { shift->dequeue(@_) } | ||||||||||||||||||||||||||
|
Subroutine
shift: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | sub dequeue { | ||||||||||||||||||||||||||||
| 42 | 28 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dequeue: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 28 | ▶ | return unless $self->{available}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 15 | my ($dequeued) = $self->_take(1); | |||||||||||||||||||||||||||
| 45 | 15 | return $dequeued; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub peek { | ||||||||||||||||||||||||||||
| 49 | 4 | ▶ | my ($self, $count) = @_; | ||||||||||||||||||||||||||
|
Subroutine
peek: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 4 | ▶ | $count = $self->{available} if $count > $self->{available}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 4 | my @peeked = @{$self->{_queue}}[0 .. $count - 1]; | |||||||||||||||||||||||||||
| 52 | 4 | return @peeked; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub flush { | ||||||||||||||||||||||||||||
| 56 | 20 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 20 | my @taken = $self->_take($self->size); | |||||||||||||||||||||||||||
| 58 | 20 | ▶ | if ($self->{callback} && @taken) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"callback"} and @taken
MC/DC100%2/2
$self->{"callback"} and @taken
$self->{"callback"}
@taken
Truth table100%3/3
A and B
A= $self->{"callback"}B= @taken
| |||||||||||||||||||||||||||||
| 59 | 17 | $self->{callback}->(@taken); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 20 | return @taken; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub clear { | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 1 | $self->_take($self->size); | |||||||||||||||||||||||||||
| 67 | 1 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub _take { | ||||||||||||||||||||||||||||
| 71 | 55 | ▶ | my ($self, $count) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_take: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 72 | 55 | my @taken = splice(@{ $self->{_queue} }, 0, $count); | |||||||||||||||||||||||||||
| 73 | 55 | $self->{available} -= $count; | |||||||||||||||||||||||||||
| 74 | 55 | ▶ | $self->{available} = 0 if $self->{available} < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 55 | return @taken; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub DESTROY { | ||||||||||||||||||||||||||||
| 79 | 20 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 20 | ▶ | $self->flush if $self->{callback}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 4 | 1; | |||||||||||||||||||||||||||
| 84 | __END__ | ||||||||||||||||||||||||||||