| 1 | package WARC::Index; # -*- 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 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 29 | our @ISA = qw(); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 29 | require WARC; *WARC::Index::VERSION = \$WARC::VERSION; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | WARC::Index - base class for WARC index classes | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | use WARC::Index::File::CDX; # or ... | ||||||||||||||||||||||||||||
| 19 | use WARC::Index::File::SDBM; | ||||||||||||||||||||||||||||
| 20 | # or some other WARC::Index::File::* implementation | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | $index = attach WARC::Index::File::CDX (...); # or ... | ||||||||||||||||||||||||||||
| 23 | $index = attach WARC::Index::File::SDBM (...); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | $record = $index->search(url => $url, time => $when); | ||||||||||||||||||||||||||||
| 26 | @records = $index->search(url => $url, time => $when); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | build WARC::Index::File::CDX (...); # or ... | ||||||||||||||||||||||||||||
| 29 | build WARC::Index::File::SDBM (...); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | C<WARC::Index> is an abstract base class for indexes on WARC files and | ||||||||||||||||||||||||||||
| 34 | WARC-alike files. This class establishes the expected interface and | ||||||||||||||||||||||||||||
| 35 | provides a simple interface for building indexes. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 Methods | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =over | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =item $index = attach WARC::Index::File::* (...) | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Construct an index object using the indicated technology and whatever | ||||||||||||||||||||||||||||
| 44 | parameters the index implementation needs. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Typically, indexes are file-based and a single parameter is the name of an | ||||||||||||||||||||||||||||
| 47 | index file which in turn contains the names of the indexed WARC files. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub attach { | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | die __PACKAGE__." is an abstract base class and " | ||||||||||||||||||||||||||
|
Subroutine
attach: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | .(shift)." must override the 'attach' method" | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item $yes_or_no = $index-E<gt>searchable( $key ) | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Return true or false to reflect if the index can search for the requested | ||||||||||||||||||||||||||||
| 59 | key. Indexes may be able to search for keys that are not present in | ||||||||||||||||||||||||||||
| 60 | entries returned from those indexes. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | See the L<"Search Keys" section|WARC::Collection/"Search Keys"> of the | ||||||||||||||||||||||||||||
| 63 | C<WARC::Collection> page for details on the implemented search keys. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub searchable { | ||||||||||||||||||||||||||||
| 68 | 1 | ▶ | die __PACKAGE__." is an abstract base class and " | ||||||||||||||||||||||||||
|
Subroutine
searchable: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | .(ref shift)." must override the 'searchable' method" | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item $record = $index-E<gt>search( ... ) | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =item @records = $index-E<gt>search( ... ) | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Search an index for records matching parameters. The C<WARC::Collection> | ||||||||||||||||||||||||||||
| 77 | class uses this method to search each index in a collection. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | If the none of the requested search keys are searchable, returns an | ||||||||||||||||||||||||||||
| 80 | undefined value in scalar context and the empty list in list context. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | The details of the parameters for this method are documented in the | ||||||||||||||||||||||||||||
| 83 | L<"Search Keys" section|WARC::Collection/"Search Keys"> of the | ||||||||||||||||||||||||||||
| 84 | C<WARC::Collection> page. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub search { | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | die __PACKAGE__." is an abstract base class and " | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | .(ref shift)." must override the 'search' method" | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item build WARC::Index::File::* (into =E<gt> $dest, from =E<gt> ...) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =item build WARC::Index::File::* (from =E<gt> [...], into =E<gt> $dest) | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | The C<WARC::Index> base class B<does> provide this method, however. The | ||||||||||||||||||||||||||||
| 98 | C<build> method works by loading the corresponding index builder class and | ||||||||||||||||||||||||||||
| 99 | driving the process or simply returning the newly-constructed object. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | The C<build> method itself handles the C<from> key for specifying the files | ||||||||||||||||||||||||||||
| 102 | to index. The C<from> key can be given an array reference, after which | ||||||||||||||||||||||||||||
| 103 | more key =E<gt> value pairs may follow, or can simply use the rest of the | ||||||||||||||||||||||||||||
| 104 | argument list as its value. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | If the C<from> key is given, the C<build> method will read the indicated | ||||||||||||||||||||||||||||
| 107 | files, construct an index, and return nothing. If the C<from> key is not | ||||||||||||||||||||||||||||
| 108 | given, the C<build> method will construct and return an index builder. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | All index builders accept at least the C<into> key for specifying where to | ||||||||||||||||||||||||||||
| 111 | store the index. See the documentation for WARC::Index::File::*::Builder | ||||||||||||||||||||||||||||
| 112 | for more information. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub build { | ||||||||||||||||||||||||||||
| 117 | 7 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
build: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 7 | ▶ | croak "'build' is a class method" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | if ref $class; | ||||||||||||||||||||||||||||
| 121 | 6 | ▶ | croak "no arguments given to 'build' class method" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | unless scalar @_; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5 | my @args = (); my $from = undef; | |||||||||||||||||||||||||||
| 125 | 5 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 11 | my $key = shift; | |||||||||||||||||||||||||||
| 127 | 11 | ▶ | if ($key eq 'from') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 3 | ▶ | if (UNIVERSAL::isa($_[0], 'ARRAY')) { $from = shift } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 2 | else { $from = [splice @_] } | |||||||||||||||||||||||||||
| 130 | 8 | } else { push @args, $key, shift } | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | ▶ | croak "empty list of index sources given" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $from and scalar @$from == 0
MC/DC100%2/2
defined $from and scalar @$from == 0
defined $from
scalar @$from == 0
Truth table100%3/3
A and B
A= defined $fromB= scalar @$from == 0
| |||||||||||||||||||||||||||||
| 134 | if defined $from && scalar @$from == 0; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 4 | my $bclass = $class . q{::Builder}; | |||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||
| 138 | 29 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 4 | ▶ | unless (exists ${$class.'::'}{'Builder::'}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | { eval q{require }.$bclass; die $@ if $@ } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | my $ob = _new $bclass (@args); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 3 | ▶ | return $ob unless defined $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | $ob->add(@$from); | |||||||||||||||||||||||||||
| 148 | 2 | return (); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =back | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 Optional Methods | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Some index systems may also provide these methods: | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =over | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =item $entry = $index-E<gt>first_entry | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | An index that has a sequential ordering may provide this method to obtain | ||||||||||||||||||||||||||||
| 162 | the first entry in the index. Indexes that do not have a meaningful | ||||||||||||||||||||||||||||
| 163 | sequence amongst their entries do not provide this method. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =item $entry = $index-E<gt>entry_at( $position ) | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | An index that has a sequential ordering may provide this method to obtain | ||||||||||||||||||||||||||||
| 168 | an entry at a specified position in the index. The exact format of the | ||||||||||||||||||||||||||||
| 169 | position parameter is not specified in general, but should be a value | ||||||||||||||||||||||||||||
| 170 | previously obtained from the C<entry_position> method on an entry from the | ||||||||||||||||||||||||||||
| 171 | same index. Valid positions may be sparse. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =back | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head2 Index system registration | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | The C<WARC::Index> package also provides a registry of loaded index | ||||||||||||||||||||||||||||
| 178 | support. The C<register> function adds the calling package to the list. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Array of arrays listing index implementations and filename patterns. | ||||||||||||||||||||||||||||
| 183 | # Each element: [ Package => qr/pattern1/, qr/pattern2/, ... ] | ||||||||||||||||||||||||||||
| 184 | 29 | our @Index_Handlers = (); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =over | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item WARC::Index::register( filename =E<gt> $filename_re ) | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Add the calling package to an internal list of available index handlers. | ||||||||||||||||||||||||||||
| 191 | The calling package must be a subclass of C<WARC::Index> or this function | ||||||||||||||||||||||||||||
| 192 | will croak(). | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | The C<filename> key indicates that the calling package expects to handle | ||||||||||||||||||||||||||||
| 195 | index files with names matching the provided regex. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | sub register { | ||||||||||||||||||||||||||||
| 200 | 13 | ▶ | my %opt = @_; | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 13 | my $caller = scalar caller; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 13 | ▶ | croak "WARC::Index implementations must subclass WARC::Index" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | unless $caller->isa('WARC::Index'); | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | croak "WARC::Index implementations must handle a filename pattern" | ||||||||||||||||||||||||||||
| 207 | 12 | ▶ | unless $opt{filename}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 10 | foreach my $row (grep {$_->[0] eq $caller} @Index_Handlers) { | |||||||||||||||||||||||||||
| 210 | 2 | push @$row, $opt{filename}; # add pattern to existing row | |||||||||||||||||||||||||||
| 211 | return # ensure that there will be at most one row per package | ||||||||||||||||||||||||||||
| 212 | 2 | } | |||||||||||||||||||||||||||
| 213 | 8 | push @Index_Handlers, [$caller => $opt{filename}]; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | return # nothing | ||||||||||||||||||||||||||||
| 216 | 8 | } | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =item WARC::Index::find_handler( $filename ) | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Return the registered handler for $filename or undef if none match. If | ||||||||||||||||||||||||||||
| 221 | multiple handlers match, which one is returned is unspecified. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | sub find_handler { | ||||||||||||||||||||||||||||
| 226 | 20 | ▶ | my $filename = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_handler: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | 20 | my @match = grep {grep {$filename =~ $_} @$_[1..$#$_]} @Index_Handlers; | |||||||||||||||||||||||||||
| 228 | 20 | ▶ | return undef unless @match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 18 | return $match[0][0]; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =back | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =cut | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 29 | 1; | |||||||||||||||||||||||||||
| 237 | __END__ | ||||||||||||||||||||||||||||