WARC/Collection.pm

filter: Statementstmt 100% 76 / 76 Branchbran 100% 34 / 34 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 100% 129 / 129 CC 25 SCAR 24.6
CC
25
Cov
100%
search
17.0
assemble
7.0
searchable
3.0
CRAP 11.7
« WARC/Record/Replay.pm ↑ Summary WARC/Index/Entry.pm »
1 package WARC::Collection; # -*- 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::Collection::VERSION = \$WARC::VERSION;
9  
10 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 29 require WARC::Index;
13 29 require WARC::Index::Entries;
14  
15 =head1 NAME
16  
17 WARC::Collection - Interface to a group of WARC files
18  
19 =head1 SYNOPSIS
20  
21 use WARC::Collection;
22  
23 $collection = assemble WARC::Collection ($index_1, $index_2, ...);
24 $collection = assemble WARC::Collection from => ($index_1, ...);
25  
26 $yes_or_no = $collection->searchable( $key );
27  
28 $record = $collection->search(url => $url, time => $when);
29 @records = $collection->search(url => $url, time => $when);
30  
31 =cut
32  
33 # This implementation uses a hash as the underlying structure.
34 # Keys defined by this class:
35 #
36 # indexes
37 # Array of indexes used for this collection.
38  
39 =head1 DESCRIPTION
40  
41 The C<WARC::Collection> class is the primary means by which user code is
42 expected to use the WARC library. This class uses indexes to efficiently
43 search for records in one or more WARC files.
44  
45 =head2 Search Keys
46  
47 The C<search> method accepts a list of parameters as I<key> =E<gt> I<value>
48 pairs with each pair narrowing the search, sorting the results, or both,
49 indicated in the following list with S<"C<[N ]>">, S<"C<[ S]>">, or "C<[NS]>",
50 respectively.
51  
52 Supplying an array reference as a I<value> indicates a search where any of
53 the values in the array are acceptable. This does not affect sorting.
54  
55 The same search keys documented here are used for searching indexes, since
56 C<WARC::Collection> is a wrapper around one or more indexes, but index
57 support modules do not sort their results. Only C<WARC::Collection> sorts
58 the returned entries, so keys listed below as "sort-only" are ignored by
59 the index support modules.
60  
61 The keys supported are:
62  
63 =over
64  
65 =item C<[N ]> url
66  
67 An exact match for a URL.
68  
69 =item C<[NS]> url_prefix
70  
71 A prefix match for a URL. Prefers records with shorter URLs.
72  
73 =item C<[ S]> time
74  
75 Prefer records collected nearer to the requested time.
76  
77 =item C<[N ]> record_id
78  
79 An exact match for a (presumably unique) WARC-Record-ID.
80  
81 =item C<[N ]> segment_origin_id
82  
83 Exact match for continuation records for a WARC-Record-ID that identifies a
84 logical record stored using WARC record segmentation. Searching on this
85 key returns only the continuation records.
86  
87 =back
88  
89 =for comment
90 Matching these keys is implemented in WARC::Index::Entry::_distance_for_item
91 via %_distance_value_map and in various index support modules.
92  
93 =head2 Methods
94  
95 =over
96  
97 =item $collection = assemble WARC::Collection ($index_1, $index_2, ...);
98  
99 =item $collection = assemble WARC::Collection from =E<gt> ($index_1, ...);
100  
101 Assemble a collection of WARC files from one index or multiple indexes,
102 specified either as objects derived from C<WARC::Index> or filenames.
103  
104 While multiple indexes can be used in a collection, note that searching a
105 collection requires individually searching every index in the collection.
106  
107 =cut
108  
109 sub assemble {
110 20 ▶ my $class = shift;
Subroutine assemble: called CC=7 SCAR=19.5
Pod documented
111 20 ▶ shift if scalar @_ and $_[0] eq 'from'; # discard optional noise word
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ and $_[0] eq "from" 10 10 20
Condition100%3/3
scalar @_ and $_[0] eq "from"
!ll&&!rl&&r
1910
MC/DC100%2/2
scalar @_ and $_[0] eq "from"
scalar @_ $_[0] eq "from"
Truth table100%3/3
A and B
A= scalar @_B= $_[0] eq "from"
ABresult
0X0
100
111
112  
113 20 ▶ carp "assembling empty collection" unless scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 2 18 20
114  
115 20 my @indexes = ();
116  
117 20 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 23 19 42
118 23 my $index = shift;
119  
120 23 ▶ if (UNIVERSAL::isa($index, 'WARC::Index'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($index, 'WARC::Index')) { } 10 13 23
121 10 { push @indexes, $index } # add index object to list
122 else { # or assume filename and find an index handler
123 13 my $isys = WARC::Index::find_handler($index);
124 13 ▶ croak "no known handler for index '$index'" unless $isys;
Branch: 1 branch
BranchTrueFalseTotal
unless $isys 1 12 13
125 12 push @indexes, (attach $isys $index);
126 }
127 }
128  
129 19 bless { indexes => \@indexes }, $class
130 }
131  
132 =item $yes_or_no = $collection-E<gt>searchable( $key )
133  
134 Return true or false to reflect if any index in the collection can search
135 for the requested key.
136  
137 =cut
138  
139 sub searchable {
140 15 ▶ my $self = shift;
Subroutine searchable: called CC=3 SCAR=11.0
Pod documented
141 15 my $key = shift;
142  
143 15 foreach my $index (@{$self->{indexes}})
144 18 ▶ { return 1 if $index->searchable($key) }
Branch: 1 branch
BranchTrueFalseTotal
if $index->searchable($key) 11 7 18
145  
146 # none of the indexes recognize $key
147 4 return 0;
148 }
149  
150 =item $record = $collection-E<gt>search( ... )
151  
152 =item @records = $collection-E<gt>search( ... )
153  
154 Search the indexes for records matching the parameters and return the best
155 match in scalar context or a list of all matches in list context. The
156 returned values are C<WARC::Record> objects.
157  
158 See L</"Search Keys"> for more information about the parameters.
159  
160 =cut
161  
162 sub search {
163 66 ▶ my $self = shift;
Subroutine search: called CC=17 SCAR=28.3
Pod documented
164  
165 66 ▶ unless (defined wantarray)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 1 65 66
166 1 { carp "calling 'search' method in void context"; return }
167  
168 65 ▶ croak "no arguments given to 'search' method"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 64 65
169 unless scalar @_;
170 64 ▶ croak "odd number of arguments given to 'search' method"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 1 63 64
171 if scalar @_ % 2;
172  
173 # collect all matches from all indexes
174 63 my %results = (); # map: tag => array of index entries for record
175 63 ▶ if (grep UNIVERSAL::isa($_, 'ARRAY'), @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep UNIVERSAL::isa($_, 'ARRAY'), @_) { } 19 44 63
176 # at least one parameter is an arrayref; perform nested loop join
177 19 my @step = @_; my @state = (); my @varpos = ();
178 19 ▶ for (my $i = 1; $i <= $#step; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#step) 29 19 48
179 29 ▶ next unless UNIVERSAL::isa($step[$i], 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($step[$i], "ARRAY") 9 20 29
180 20 push @state, 0;
181 20 push @varpos, $i;
182 20 $step[$i] = $step[$i]->[0];
183 }
184 # search indexes with all combinations from the input
185 19 ▶ while ($state[0] <= $#{$_[$varpos[0]]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state[0] <= $#{$_[$varpos[0]];}) 35 19 54
186 35 foreach my $index (@{$self->{indexes}})
187 35 { foreach my $entry ($index->search(@step))
188 38 { push @{$results{$entry->tag}}, $entry } }
189 } continue {
190 # count in variable base in @state using the input arrayrefs
191 35 my $i = $#state;
192 35 ▶ $i-- while $i > 0 && $state[$i] >= $#{$_[$varpos[$i]]};
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 and $state[$i] >= $#{$_[$varpos[$i]];} and do { --$i } 4 35 39
Condition100%3/3
$i > 0 and $state[$i] >= $#{$_[$varpos[$i]];}
!ll&&!rl&&r
3144
MC/DC100%2/2
$i > 0 and $state[$i] >= $#{$_[$varpos[$i]];}
$i > 0 $state[$i] >= $#{$_[$varpos[$i]];}
Truth table100%3/3
A and B
A= $i > 0B= $state[$i] >= $#{$_[$varpos[$i]];}
ABresult
0X0
100
111
193 35 $step[$varpos[$i]] = $_[$varpos[$i]]->[++$state[$i]];
194 35 ▶ $step[$varpos[$i]] = $_[$varpos[$i]]->[$state[$i] = 0]
Branch: 1 branch
BranchTrueFalseTotal
++$i <= $#state and do { $step[$varpos[$i]] = $_[$varpos[$i]][$state[$i] = 0] } 4 35 39
195 while ++$i <= $#state;
196 }
197 } else {
198 # simple case with single values; only one scan needed
199 44 foreach my $index (@{$self->{indexes}})
200 66 { foreach my $entry ($index->search(@_))
201 70 { push @{$results{$entry->tag}}, $entry } }
202 }
203  
204 # coalesce and sort the collected index entries
205 my @results =
206 63 map {coalesce WARC::Index::Entries ($results{$_})} keys %results;
207 @results = # sort by distance using Schwartzian transform
208 108 (map { $_->[0] } sort { $a->[1] <=> $b->[1] }
209 63 map { [$_, scalar $_->distance(@_)] } @results);
210  
211 # return either the entire sorted list or the best match
212 63 ▶ if (wantarray) { return map {$_->record(collection => $self)} @results }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 31 32 63
213 else { return (scalar @results
214 32 ▶ ? $results[0]->record(collection => $self) : undef) }
Branch: 1 branch
BranchTrueFalseTotal
scalar @results ? : 31 1 32
215 }
216  
217 =back
218  
219 =cut
220  
221 29 1;
222 __END__
« WARC/Record/Replay.pm ↑ Summary WARC/Index/Entry.pm »