WARC/Record/FromVolume.pm

filter: Statementstmt 98.6% 151 / 153 Branchbran 80.4% 66 / 82 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 24 / 24 Podpod 100% 13 / 13 totaltotal 93.8% 274 / 292 CC 46 SCAR 21.3
CC
46
Cov
93%
_read
18.1
next
12.5
compareTo
6.0
CRAP 8.4
« WARC/Index/Entry.pm ↑ Summary WARC/Index/Volatile.pm »
1 package WARC::Record::FromVolume; # -*- CPerl -*-
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 our @ISA = qw(WARC::Record);
7 28 our @CARP_NOT = (@ISA, qw(WARC::Volume WARC::Record::Stub));
8  
9 28 ▶use WARC; *WARC::Record::FromVolume::VERSION = \$WARC::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Fcntl 'SEEK_SET';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Symbol 'geniosym';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 28 ▶use IO::Uncompress::Gunzip '$GunzipError';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 28 require WARC::Fields;
17 28 require WARC::Record;
18 28 require WARC::Record::Block;
19 28 require WARC::Record::Replay;
20  
21 # The overload to a method call is inherited.
22 sub compareTo {
23 134 ▶ my $a = shift;
Subroutine compareTo: called CC=6 SCAR=17.9
Pod documented
24 134 my $b = shift;
25 134 my $swap = shift;
26  
27 # sort in-memory-only records ahead of on-disk records
28 134 ▶ return $swap ? -1 : 1 unless defined $b->volume;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $b->volume 3 131 134
$swap ? : 1 2 3
29  
30 131 ▶ my $cmp =
Branch: 1 branch
BranchTrueFalseTotal
$a->volume->filename eq $b->volume->filename || $a->volume->_file_tag eq $b->volume->_file_tag ? : 119 12 131
Condition100%3/3
$a->volume->filename eq $b->volume->filename or $a->volume->_file_tag eq $b->volume->_file_tag
l!l&&r!l&&!r
118112
MC/DC100%2/2
$a->volume->filename eq $b->volume->filename or $a->volume->_file_tag eq $b->volume->_file_tag
$a->volume->filename eq $b->volume->filename $a->volume->_file_tag eq $b->volume->_file_tag
Truth table100%3/3
A or B
A= $a->volume->filename eq $b->volume->filenameB= $a->volume->_file_tag eq $b->volume->_file_tag
ABresult
1X1
011
000
31 ((($a->volume->filename eq $b->volume->filename)
32 || ($a->volume->_file_tag eq $b->volume->_file_tag))
33 ? ($a->offset <=> $b->offset)
34 : ($a->volume->filename cmp $b->volume->filename));
35  
36 131 ▶ return $swap ? 0-$cmp : 0+$cmp;
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 1 130 131
37 }
38  
39 # This implementation uses a hash as the underlying structure.
40  
41 # Keys inherited from WARC::Record base class:
42 #
43 # fields
44  
45 # Keys defined by this class:
46 #
47 # volume
48 # Parent WARC::Volume object
49 # collection (optional)
50 # Parent WARC::Collection object, if record found via a collection
51 # offset
52 # Offset of start-of-record within parent volume
53 # compression
54 # Name of decompression filter used with this record
55 # data_offset
56 # Offset of data block within record (possibly compressed)
57 # payload_offset (optional)
58 # Offset of record payload within data block
59 # (Defined by this class, but only set upon protocol replay.)
60 # payload_encodings (optional)
61 # Array of transfer encodings, an undefined value, and content
62 # encodings for the payload data for this record.
63 # (Defined by this class, but only set upon protocol replay.)
64 # (NOT YET IMPLEMENTED)
65 # sl_packed_size
66 # Size of compressed data block according to "sl" gzip extension
67 # sl_full_size
68 # Size of uncompressed data block according to "sl" gzip extension
69 # protocol
70 # WARC version found at start of record
71 # logical (optional)
72 # Weak reference to logical record object containing this segment
73 # (Defined by this class, but only set by WARC::Record::Logical.)
74  
75 # Keys tested by logical record heuristics:
76 #
77 # compression
78 # defined iff record is compressed
79 # sl_packed_size
80 # defined iff compressed record can be skipped without reading data block
81  
82 # Keys used in index writers:
83 #
84 # sl_packed_size
85 # used for "S" field in CDX indexes
86  
87 1763 ▶sub DESTROY { our $_total_destroyed; $_total_destroyed++ }
Subroutine DESTROY: called CC=1 SCAR=0.0
88  
89 sub _dbg_dump {
90 36 ▶ my $self = shift;
Subroutine _dbg_dump: called CC=4 SCAR=13.9
91  
92 36 my $out = 'WARC '.$self->field('WARC-Type').' record ['.$self->protocol.']';
93 36 ▶ $out .= ' [via '.$self->{compression}.']' if defined $self->{compression};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"compression"} 32 4 36
94 36 $out .= "\n";
95  
96 36 $out .= ' id '.$self->id."\n";
97  
98 36 $out .= ' at '.$self->offset.' in '.$self->volume."\n";
99 $out .= ' "sl" header: '.$self->{sl_packed_size}.' packed from '
100 36 ▶ .$self->{sl_full_size}." octets\n" if defined $self->{sl_full_size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"sl_full_size"} 16 20 36
101  
102 36 $out .= ' data begins at offset '.$self->{data_offset};
103 36 ▶ $out .= ' within '.(defined $self->{compression} ? 'record' : 'volume');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'compression'} ? : 32 4 36
104 36 $out .="\n";
105  
106 36 return $out;
107 }
108  
109 sub _get_compression_error {
110 3 ▶ my $self = shift;
Subroutine _get_compression_error: called CC=3 SCAR=11.0
111  
112 3 ▶ if (not defined $self->{compression}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'compression'}) { } 1 2 3
elsif ($self->{'compression'} eq 'IO::Uncompress::Gunzip') { } 1 1 2
113 1 return '(record not compressed)';
114 } elsif ($self->{compression} eq 'IO::Uncompress::Gunzip') {
115 1 return $GunzipError;
116 } else {
117 1 die "unknown compression method";
118 }
119 }
120  
121 1 ▶sub new { croak "WARC records are read from volumes" }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
122  
123 sub block {
124 2 ▶ my $self = shift;
Subroutine block: called CC=2 SCAR=6.9
Pod documented
125  
126 2 ▶ croak "attempt to set block on record from volume" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
127  
128 1 return undef;
129 }
130  
131 sub _read {
132 670 ▶ my $class = shift;
Subroutine _read: called CC=18 SCAR=29.0
133 670 my $volume = shift;
134 670 my $offset = shift;
135  
136 670 ▶ croak "WARC::Record::FromVolume::_read is a class method"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 1 669 670
137 unless defined $class;
138 669 ▶ croak "attempt to read WARC record from undefined volume"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $volume 1 668 669
139 unless defined $volume;
140 668 ▶ croak "attempt to read WARC record from undefined offset"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 667 668
141 unless defined $offset;
142  
143 667 my $handle;
144 667 ▶ if (ref $offset) { # I/O handle passed in instead
Branch: 1 branch
BranchTrueFalseTotal
if (ref $offset) { } 4 663 667
145 4 $handle = $offset;
146 4 $offset = tell $handle;
147 } else { # open new handle and seek to offset
148 663 $handle = $volume->open;
149 663 ▶ seek $handle, $offset, SEEK_SET or die "seek: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $handle, $offset, 0 or die "seek: $!" 0 663 663
150 }
151  
152 667 my %ob = (volume => $volume, offset => $offset);
153  
154 667 my $magic; my $protocol = '';
155 667 ▶ defined(read $handle, $magic, 6) or die "read: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined read($handle, $magic, 6) or die "read: $!" 0 667 667
156 667 ▶ return undef if $magic eq ''; # end-of-file reached
Branch: 1 branch
BranchTrueFalseTotal
if $magic eq "" 29 638 667
157  
158 638 ▶ if ($magic eq 'WARC/1') {
Branch: 2 branches
BranchTrueFalseTotal
if ($magic eq 'WARC/1') { } 546 92 638
elsif (unpack('H4', $magic) eq '1f8b') { } 90 2 92
159 # uncompressed WARC record found ==> pass it on through
160 546 $protocol = $magic;
161 } elsif (unpack('H4', $magic) eq '1f8b') {
162 # gzip signature found ==> check for extension header and stack filter
163  
164 90 ▶ if (unpack('x3C', $magic) & 0x04) { # FLG.FEXTRA is set
Branch: 1 branch
BranchTrueFalseTotal
if (unpack("x3C", $magic) & 4) 35 55 90
165 35 ▶ defined(read $handle, $magic, 6, 6) or die "read: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined read($handle, $magic, 6, 6) or die "read: $!" 0 35 35
166 35 my $xlen = unpack 'v', substr $magic, -2;
167 35 ▶ my $extra; defined(read $handle, $extra, $xlen) or die "read: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined read($handle, $extra, $xlen) or die "read: $!" 0 35 35
168 35 my @extra = unpack '(a2 v/a*)*', $extra;
169 35 $magic .= $extra;
170 # @extra is now (tag => $data)...
171 35 ▶ for (my $i = 0; $i < @extra; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @extra) 30 35 65
172 30 ▶ if ($extra[$i] eq 'sl' and length($extra[1+$i]) == 8)
Branch: 1 branch
BranchTrueFalseTotal
if ($extra[$i] eq "sl" and length $extra[1 + $i] == 8) 20 10 30
Condition100%3/3
$extra[$i] eq "sl" and length $extra[1 + $i] == 8
!ll&&!rl&&r
5520
MC/DC100%2/2
$extra[$i] eq "sl" and length $extra[1 + $i] == 8
$extra[$i] eq "sl" length $extra[1 + $i] == 8
Truth table100%3/3
A and B
A= $extra[$i] eq "sl"B= length $extra[1 + $i] == 8
ABresult
0X0
100
111
173 20 { @ob{qw/sl_packed_size sl_full_size/} = unpack 'VV', $extra[1+$i] }
174 }
175 }
176  
177 90 ▶ $handle = new IO::Uncompress::Gunzip ($handle,
Branch: 1 branch
BranchTrueFalseTotal
$handle = "IO::Uncompress::Gunzip"->new($handle, "Prime", $magic, "MultiStream", 0, "AutoClose", 1, "Transparent", 0) or die "IO::Uncompress::Gunzip: $WARC::Record::FromVolume::GunzipError" 0 90 90
178 Prime => $magic, MultiStream => 0,
179 AutoClose => 1, Transparent => 0)
180 or die "IO::Uncompress::Gunzip: $GunzipError";
181 90 $ob{compression} = 'IO::Uncompress::Gunzip';
182 } else
183 2 { croak "WARC record header not found at offset $offset in $volume\n"
184 ." found [".join(' ', unpack '(H2)*', $magic)."] instead" }
185  
186 # read WARC version
187 636 $protocol .= <$handle>;
188 636 $protocol =~ s/[[:space:]]+$//;
189 # The WARC version read from the file is appended because an
190 # uncompressed WARC record is recognized by the first six bytes of the
191 # WARC version tag, which were transferred to $protocol if found.
192 636 ▶ $protocol =~ m/^WARC/
Branch: 1 branch
BranchTrueFalseTotal
$protocol =~ /^WARC/ or croak("WARC record header not found after decompression\n found [" . join(" ", unpack("(H2)*", $protocol)) . '] instead') 0 636 636
193 or croak "WARC record header not found after decompression\n"
194 ." found [".join(' ', unpack '(H2)*', $protocol)."] instead";
195 636 $ob{protocol} = $protocol;
196  
197 636 $ob{fields} = parse WARC::Fields from => $handle;
198 636 $ob{fields}->set_readonly;
199  
200 636 $ob{data_offset} = tell $handle;
201  
202 636 close $handle;
203  
204 636 { our $_total_read; $_total_read++ }
205  
206 636 bless \%ob, $class;
207 }
208  
209 73 ▶sub protocol { (shift)->{protocol} }
Subroutine protocol: called CC=1 SCAR=0.0
Pod documented
210  
211 1493 ▶sub volume { (shift)->{volume} }
Subroutine volume: called CC=1 SCAR=0.0
Pod documented
212  
213 866 ▶sub offset { (shift)->{offset} }
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
214  
215 sub logical {
216 174 ▶ my $self = shift;
Subroutine logical: called CC=3 SCAR=12.7
Pod documented
217  
218 174 my $segment_header_value = $self->field('WARC-Segment-Number');
219 174 ▶ if (defined $self->{logical}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'logical'}) { } 0 174 174
elsif (defined $segment_header_value) { } 0 174 174
220 0 return $self->{logical}; # cached object remains valid ==> return it
221 } elsif (defined $segment_header_value) {
222 0 return _read WARC::Record::Logical $self;
223 } else {
224 174 return $self; # no continuation records present
225 }
226 }
227  
228 72 ▶sub segments { if (wantarray) { return shift } else { return 1 } }
Subroutine segments: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 36 36 72
229  
230 sub next {
231 380 ▶ my $self = shift;
Subroutine next: called CC=12 SCAR=25.2
Pod documented
232  
233 380 my $next = undef;
234  
235 380 ▶ if ($self->{sl_packed_size}) { # gzip "sl" extended header available
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'sl_packed_size'}) { } 12 368 380
elsif (not defined $self->{'compression'}) { } 336 32 368
236 12 my $handle = $self->volume->open;
237  
238 # seek to read 32-bit ISIZE field at end of gzip stream
239 12 ▶ seek $handle, $self->offset + $self->{sl_packed_size} - 4, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $handle, $self->offset + $self->{"sl_packed_size"} - 4, 0 or die "seek: $!" 0 12 12
240 or die "seek: $!";
241 12 ▶ my $isize; defined(read $handle, $isize, 4) or die "read: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined read($handle, $isize, 4) or die "read: $!" 0 12 12
242  
243 12 ▶ if (length $isize > 0 # read off the end yields nothing
Branch: 1 branch
BranchTrueFalseTotal
if (length $isize > 0 and $self->{'sl_full_size'} == unpack('V', $isize)) { } 4 8 12
Condition100%3/3
length $isize > 0 and $self->{"sl_full_size"} == unpack("V", $isize)
!ll&&!rl&&r
444
MC/DC100%2/2
length $isize > 0 and $self->{"sl_full_size"} == unpack("V", $isize)
length $isize > 0 $self->{"sl_full_size"} == unpack("V", $isize)
Truth table100%3/3
A and B
A= length $isize > 0B= $self->{"sl_full_size"} == unpack("V", $isize)
ABresult
0X0
100
111
244 and $self->{sl_full_size} == unpack 'V', $isize) { # ... and looks valid
245 4 $next = _read WARC::Record::FromVolume $self->volume, $handle;
246 4 close $handle;
247 4 return $next;
248 } else {
249 8 carp "extended 'sl' header was found to be invalid\n"
250 .' in record at '.($self->offset).' in '.($self->volume);
251 }
252 } elsif (not defined $self->{compression}) { # WARC record is not compressed
253 return _read WARC::Record::FromVolume $self->volume,
254 336 $self->{data_offset} + $self->field('Content-Length') + 4;
255 }
256  
257 # if we get here, we have to scan for the end of the record
258 40 my $handle = $self->volume->open;
259 40 ▶ seek $handle, $self->offset, SEEK_SET or die "seek: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $handle, $self->offset, 0 or die "seek: $!" 0 40 40
260  
261 my $zhandle = $self->{compression}->new
262 40 ▶ ($handle, MultiStream => 0, AutoClose => 0)
Branch: 1 branch
BranchTrueFalseTotal
my $zhandle = $self->{"compression"}->new($handle, "MultiStream", 0, "AutoClose", 0) or die qq[$self->{"compression"}: ] . $self->_get_compression_error 0 40 40
263 or die "$self->{compression}: ".$self->_get_compression_error;
264 40 ▶ seek $zhandle, $self->{data_offset} + $self->field('Content-Length'), SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $zhandle, $self->{"data_offset"} + $self->field("Content-Length"), 0 or die "zseek: $! " . $self->_get_compression_error 0 40 40
265 or die "zseek: $! ".$self->_get_compression_error;
266 40 ▶ my $end; defined(read $zhandle, $end, 4)
Branch: 1 branch
BranchTrueFalseTotal
defined read($zhandle, $end, 4) or die "zread: $! " . $self->_get_compression_error 0 40 40
267 or die "zread: $! ".$self->_get_compression_error;
268 40 ▶ croak "end-of-record marker not found" unless $end eq (WARC::CRLF x 2);
Branch: 1 branch
BranchTrueFalseTotal
unless $end eq "\r\n\r\n" 0 40 40
269  
270 # The main handle is somewhere *after* the actual end of the block
271 # because IO::Uncompress::Gunzip reads ahead. We can get the contents
272 # of that "read ahead" buffer and use that to adjust our final offset.
273 40 $next = _read WARC::Record::FromVolume $self->volume,
274 (tell($handle) - length($zhandle->trailingData));
275  
276 40 close $zhandle; close $handle;
277  
278 40 return $next;
279 }
280  
281 sub open_block {
282 140 ▶ my $self = shift;
Subroutine open_block: called CC=1 SCAR=0.0
Pod documented
283  
284 140 my $xhandle = Symbol::geniosym;
285 140 tie *$xhandle, 'WARC::Record::Block', $self;
286  
287 140 return $xhandle;
288 }
289  
290 138 ▶sub open_continued { (shift)->logical->open_block }
Subroutine open_continued: called CC=1 SCAR=0.0
Pod documented
291  
292 sub replay {
293 148 ▶ my $self = shift;
Subroutine replay: called CC=3 SCAR=11.0
Pod documented
294  
295 148 my @handlers = WARC::Record::Replay::find_handlers($self);
296  
297 148 my $result = undef;
298 148 ▶ $result = (shift @handlers)->($self)
Branch: 1 branch
BranchTrueFalseTotal
scalar @handlers and not defined $result and do { $result = shift(@handlers)->($self) } 130 148 278
Condition100%3/3
scalar @handlers and not defined $result
!ll&&!rl&&r
47101130
MC/DC100%2/2
scalar @handlers and not defined $result
scalar @handlers not defined $result
Truth table100%3/3
A and not(B)
A= scalar @handlersB= not defined $result
ABresult
0X0
100
111
299 while scalar @handlers && !defined $result;
300  
301 148 return $result;
302 }
303  
304 sub open_payload {
305 8 ▶ my $self = shift;
Subroutine open_payload: called CC=2 SCAR=7.0
Pod documented
306  
307 8 my $replay = $self->replay;
308 8 ▶ return undef unless $replay; # no payload found
Branch: 1 branch
BranchTrueFalseTotal
unless $replay 0 8 8
309  
310 8 my $xhandle = Symbol::geniosym;
311 8 tie *$xhandle, 'WARC::Record::Payload', $self, $replay;
312  
313 8 return $xhandle;
314 }
315  
316 28 1;
317 __END__
« WARC/Index/Entry.pm ↑ Summary WARC/Index/Volatile.pm »