| 1 | package SWISH::Prog::Native::Indexer; | ||
| 2 | 10 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 10 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 10 | ▶ | use base qw( SWISH::Prog::Indexer ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 10 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 10 | ▶ | use File::Temp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use SWISH::Prog::Native::InvIndex; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use SWISH::Prog::Config; | ||
| 9 | use Scalar::Util qw( blessed ); | ||
| 10 | use File::Copy (); | ||
| 11 | |||
| 12 | our $VERSION = '0.75'; | ||
| 13 | |||
| 14 | my $invindex_class = 'SWISH::Prog::Native::InvIndex'; | ||
| 15 | |||
| 16 | __PACKAGE__->mk_accessors(qw( fh exe opts )); | ||
| 17 | |||
| 18 | =head1 NAME | ||
| 19 | |||
| 20 | SWISH::Prog::Native::Indexer - wrapper around Swish-e binary | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | use SWISH::Prog::Native::Indexer; | ||
| 25 | my $indexer = SWISH::Prog::Native::Indexer->new( | ||
| 26 | invindex => SWISH::Prog::Native::InvIndex->new, | ||
| 27 | config => SWISH::Prog::Config->new, | ||
| 28 | count => 0, | ||
| 29 | clobber => 1, | ||
| 30 | flush => 10000, | ||
| 31 | started => time(), | ||
| 32 | ); | ||
| 33 | $indexer->start; | ||
| 34 | for my $doc (@list_of_docs) { | ||
| 35 | $indexer->process($doc); | ||
| 36 | } | ||
| 37 | $indexer->finish; | ||
| 38 | |||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | The Native Indexer is a wrapper around the swish-e version 2.x binary tool. | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =head2 new | ||
| 47 | |||
| 48 | Create indexer object. All the following parameters are also accessor methods. | ||
| 49 | |||
| 50 | =over | ||
| 51 | |||
| 52 | =item index | ||
| 53 | |||
| 54 | A SWISH::Prog::InvIndex::Native object. | ||
| 55 | |||
| 56 | =item config | ||
| 57 | |||
| 58 | A SWISH::Prog::Config object. | ||
| 59 | |||
| 60 | =item exe | ||
| 61 | |||
| 62 | The path to the C<swish-e> executable. If empty, will just look in $ENV{PATH}. | ||
| 63 | |||
| 64 | =item verbose | ||
| 65 | |||
| 66 | Takes same args as C<swish-e -v> option. | ||
| 67 | |||
| 68 | =item warnings | ||
| 69 | |||
| 70 | Takes same args as C<swish-e -W> option. | ||
| 71 | |||
| 72 | =item opts | ||
| 73 | |||
| 74 | String of options passed directly to the swish-e program. | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | =head2 init | ||
| 81 | |||
| 82 | Initialize object. Called by new(). | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub init { | ||
| 87 | my $self = shift; | ||
| 88 | $self->SUPER::init(@_); | ||
| 89 | |||
| 90 | # default config | ||
| 91 | $self->{config} ||= SWISH::Prog::Config->new; | ||
| 92 | |||
| 93 | # default index | ||
| 94 | $self->{invindex} ||= $self->{config}->IndexFile || $invindex_class->new; | ||
| 95 | |||
| 96 | if ( $self->{invindex} && !blessed( $self->{invindex} ) ) { | ||
| 97 | $self->{invindex} = $invindex_class->new( path => $self->{invindex} ); | ||
| 98 | } | ||
| 99 | |||
| 100 | unless ( $self->invindex->isa($invindex_class) ) { | ||
| 101 | croak ref($self) . " requires $invindex_class-derived object"; | ||
| 102 | } | ||
| 103 | |||
| 104 | $self->{exe} ||= 'swish-e'; # let PATH find it | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | =head2 swish_check | ||
| 109 | |||
| 110 | Returns true if the exe() executable works, false otherwise. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub swish_check { | ||
| 115 | my $self = shift; | ||
| 116 | if ( exists $self->{_exe_version} ) { | ||
| 117 | return $self->{_exe_version}; | ||
| 118 | } | ||
| 119 | my $cmd = $self->exe . " -V"; | ||
| 120 | chomp( my @vers = `$cmd` ); | ||
| 121 | if ( !@vers ) { | ||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | $self->{_exe_version} = $vers[0]; | ||
| 125 | $self->{_exe_version} =~ s/SWISH-E //; | ||
| 126 | return $self->{_exe_version}; | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 start( [cmd] ) | ||
| 130 | |||
| 131 | Start the indexer on its merry way. Stores the filehandle | ||
| 132 | in fh(). | ||
| 133 | |||
| 134 | Returns the $indexer object. | ||
| 135 | |||
| 136 | You likely don't want to pass I<cmd> in but let start() construct | ||
| 137 | it for you. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub start { | ||
| 142 | my $self = shift; | ||
| 143 | $self->SUPER::start(@_); | ||
| 144 | |||
| 145 | my $index = $self->invindex->file; | ||
| 146 | my $v = $self->verbose || 0; | ||
| 147 | my $w = $self->warnings || 0; # suffer the peril! | ||
| 148 | my $opts = $self->opts || ''; | ||
| 149 | my $exe = $self->exe; | ||
| 150 | |||
| 151 | my $swish_version = $self->swish_check; | ||
| 152 | my $cmd = shift | ||
| 153 | || "$exe $opts -f $index -v$v -W$w -S prog -i stdin"; | ||
| 154 | |||
| 155 | # swish3 compat only in 2.4.8 or higher | ||
| 156 | if ( $swish_version ge '2.4.8' || $swish_version ge '2.5.8' ) { | ||
| 157 | $cmd .= " -D '\\x03' "; | ||
| 158 | } | ||
| 159 | |||
| 160 | if ( !$self->config->file ) { | ||
| 161 | $self->config->write2( 0, 1 ); # write in prog mode | ||
| 162 | } | ||
| 163 | my $config_file = $self->config->file; | ||
| 164 | $cmd .= ' -c ' . $config_file; | ||
| 165 | |||
| 166 | $self->debug and carp "opening: $cmd"; | ||
| 167 | |||
| 168 | local $| = 1; | ||
| 169 | |||
| 170 | open( SWISH, "| $cmd" ) or croak "can't exec $cmd: $!\n"; | ||
| 171 | |||
| 172 | # must print bytes as is even if swish-e won't index them as UTF-8 | ||
| 173 | binmode( SWISH, ':raw' ); | ||
| 174 | |||
| 175 | $self->fh( *SWISH{IO} ); | ||
| 176 | |||
| 177 | return $self; | ||
| 178 | } | ||
| 179 | |||
| 180 | =head2 fh | ||
| 181 | |||
| 182 | Get or set the open() filehandle for the swish-e process. B<CAUTION:> | ||
| 183 | don't set unless you know what you're doing. | ||
| 184 | |||
| 185 | You can print() to the filehandle using the SWISH::Prog index() method. | ||
| 186 | Or do it directly like: | ||
| 187 | |||
| 188 | print { $indexer->fh } "your headers and body here"; | ||
| 189 | |||
| 190 | The filehandle is close()'d by the finish() method. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | =head2 finish | ||
| 195 | |||
| 196 | Close the open fh() filehandle and check for any errors. | ||
| 197 | |||
| 198 | Called by the magic DESTROY method so $indexer will finish() | ||
| 199 | whenever it goes out of scope. | ||
| 200 | |||
| 201 | =cut | ||
| 202 | |||
| 203 | sub DESTROY { | ||
| 204 | shift->finish(); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub finish { | ||
| 208 | my $self = shift; | ||
| 209 | return 1 unless $self->fh; | ||
| 210 | |||
| 211 | # close indexer filehandle | ||
| 212 | my $e = close( $self->fh ); | ||
| 213 | unless ($e) { | ||
| 214 | if ( $? == 0 ) { | ||
| 215 | |||
| 216 | # false positive ?? | ||
| 217 | return; | ||
| 218 | } | ||
| 219 | |||
| 220 | carp "error $e: can't close indexer (\$?: $?): $!\n"; | ||
| 221 | |||
| 222 | if ( $? == 256 ) { | ||
| 223 | |||
| 224 | # no docs indexed | ||
| 225 | # TODO remove temp indexes | ||
| 226 | |||
| 227 | } | ||
| 228 | |||
| 229 | } | ||
| 230 | |||
| 231 | # destroy fh, in case close() didn't do it. | ||
| 232 | $self->fh(undef); | ||
| 233 | |||
| 234 | # write header | ||
| 235 | $self->config->write3( $self->invindex->meta_file->stringify ); | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | =head2 merge( @I<InvIndex objects> ) | ||
| 240 | |||
| 241 | merge() will merge @I<SWISH::Prog::Native::InvIndex objects> | ||
| 242 | together with the index named in the calling Indexer object. | ||
| 243 | |||
| 244 | Returns the $indexer object on success, croaks on failure. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | sub merge { | ||
| 249 | my $self = shift; | ||
| 250 | if ( !@_ ) { | ||
| 251 | croak "merge() requires some InvIndex objects to work with"; | ||
| 252 | } | ||
| 253 | |||
| 254 | my $invindex_class = blessed( $self->invindex ); | ||
| 255 | |||
| 256 | # we want a collection of path names to work with | ||
| 257 | my @names; | ||
| 258 | for (@_) { | ||
| 259 | if ( blessed($_) and $_->isa($invindex_class) ) { | ||
| 260 | push( @names, $_->file->stringify ); | ||
| 261 | } | ||
| 262 | elsif ( -s $_ ) { | ||
| 263 | push( @names, "$_" ); # force whatever it is to stringify | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | croak "$_ is not a InvIndex object or a file path"; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | for (@names) { | ||
| 271 | if ( !-s "$_.prop" ) | ||
| 272 | { # test .prop file since that is both 2.4 and 2.6 | ||
| 273 | croak "$_ appears to be empty: $!"; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | if ( scalar(@names) > 60 ) { | ||
| 278 | carp "Likely too many indexes to merge at one time!" | ||
| 279 | . "Your OS may have an open file limit."; | ||
| 280 | } | ||
| 281 | my $to_merge = join( ' ', @names, $self->invindex->file ); | ||
| 282 | my $current_path = $self->invindex->path; | ||
| 283 | my $verbose = $self->verbose || 0; | ||
| 284 | my $opts = $self->opts || ''; | ||
| 285 | my $exe = $self->exe || 'swish-e'; | ||
| 286 | |||
| 287 | # we can't replace the index in-place | ||
| 288 | # so we create a new temp index, then mv() back | ||
| 289 | my $tmpindex = $invindex_class->new( | ||
| 290 | path => $current_path->parent->subdir('tmpmerge.index') ); | ||
| 291 | $tmpindex->path->mkpath( $self->debug ); | ||
| 292 | my $cmd = "$exe $opts -v$verbose -M $to_merge $tmpindex/index.swish-e"; | ||
| 293 | |||
| 294 | $self->debug and carp "opening: $cmd"; | ||
| 295 | |||
| 296 | local $| = 1; | ||
| 297 | |||
| 298 | open( SWISH, "$cmd |" ) | ||
| 299 | or croak "can't start merge: $!\n"; | ||
| 300 | |||
| 301 | while (<SWISH>) { | ||
| 302 | if ( $verbose or $self->debug ) { | ||
| 303 | print STDERR $_; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | close(SWISH) or croak "can't close merge(): $cmd: $! ($?)\n"; | ||
| 308 | |||
| 309 | # assume that the header file is the same for | ||
| 310 | # all the merged files, and preserve this one. | ||
| 311 | my $header = $self->invindex->meta_file->stringify; | ||
| 312 | File::Copy::copy( $header, $tmpindex->meta_file->stringify ) | ||
| 313 | or croak "copy $header -> $tmpindex failed: $!"; | ||
| 314 | |||
| 315 | # archive the existing just in case | ||
| 316 | my $archive = "$current_path.$$"; | ||
| 317 | File::Copy::move( $current_path, $archive ) | ||
| 318 | or croak "move $current_path -> $archive failed: $!"; | ||
| 319 | |||
| 320 | if ( !File::Copy::move( $tmpindex, $current_path ) ) { | ||
| 321 | carp "move $tmpindex -> $current_path failed: $!"; | ||
| 322 | carp "restoring original index $current_path"; | ||
| 323 | File::Copy::move( $archive, $current_path ) | ||
| 324 | or croak "move $archive -> $current_path failed: $!"; | ||
| 325 | } | ||
| 326 | Path::Class::dir($archive)->rmtree | ||
| 327 | or croak "failed to rmtree $archive: $!"; | ||
| 328 | |||
| 329 | return $self; | ||
| 330 | } | ||
| 331 | |||
| 332 | =head2 process( I<doc> ) | ||
| 333 | |||
| 334 | process() will parse and index I<doc>. I<doc> should be a | ||
| 335 | SWISH::Prog::Doc instance. | ||
| 336 | |||
| 337 | Will croak() on failure. | ||
| 338 | |||
| 339 | =cut | ||
| 340 | |||
| 341 | sub process { | ||
| 342 | my $self = shift; | ||
| 343 | my $doc = $self->SUPER::process(@_); | ||
| 344 | $doc->version(2); | ||
| 345 | |||
| 346 | if ( $self->debug ) { | ||
| 347 | warn $doc; | ||
| 348 | } | ||
| 349 | |||
| 350 | print { $self->fh } $doc | ||
| 351 | or croak "failed to print to filehandle " . $self->fh . ": $!\n"; | ||
| 352 | |||
| 353 | return $doc; | ||
| 354 | } | ||
| 355 | |||
| 356 | =head2 add( I<doc> ) | ||
| 357 | |||
| 358 | Add I<doc> to the index. | ||
| 359 | |||
| 360 | Note this is slower than merge(). If you have multiple I<doc> objects, | ||
| 361 | create a new Indexer object and process() them all, then merge() the two | ||
| 362 | InvIndex objects. | ||
| 363 | |||
| 364 | my $indexer = SWISH::Prog::Native::Indexer->new(invindex => 'tmpmerge'); | ||
| 365 | $indexer->start; | ||
| 366 | for my $doc (@list_of_docs) { | ||
| 367 | $indexer->process($doc); | ||
| 368 | } | ||
| 369 | $indexer->finish; | ||
| 370 | $indexer->merge( 'path/to/other/index' ); | ||
| 371 | |||
| 372 | =cut | ||
| 373 | |||
| 374 | sub add { | ||
| 375 | my $self = shift; | ||
| 376 | my $doc = shift or croak "need SWISH::Prog::Doc object to add()"; | ||
| 377 | unless ( $doc->isa('SWISH::Prog::Doc') ) { | ||
| 378 | croak "$doc is not a SWISH::Prog::Doc object"; | ||
| 379 | } | ||
| 380 | |||
| 381 | # create a temporary invindex of $doc | ||
| 382 | my $invindex_class = blessed( $self->invindex ); | ||
| 383 | my $tmpdir = Path::Class::dir( File::Temp::tempdir( CLEANUP => 1 ) ); | ||
| 384 | my $tmpinvindex = $invindex_class->new( path => $tmpdir ); | ||
| 385 | |||
| 386 | # spawn a new indexer with similar attributes | ||
| 387 | my $indexer = blessed($self)->new( | ||
| 388 | verbose => $self->verbose, | ||
| 389 | debug => $self->debug, | ||
| 390 | invindex => $tmpinvindex, | ||
| 391 | config => $self->config, | ||
| 392 | ); | ||
| 393 | $indexer->start; | ||
| 394 | $indexer->process($doc); | ||
| 395 | $indexer->finish; | ||
| 396 | |||
| 397 | # merge it | ||
| 398 | $self->merge($tmpinvindex); | ||
| 399 | |||
| 400 | # remove temp invindex | ||
| 401 | $tmpdir->rmtree or croak "failed to clean up temp invindex $tmpdir: $!"; | ||
| 402 | |||
| 403 | return $self; | ||
| 404 | } | ||
| 405 | |||
| 406 | 1; | ||
| 407 | |||
| 408 | __END__ | ||