WARC/Index/File/CDX.pm

filter: Statementstmt 100% 139 / 139 Branchbran 96.6% 58 / 60 Conditioncond 100% 11 / 12 MC/DCmcdc 100% 7 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 99.1% 236 / 240 CC 35 SCAR 16.1
CC
35
Cov
99%
attach
8.0
_search_best_match
8.0
entry_at
5.0
CRAP 5.0
« WARC/Index.pm ↑ Summary WARC/Fields.pm »
1 package WARC::Index::File::CDX; # -*- CPerl -*-
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 require WARC::Index;
7 1 our @ISA = qw(WARC::Index);
8  
9 1 require WARC; *WARC::Index::File::CDX::VERSION = \$WARC::VERSION;
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Fcntl 'SEEK_SET';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 require File::Spec::Unix;
15  
16 1 require WARC::Date;
17 1 require WARC::Index::File::CDX::Entry;
18 1 require WARC::Volume;
19  
20 1 WARC::Index::register(filename => qr/[.]cdx$/);
21  
22 1 our %CDX_Field_Index_Key_Map =
23 (a => 'url', b => 'time', u => 'record_id');
24  
25 our %CDX_Import_Map =
26 (time => sub {
27 19 ▶ my $cdx_date = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
28 19 ▶ croak "invalid CDX datestamp"
Branch: 1 branch
BranchTrueFalseTotal
unless $cdx_date =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/ 0 19 19
29 unless $cdx_date =~ m/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/;
30 19 return WARC::Date->from_string(sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',
31 $1, $2, $3, $4, $5, $6))},
32 1 );
33  
34 # This implementation uses a hash as the underlying structure.
35 # Keys defined by this class:
36 #
37 # cdx_file
38 # file name of CDX file
39 # delimiter
40 # delimiter character used in CDX file
41 # fields
42 # array of CDX field codes, in order
43 # field_index
44 # hash mapping CDX field code letter => position
45 # key_index
46 # hash mapping WARC::Index search keys => position
47 # volumes
48 # hash mapping WARC file names => WARC::Volume objects
49  
50 sub _dbg_dump {
51 6 ▶ my $self = shift;
Subroutine _dbg_dump: called CC=1 SCAR=0.0
52  
53 6 my $out = __PACKAGE__ . "\n in " . $self->{cdx_file} . "\n";
54 $out .= sprintf ' delimiter ASCII %d/%d ',
55 6 (ord $self->{delimiter})/16, (ord $self->{delimiter})%16;
56 6 $out .= ' '.join(' ', 'CDX', @{$self->{fields}})."\n";
57  
58 6 return $out;
59 }
60  
61 sub attach {
62 13 ▶ my $class = shift;
Subroutine attach: called CC=8 SCAR=20.8
Pod documented
63 13 my $cdx_file = shift;
64  
65 13 local $/ = "\012"; # ASCII 0/10 LF
66 13 ▶ open my $cdx, '<', $cdx_file or croak "$cdx_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cdx, "<", $cdx_file or croak("${cdx_file}: $!") 1 12 13
67 12 binmode $cdx, ':raw';
68  
69 12 my $header = <$cdx>;
70 12 ▶ croak "could not read CDX header line from $cdx_file: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 1 11 12
71 unless defined $header;
72  
73 11 chomp $header;
74 11 my $delimiter = substr $header, 0, 1;
75 11 ▶ croak "no CDX marker found in $cdx_file"
Branch: 1 branch
BranchTrueFalseTotal
unless "CDX" eq substr($header, 1, 3) and $delimiter eq substr($header, 4, 1) 2 9 11
Condition100%3/3
"CDX" eq substr($header, 1, 3) and $delimiter eq substr($header, 4, 1)
!ll&&!rl&&r
119
MC/DC100%2/2
"CDX" eq substr($header, 1, 3) and $delimiter eq substr($header, 4, 1)
"CDX" eq substr($header, 1, 3) $delimiter eq substr($header, 4, 1)
Truth table100%3/3
A and B
A= "CDX" eq substr($header, 1, 3)B= $delimiter eq substr($header, 4, 1)
ABresult
0X0
100
111
76 unless 'CDX' eq substr $header, 1, 3
77 and $delimiter eq substr $header, 4, 1;
78  
79 9 my @fields = split /\Q$delimiter/, substr $header, 5;
80  
81 9 my %field_index = map {$fields[$_] => $_} 0 .. $#fields;
82 my %key_index =
83 10 map {$CDX_Field_Index_Key_Map{$fields[$_]} => $_}
84 9 grep defined $CDX_Field_Index_Key_Map{$fields[$_]}, 0 .. $#fields;
85  
86 croak "CDX file $cdx_file does not index WARC file name"
87 9 ▶ unless defined $field_index{g};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field_index{"g"} 1 8 9
88 croak "CDX file $cdx_file does not index record offset"
89 8 ▶ unless defined $field_index{v} or defined $field_index{V};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field_index{"v"} or defined $field_index{"V"} 1 7 8
Condition100%3/3
defined $field_index{"v"} or defined $field_index{"V"}
l!l&&r!l&&!r
251
MC/DC100%2/2
defined $field_index{"v"} or defined $field_index{"V"}
defined $field_index{"v"} defined $field_index{"V"}
Truth table100%3/3
A or B
A= defined $field_index{"v"}B= defined $field_index{"V"}
ABresult
1X1
011
000
90  
91 7 bless {cdx_file => $cdx_file, delimiter => $delimiter, fields => \@fields,
92 field_index => \%field_index, key_index => \%key_index}, $class
93 }
94  
95 sub _get_volume {
96 83 ▶ my $self = shift;
Subroutine _get_volume: called CC=2 SCAR=6.9
97 83 my $name = shift;
98  
99 83 ▶ return $self->{volumes}{$name} if defined $self->{volumes}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"volumes"}{$name} 77 6 83
100  
101 # otherwise...
102 6 my ($vol, $cdx_dirs, $file) = File::Spec->splitpath($self->{cdx_file});
103 6 my @cdx_dirs = File::Spec->splitdir($cdx_dirs);
104 6 my ($wvol, $warc_dirs, $warc_file) = File::Spec::Unix->splitpath($name);
105 6 my @warc_dirs = File::Spec::Unix->splitdir($warc_dirs);
106 6 my $warcfilename =
107 File::Spec->catpath($vol, File::Spec->catdir(@cdx_dirs,
108 @warc_dirs), $warc_file);
109 6 return $self->{volumes}{$name} = mount WARC::Volume ($warcfilename);
110 }
111  
112 sub _parse_cdx_entry {
113 95 ▶ my $self = shift;
Subroutine _parse_cdx_entry: called CC=5 SCAR=16.1
114 95 my $pos = shift;
115 95 my $entry = shift;
116 # uncoverable condition right
117 95 ▶ my $entry_length = $entry && length $entry;
Condition100%3/3
$entry && length $entry
!ll&&!rl&&r
12083
MC/DC50%1/2
$entry && length $entry
$entry -length $entry
Truth table66%2/3
A && B
A= $entryB= length $entry
ABresult
0X0
100
111
118  
119 95 ▶ return undef unless $entry_length; # as occurs at end-of-file
Branch: 1 branch
BranchTrueFalseTotal
unless $entry_length 12 83 95
120  
121 83 chomp $entry;
122 83 my @fields = split /\Q$self->{delimiter}/, $entry;
123 83 my $volname = $fields[$self->{field_index}{g}];
124 my %entry =
125 ( _index => $self, _entry_offset => $pos, _entry_length => $entry_length,
126 _g__volume => $self->_get_volume($volname),
127 _Vv__record_offset =>
128 $fields[$self->{field_index}{($volname =~ m/[.]w?arc$/ ? 'v' : 'V')}],
129 map {$_ => (defined $CDX_Import_Map{$_}
130 ? $CDX_Import_Map{$_}->($fields[$self->{key_index}{$_}])
131 83 ▶ : $fields[$self->{key_index}{$_}])} keys %{$self->{key_index}}
Branch: 2 branches
BranchTrueFalseTotal
$volname =~ /[.]w?arc$/ ? : 4 79 83
defined $WARC::Index::File::CDX::CDX_Import_Map{$_} ? : 19 52 71
132 );
133  
134 83 bless \%entry, (ref $self).'::Entry';
135 }
136  
137 sub searchable {
138 16 ▶ my $self = shift;
Subroutine searchable: called CC=2 SCAR=6.9
Pod documented
139 16 my $key = shift;
140  
141 16 ▶ return defined $self->{key_index}{url} if $key eq 'url_prefix';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "url_prefix" 4 12 16
142 12 return defined $self->{key_index}{$key};
143 }
144  
145 sub _search_all {
146 14 ▶ my $self = shift;
Subroutine _search_all: called CC=5 SCAR=16.1
147  
148 14 local $/ = "\012"; # ASCII 0/10 LF
149 14 my @results = ();
150  
151 14 ▶ open my $cdx, '<', $self->{cdx_file} or croak "$self->{cdx_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cdx, "<", $self->{"cdx_file"} or croak(qq[$self->{"cdx_file"}: $!]) 1 13 14
152 13 binmode $cdx, ':raw';
153  
154 13 my $header = <$cdx>; # skip header to reach first entry
155 13 my $offset = tell $cdx;
156 13 my $entry = $self->_parse_cdx_entry($offset, scalar <$cdx>);
157 13 ▶ return () unless defined $entry->distance(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->distance(@_) 6 7 13
158  
159 7 ▶ while (defined $entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) 28 7 35
160 28 ▶ push @results, $entry unless 0 > $entry->distance(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless 0 > $entry->distance(@_) 11 17 28
161 28 $offset = tell $cdx; # ... and advance ...
162 28 $entry = $self->_parse_cdx_entry($offset, scalar <$cdx>);
163 }
164  
165 7 return @results;
166 }
167  
168 sub _search_best_match {
169 18 ▶ my $self = shift;
Subroutine _search_best_match: called CC=8 SCAR=20.8
170  
171 18 local $/ = "\012"; # ASCII 0/10 LF
172 18 my $result = undef;
173 18 my $result_distance = -1;
174  
175 18 ▶ open my $cdx, '<', $self->{cdx_file} or croak "$self->{cdx_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cdx, "<", $self->{"cdx_file"} or croak(qq[$self->{"cdx_file"}: $!]) 1 17 18
176 17 binmode $cdx, ':raw';
177  
178 17 my $header = <$cdx>; # skip header to reach first entry
179 17 my $offset = tell $cdx;
180 17 my $entry = $self->_parse_cdx_entry($offset, scalar <$cdx>);
181 17 ▶ return undef unless defined $entry->distance(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->distance(@_) 6 11 17
182  
183 11 ▶ while (defined $entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entry) 31 2 33
184 31 my $distance = $entry->distance(@_);
185 31 ▶ unless (0 > $distance) {
Branch: 1 branch
BranchTrueFalseTotal
unless (0 > $distance) 19 12 31
186 19 ▶ if ($result_distance < 0 # first match found
Branch: 1 branch
BranchTrueFalseTotal
if ($result_distance < 0 or $distance < $result_distance) 16 3 19
Condition100%3/3
$result_distance < 0 or $distance < $result_distance
l!l&&r!l&&!r
1153
MC/DC100%2/2
$result_distance < 0 or $distance < $result_distance
$result_distance < 0 $distance < $result_distance
Truth table100%3/3
A or B
A= $result_distance < 0B= $distance < $result_distance
ABresult
1X1
011
000
187 or $distance < $result_distance) # or better match found
188 16 { $result = $entry; $result_distance = $distance }
189 }
190 31 ▶ return $result if $result_distance == 0; # no better match possible
Branch: 1 branch
BranchTrueFalseTotal
if $result_distance == 0 9 22 31
191 22 $offset = tell $cdx; # ... and advance ...
192 22 $entry = $self->_parse_cdx_entry($offset, scalar <$cdx>);
193 }
194  
195 2 return $result;
196 }
197  
198 sub search {
199 35 ▶ my $self = shift;
Subroutine search: called CC=5 SCAR=16.1
Pod documented
200  
201 35 ▶ unless (defined wantarray)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 1 34 35
202 1 { carp "calling 'search' method in void context"; return }
203  
204 34 ▶ croak "no arguments given to 'search' method"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 33 34
205 unless scalar @_;
206 33 ▶ croak "odd number of arguments given to 'search' method"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 1 32 33
207 if scalar @_ % 2;
208  
209 32 ▶ if (wantarray) { return $self->_search_all(@_) }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 14 18 32
210 18 else { return $self->_search_best_match(@_) }
211 }
212  
213 sub first_entry {
214 4 ▶ my $self = shift;
Subroutine first_entry: called CC=2 SCAR=6.9
Pod documented
215  
216 4 local $/ = "\012"; # ASCII 0/10 LF
217  
218 4 ▶ open my $cdx, '<', $self->{cdx_file} or croak "$self->{cdx_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cdx, "<", $self->{"cdx_file"} or croak(qq[$self->{"cdx_file"}: $!]) 1 3 4
219 3 binmode $cdx, ':raw';
220  
221 3 my $header = <$cdx>; # skip header to reach first entry
222 3 my $offset = tell $cdx;
223 3 return $self->_parse_cdx_entry($offset, scalar <$cdx>);
224 }
225  
226 sub entry_at {
227 15 ▶ my $self = shift;
Subroutine entry_at: called CC=5 SCAR=16.1
Pod documented
228 15 my $offset = shift;
229  
230 15 local $/ = "\012"; # ASCII 0/10 LF
231  
232 15 ▶ open my $cdx, '<', $self->{cdx_file} or croak "$self->{cdx_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $cdx, "<", $self->{"cdx_file"} or croak(qq[$self->{"cdx_file"}: $!]) 1 14 15
233 14 binmode $cdx, ':raw';
234  
235 # one octet before requested position must be an end-of-line marker
236 14 ▶ seek $cdx, $offset - 1, SEEK_SET or croak "seek $self->{cdx_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $cdx, $offset - 1, 0 or croak(qq[seek $self->{"cdx_file"}: $!]) 1 13 14
237 13 ▶ my $eol; defined(read $cdx, $eol, 1) or croak "read $self->{cdx_file}: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined read($cdx, $eol, 1) or croak(qq[read $self->{"cdx_file"}: $!]) 0 13 13
238 13 ▶ croak "offset $offset in $self->{cdx_file} not a record boundary"
Branch: 1 branch
BranchTrueFalseTotal
unless $eol eq $/ 1 12 13
239 unless $eol eq $/;
240  
241 12 return $self->_parse_cdx_entry($offset, scalar <$cdx>);
242 }
243  
244 1 1;
245 __END__
« WARC/Index.pm ↑ Summary WARC/Fields.pm »