WARC/Index/Volatile.pm

filter: Statementstmt 100% 186 / 186 Branchbran 98.4% 65 / 66 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 26 / 26 Podpod 100% 6 / 6 totaltotal 98.3% 298 / 303 CC 43 SCAR 19.6
CC
43
Cov
99%
search
17.0
build
8.0
_add_record
7.1
CRAP 7.1
« WARC/Record/FromVolume.pm ↑ Summary WARC/Index.pm »
1 package WARC::Index::Volatile; # -*- CPerl -*-
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use WARC::Index;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use WARC::Index::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use WARC::Index::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @ISA = qw(WARC::Index WARC::Index::Builder);
13  
14 2 ▶use WARC; *WARC::Index::Volatile::VERSION = \$WARC::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 require WARC::Volume;
19  
20 2 our @Default_Column_Set = qw/record_id/;
21  
22 2 WARC::Index::register(filename => qr/[.]warc(?:[.]gz)?$/);
23  
24 # This implementation uses a hash as the underlying structure.
25 # Keys defined by this class:
26 #
27 # volumes
28 # array of WARC::Volume objects known to this index
29 # -- This field is used to intern volume objects within an index.
30 # volume_position_by_tag
31 # hash mapping volume tags to positions in volumes array
32 # -- This field is used to intern volume objects within an index.
33 # entries
34 # hash mapping volume tags to arrays of index entries
35 # -- This field is used to intern index entries within an index.
36 # entry_position_by_tag_offset
37 # hash mapping volume tags to hashes mapping record offset pairs to
38 # positions in entries arrays
39 # -- This field is used to intern index entries within an index.
40 # by
41 # nested hash mapping indexed columns and values to arrays of entries
42  
43 sub _dbg_dump {
44 3 ▶ my $self = shift;
Subroutine _dbg_dump: called CC=2 SCAR=6.9
45  
46 my $out =
47 3 (ref $self)." over ".(scalar @{$self->{volumes}})." volume(s):\n";
48 3 $out .= " $_\n" for map $_->filename, @{$self->{volumes}};
49  
50 3 return $out;
51 }
52  
53 # implement WARC::Index interface
54 5 ▶sub attach { my $class = shift; $class->build(from => \@_) }
Subroutine attach: called CC=1 SCAR=0.0
Pod documented
55  
56 # override build method inherited from WARC::Index
57 sub build {
58 13 ▶ my $class = shift;
Subroutine build: called CC=8 SCAR=20.8
Pod documented
59  
60 13 my @columns = @Default_Column_Set;
61 13 my @from = ();
62  
63 13 ▶ unless (defined wantarray)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 1 12 13
64 1 { carp "building volatile index in void context"; return }
65  
66 12 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 17 11 28
67 17 my $key = shift;
68 17 ▶ if ($key eq 'from') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'from') { } 10 7 17
elsif ($key eq 'columns') { } 6 1 7
69 10 ▶ if (UNIVERSAL::isa($_[0], 'ARRAY')) { @from = @{(shift)} }
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'ARRAY')) { } 8 2 10
70 2 else { @from = splice @_ }
71 6 } elsif ($key eq 'columns') { @columns = @{(shift)} }
72 1 else { croak "unknown option '$key' building volatile index" }
73 }
74  
75 11 my $_dvmap = \%WARC::Index::Entry::_distance_value_map;
76 croak "unknown index column requested"
77 11 ▶ if grep !defined $_dvmap->{$_}, @columns;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!defined($_dvmap->{$_})), @columns) 1 10 11
78  
79 my $index = bless
80 13 { by => {(map {$_ => {}} (grep $_dvmap->{$_}[0] eq 'exact', @columns)),
81 10 (map {$_ => []} (grep $_dvmap->{$_}[0] eq 'prefix', @columns))}
82 }, $class;
83  
84 10 $index->add($_) for @from;
85  
86 10 { our $_total_constructed; $_total_constructed++ }
87  
88 10 return $index;
89 }
90 10 ▶sub DESTROY { our $_total_destroyed; $_total_destroyed++ }
Subroutine DESTROY: called CC=1 SCAR=0.0
91  
92 sub searchable {
93 3 ▶ my $self = shift;
Subroutine searchable: called CC=1 SCAR=0.0
Pod documented
94 3 my $key = shift;
95  
96 3 return defined $self->{by}{$key};
97 }
98  
99 sub search {
100 57 ▶ my $self = shift;
Subroutine search: called CC=17 SCAR=28.3
Pod documented
101  
102 57 ▶ unless (defined wantarray)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 1 56 57
103 1 { carp "calling 'search' method in void context"; return }
104  
105 56 ▶ croak "no arguments given to 'search' method"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 55 56
106 unless scalar @_;
107 55 ▶ croak "odd number of arguments given to 'search' method"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 1 54 55
108 if scalar @_ % 2;
109  
110 54 my $key = undef; my $val = undef;
111 54 ▶ for (my $i = 0; $i < $#_; $i += 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#_) 71 52 123
112 71 ▶ { (($key, $val) = @_[$i,1+$i]), last if ref $self->{by}{$_[$i]} eq 'ARRAY' }
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"by"}{$_[$i]} eq "ARRAY" 2 69 71
113 54 ▶ for (my $i = 0; $i < $#_; $i += 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#_) 54 3 57
114 54 ▶ { (($key, $val) = @_[$i,1+$i]), last if ref $self->{by}{$_[$i]} eq 'HASH' }
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"by"}{$_[$i]} eq "HASH" 51 3 54
115 54 ▶ croak "no usable search key" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 1 53 54
116  
117 53 my $mode = $WARC::Index::Entry::_distance_value_map{$key}[0];
118 53 my $refkey = $WARC::Index::Entry::_distance_value_map{$key}[1];
119  
120 53 my $rows;
121 53 ▶ if ($mode eq 'exact') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'exact') { } 51 2 53
elsif ($mode eq 'prefix') { } 1 1 2
122 51 $rows = $self->{by}{$key}{$val};
123 } elsif ($mode eq 'prefix') {
124 1 $rows = [grep $_->value($refkey) =~ m/^\Q$val/, @{$self->{by}{$key}}];
125 1 } else { die "unimplemented search mode $mode" }
126  
127 52 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 49 3 52
128 49 { return grep { $_->distance(@_) >= 0 } @$rows }
129 else {
130 3 my $result = undef; my $result_distance = -1;
131 3 foreach my $entry (@$rows) {
132 13 my $distance = $entry->distance(@_);
133 13 ▶ unless (0 > $distance) {
Branch: 1 branch
BranchTrueFalseTotal
unless (0 > $distance) 10 3 13
134 10 ▶ if ($result_distance < 0 # first match found
Branch: 1 branch
BranchTrueFalseTotal
if ($result_distance < 0 or $distance < $result_distance) 4 6 10
Condition100%3/3
$result_distance < 0 or $distance < $result_distance
l!l&&r!l&&!r
316
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
135 or $distance < $result_distance) # or better match found
136 4 { $result = $entry; $result_distance = $distance }
137 }
138 13 ▶ return $result if $result_distance == 0; # no better match possible
Branch: 1 branch
BranchTrueFalseTotal
if $result_distance == 0 2 11 13
139 }
140 1 return $result;
141 }
142 }
143  
144 sub first_entry {
145 9 ▶ my $self = shift;
Subroutine first_entry: called CC=1 SCAR=0.0
Pod documented
146  
147 9 return $self->{entries}{$self->{volumes}[0]->_file_tag}[0];
148 }
149  
150 # implement WARC::Index::Builder interface
151 sub _intern_volume ($$) {
152 244 ▶ my $index = shift;
Subroutine _intern_volume: called CC=2 SCAR=6.9
153 244 my $volume = shift;
154  
155 244 my $voltag = $volume->_file_tag;
156 $index->{volume_position_by_tag}{$voltag} =
157 13 ((push @{$index->{volumes}}, $volume) - 1)
158 244 ▶ unless defined $index->{volume_position_by_tag}{$voltag};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index->{"volume_position_by_tag"}{$voltag} 13 231 244
159 244 $volume = $index->{volumes}[$index->{volume_position_by_tag}{$voltag}];
160  
161 244 return $volume, $voltag;
162 }
163 sub _index_entry ($$) {
164 238 ▶ my $index = shift;
Subroutine _index_entry: called CC=5 SCAR=16.1
165 238 my $entry = shift;
166  
167 238 foreach my $key (keys %{$index->{by}}) {
168 368 my $refkey = $WARC::Index::Entry::_distance_value_map{$key}[1];
169 368 ▶ next unless defined $entry->value($refkey);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->value($refkey) 15 353 368
170 353 ▶ if (ref $index->{by}{$key} eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $index->{'by'}{$key} eq 'HASH') { } 306 47 353
elsif (ref $index->{'by'}{$key} eq 'ARRAY') { } 46 1 47
171 306 { push @{$index->{by}{$key}{$entry->{$refkey}}}, $entry }
172 elsif (ref $index->{by}{$key} eq 'ARRAY')
173 46 { push @{$index->{by}{$key}}, $entry } # defer sort to outer call
174 1 else { die "unknown object in $key index slot" }
175 }
176 }
177  
178 sub _add_record {
179 218 ▶ my $index = shift;
Subroutine _add_record: called CC=7 SCAR=19.5
180 218 my $record = shift;
181  
182 218 my $volume; my $voltag;
183 218 ($volume, $voltag) = _intern_volume $index, $record->volume;
184  
185 218 my $offset = $record->offset;
186 218 my $entry = WARC::Index::Volatile::Entry->_new
187 ( _index => $index, _volume => $volume, _record_offset => $offset );
188  
189 # intern entry
190 218 ▶ return if defined $index->{entry_position_by_tag_offset}{$voltag}{$offset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $index->{"entry_position_by_tag_offset"}{$voltag}{$offset} 5 213 218
191 $index->{entry_position_by_tag_offset}{$voltag}{$offset} =
192 213 ((push @{$index->{entries}{$voltag}}, $entry) - 1);
193  
194 # populate entry
195 213 $entry->{time} = $record->date;
196 213 $entry->{record_id} = $record->id;
197 $entry->{segment_origin_id} = $record->field('WARC-Segment-Origin-ID')
198 if exists $index->{by}{segment_origin_id}
199 213 ▶ && $record->type eq 'continuation';
Branch: 1 branch
BranchTrueFalseTotal
if exists $index->{"by"}{"segment_origin_id"} and $record->type eq "continuation" 0 213 213
Condition33%1/3
exists $index->{"by"}{"segment_origin_id"} and $record->type eq "continuation"
!ll&&!rl&&r
21300
MC/DC0%0/2
exists $index->{"by"}{"segment_origin_id"} and $record->type eq "continuation"
exists $index->{"by"}{"segment_origin_id"} $record->type eq "continuation"
Truth table33%1/3
A and B
A= exists $index->{"by"}{"segment_origin_id"}B= $record->type eq "continuation"
ABresult
0X0
100
111
200 $entry->{url} = $record->field('WARC-Target-URI')
201 if (exists $index->{by}{url} || exists $index->{by}{url_prefix})
202 213 ▶ && defined $record->field('WARC-Target-URI');
Branch: 1 branch
BranchTrueFalseTotal
if exists $index->{"by"}{"url"} || exists $index->{"by"}{"url_prefix"} and defined $record->field("WARC-Target-URI") 115 98 213
Condition100%6/6
exists $index->{"by"}{"url"} || exists $index->{"by"}{"url_prefix"} and defined $record->field("WARC-Target-URI")
!ll&&!rl&&r
8315115
exists $index->{"by"}{"url"} or exists $index->{"by"}{"url_prefix"}
l!l&&r!l&&!r
795183
MC/DC100%3/3
exists $index->{"by"}{"url"} || exists $index->{"by"}{"url_prefix"} and defined $record->field("WARC-Target-URI")
exists $index->{"by"}{"url"} exists $index->{"by"}{"url_prefix"} defined $record->field("WARC-Target-URI")
Truth table100%5/5
A or B and C
A= exists $index->{"by"}{"url"}B= exists $index->{"by"}{"url_prefix"}C= defined $record->field("WARC-Target-URI")
ABCresult
00X0
1X00
0100
1X11
0111
203  
204 213 _index_entry $index, $entry;
205 }
206  
207 sub _add_entry ($$) {
208 51 ▶ my $index = shift;
Subroutine _add_entry: called CC=3 SCAR=11.0
209 51 my $source = shift;
210  
211 51 ▶ if (grep !defined $source->value($_), keys %{$index->{by}})
Branch: 1 branch
BranchTrueFalseTotal
if (grep((!defined($source->value($_))), keys %{$$index{'by'};})) { } 25 26 51
212 # at least one column not in source index; index the record instead
213 25 { $index->_add_record($source->record) }
214 else {
215 # volume and offset can be retrieved from a stub record without I/O
216 26 my $rstub = $source->record; my $volume; my $voltag;
217 26 ($volume, $voltag) = _intern_volume $index, $rstub->volume;
218 26 my $offset = $rstub->offset;
219 my $entry = WARC::Index::Volatile::Entry->_new
220 ( _index => $index, _volume => $volume, _record_offset => $offset,
221 26 map { $_ => $source->value($_) } keys %{$index->{by}} );
222  
223 # intern entry
224 26 ▶ return if defined $index->{entry_position_by_tag_offset}{$voltag}{$offset};
Branch: 1 branch
BranchTrueFalseTotal
if defined $index->{"entry_position_by_tag_offset"}{$voltag}{$offset} 1 25 26
225 $index->{entry_position_by_tag_offset}{$voltag}{$offset} =
226 25 ((push @{$index->{entries}{$voltag}}, $entry) - 1);
227  
228 25 _index_entry $index, $entry;
229 }
230 }
231  
232 sub add {
233 17 ▶ my $self = shift;
Subroutine add: called CC=3 SCAR=11.0
Pod documented
234  
235 17 $self->SUPER::add(@_);
236  
237 # sort any array-based columns
238 15 foreach my $key (keys %{$self->{by}}) {
239 23 ▶ next unless ref $self->{by}{$key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"by"}{$key} eq "ARRAY" 21 2 23
240 2 my $refkey = $WARC::Index::Entry::_distance_value_map{$key}[1];
241 2 @{$self->{by}{$key}} =
242 2 sort { $a->value($refkey) cmp $b->value($refkey) } @{$self->{by}{$key}};
243 }
244 }
245  
246 {
247 package WARC::Index::Volatile::Entry;
248  
249 2 our @ISA = qw(WARC::Index::Entry);
250  
251 # This implementation uses a hash as the underlying structure.
252 #
253 # Accessible search keys are stored directly in the hash, while internal
254 # values are stored with names with a leading underscore.
255  
256 # Keys defined by this class:
257 #
258 # _index
259 # weak reference to parent index
260 # _volume
261 # reference to volume containing record
262 # _record_offset
263 # offset of record within containing volume
264  
265 208 ▶ sub index { (shift)->{_index} }
Subroutine index: called CC=1 SCAR=0.0
266 489 ▶ sub volume { (shift)->{_volume} }
Subroutine volume: called CC=1 SCAR=0.0
267 431 ▶ sub record_offset { (shift)->{_record_offset} }
Subroutine record_offset: called CC=1 SCAR=0.0
268  
269 sub next {
270 208 ▶ my $self = shift;
Subroutine next: called CC=3 SCAR=11.0
271  
272 208 my $idx = $self->index;
273 208 my $vt = $self->volume->_file_tag;
274 208 my $off = $self->record_offset;
275  
276 my $next = $idx->{entries}{$vt}
277 208 [1+$idx->{entry_position_by_tag_offset}{$vt}{$off}];
278 208 ▶ return $next if defined $next;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next 198 10 208
279  
280 10 my $nextvol = $idx->{volumes}[1+$idx->{volume_position_by_tag}{$vt}];
281 10 ▶ $next = $idx->{entries}{$nextvol->_file_tag}[0] if defined $nextvol;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nextvol 2 8 10
282  
283 10 return $next;
284 }
285  
286 sub value {
287 1198 ▶ my $self = shift;
Subroutine value: called CC=2 SCAR=6.9
288 1198 my $key = shift;
289  
290 1198 ▶ return undef if $key =~ m/^_/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^_/ 1 1197 1198
291 1197 return $self->{$key};
292 }
293  
294 sub _new {
295 244 ▶ my $class = shift;
Subroutine _new: called CC=1 SCAR=0.0
296  
297 244 my $entry = bless { @_ }, $class;
298 244 Scalar::Util::weaken $entry->{_index};
299 244 return $entry;
300 }
301 }
302  
303 2 1;
304 __END__
« WARC/Record/FromVolume.pm ↑ Summary WARC/Index.pm »