File Coverage

blib/lib/Brackup/ChunkIterator.pm
Criterion Covered Total %
statement 54 54 100.0
branch 8 8 100.0
condition n/a
subroutine 13 13 100.0
pod 0 3 0.0
total 75 78 96.1


line stmt bran cond sub pod time code
1             package Brackup::ChunkIterator;
2 13     13   63 use strict;
  13         26  
  13         402  
3 13     13   67 use warnings;
  13         23  
  13         389  
4 13     13   67 use Carp qw(croak);
  13         22  
  13         4824  
5              
6             sub new {
7 8     8 0 39 my ($class, @files) = @_;
8 8         84 return bless {
9             filelist => \@files,
10             chunkmag => [],
11             }, $class;
12             }
13              
14             # returns either PositionedChunks, or, in cases of files
15             # without contents (like directories/symlinks), returns
16             # File objects... returns undef on end of files/chunks.
17             sub next {
18 154     154 0 2252 my $self = shift;
19              
20             # magazine already loaded? fire.
21 154         635 my $next = shift @{ $self->{chunkmag} };
  154         1298  
22 154 100       665 return $next if $next;
23              
24             # else reload...
25 136 100       207 my $file = shift @{ $self->{filelist} } or
  136         1826  
26             return undef;
27              
28 110         5387 ($next, @{$self->{chunkmag}}) = $file->chunks;
  110         1490  
29 110 100       1143 return $next if $next;
30 20         436 return $file;
31             }
32              
33             sub mux_into {
34 3     3 0 7 my ($self, $n_copies) = @_;
35 3         5 my @iters;
36 3         15 for (1..$n_copies) {
37 6         55 push @iters, Brackup::ChunkIterator::SlaveIterator->new;
38             }
39             my $on_empty = sub {
40 67     67   1234 my $next = $self->next;
41 67         681 foreach my $peer (@iters) {
42 134         445 push @{$peer->{mag}}, $next;
  134         788  
43             }
44 3         34 };
45 3         10 foreach (@iters) {
46 6         43 $_->{on_empty} = $on_empty;
47             }
48 3         17 return @iters;
49             }
50              
51             package Brackup::ChunkIterator::SlaveIterator;
52 13     13   73 use strict;
  13         26  
  13         393  
53 13     13   66 use warnings;
  13         22  
  13         364  
54 13     13   63 use Carp qw(croak);
  13         25  
  13         2108  
55              
56             sub new {
57 6     6   11 my $class = shift;
58 6         56 return bless {
59             'on_empty' => undef, # subref
60             'mag' => [],
61             }, $class;
62             }
63              
64             sub next {
65 116     116   2144 my $self = shift;
66             # the magazine itself could be true, but contain only undef: (undef),
67             # which signals the end.
68 116 100       218 return shift @{$self->{mag}} if @{$self->{mag}};
  49         1562  
  116         503  
69 67         707 $self->{on_empty}->();
70 67         130 return shift @{$self->{mag}};
  67         479  
71             }
72              
73             sub behind_by {
74 43     43   80 my $self = shift;
75 43         72 return scalar @{$self->{mag}};
  43         267  
76             }
77              
78             1;