| 1 | package SWISH::Prog::Lucy::Indexer; | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '0.25'; | ||
| 6 | |||
| 7 | 2 | ▶ | use base qw( SWISH::Prog::Indexer ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use SWISH::Prog::Lucy::InvIndex; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | use Sys::Hostname qw( hostname ); | ||
| 11 | use Lucy::Index::Indexer; | ||
| 12 | use Lucy::Plan::Schema; | ||
| 13 | use Lucy::Plan::FullTextType; | ||
| 14 | use Lucy::Plan::StringType; | ||
| 15 | use Lucy::Analysis::PolyAnalyzer; | ||
| 16 | |||
| 17 | use Carp; | ||
| 18 | use SWISH::3 qw( :constants ); | ||
| 19 | use Scalar::Util qw( blessed ); | ||
| 20 | use Data::Dump qw( dump ); | ||
| 21 | use Search::Tools::UTF8; | ||
| 22 | use Path::Class::File::Lockable; | ||
| 23 | |||
| 24 | __PACKAGE__->mk_accessors(qw( highlightable_fields )); | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | SWISH::Prog::Lucy::Indexer - Swish3 Apache Lucy indexer | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | use SWISH::Prog::Lucy::Indexer; | ||
| 33 | my $indexer = SWISH::Prog::Lucy::Indexer->new( | ||
| 34 | config => SWISH::Prog::Config->new(), | ||
| 35 | invindex => SWISH::Prog::Lucy::InvIndex->new(), | ||
| 36 | highlightable_fields => 0, | ||
| 37 | ); | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | SWISH::Prog::Lucy::Indexer is an Apache Lucy based indexer | ||
| 42 | class for Swish3. | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | Only new and overridden methods are documented here. See | ||
| 47 | the L<SWISH::Prog::Indexer> documentation. | ||
| 48 | |||
| 49 | =head2 init | ||
| 50 | |||
| 51 | Implements basic object set up. Called internally by new(). | ||
| 52 | If you override this method, be sure to call SUPER::init(@_) or the | ||
| 53 | equivalent. | ||
| 54 | |||
| 55 | In addition to the attributes documented in SWISH::Prog::Indexer, | ||
| 56 | this class implements the following attributes: | ||
| 57 | |||
| 58 | =over | ||
| 59 | |||
| 60 | =item highlightable_fields | ||
| 61 | |||
| 62 | Value should be 0 or 1. Default is 0. Passed directly to the | ||
| 63 | constructor for Lucy::Plan::FullTextField objects as the value | ||
| 64 | for the C<highlightable> option. | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub init { | ||
| 71 | my $self = shift; | ||
| 72 | $self->SUPER::init(@_); | ||
| 73 | |||
| 74 | $self->{highlightable_fields} = 0 | ||
| 75 | unless defined $self->{highlightable_fields}; | ||
| 76 | |||
| 77 | $self->{invindex} ||= SWISH::Prog::Lucy::InvIndex->new; | ||
| 78 | |||
| 79 | if ( $self->{invindex} && !blessed( $self->{invindex} ) ) { | ||
| 80 | $self->{invindex} | ||
| 81 | = SWISH::Prog::Lucy::InvIndex->new( path => $self->{invindex} ); | ||
| 82 | } | ||
| 83 | |||
| 84 | unless ( $self->invindex->isa('SWISH::Prog::Lucy::InvIndex') ) { | ||
| 85 | croak ref($self) | ||
| 86 | . " requires SWISH::Prog::Xapian::InvIndex-derived object"; | ||
| 87 | } | ||
| 88 | |||
| 89 | # config resolution order | ||
| 90 | # 1. default config via SWISH::3->new | ||
| 91 | |||
| 92 | # TODO can pass s3 in? | ||
| 93 | $self->{s3} ||= SWISH::3->new( | ||
| 94 | handler => sub { | ||
| 95 | $self->_handler(@_); | ||
| 96 | } | ||
| 97 | ); | ||
| 98 | |||
| 99 | #SWISH::3->describe( $self->{s3} ); | ||
| 100 | |||
| 101 | # 2. any existing header file. | ||
| 102 | my $swish_3_index | ||
| 103 | = $self->invindex->path->file( SWISH_HEADER_FILE() )->stringify; | ||
| 104 | |||
| 105 | if ( -r $swish_3_index ) { | ||
| 106 | $self->{s3}->config->add($swish_3_index); | ||
| 107 | } | ||
| 108 | |||
| 109 | # 3. via 'config' param passed to this method | ||
| 110 | if ( exists $self->{config} ) { | ||
| 111 | |||
| 112 | # this utility method defined in base SWISH::Prog::Indexer class. | ||
| 113 | $self->_verify_swish3_config(); | ||
| 114 | } | ||
| 115 | |||
| 116 | # 4. always turn off tokenizer, preferring Lucy do it | ||
| 117 | $self->{s3}->analyzer->set_tokenize(0); | ||
| 118 | |||
| 119 | my $config = $self->{s3}->config; | ||
| 120 | my $lang = $config->get_index->get( SWISH_INDEX_STEMMER_LANG() ) | ||
| 121 | || 'none'; | ||
| 122 | $self->{_lang} = $lang; # cache for finish() | ||
| 123 | my $schema = Lucy::Plan::Schema->new(); | ||
| 124 | my $analyzers = {}; | ||
| 125 | my $case_folder = Lucy::Analysis::CaseFolder->new; | ||
| 126 | my $tokenizer = Lucy::Analysis::RegexTokenizer->new; | ||
| 127 | my $multival_tokenizer | ||
| 128 | = Lucy::Analysis::RegexTokenizer->new( | ||
| 129 | pattern => '[^' . SWISH_TOKENPOS_BUMPER() . ']+' ); | ||
| 130 | |||
| 131 | # mimic StringType fields that require case and/or multival parsing. | ||
| 132 | $analyzers->{store_lc} = Lucy::Analysis::PolyAnalyzer->new( | ||
| 133 | analyzers => [ $multival_tokenizer, $case_folder ] ); | ||
| 134 | $analyzers->{store} = $multival_tokenizer; | ||
| 135 | |||
| 136 | # stemming means we fold case and tokenize too. | ||
| 137 | if ( $lang and $lang =~ m/^\w\w$/ ) { | ||
| 138 | my $stemmer | ||
| 139 | = Lucy::Analysis::SnowballStemmer->new( language => $lang ); | ||
| 140 | $analyzers->{fulltext_lc} | ||
| 141 | = Lucy::Analysis::PolyAnalyzer->new( analyzers => | ||
| 142 | [ $multival_tokenizer, $case_folder, $tokenizer, $stemmer ] ); | ||
| 143 | $analyzers->{fulltext} = Lucy::Analysis::PolyAnalyzer->new( | ||
| 144 | analyzers => [ $multival_tokenizer, $tokenizer, $stemmer ] ); | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | $analyzers->{fulltext_lc} | ||
| 148 | = Lucy::Analysis::PolyAnalyzer->new( | ||
| 149 | analyzers => [ $multival_tokenizer, $case_folder, $tokenizer, ], | ||
| 150 | ); | ||
| 151 | $analyzers->{fulltext} = Lucy::Analysis::PolyAnalyzer->new( | ||
| 152 | analyzers => [ $multival_tokenizer, $tokenizer ] ); | ||
| 153 | } | ||
| 154 | |||
| 155 | # cache our objects for later | ||
| 156 | $self->{__lucy}->{analyzers} = $analyzers; | ||
| 157 | $self->{__lucy}->{schema} = $schema; | ||
| 158 | |||
| 159 | # build the Lucy fields, which are a merger of MetaNames+PropertyNames | ||
| 160 | my %fields; | ||
| 161 | |||
| 162 | my $built_in_props = SWISH_DOC_PROP_MAP(); | ||
| 163 | |||
| 164 | my $metanames = $config->get_metanames; | ||
| 165 | my $meta_keys = $metanames->keys; | ||
| 166 | for my $name (@$meta_keys) { | ||
| 167 | my $mn = $metanames->get($name); | ||
| 168 | my $alias = $mn->alias_for; | ||
| 169 | $fields{$name}->{is_meta} = 1; | ||
| 170 | $fields{$name}->{is_meta_alias} = $alias; | ||
| 171 | $fields{$name}->{bias} = $mn->bias; | ||
| 172 | if ( exists $built_in_props->{$name} ) { | ||
| 173 | $fields{$name}->{is_prop} = 1; | ||
| 174 | $fields{$name}->{sortable} = 1; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | my $properties = $config->get_properties; | ||
| 179 | my $property_keys = $properties->keys; | ||
| 180 | for my $name (@$property_keys) { | ||
| 181 | if ( exists $built_in_props->{$name} ) { | ||
| 182 | croak | ||
| 183 | "$name is a built-in PropertyName and should not be defined in config"; | ||
| 184 | } | ||
| 185 | my $property = $properties->get($name); | ||
| 186 | my $alias = $property->alias_for; | ||
| 187 | $fields{$name}->{is_prop} = 1; | ||
| 188 | $fields{$name}->{is_prop_alias} = $alias; | ||
| 189 | if ( $property->sort ) { | ||
| 190 | $fields{$name}->{sortable} = 1; | ||
| 191 | } | ||
| 192 | for my $attr (qw( ignore_case verbatim max )) { | ||
| 193 | $fields{$name}->{$attr} = $property->$attr; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | $self->{_fields} = \%fields; | ||
| 198 | |||
| 199 | my $property_only = Lucy::Plan::StringType->new( sortable => 1, ); | ||
| 200 | my $store_no_sort = Lucy::Plan::StringType->new( | ||
| 201 | sortable => 0, | ||
| 202 | stored => 1, | ||
| 203 | ); | ||
| 204 | |||
| 205 | for my $name ( keys %fields ) { | ||
| 206 | my $field = $fields{$name}; | ||
| 207 | my $key = $name; | ||
| 208 | |||
| 209 | # if a field is purely an alias, skip it. | ||
| 210 | if ( defined $field->{is_meta_alias} | ||
| 211 | and defined $field->{is_prop_alias} ) | ||
| 212 | { | ||
| 213 | $field->{store_as}->{ $field->{is_meta_alias} } = 1; | ||
| 214 | $field->{store_as}->{ $field->{is_prop_alias} } = 1; | ||
| 215 | next; | ||
| 216 | } | ||
| 217 | |||
| 218 | if ( $field->{is_meta} and !$field->{is_prop} ) { | ||
| 219 | if ( defined $field->{is_meta_alias} ) { | ||
| 220 | $key = $field->{is_meta_alias}; | ||
| 221 | $field->{store_as}->{$key} = 1; | ||
| 222 | next; | ||
| 223 | } | ||
| 224 | |||
| 225 | #warn "spec meta $name"; | ||
| 226 | $schema->spec_field( | ||
| 227 | name => $name, | ||
| 228 | type => Lucy::Plan::FullTextType->new( | ||
| 229 | analyzer => $analyzers->{fulltext_lc}, | ||
| 230 | stored => 0, | ||
| 231 | boost => $field->{bias} || 1.0, | ||
| 232 | highlightable => $self->highlightable_fields, | ||
| 233 | ), | ||
| 234 | ); | ||
| 235 | } | ||
| 236 | |||
| 237 | # this is the trickiest case, because the field | ||
| 238 | # is both prop+meta and could be an alias for one | ||
| 239 | # and a real for the other. | ||
| 240 | # NOTE we have already eliminated (above) the case where | ||
| 241 | # the field is an alias for both. | ||
| 242 | # | ||
| 243 | # NOTE metaname==1 means we tokenize contents | ||
| 244 | elsif ( $field->{is_meta} and $field->{is_prop} ) { | ||
| 245 | if ( defined $field->{is_meta_alias} ) { | ||
| 246 | $key = $field->{is_meta_alias}; | ||
| 247 | $field->{store_as}->{$key} = 1; | ||
| 248 | } | ||
| 249 | elsif ( defined $field->{is_prop_alias} ) { | ||
| 250 | $key = $field->{is_prop_alias}; | ||
| 251 | $field->{store_as}->{$key} = 1; | ||
| 252 | } | ||
| 253 | |||
| 254 | #warn "spec meta+prop $name" . dump($field); | ||
| 255 | my $analyzer = $analyzers->{fulltext_lc}; | ||
| 256 | if ( !$field->{ignore_case} ) { | ||
| 257 | $analyzer = $analyzers->{fulltext}; | ||
| 258 | } | ||
| 259 | $schema->spec_field( | ||
| 260 | name => $name, | ||
| 261 | type => Lucy::Plan::FullTextType->new( | ||
| 262 | analyzer => $analyzer, | ||
| 263 | highlightable => $self->highlightable_fields, | ||
| 264 | sortable => $field->{sortable}, | ||
| 265 | boost => $field->{bias} || 1.0, | ||
| 266 | ), | ||
| 267 | ); | ||
| 268 | } | ||
| 269 | elsif (!$field->{is_meta} | ||
| 270 | and $field->{is_prop} | ||
| 271 | and !$field->{sortable} ) | ||
| 272 | { | ||
| 273 | if ( defined $field->{is_prop_alias} ) { | ||
| 274 | $key = $field->{is_prop_alias}; | ||
| 275 | $field->{store_as}->{$key} = 1; | ||
| 276 | next; | ||
| 277 | } | ||
| 278 | |||
| 279 | #warn "spec prop !sort $name"; | ||
| 280 | my $an_key = 'store'; | ||
| 281 | if ( $field->{ignore_case} ) { | ||
| 282 | $an_key = 'store_lc'; | ||
| 283 | } | ||
| 284 | |||
| 285 | # StringType has no analyzer | ||
| 286 | # so we must switch to FullTextType and | ||
| 287 | # use a case-folding-only analyzer. | ||
| 288 | my $type = Lucy::Plan::FullTextType->new( | ||
| 289 | analyzer => $analyzers->{$an_key}, | ||
| 290 | highlightable => $self->highlightable_fields, | ||
| 291 | sortable => 0, | ||
| 292 | boost => $field->{bias} || 1.0, | ||
| 293 | ); | ||
| 294 | $schema->spec_field( | ||
| 295 | name => $name, | ||
| 296 | type => $type, | ||
| 297 | ); | ||
| 298 | } | ||
| 299 | elsif (!$field->{is_meta} | ||
| 300 | and $field->{is_prop} | ||
| 301 | and $field->{sortable} ) | ||
| 302 | { | ||
| 303 | if ( defined $field->{is_prop_alias} ) { | ||
| 304 | $key = $field->{is_prop_alias}; | ||
| 305 | $field->{store_as}->{$key} = 1; | ||
| 306 | next; | ||
| 307 | } | ||
| 308 | |||
| 309 | #warn "spec prop sort $name"; | ||
| 310 | my $an_key = 'store'; | ||
| 311 | if ( $field->{ignore_case} ) { | ||
| 312 | $an_key = 'store_lc'; | ||
| 313 | } | ||
| 314 | |||
| 315 | # if StringType has no analyzer | ||
| 316 | # so we must switch to FullTextType and | ||
| 317 | # use a case-folding-only analyzer. | ||
| 318 | my $type = Lucy::Plan::FullTextType->new( | ||
| 319 | analyzer => $analyzers->{$an_key}, | ||
| 320 | highlightable => $self->highlightable_fields, | ||
| 321 | sortable => 1, | ||
| 322 | boost => $field->{bias} || 1.0, | ||
| 323 | ); | ||
| 324 | $schema->spec_field( | ||
| 325 | name => $name, | ||
| 326 | type => $type, | ||
| 327 | ); | ||
| 328 | } | ||
| 329 | $field->{store_as}->{$name} = 1; | ||
| 330 | } | ||
| 331 | |||
| 332 | for my $name ( keys %$built_in_props ) { | ||
| 333 | if ( exists $fields{$name} ) { | ||
| 334 | my $field = $fields{$name}; | ||
| 335 | |||
| 336 | #carp "found $name in built-in props: " . dump($field); | ||
| 337 | |||
| 338 | # in theory this should never happen. | ||
| 339 | if ( !$field->{is_prop} ) { | ||
| 340 | croak | ||
| 341 | "$name is a built-in PropertyName but not defined as a PropertyName in config"; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | # default property | ||
| 346 | else { | ||
| 347 | $schema->spec_field( name => $name, type => $property_only ); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | #dump( \%fields ); | ||
| 352 | |||
| 353 | # TODO can pass lucy in? | ||
| 354 | my $hostname = hostname() or croak "Can't get unique hostname"; | ||
| 355 | my $manager = Lucy::Index::IndexManager->new( host => $hostname ); | ||
| 356 | $self->{lucy} ||= Lucy::Index::Indexer->new( | ||
| 357 | schema => $schema, | ||
| 358 | index => $self->invindex->path, | ||
| 359 | create => 1, | ||
| 360 | manager => $manager, | ||
| 361 | ); | ||
| 362 | |||
| 363 | return $self; | ||
| 364 | } | ||
| 365 | |||
| 366 | sub _add_new_field { | ||
| 367 | my ( $self, $metaname, $propname ) = @_; | ||
| 368 | my $fields = $self->{_fields}; | ||
| 369 | my $alias = $metaname->alias_for; | ||
| 370 | my $name = $metaname->name; | ||
| 371 | if ( !exists $fields->{$name} ) { | ||
| 372 | $fields->{$name} = {}; | ||
| 373 | } | ||
| 374 | my $field = $fields->{$name}; | ||
| 375 | $field->{is_meta} = 1; | ||
| 376 | $field->{is_meta_alias} = $alias; | ||
| 377 | $field->{bias} = $metaname->bias; | ||
| 378 | $field->{store_as}->{$name} = 1; | ||
| 379 | |||
| 380 | if ($propname) { | ||
| 381 | my $prop_alias = $propname->alias_for; | ||
| 382 | $field->{is_prop} = 1; | ||
| 383 | $field->{is_prop_alias} = $prop_alias; | ||
| 384 | if ( $propname->sort ) { | ||
| 385 | $field->{sortable} = 1; | ||
| 386 | } | ||
| 387 | for my $attr (qw( ignore_case verbatim max )) { | ||
| 388 | $field->{$attr} = $propname->$attr; | ||
| 389 | } | ||
| 390 | } | ||
| 391 | |||
| 392 | # a newly defined MetaName matching an already-defined PropertyName | ||
| 393 | # or a new MetaName+PropertyName | ||
| 394 | if ( $field->{is_prop} ) { | ||
| 395 | $self->{__lucy}->{schema}->spec_field( | ||
| 396 | name => $name, | ||
| 397 | type => Lucy::Plan::FullTextType->new( | ||
| 398 | analyzer => ( | ||
| 399 | $field->{ignore_case} | ||
| 400 | ? $self->{__lucy}->{analyzers}->{fulltext_lc} | ||
| 401 | : $self->{__lucy}->{analyzers}->{fulltext} | ||
| 402 | ), | ||
| 403 | highlightable => $self->highlightable_fields, | ||
| 404 | sortable => $field->{sortable}, | ||
| 405 | boost => $field->{bias} || 1.0, | ||
| 406 | ), | ||
| 407 | ); | ||
| 408 | } | ||
| 409 | |||
| 410 | # just a new MetaName | ||
| 411 | else { | ||
| 412 | |||
| 413 | $self->{__lucy}->{schema}->spec_field( | ||
| 414 | name => $name, | ||
| 415 | type => Lucy::Plan::FullTextType->new( | ||
| 416 | analyzer => $self->{__lucy}->{analyzers}->{fulltext_lc}, | ||
| 417 | stored => 0, | ||
| 418 | boost => $field->{bias} || 1.0, | ||
| 419 | highlightable => $self->highlightable_fields, | ||
| 420 | ), | ||
| 421 | ); | ||
| 422 | |||
| 423 | } | ||
| 424 | |||
| 425 | #warn "Added new field $name: " . dump( $field ); | ||
| 426 | |||
| 427 | return $field; | ||
| 428 | } | ||
| 429 | |||
| 430 | =head2 process( I<doc> ) | ||
| 431 | |||
| 432 | Overrides base method to parse the I<doc> (a SWISH::Prog::Doc object) | ||
| 433 | with the SWISH::3 parse_buffer() method. | ||
| 434 | |||
| 435 | =cut | ||
| 436 | |||
| 437 | sub process { | ||
| 438 | my $self = shift; | ||
| 439 | my $doc = $self->SUPER::process(@_); | ||
| 440 | $self->{s3}->parse_buffer("$doc"); | ||
| 441 | return $doc; | ||
| 442 | } | ||
| 443 | |||
| 444 | my $doc_prop_map = SWISH_DOC_PROP_MAP(); | ||
| 445 | |||
| 446 | sub _handler { | ||
| 447 | my ( $self, $data ) = @_; | ||
| 448 | my $config = $data->config; | ||
| 449 | my $conf_props = $config->get_properties; | ||
| 450 | my $conf_metas = $config->get_metanames; | ||
| 451 | |||
| 452 | # will hold all the parsed text, keyed by field name | ||
| 453 | my %doc; | ||
| 454 | |||
| 455 | # Swish built-in fields first | ||
| 456 | for my $propname ( keys %$doc_prop_map ) { | ||
| 457 | my $attr = $doc_prop_map->{$propname}; | ||
| 458 | $doc{$propname} = [ $data->doc->$attr ]; | ||
| 459 | } | ||
| 460 | |||
| 461 | # fields parsed from document | ||
| 462 | my $props = $data->properties; | ||
| 463 | my $metas = $data->metanames; | ||
| 464 | |||
| 465 | # field def cache | ||
| 466 | my $fields = $self->{_fields}; | ||
| 467 | |||
| 468 | # may need to add newly-discovered fields from $metas | ||
| 469 | # that were added via UndefinedMetaTags e.g. | ||
| 470 | for my $mname ( keys %$metas ) { | ||
| 471 | if ( !exists $fields->{$mname} ) { | ||
| 472 | |||
| 473 | #warn "New field: $mname\n"; | ||
| 474 | my $prop; | ||
| 475 | if ( exists $props->{$mname} ) { | ||
| 476 | $prop = $conf_props->get($mname); | ||
| 477 | } | ||
| 478 | $self->_add_new_field( $conf_metas->get($mname), $prop ); | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | #dump $fields; | ||
| 483 | #dump $props; | ||
| 484 | #dump $metas; | ||
| 485 | for my $fname ( sort keys %$fields ) { | ||
| 486 | my $field = $self->{_fields}->{$fname}; | ||
| 487 | next if $field->{is_prop_alias}; | ||
| 488 | next if $field->{is_meta_alias}; | ||
| 489 | |||
| 490 | my @keys = keys %{ $field->{store_as} }; | ||
| 491 | |||
| 492 | for my $key (@keys) { | ||
| 493 | |||
| 494 | # prefer properties over metanames because | ||
| 495 | # properties have verbatim flag, which affects | ||
| 496 | # the stored whitespace. | ||
| 497 | |||
| 498 | if ( $field->{is_prop} and !exists $doc_prop_map->{$fname} ) { | ||
| 499 | push( @{ $doc{$key} }, @{ $props->{$fname} } ); | ||
| 500 | } | ||
| 501 | elsif ( $field->{is_meta} ) { | ||
| 502 | push( @{ $doc{$key} }, @{ $metas->{$fname} } ); | ||
| 503 | } | ||
| 504 | else { | ||
| 505 | croak "field '$fname' is neither a PropertyName nor MetaName"; | ||
| 506 | } | ||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | # serialize the doc with our tokenpos_bump char | ||
| 511 | for my $k ( keys %doc ) { | ||
| 512 | $doc{$k} = to_utf8( join( "\003", @{ $doc{$k} } ) ); | ||
| 513 | } | ||
| 514 | |||
| 515 | $self->debug and carp dump \%doc; | ||
| 516 | |||
| 517 | # make sure we delete any existing doc with same URI | ||
| 518 | $self->{lucy}->delete_by_term( | ||
| 519 | field => 'swishdocpath', | ||
| 520 | term => $doc{swishdocpath} | ||
| 521 | ); | ||
| 522 | |||
| 523 | $self->{lucy}->add_doc( \%doc ); | ||
| 524 | } | ||
| 525 | |||
| 526 | =head2 finish | ||
| 527 | |||
| 528 | Calls commit() on the internal Lucy::Indexer object, | ||
| 529 | writes the C<swish.xml> header file and calls the superclass finish() | ||
| 530 | method. | ||
| 531 | |||
| 532 | =cut | ||
| 533 | |||
| 534 | my @chars = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9 ); | ||
| 535 | |||
| 536 | sub finish { | ||
| 537 | my $self = shift; | ||
| 538 | |||
| 539 | return 0 if $self->{_is_finished}; | ||
| 540 | |||
| 541 | # get a lock on our header file till | ||
| 542 | # this entire transaction is complete. | ||
| 543 | # Note that we trust the Lucy locking feature | ||
| 544 | # to have prevented any other process | ||
| 545 | # from getting a lock on the invindex itself, | ||
| 546 | # but we want to make sure nothing interrupts | ||
| 547 | # us from writing our own header after calling ->commit(). | ||
| 548 | my $invindex = $self->invindex->path; | ||
| 549 | my $header = $invindex->file( SWISH_HEADER_FILE() )->stringify; | ||
| 550 | my $lock_file = Path::Class::File::Lockable->new($header); | ||
| 551 | if ( $lock_file->locked ) { | ||
| 552 | croak "Lock file found on $header -- cannot commit indexing changes"; | ||
| 553 | } | ||
| 554 | $lock_file->lock; | ||
| 555 | |||
| 556 | # commit our changes | ||
| 557 | $self->{lucy}->commit(); | ||
| 558 | |||
| 559 | # get total doc count | ||
| 560 | my $polyreader = Lucy::Index::PolyReader->open( index => "$invindex", ); | ||
| 561 | my $doc_count = $polyreader->doc_count(); | ||
| 562 | |||
| 563 | # write header | ||
| 564 | my $index = $self->{s3}->config->get_index; | ||
| 565 | |||
| 566 | # poor man's uuid | ||
| 567 | my $uuid = join( "", @chars[ map { rand @chars } ( 1 .. 24 ) ] ); | ||
| 568 | |||
| 569 | $index->set( SWISH_INDEX_NAME(), "$invindex" ); | ||
| 570 | $index->set( SWISH_INDEX_FORMAT(), 'Lucy' ); | ||
| 571 | $index->set( SWISH_INDEX_STEMMER_LANG(), $self->{_lang} ); | ||
| 572 | $index->set( "DocCount", $doc_count ); | ||
| 573 | $index->set( "UUID", $uuid ); | ||
| 574 | |||
| 575 | $self->{s3}->config->write($header); | ||
| 576 | |||
| 577 | # transaction complete | ||
| 578 | $lock_file->unlock; | ||
| 579 | |||
| 580 | $self->debug and carp "wrote $header with uuid $uuid"; | ||
| 581 | |||
| 582 | $self->{s3} = undef; # invalidate this indexer | ||
| 583 | |||
| 584 | $self->SUPER::finish(@_); | ||
| 585 | |||
| 586 | $self->{_is_finished} = 1; | ||
| 587 | |||
| 588 | $self->debug and carp "$doc_count docs indexed"; | ||
| 589 | |||
| 590 | return $doc_count; | ||
| 591 | } | ||
| 592 | |||
| 593 | =head2 get_lucy | ||
| 594 | |||
| 595 | Returns the internal Lucy::Index::Indexer object. | ||
| 596 | |||
| 597 | =cut | ||
| 598 | |||
| 599 | sub get_lucy { | ||
| 600 | return shift->{lucy}; | ||
| 601 | } | ||
| 602 | |||
| 603 | =head2 abort | ||
| 604 | |||
| 605 | Sets the internal Lucy::Index::Indexer to undef, | ||
| 606 | which should release any locks on the index. | ||
| 607 | Also flags the SWISH::Prog::Lucy::Indexer object | ||
| 608 | as stale. | ||
| 609 | |||
| 610 | =cut | ||
| 611 | |||
| 612 | sub abort { | ||
| 613 | my $self = shift; | ||
| 614 | $self->{lucy} = undef; | ||
| 615 | $self->{_is_finished} = 1; | ||
| 616 | $self->{s3} = undef; | ||
| 617 | } | ||
| 618 | |||
| 619 | 1; | ||
| 620 | |||
| 621 | __END__ | ||