S3/FileIterator.pm

filter: Statementstmt 90.1% 91 / 101 Branchbran 82.6% 38 / 46 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 90.0% 18 / 20 Podpod 58.3% 7 / 12 totaltotal 85.7% 163 / 190 CC 29 SCAR 18.7
CC
29
Cov
88%
next
12.0
next_page
11.0
_fetch
10.1
CRAP 6.5
« S3/Request/SetBucketAccessControl.pm ↑ Summary S3/Roles/Bucket.pm »
1 package AWS::S3::FileIterator;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use AWS::S3::Owner;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use AWS::S3::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 7 my ($class, %args) = @_;
11  
12 7 my $s = bless {
13 data => [ ],
14 page_number => 0,
15 idx => 0,
16 %args,
17 }, $class;
18 7 $s->_init;
19 4 return $s;
20 }
21  
22 7 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.1
23 7 my ( $s ) = @_;
24  
25 7 foreach ( qw( bucket page_size page_number ) ) {
26 confess "Required argument '$_' was not provided"
27 18 ▶ unless $s->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{$_} 3 15 18
28 } # end foreach()
29  
30 4 $s->{page_number}--;
31 4 ▶ $s->{marker} = '' unless defined( $s->{marker} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s->{"marker"} 3 1 4
32 4 $s->{__fetched_first_page} = 0;
33 4 $s->{data} = [];
34 4 ▶ $s->{pattern} ||= qr(.*);
Condition100%2/2
$s->{"pattern"} ||= qr/.*/
l!l
22
MC/DC100%1/1
$s->{"pattern"} ||= qr/.*/
$s->{"pattern"}
Truth table100%2/2
A
A= $s->{"pattern"}
Aresult
11
00
35 } # end _init()
36  
37 2 ▶sub marker { shift->{marker} }
Subroutine marker: called CC=1 SCAR=0.0
Pod documented
38 19 ▶sub pattern { shift->{pattern} }
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
39 1 ▶sub bucket { shift->{bucket} }
Subroutine bucket: called CC=1 SCAR=0.0
Pod undocumented
40 12 ▶sub page_size { shift->{page_size} }
Subroutine page_size: called CC=1 SCAR=0.0
Pod undocumented
41 2 ▶sub prefix { shift->{prefix} }
Subroutine prefix: called CC=1 SCAR=0.0
Pod documented
42  
43 1 ▶sub has_prev {
Subroutine has_prev: called CC=1 SCAR=0.0
Pod documented
44 1 my $s = shift;
45  
46 1 return $s->page_number > 1;
47 } # end has_prev()
48  
49 1 ▶sub has_next { shift->{has_next} }
Subroutine has_next: called CC=1 SCAR=0.0
Pod documented
50  
51 0 ▶sub next {
Subroutine next: not called CC=3 SCAR=24.8
Pod undocumented
52 0 my $s = shift;
53  
54 0 ▶ if( exists( $s->{data}->[ $s->{idx} ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->{'data'}[$s->{'idx'}]) { } 0 0 0
55 0 return $s->{data}->[ $s->{idx}++ ];
56 } else {
57 # End of the current resultset, see if we can get another page of records:
58 0 ▶ if( my $page = $s->next_page ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page = $s->next_page) { } 0 0 0
59 0 $s->{data} = $page;
60 0 $s->{idx} = 0;
61 0 return $s->{data}->[ $s->{idx}++ ];
62 } else {
63 # No more pages, no more data:
64 0 return;
65 }
66 }
67 }
68  
69 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
70 0 my $s = shift;
71 0 $s->{idx} = 0;
72 }
73  
74  
75 6 ▶sub page_number {
Subroutine page_number: called CC=2 SCAR=6.9
Pod documented
76 6 my $s = shift;
77 6 ▶ @_ ? $s->{page_number} = $_[0] - 1 : $s->{page_number};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 5 6
78 } # end page_number()
79  
80 # S3 returns files 100 at a time. If we want more or less than 100, we can't
81 # just fetch the next page over and over - that would be inefficient and likely
82 # to cause errors.
83  
84 # If the page size is 5 and page number is 2, then we:
85 # - fetch 100 items
86 # - store them
87 # - iterate internally until we get to 'page 2'
88 # - return the result.
89 # If the page size is 105 and page number is 1, then we:
90 # - fetch 100 items
91 # - fetch the next 100 items
92 # - return the first 105 items, keeping the remaining 95 items
93 # - on page '2', fetch the next 100 items and return 105 items, saving 90 items.
94 # If the page size is 105 and page number is 3, then we:
95 # - fetch items until our internal 'start' marker is 316-420
96 # - return items 316-420
97 11 ▶sub next_page {
Subroutine next_page: called CC=11 SCAR=24.0
Pod documented
98 11 my $s = shift;
99  
100 # Advance to page X before proceding:
101 11 ▶ if ( ( !$s->{__fetched_first_page}++ ) && $s->page_number ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $s->{"__fetched_first_page"}++ and $s->page_number) 1 10 11
Condition100%3/3
not $s->{"__fetched_first_page"}++ and $s->page_number
!ll&&!rl&&r
911
MC/DC100%2/2
not $s->{"__fetched_first_page"}++ and $s->page_number
not $s->{"__fetched_first_page"}++ $s->page_number
Truth table100%3/3
not(A) and B
A= not $s->{"__fetched_first_page"}++B= $s->page_number
ABresult
0X0
100
111
102  
103 # Advance to $s->page_number
104 1 my $start_page = $s->page_number;
105 1 my $to_discard = $start_page * $s->page_size;
106 1 my $discarded = 0;
107 1 while ( 1 ) {
108 6 ▶ my $item = $s->_next
Branch: 1 branch
BranchTrueFalseTotal
my $item = $s->_next or last 6 0 6
109 or last;
110 6 ▶ $discarded++ if $item->{key} =~ $s->pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"key"} =~ $s->pattern 5 1 6
111 6 ▶ last if $discarded > $to_discard;
Branch: 1 branch
BranchTrueFalseTotal
if $discarded > $to_discard 1 5 6
112 } # end while()
113 } # end if()
114  
115 11 my @chunk = ();
116 11 ▶ while ( my $item = $s->_next() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $s->_next) 11 1 12
117 11 ▶ next unless $item->{key} =~ $s->pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"key"} =~ $s->pattern 1 10 11
118 10 push @chunk, $item;
119 10 ▶ last if @chunk == $s->page_size;
Branch: 1 branch
BranchTrueFalseTotal
if @chunk == $s->page_size 10 0 10
120 } # end while()
121  
122 my @out = map {
123 11 my $owner = AWS::S3::Owner->new( %{ $_->{owner} } );
124 10 delete $_->{owner};
125 10 AWS::S3::File->new( %$_, owner => $owner );
126 } @chunk;
127  
128 11 $s->{page_number}++;
129  
130 11 ▶ return unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 1 10 11
131 10 ▶ wantarray ? @out : \@out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 9 10
132 } # end next_page()
133  
134 18 ▶sub _next {
Subroutine _next: called CC=3 SCAR=11.0
135 18 my $s = shift;
136  
137 18 ▶ if ( my $item = shift( @{ $s->{data} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @{$$s{'data'};}) { } 13 5 18
138 13 return $item;
139 } else {
140 5 ▶ if ( my @chunk = $s->_fetch() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@chunk) = $s->_fetch) { } 4 1 5
141 4 push @{ $s->{data} }, @chunk;
142 4 return shift( @{ $s->{data} } );
143 } else {
144 1 return;
145 } # end if()
146 } # end if()
147 } # end _next()
148  
149 5 ▶sub _fetch {
Subroutine _fetch: called CC=10 SCAR=23.1
150 5 my ( $s ) = @_;
151  
152 5 my $path = $s->{bucket}->name . '/';
153 5 my %params = ();
154 5 ▶ $params{marker} = $s->{marker} if $s->{marker};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"marker"} 3 2 5
155 5 ▶ $params{prefix} = $s->{prefix} if $s->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"prefix"} 3 2 5
156 5 $params{max_keys} = 1000;
157 5 ▶ $params{delimiter} = $s->{delimiter} if $s->{delimiter};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"delimiter"} 0 5 5
158  
159 5 my $type = 'ListBucket';
160 5 my $request = $s->{bucket}->s3->request( $type, %params, bucket => $s->{bucket}->name );
161 5 my $response = $request->request();
162  
163 5 ▶ $s->{has_next} = ( $response->xpc->findvalue( '//s3:IsTruncated' ) || '' ) eq 'true' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($response->xpc->findvalue('//s3:IsTruncated') || '') eq 'true' ? : 2 3 5
Condition50%1/2
$response->xpc->findvalue('//s3:IsTruncated') || ''
l!l
50
MC/DC0%0/1
$response->xpc->findvalue('//s3:IsTruncated') || ''
$response->xpc->findvalue('//s3:IsTruncated')
Truth table50%1/2
A
A= $response->xpc->findvalue('//s3:IsTruncated')
Aresult
11
00
164  
165 5 my @files = ();
166 5 foreach my $node ( $response->xpc->findnodes( '//s3:Contents' ) ) {
167 28 my ( $owner_node ) = $response->xpc->findnodes( './/s3:Owner', $node );
168 28 my $owner = {
169 id => $response->xpc->findvalue( './/s3:ID', $owner_node ),
170 display_name => $response->xpc->findvalue( './/s3:DisplayName', $owner_node )
171 };
172 28 my $etag = $response->xpc->findvalue( './/s3:ETag', $node );
173 push @files,
174 {
175 bucket => $s->{bucket},
176 28 key => $response->xpc->findvalue( './/s3:Key', $node ),
177 lastmodified => $response->xpc->findvalue( './/s3:LastModified', $node ),
178 etag => $response->xpc->findvalue( './/s3:ETag', $node ),
179 size => $response->xpc->findvalue( './/s3:Size', $node ),
180 owner => $owner,
181 };
182 } # end foreach()
183  
184 5 ▶ if ( @files ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 4 1 5
185 4 $s->{marker} = $files[-1]->{key};
186 } # end if()
187  
188 5 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 5 5
189 5 ▶ @files ? return @files : return;
Branch: 1 branch
BranchTrueFalseTotal
@files ? : 4 1 5
190 } # end _fetch()
191  
192 6 1;
193  
194 __END__
« S3/Request/SetBucketAccessControl.pm ↑ Summary S3/Roles/Bucket.pm »