| 1 | package KinoSearch1::InvIndexer; | ||||||||||||||||||||||||||||||||
| 2 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use base qw( KinoSearch1::Util::Class ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use constant UNINITIALIZED => 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 34 | ▶ | use constant INITIALIZED => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use constant FINISHED => 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 34 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 34 | __PACKAGE__->init_instance_vars( | |||||||||||||||||||||||||||||||
| 13 | # constructor args / members | ||||||||||||||||||||||||||||||||
| 14 | create => undef, | ||||||||||||||||||||||||||||||||
| 15 | invindex => undef, | ||||||||||||||||||||||||||||||||
| 16 | analyzer => undef, | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | # members | ||||||||||||||||||||||||||||||||
| 19 | reader => undef, | ||||||||||||||||||||||||||||||||
| 20 | analyzers => undef, | ||||||||||||||||||||||||||||||||
| 21 | sinfos => undef, | ||||||||||||||||||||||||||||||||
| 22 | finfos => undef, | ||||||||||||||||||||||||||||||||
| 23 | doc_template => undef, | ||||||||||||||||||||||||||||||||
| 24 | frozen_doc => undef, | ||||||||||||||||||||||||||||||||
| 25 | similarity => undef, | ||||||||||||||||||||||||||||||||
| 26 | field_sims => undef, | ||||||||||||||||||||||||||||||||
| 27 | seg_writer => undef, | ||||||||||||||||||||||||||||||||
| 28 | write_lock => undef, | ||||||||||||||||||||||||||||||||
| 29 | state => UNINITIALIZED, | ||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 34 | ▶ | use Storable qw( freeze thaw ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 34 | ▶ | use File::Spec::Functions qw( catfile tmpdir ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 34 | ▶ | use KinoSearch1::Document::Doc; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 34 | ▶ | use KinoSearch1::Document::Field; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 38 | 34 | ▶ | use KinoSearch1::Analysis::Analyzer; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 39 | 34 | ▶ | use KinoSearch1::Store::FSInvIndex; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 40 | 34 | ▶ | use KinoSearch1::Index::FieldInfos; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 41 | 34 | ▶ | use KinoSearch1::Index::FieldsReader; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 42 | 34 | ▶ | use KinoSearch1::Index::IndexReader; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | 34 | ▶ | use KinoSearch1::Index::SegInfos; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 34 | ▶ | use KinoSearch1::Index::SegWriter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 45 | 34 | ▶ | use KinoSearch1::Index::IndexFileNames qw( | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 46 | WRITE_LOCK_NAME | ||||||||||||||||||||||||||||||||
| 47 | COMMIT_LOCK_NAME | ||||||||||||||||||||||||||||||||
| 48 | WRITE_LOCK_TIMEOUT | ||||||||||||||||||||||||||||||||
| 49 | COMMIT_LOCK_TIMEOUT | ||||||||||||||||||||||||||||||||
| 50 | 34 | ); | |||||||||||||||||||||||||||||||
| 51 | 34 | ▶ | use KinoSearch1::Search::Similarity; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 63 | ▶ | sub init_instance { | ||||||||||||||||||||||||||||||
|
Subroutine
init_instance: called CC=10 SCAR=23.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 54 | 63 | my $self = shift; | |||||||||||||||||||||||||||||||
| 55 | 63 | $self->{analyzers} = {}; | |||||||||||||||||||||||||||||||
| 56 | 63 | $self->{field_sims} = {}; | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | # use a no-op Analyzer if not supplied | ||||||||||||||||||||||||||||||||
| 59 | 63 | ▶ | $self->{analyzer} ||= KinoSearch1::Analysis::Analyzer->new; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"analyzer"} ||= "KinoSearch1::Analysis::Analyzer"->new
MC/DC0%0/2
$self->{"analyzer"} ||= "KinoSearch1::Analysis::Analyzer"->new
$self->{"analyzer"}
"KinoSearch1::Analysis::Analyzer"->new
Truth table66%2/3
A ||= B
A= $self->{"analyzer"}B= "KinoSearch1::Analysis::Analyzer"->new
| |||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | # create a few members | ||||||||||||||||||||||||||||||||
| 62 | 63 | $self->{similarity} = KinoSearch1::Search::Similarity->new; | |||||||||||||||||||||||||||||||
| 63 | 63 | $self->{sinfos} = KinoSearch1::Index::SegInfos->new; | |||||||||||||||||||||||||||||||
| 64 | 63 | $self->{doc_template} = KinoSearch1::Document::Doc->new; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | # confirm or create an InvIndex object | ||||||||||||||||||||||||||||||||
| 67 | 63 | my $invindex; | |||||||||||||||||||||||||||||||
| 68 | 63 | ▶ | if ( blessed( $self->{invindex} ) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($self->{"invindex"}) and $self->{"invindex"}->isa("KinoSearch1::Store::InvIndex")
MC/DC50%1/2
builtin::blessed($self->{"invindex"}) and $self->{"invindex"}->isa("KinoSearch1::Store::InvIndex")
builtin::blessed($self->{"invindex"})
$self->{"invindex"}->isa("KinoSearch1::Store::InvIndex")
Truth table66%2/3
A and B
A= builtin::blessed($self->{"invindex"})B= $self->{"invindex"}->isa("KinoSearch1::Store::InvIndex")
| |||||||||||||||||||||||||||||||||
| 69 | and $self->{invindex}->isa('KinoSearch1::Store::InvIndex') ) | ||||||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||||||
| 71 | 47 | $invindex = $self->{invindex}; | |||||||||||||||||||||||||||||||
| 72 | $self->{create} = $invindex->get_create | ||||||||||||||||||||||||||||||||
| 73 | 47 | ▶ | unless defined $self->{create}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | elsif ( defined $self->{invindex} ) { | ||||||||||||||||||||||||||||||||
| 76 | $invindex = $self->{invindex} = KinoSearch1::Store::FSInvIndex->new( | ||||||||||||||||||||||||||||||||
| 77 | create => $self->{create}, | ||||||||||||||||||||||||||||||||
| 78 | path => $self->{invindex}, | ||||||||||||||||||||||||||||||||
| 79 | 16 | ); | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | else { | ||||||||||||||||||||||||||||||||
| 82 | 0 | croak("Required parameter 'invindex' not supplied"); | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | # get a write lock for this invindex. | ||||||||||||||||||||||||||||||||
| 86 | 63 | my $write_lock = $invindex->make_lock( | |||||||||||||||||||||||||||||||
| 87 | lock_name => WRITE_LOCK_NAME, | ||||||||||||||||||||||||||||||||
| 88 | timeout => WRITE_LOCK_TIMEOUT, | ||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||
| 90 | 63 | ▶ | if ( $write_lock->obtain ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 91 | # only assign if successful, otherwise DESTROY unlocks (bad!) | ||||||||||||||||||||||||||||||||
| 92 | 63 | $self->{write_lock} = $write_lock; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||||||
| 95 | 0 | croak( "invindex locked: " . $write_lock->get_lock_name ); | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | # read/write SegInfos | ||||||||||||||||||||||||||||||||
| 99 | 63 | eval { | |||||||||||||||||||||||||||||||
| 100 | $invindex->run_while_locked( | ||||||||||||||||||||||||||||||||
| 101 | lock_name => COMMIT_LOCK_NAME, | ||||||||||||||||||||||||||||||||
| 102 | timeout => COMMIT_LOCK_TIMEOUT, | ||||||||||||||||||||||||||||||||
| 103 | 63 | ▶ | do_body => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 104 | $self->{create} | ||||||||||||||||||||||||||||||||
| 105 | ? $self->{sinfos}->write_infos($invindex) | ||||||||||||||||||||||||||||||||
| 106 | 63 | ▶ | : $self->{sinfos}->read_infos($invindex); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||||||
| 108 | 63 | ); | |||||||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||||||
| 110 | 63 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | $self->{create} | ||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | ? croak("failed to create invindex: $@") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | : croak("failed to open existing invindex: $@"); | ||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | # get a finfos and maybe a reader | ||||||||||||||||||||||||||||||||
| 117 | 63 | ▶ | if ( $self->{create} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 45 | $self->{finfos} = KinoSearch1::Index::FieldInfos->new; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||||||
| 121 | $self->{reader} | ||||||||||||||||||||||||||||||||
| 122 | 18 | = KinoSearch1::Index::IndexReader->new( invindex => $invindex ); | |||||||||||||||||||||||||||||||
| 123 | 18 | $self->{finfos} = $self->{reader}->generate_field_infos; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | # more initialization is coming after fields are spec'd... | ||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 62 | ▶ | sub _delayed_init { | ||||||||||||||||||||||||||||||
|
Subroutine
_delayed_init: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||
| 130 | 62 | my $self = shift; | |||||||||||||||||||||||||||||||
| 131 | my ( $invindex, $finfos, $field_sims ) | ||||||||||||||||||||||||||||||||
| 132 | 62 | = @{$self}{qw( invindex finfos field_sims )}; | |||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | confess("finish has been called") | ||||||||||||||||||||||||||||||||
| 135 | 62 | ▶ | if $self->{state} == FINISHED; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | confess("internal error: already initialized") | ||||||||||||||||||||||||||||||||
| 137 | 62 | ▶ | if $self->{state} == INITIALIZED; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 62 | $self->{state} = INITIALIZED; | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | # create a cloning template | ||||||||||||||||||||||||||||||||
| 141 | 62 | my $doc = $self->{doc_template}; | |||||||||||||||||||||||||||||||
| 142 | 62 | for my $field ( $doc->get_fields ) { | |||||||||||||||||||||||||||||||
| 143 | 127 | $field->set_field_num( $finfos->get_field_num( $field->get_name ) ); | |||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | 62 | $self->{frozen_doc} = freeze($doc); | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | # set sim for each field | ||||||||||||||||||||||||||||||||
| 148 | 62 | my $main_sim = $self->{similarity}; | |||||||||||||||||||||||||||||||
| 149 | 62 | for my $finfo ( $finfos->get_infos ) { | |||||||||||||||||||||||||||||||
| 150 | 132 | ▶ | $field_sims->{ $finfo->get_name } ||= $main_sim; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$$field_sims{$finfo->get_name} ||= $main_sim
MC/DC0%0/2
$$field_sims{$finfo->get_name} ||= $main_sim
$$field_sims{$finfo->get_name}
$main_sim
Truth table66%2/3
A ||= B
A= $$field_sims{$finfo->get_name}B= $main_sim
| |||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | # name a new segment and create a SegWriter | ||||||||||||||||||||||||||||||||
| 154 | 62 | my $out_seg_name = $self->_new_seg_name; | |||||||||||||||||||||||||||||||
| 155 | 62 | $self->{seg_writer} = KinoSearch1::Index::SegWriter->new( | |||||||||||||||||||||||||||||||
| 156 | invindex => $invindex, | ||||||||||||||||||||||||||||||||
| 157 | seg_name => $out_seg_name, | ||||||||||||||||||||||||||||||||
| 158 | finfos => $finfos->clone, | ||||||||||||||||||||||||||||||||
| 159 | field_sims => $field_sims, | ||||||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 128 | ▶ | sub spec_field { | ||||||||||||||||||||||||||||||
|
Subroutine
spec_field: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 164 | 128 | my $self = shift; | |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | # don't allow new fields to be spec'd once the seg is in motion | ||||||||||||||||||||||||||||||||
| 167 | croak("Too late to spec field (new_doc has been called)") | ||||||||||||||||||||||||||||||||
| 168 | 128 | ▶ | unless $self->{state} == UNINITIALIZED; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | # detect or define a Field object | ||||||||||||||||||||||||||||||||
| 171 | 128 | my $field; | |||||||||||||||||||||||||||||||
| 172 | 128 | ▶ | if ( blessed( $_[0] ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | 0 | $field = shift; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||||||
| 176 | 128 | eval { $field = KinoSearch1::Document::Field->new(@_) }; | |||||||||||||||||||||||||||||||
| 177 | 128 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | # cache fnm_bits and fdt_bits | ||||||||||||||||||||||||||||||||
| 181 | 128 | $field->set_fnm_bits( | |||||||||||||||||||||||||||||||
| 182 | KinoSearch1::Index::FieldInfos->encode_fnm_bits($field) ); | ||||||||||||||||||||||||||||||||
| 183 | 128 | $field->set_fdt_bits( | |||||||||||||||||||||||||||||||
| 184 | KinoSearch1::Index::FieldsReader->encode_fdt_bits($field) ); | ||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | # establish which analyzer will be used against the field | ||||||||||||||||||||||||||||||||
| 187 | $self->{analyzers}{ $field->get_name } | ||||||||||||||||||||||||||||||||
| 188 | 128 | ▶ | = ( $field->get_analyzer || $self->{analyzer} ); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$field->get_analyzer || $self->{'analyzer'}
MC/DC0%0/2
$field->get_analyzer || $self->{'analyzer'}
$field->get_analyzer
$self->{'analyzer'}
Truth table66%2/3
A || B
A= $field->get_analyzerB= $self->{'analyzer'}
| |||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | # don't copy the analyzer into the template, so that it can be overridden | ||||||||||||||||||||||||||||||||
| 191 | 128 | $field->set_analyzer(undef); | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | # add the field to the finfos and the template. | ||||||||||||||||||||||||||||||||
| 194 | 128 | $self->{finfos}->add_field($field); | |||||||||||||||||||||||||||||||
| 195 | 128 | $self->{doc_template}->add_field($field); | |||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 14017 | ▶ | sub new_doc { | ||||||||||||||||||||||||||||||
|
Subroutine
new_doc: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 199 | 14017 | my $self = shift; | |||||||||||||||||||||||||||||||
| 200 | 14017 | ▶ | $self->_delayed_init unless $self->{state} == INITIALIZED; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | 14017 | return thaw( $self->{frozen_doc} ); | |||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 1 | ▶ | sub set_similarity { | ||||||||||||||||||||||||||||||
|
Subroutine
set_similarity: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | if ( @_ == 3 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 206 | 1 | my ( $self, $field_name, $sim ) = @_; | |||||||||||||||||||||||||||||||
| 207 | 1 | $self->{field_sims}{$field_name} = $sim; | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | else { | ||||||||||||||||||||||||||||||||
| 210 | 0 | $_[0]->{similarity} = $_[1]; | |||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | 14017 | ▶ | sub add_doc { | ||||||||||||||||||||||||||||||
|
Subroutine
add_doc: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 215 | 14017 | my ( $self, $doc ) = @_; | |||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | # assign analyzers | ||||||||||||||||||||||||||||||||
| 218 | 14017 | for my $field ( $doc->get_fields ) { | |||||||||||||||||||||||||||||||
| 219 | 15390 | ▶ | if ( $field->get_analyzed ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 220 | 15370 | ▶ | next if $field->get_analyzer; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 221 | 15370 | my $fieldname = $field->get_name; | |||||||||||||||||||||||||||||||
| 222 | 15370 | $field->set_analyzer( $self->{analyzers}{$fieldname} ); | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | # add doc to output segment | ||||||||||||||||||||||||||||||||
| 227 | 14017 | $self->{seg_writer}->add_doc($doc); | |||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | sub add_invindexes { | ||||||||||||||||||||||||||||||
|
Subroutine
add_invindexes: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 231 | 1 | my ( $self, @invindexes ) = @_; | |||||||||||||||||||||||||||||||
| 232 | confess("Can't call add_invindexes after new_doc") | ||||||||||||||||||||||||||||||||
| 233 | 1 | ▶ | if $self->{state} == INITIALIZED; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | # verify or obtain InvIndex objects | ||||||||||||||||||||||||||||||||
| 236 | 1 | for (@invindexes) { | |||||||||||||||||||||||||||||||
| 237 | 2 | ▶ | if ( !a_isa_b( $_, 'KinoSearch1::Store::InvIndex' ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 0 | $_ = KinoSearch1::Store::FSInvIndex->new( path => $_ ); | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | # get a reader for each invindex | ||||||||||||||||||||||||||||||||
| 243 | my @readers | ||||||||||||||||||||||||||||||||
| 244 | 1 | = map { KinoSearch1::Index::IndexReader->new( invindex => $_ ) } | |||||||||||||||||||||||||||||||
| 245 | @invindexes; | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | # merge finfos and init | ||||||||||||||||||||||||||||||||
| 248 | 1 | for my $reader (@readers) { | |||||||||||||||||||||||||||||||
| 249 | 2 | $self->{finfos}->consolidate( $reader->get_finfos ); | |||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | 1 | $self->_delayed_init; | |||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | # add all segments in each of the supplied invindexes | ||||||||||||||||||||||||||||||||
| 254 | 1 | my $seg_writer = $self->{seg_writer}; | |||||||||||||||||||||||||||||||
| 255 | 1 | for my $reader (@readers) { | |||||||||||||||||||||||||||||||
| 256 | 2 | $seg_writer->add_segment($_) for $reader->segreaders_to_merge('all'); | |||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 1 | ▶ | sub delete_docs_by_term { | ||||||||||||||||||||||||||||||
|
Subroutine
delete_docs_by_term: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 261 | 1 | my ( $self, $term ) = @_; | |||||||||||||||||||||||||||||||
| 262 | 1 | ▶ | confess("Not a KinoSearch1::Index::Term") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 263 | unless a_isa_b( $term, 'KinoSearch1::Index::Term' ); | ||||||||||||||||||||||||||||||||
| 264 | 1 | ▶ | return unless $self->{reader}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | 1 | ▶ | $self->_delayed_init unless $self->{state} == INITIALIZED; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 266 | 1 | $self->{reader}->delete_docs_by_term($term); | |||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 34 | our %finish_defaults = ( optimize => 0, ); | |||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 63 | ▶ | sub finish { | ||||||||||||||||||||||||||||||
|
Subroutine
finish: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 273 | 63 | my $self = shift; | |||||||||||||||||||||||||||||||
| 274 | 63 | ▶ | confess kerror() unless verify_args( \%finish_defaults, @_ ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | 63 | my %args = ( %finish_defaults, @_ ); | |||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | # if no changes were made to the index, don't write anything | ||||||||||||||||||||||||||||||||
| 278 | 63 | ▶ | if ( $self->{state} == UNINITIALIZED ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 279 | 3 | ▶ | if ( !$args{optimize} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 280 | 1 | return; | |||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | else { | ||||||||||||||||||||||||||||||||
| 283 | 2 | $self->_delayed_init; | |||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | my ( $invindex, $sinfos, $seg_writer ) | ||||||||||||||||||||||||||||||||
| 288 | 62 | = @{$self}{qw( invindex sinfos seg_writer )}; | |||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | # perform segment merging | ||||||||||||||||||||||||||||||||
| 291 | my @to_merge | ||||||||||||||||||||||||||||||||
| 292 | = $self->{reader} | ||||||||||||||||||||||||||||||||
| 293 | ? $self->{reader}->segreaders_to_merge( $args{optimize} ) | ||||||||||||||||||||||||||||||||
| 294 | 62 | ▶ | : (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 295 | 62 | $seg_writer->add_segment($_) for @to_merge; | |||||||||||||||||||||||||||||||
| 296 | 62 | $sinfos->delete_segment( $_->get_seg_name ) for @to_merge; | |||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | # finish the segment | ||||||||||||||||||||||||||||||||
| 299 | 62 | $seg_writer->finish; | |||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | # now that the seg is complete, write its info to the 'segments' file | ||||||||||||||||||||||||||||||||
| 302 | 62 | my $doc_count = $seg_writer->get_doc_count; | |||||||||||||||||||||||||||||||
| 303 | 62 | ▶ | if ($doc_count) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 304 | 61 | $sinfos->add_info( | |||||||||||||||||||||||||||||||
| 305 | KinoSearch1::Index::SegInfo->new( | ||||||||||||||||||||||||||||||||
| 306 | seg_name => $seg_writer->get_seg_name, | ||||||||||||||||||||||||||||||||
| 307 | doc_count => $doc_count, | ||||||||||||||||||||||||||||||||
| 308 | invindex => $invindex, | ||||||||||||||||||||||||||||||||
| 309 | ) | ||||||||||||||||||||||||||||||||
| 310 | ); | ||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||
| 313 | # commit changes to the invindex | ||||||||||||||||||||||||||||||||
| 314 | $invindex->run_while_locked( | ||||||||||||||||||||||||||||||||
| 315 | lock_name => COMMIT_LOCK_NAME, | ||||||||||||||||||||||||||||||||
| 316 | timeout => COMMIT_LOCK_TIMEOUT, | ||||||||||||||||||||||||||||||||
| 317 | 62 | ▶ | do_body => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 318 | 62 | ▶ | $self->{reader}->commit_deletions if defined $self->{reader}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 319 | 62 | $sinfos->write_infos($invindex); | |||||||||||||||||||||||||||||||
| 320 | }, | ||||||||||||||||||||||||||||||||
| 321 | 62 | ); | |||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 62 | my @files_to_delete = $self->_generate_deletions_list( \@to_merge ); | |||||||||||||||||||||||||||||||
| 324 | 62 | push @files_to_delete, $self->_read_delqueue; | |||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | # close reader, so that we can delete its files if appropriate | ||||||||||||||||||||||||||||||||
| 327 | 62 | ▶ | $self->{reader}->close if defined $self->{reader}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | 62 | $self->_purge_merged(@files_to_delete); | |||||||||||||||||||||||||||||||
| 330 | 62 | $self->_release_locks; | |||||||||||||||||||||||||||||||
| 331 | 62 | $self->{state} = FINISHED; | |||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | # Given an array of SegReaders, return a list of their files. | ||||||||||||||||||||||||||||||||
| 335 | 62 | ▶ | sub _generate_deletions_list { | ||||||||||||||||||||||||||||||
|
Subroutine
_generate_deletions_list: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 336 | 62 | my ( $self, $readers_to_merge ) = @_; | |||||||||||||||||||||||||||||||
| 337 | 62 | my $invindex = $self->{invindex}; | |||||||||||||||||||||||||||||||
| 338 | 62 | my @segs_to_merge = map { $_->get_seg_name } @$readers_to_merge; | |||||||||||||||||||||||||||||||
| 339 | 28 | my @deletions = grep { $invindex->file_exists($_) } | |||||||||||||||||||||||||||||||
| 340 | 62 | map { ( "$_.cfs", "$_.del" ) } @segs_to_merge; | |||||||||||||||||||||||||||||||
| 341 | 62 | return @deletions; | |||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | # Retrieve a list of files that weren't successfully deleted before. | ||||||||||||||||||||||||||||||||
| 345 | 62 | ▶ | sub _read_delqueue { | ||||||||||||||||||||||||||||||
|
Subroutine
_read_delqueue: called CC=2 SCAR=8.7
| |||||||||||||||||||||||||||||||||
| 346 | 62 | my ( $self, $readers_to_merge ) = @_; | |||||||||||||||||||||||||||||||
| 347 | 62 | my $invindex = $self->{invindex}; | |||||||||||||||||||||||||||||||
| 348 | 62 | my @deletions; | |||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | 62 | ▶ | if ( $invindex->file_exists('delqueue') ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 351 | 0 | my $instream = $invindex->open_instream('delqueue'); | |||||||||||||||||||||||||||||||
| 352 | 0 | my $num_in_queue = $instream->lu_read('i'); | |||||||||||||||||||||||||||||||
| 353 | 0 | @deletions = $instream->lu_read("T$num_in_queue"); | |||||||||||||||||||||||||||||||
| 354 | 0 | $instream->close; | |||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||
| 357 | 62 | return @deletions; | |||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||
| 360 | # Delete segments that have been folded into the new segment. | ||||||||||||||||||||||||||||||||
| 361 | 62 | ▶ | sub _purge_merged { | ||||||||||||||||||||||||||||||
|
Subroutine
_purge_merged: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||||||
| 362 | 62 | my ( $self, @deletions ) = @_; | |||||||||||||||||||||||||||||||
| 363 | 62 | my $invindex = $self->{invindex}; | |||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | 62 | my @delqueue; | |||||||||||||||||||||||||||||||
| 366 | 62 | for my $deletion (@deletions) { | |||||||||||||||||||||||||||||||
| 367 | 14 | eval { $invindex->delete_file($deletion) }; | |||||||||||||||||||||||||||||||
| 368 | # Win32: if the deletion fails (because a reader is open), queue it | ||||||||||||||||||||||||||||||||
| 369 | 14 | ▶ | if ( $@ and $invindex->file_exists($deletion) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ and $invindex->file_exists($deletion)
MC/DC0%0/2
$@ and $invindex->file_exists($deletion)
$@
$invindex->file_exists($deletion)
Truth table33%1/3
A and B
A= $@B= $invindex->file_exists($deletion)
| |||||||||||||||||||||||||||||||||
| 370 | 0 | push @delqueue, $deletion; | |||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||
| 374 | 62 | $self->_write_delqueue(@delqueue); | |||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | 62 | ▶ | sub _write_delqueue { | ||||||||||||||||||||||||||||||
|
Subroutine
_write_delqueue: called CC=3 SCAR=14.8
| |||||||||||||||||||||||||||||||||
| 378 | 62 | my ( $self, @delqueue ) = @_; | |||||||||||||||||||||||||||||||
| 379 | 62 | my $invindex = $self->{invindex}; | |||||||||||||||||||||||||||||||
| 380 | 62 | my $num_files = scalar @delqueue; | |||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | 62 | ▶ | if ($num_files) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 383 | # we have files that weren't successfully deleted, so write list | ||||||||||||||||||||||||||||||||
| 384 | 0 | my $outstream = $invindex->open_outstream('delqueue.new'); | |||||||||||||||||||||||||||||||
| 385 | 0 | $outstream->lu_write( "iT$num_files", $num_files, @delqueue ); | |||||||||||||||||||||||||||||||
| 386 | 0 | $outstream->close; | |||||||||||||||||||||||||||||||
| 387 | 0 | $invindex->rename_file( 'delqueue.new', 'delqueue' ); | |||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||
| 389 | elsif ( $invindex->file_exists('delqueue') ) { | ||||||||||||||||||||||||||||||||
| 390 | # no files to delete, so delete the delqueue file if it's there | ||||||||||||||||||||||||||||||||
| 391 | 0 | $invindex->delete_file('delqueue'); | |||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | # Release the write lock - if it's there. | ||||||||||||||||||||||||||||||||
| 396 | 123 | ▶ | sub _release_locks { | ||||||||||||||||||||||||||||||
|
Subroutine
_release_locks: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 397 | 123 | my $self = shift; | |||||||||||||||||||||||||||||||
| 398 | 123 | ▶ | if ( defined $self->{write_lock} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 399 | 63 | ▶ | $self->{write_lock}->release if $self->{write_lock}->is_locked; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 400 | 63 | undef $self->{write_lock}; | |||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||
| 404 | # Generate segment names (no longer Lucene compatible, as of 0.06). | ||||||||||||||||||||||||||||||||
| 405 | 62 | ▶ | sub _new_seg_name { | ||||||||||||||||||||||||||||||
|
Subroutine
_new_seg_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 406 | 62 | my $self = shift; | |||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | 62 | my $counter = $self->{sinfos}->get_counter; | |||||||||||||||||||||||||||||||
| 409 | 62 | $self->{sinfos}->set_counter( ++$counter ); | |||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | 62 | return "_$counter"; | |||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||
| 414 | 61 | ▶ | sub DESTROY { shift->_release_locks } | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||
| 416 | 34 | 1; | |||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||
| 418 | __END__ | ||||||||||||||||||||||||||||||||