WARC/Index/Entry.pm

filter: Statementstmt 100% 72 / 72 Branchbran 100% 40 / 40 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 100% 142 / 142 CC 26 SCAR 21.6
CC
26
Cov
100%
_distance_for_item
16.0
_distance_summary
6.0
distance
5.0
CRAP 8.7
« WARC/Collection.pm ↑ Summary WARC/Record/FromVolume.pm »
1 package WARC::Index::Entry; # -*- CPerl -*-
2  
3 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 29 our @ISA = qw();
7  
8 29 require WARC; *WARC::Index::Entry::VERSION = \$WARC::VERSION;
9  
10 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 29 require WARC::Record::Stub;
13  
14 =head1 NAME
15  
16 WARC::Index::Entry - abstract base class for WARC::Index entries
17  
18 =head1 SYNOPSIS
19  
20 use WARC; # or ...
21 use WARC::Index;
22  
23 # WARC::Index::Entry objects are returned from directly searching an index
24  
25 # match search criteria against entry
26 $distance = $entry->distance( ... );
27 @report = $entry->distance( ... );
28  
29 $index = $entry->index; # get index containing entry
30 $volume = $entry->volume; # get WARC::Volume containing record
31 $record = $entry->record; # get WARC record
32  
33 =head1 DESCRIPTION
34  
35 =head2 Common Methods
36  
37 Entries from all index systems support these methods:
38  
39 =over
40  
41 =item @report = $entry-E<gt>distance( ... )
42  
43 =item $distance = $entry-E<gt>distance( ... )
44  
45 In list context, return a detailed report mapping each search I<key> to a
46 distance value. In scalar context, return an overall summary distance,
47 such that sorting entries by the return values of this method in ascending
48 order will place the closest matches at the top of the list.
49  
50 A valid distance is non-negative. Negative distance values indicate that
51 the record does not match the criteria at all. An undefined value
52 indicates that the entry is from an index that does not store the
53 information needed to evaluate distance for that search key. Undefined
54 values are ignored when computing the summarized distance, but the
55 summarized distance will be negative if any keys do not match at all and
56 itself undefined if none of the requested keys can be evaluated.
57  
58 For details on available search keys, see the L<"Search Keys"
59 section|WARC::Collection/"Search Keys"> of the C<WARC::Collection> page.
60 If multiple values are given in an arrayref, the best match is reported.
61  
62 =cut
63  
64 sub distance {
65 698 ▶ my $self = shift;
Subroutine distance: called CC=5 SCAR=16.1
Pod documented
66  
67 698 ▶ unless (defined wantarray)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 1 697 698
68 1 { carp "calling 'distance' method in void context"; return }
69  
70 697 ▶ croak "no arguments given to 'distance' method"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 696 697
71 unless scalar @_;
72 696 ▶ croak "odd number of arguments given to 'distance' method"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 1 695 696
73 if scalar @_ % 2;
74  
75 695 ▶ if (wantarray) { return $self->_distance_report(@_) }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 20 675 695
76 675 else { return $self->_distance_summary(@_) }
77 }
78  
79 sub _distance_report {
80 20 ▶ my $self = shift;
Subroutine _distance_report: called CC=2 SCAR=6.9
81 20 my @report = ();
82  
83 20 ▶ for (my $i = 0; $i < @_; $i += 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 24 20 44
84 24 { push @report, $_[$i] => $self->_distance_for_item($_[$i] => $_[1+$i]) }
85  
86 20 return @report;
87 }
88  
89 sub _distance_summary {
90 675 ▶ my $self = shift;
Subroutine _distance_summary: called CC=6 SCAR=17.9
91  
92 675 my $summary = 0;
93 675 my $match = 1;
94 675 my $seen = 0;
95  
96 675 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 936 673 1609
97 936 my $distance = $self->_distance_for_item(splice @_, 0, 2);
98 934 ▶ next unless defined $distance;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $distance 14 920 934
99 920 $seen++;
100 920 ▶ if ($distance < 0) { $match = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($distance < 0) { } 475 445 920
101 445 else { $summary += $distance }
102 }
103  
104 673 ▶ return undef unless $seen;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen 13 660 673
105 660 ▶ return $match ? $summary : -(1+$summary);
Branch: 1 branch
BranchTrueFalseTotal
$match ? : 314 346 660
106 }
107  
108 # Single Point of Truth for index key definitions
109 29 our %_distance_value_map =
110 ( time => [numeric => 'time'],
111 record_id => [exact => 'record_id'],
112 segment_origin_id => [exact => 'segment_origin_id'],
113 url => [exact => 'url'],
114 url_prefix => [prefix => 'url'],
115 );
116  
117 sub _distance_for_item {
118 960 ▶ my $self = shift;
Subroutine _distance_for_item: called CC=16 SCAR=27.7
119 960 my $item = shift;
120 960 my @sought = (scalar shift);
121 960 ▶ @sought = @{$sought[0]} if UNIVERSAL::isa($sought[0], 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($sought[0], "ARRAY") 55 905 960
122  
123 croak "index distance requested for unknown item $item"
124 960 ▶ unless defined $_distance_value_map{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $WARC::Index::Entry::_distance_value_map{$item} 1 959 960
125 959 my $actual = $self->value($_distance_value_map{$item}[1]);
126  
127 959 ▶ return undef unless defined $actual;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $actual 16 943 959
128  
129 943 my $mode = $_distance_value_map{$item}[0];
130  
131 943 my $distance = -1;
132 943 ▶ if ($mode eq 'exact') {
Branch: 3 branches
BranchTrueFalseTotal
if ($mode eq 'exact') { } 725 218 943
elsif ($mode eq 'numeric') { } 124 94 218
elsif ($mode eq 'prefix') { } 93 1 94
133 725 foreach my $sought (@sought) {
134 758 ▶ $distance = 0 if $sought eq $actual;
Branch: 1 branch
BranchTrueFalseTotal
if $sought eq $actual 284 474 758
135 }
136 } elsif ($mode eq 'numeric') {
137 124 foreach my $sought (@sought) {
138 128 my $here = abs($actual - $sought);
139 128 ▶ $distance = $here if $distance < 0 || $here < $distance;
Branch: 1 branch
BranchTrueFalseTotal
if $distance < 0 or $here < $distance 126 2 128
Condition100%3/3
$distance < 0 or $here < $distance
l!l&&r!l&&!r
12422
MC/DC100%2/2
$distance < 0 or $here < $distance
$distance < 0 $here < $distance
Truth table100%3/3
A or B
A= $distance < 0B= $here < $distance
ABresult
1X1
011
000
140 }
141 } elsif ($mode eq 'prefix') {
142 93 foreach my $sought (@sought) {
143 97 ▶ next unless $sought eq substr $actual, 0, length $sought;
Branch: 1 branch
BranchTrueFalseTotal
unless $sought eq substr($actual, 0, length $sought) 41 56 97
144 56 my $here = length($actual) - length($sought);
145 56 ▶ $distance = $here if $distance < 0 || $here < $distance;
Branch: 1 branch
BranchTrueFalseTotal
if $distance < 0 or $here < $distance 54 2 56
Condition100%3/3
$distance < 0 or $here < $distance
l!l&&r!l&&!r
5222
MC/DC100%2/2
$distance < 0 or $here < $distance
$distance < 0 $here < $distance
Truth table100%3/3
A or B
A= $distance < 0B= $here < $distance
ABresult
1X1
011
000
146 }
147 1 } else { die "unknown mode '$mode' for item '$item'" }
148 942 return $distance;
149 }
150  
151 =item $index = $entry-E<gt>index
152  
153 Return the C<WARC::Index> containing this entry.
154  
155 =cut
156  
157 sub index {
158 1 ▶ die __PACKAGE__." is an abstract base class and "
Subroutine index: called CC=1 SCAR=0.0
Pod documented
159 .(ref shift)." must override the 'index' method"
160 }
161  
162 =item $volume = $entry-E<gt>volume
163  
164 Return the C<WARC::Volume> object representing the file in which this index
165 entry's record is located.
166  
167 =cut
168  
169 sub volume {
170 1 ▶ die __PACKAGE__." is an abstract base class and "
Subroutine volume: called CC=1 SCAR=0.0
Pod documented
171 .(ref shift)." must override the 'volume' method"
172 }
173  
174 =item $record = $entry-E<gt>record( ... )
175  
176 Return the C<WARC::Record> this index entry represents. Arguments if given
177 are additional key =E<gt> value pairs for the record object.
178  
179 =cut
180  
181 sub record {
182 235 ▶ my $self = shift;
Subroutine record: called CC=1 SCAR=0.0
Pod documented
183 235 return new WARC::Record::Stub ($self->volume, $self->record_offset, @_);
184 }
185  
186 =item $record_offset = $entry-E<gt>record_offset
187  
188 Return the file offset at which this index entry's record is located.
189  
190 =cut
191  
192 sub record_offset {
193 1 ▶ die __PACKAGE__." is an abstract base class and "
Subroutine record_offset: called CC=1 SCAR=0.0
Pod documented
194 .(ref shift)." must override the 'offset' method"
195 }
196  
197 =item $value = $entry-E<gt>value( $key )
198  
199 Return the value this index entry holds for a given search key.
200  
201 =cut
202  
203 sub value {
204 1 ▶ die __PACKAGE__." is an abstract base class and "
Subroutine value: called CC=1 SCAR=0.0
Pod documented
205 .(ref shift)." must override the 'value' method"
206 }
207  
208 =item $tag = $entry-E<gt>tag
209  
210 Return a tag for this index entry. The exact format of the tag is
211 unspecified and platform-dependent. Two index entries that refer to
212 different records are guaranteed (if the underlying system software behaves
213 correctly) to have different tag values, while two entries that refer to
214 the same record in the same volume will normally have the same tag value,
215 except in edge cases.
216  
217 =cut
218  
219 sub tag {
220 44 ▶ my $self = shift;
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
221 44 return (($self->volume->_file_tag).':'.($self->record_offset));
222 }
223  
224 =back
225  
226 =head2 Optional Methods
227  
228 Some index entries may additionally support any of these methods:
229  
230 =over
231  
232 =item $next_entry = $entry-E<gt>next
233  
234 Indexes with an inherent sequence of entries may provide a method to obtain
235 the next entry in the index. Some index systems have this, while others do
236 not have a meaningful order amongst their entries.
237  
238 =item $position = $entry-E<gt>entry_position
239  
240 Indexes with an inherent sequence of entries may provide a method to obtain
241 some kind of index-specific entry number or location parameter. This is
242 most useful for metaindexes to record the location of an index entry.
243  
244 =back
245  
246 =cut
247  
248 29 1;
249 __END__
« WARC/Collection.pm ↑ Summary WARC/Record/FromVolume.pm »