| 1 | package Plucene::Index::DocumentWriter; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | Plucene::Index::DocumentWriter - the document writer | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | my $writer = Plucene::Index::DocumentWriter | ||||||||||||||||||||
| 10 | ->new($directory, $analyser, $max_field_length); | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | $writer->add_document($segment, $doc); | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | This is the document writer class. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head2 METHODS | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 17 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 17 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 17 | ▶ | use File::Slurp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 17 | ▶ | use Plucene::Index::FieldInfos; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 17 | ▶ | use Plucene::Index::FieldsWriter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 17 | ▶ | use Plucene::Index::Term; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | 17 | ▶ | use Plucene::Index::TermInfo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 17 | ▶ | use Plucene::Index::TermInfosWriter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 17 | ▶ | use Plucene::Search::Similarity; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 17 | ▶ | use Plucene::Store::OutputStream; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 17 | ▶ | use IO::Scalar; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head2 new | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | my $writer = Plucene::Index::DocumentWriter | ||||||||||||||||||||
| 39 | ->new($directory, $analyser, $max_field_length); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | This will create a new Plucene::Index::DocumentWriter object with the passed | ||||||||||||||||||||
| 42 | in arguments. | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 257 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 47 | 257 | my ($self, $d, $a, $mfl) = @_; | |||||||||||||||||||
| 48 | 257 | bless { | |||||||||||||||||||
| 49 | directory => $d, | ||||||||||||||||||||
| 50 | analyzer => $a, | ||||||||||||||||||||
| 51 | max_field_length => $mfl, | ||||||||||||||||||||
| 52 | postings => {}, | ||||||||||||||||||||
| 53 | }, $self; | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head2 add_document | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | $writer->add_document($segment, $doc); | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 257 | ▶ | sub add_document { | ||||||||||||||||||
|
Subroutine
add_document: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 63 | 257 | my ($self, $segment, $doc) = @_; | |||||||||||||||||||
| 64 | 257 | my $fi = Plucene::Index::FieldInfos->new(); | |||||||||||||||||||
| 65 | 257 | $fi->add($doc); | |||||||||||||||||||
| 66 | 257 | $fi->write("$self->{directory}/$segment.fnm"); | |||||||||||||||||||
| 67 | 257 | $self->{field_infos} = $fi; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | my $fw = | ||||||||||||||||||||
| 70 | 257 | Plucene::Index::FieldsWriter->new($self->{directory}, $segment, $fi); | |||||||||||||||||||
| 71 | 257 | $fw->add_document($doc); | |||||||||||||||||||
| 72 | 257 | $self->{postings} = {}; | |||||||||||||||||||
| 73 | 257 | $self->{field_lengths} = []; | |||||||||||||||||||
| 74 | 257 | $self->_invert_document($doc); | |||||||||||||||||||
| 75 | my @postings = sort { | ||||||||||||||||||||
| 76 | $a->{term}->{field} cmp $b->{term}->{field} | ||||||||||||||||||||
| 77 | || $a->{term}->{text} cmp $b->{term}->{text} | ||||||||||||||||||||
| 78 | 257 | ▶ | } values %{ $self->{postings} }; | ||||||||||||||||||
|
Condition66%2/3
$Plucene::Index::DocumentWriter::a->{'term'}{'field'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'field'} || $Plucene::Index::DocumentWriter::a->{'term'}{'text'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'text'}
MC/DC0%0/2
$Plucene::Index::DocumentWriter::a->{'term'}{'field'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'field'} || $Plucene::Index::DocumentWriter::a->{'term'}{'text'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'text'}
$Plucene::Index::DocumentWriter::a->{'term'}{'field'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'field'}
$Plucene::Index::DocumentWriter::a->{'term'}{'text'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'text'}
Truth table66%2/3
A || B
A= $Plucene::Index::DocumentWriter::a->{'term'}{'field'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'field'}B= $Plucene::Index::DocumentWriter::a->{'term'}{'text'} cmp $Plucene::Index::DocumentWriter::b->{'term'}{'text'}
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 257 | $self->_write_postings($segment, @postings); | |||||||||||||||||||
| 81 | 257 | $self->_write_norms($doc, $segment); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 257 | ▶ | sub _invert_document { | ||||||||||||||||||
|
Subroutine
_invert_document: called CC=6 SCAR=18.1
| |||||||||||||||||||||
| 85 | 257 | my ($self, $doc) = @_; | |||||||||||||||||||
| 86 | 257 | for my $field (grep $_->is_indexed, $doc->fields) { | |||||||||||||||||||
| 87 | 370 | my $name = $field->name; | |||||||||||||||||||
| 88 | 370 | my $fn = $self->{field_infos}->field_number($name); | |||||||||||||||||||
| 89 | 370 | my $pos = $self->{field_lengths}->[$fn]; | |||||||||||||||||||
| 90 | 370 | ▶ | if (!$field->is_tokenized) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 48 | $self->_add_position($name, $field->string, $pos++); | |||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||
| 93 | my $reader = $field->reader | ||||||||||||||||||||
| 94 | 322 | ▶ | || IO::Scalar->new(\$field->{string}); | ||||||||||||||||||
|
Condition33%1/3
$field->reader || 'IO::Scalar'->new(\$field->{'string'})
MC/DC0%0/2
$field->reader || 'IO::Scalar'->new(\$field->{'string'})
$field->reader
'IO::Scalar'->new(\$field->{'string'})
Truth table33%1/3
A || B
A= $field->readerB= 'IO::Scalar'->new(\$field->{'string'})
| |||||||||||||||||||||
| 95 | my $stream = $self->{analyzer}->tokenstream({ | ||||||||||||||||||||
| 96 | 322 | field => $name, | |||||||||||||||||||
| 97 | reader => $reader | ||||||||||||||||||||
| 98 | }); | ||||||||||||||||||||
| 99 | 322 | ▶ | while (my $t = $stream->next) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 143694 | $self->_add_position($name, $t->text, $pos++); | |||||||||||||||||||
| 101 | 143694 | ▶ | last if $pos > $self->{max_field_length}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | 370 | $self->{field_lengths}->[$fn] = $pos; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 143742 | ▶ | sub _add_position { | ||||||||||||||||||
|
Subroutine
_add_position: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 109 | 143742 | my ($self, $field, $text, $pos) = @_; | |||||||||||||||||||
| 110 | 143742 | my $ti = $self->{postings}->{"$field\0$text"}; | |||||||||||||||||||
| 111 | 143742 | ▶ | if ($ti) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 110866 | $ti->{positions}->[ $ti->freq ] = $pos; | |||||||||||||||||||
| 113 | 110866 | $ti->{freq}++; | |||||||||||||||||||
| 114 | 110866 | return; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | 32876 | $self->{postings}->{"$field\0$text"} = Plucene::Index::Posting->new({ | |||||||||||||||||||
| 117 | term => Plucene::Index::Term->new({ field => $field, text => $text }), | ||||||||||||||||||||
| 118 | positions => [$pos], | ||||||||||||||||||||
| 119 | freq => 1, | ||||||||||||||||||||
| 120 | }); | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 257 | ▶ | sub _write_postings { | ||||||||||||||||||
|
Subroutine
_write_postings: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 124 | 257 | my ($self, $segment, @postings) = @_; | |||||||||||||||||||
| 125 | 257 | my (@freqs, @proxs); | |||||||||||||||||||
| 126 | my $tis = | ||||||||||||||||||||
| 127 | Plucene::Index::TermInfosWriter->new($self->{directory}, $segment, | ||||||||||||||||||||
| 128 | 257 | $self->{field_infos}); | |||||||||||||||||||
| 129 | 257 | my $ti = Plucene::Index::TermInfo->new(); | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 257 | for my $posting (@postings) { | |||||||||||||||||||
| 132 | 32876 | $ti->doc_freq(1); | |||||||||||||||||||
| 133 | 32876 | $ti->freq_pointer(scalar @freqs); | |||||||||||||||||||
| 134 | 32876 | $ti->prox_pointer(scalar @proxs); | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 32876 | $tis->add($posting->term, $ti); | |||||||||||||||||||
| 137 | 32876 | my $f = $posting->freq; | |||||||||||||||||||
| 138 | 32876 | ▶ | push @freqs, ($f == 1) ? 1 : (0, $f); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 32876 | my $last_pos = 0; | |||||||||||||||||||
| 140 | 32876 | my $positions = $posting->positions; | |||||||||||||||||||
| 141 | 32876 | for my $j (0 .. $f - 1) { | |||||||||||||||||||
| 142 | 143742 | ▶ | my $pos = $positions->[$j] || 0; | ||||||||||||||||||
|
Condition100%2/2
$positions->[$j] || 0
MC/DC100%1/1
$positions->[$j] || 0
$positions->[$j]
Truth table100%2/2
A
A= $positions->[$j]
| |||||||||||||||||||||
| 143 | 143742 | push @proxs, $pos - $last_pos; | |||||||||||||||||||
| 144 | 143742 | $last_pos = $pos; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 257 | write_file("$self->{directory}/$segment.frq" => pack('(w)*', @freqs)); | |||||||||||||||||||
| 149 | 257 | write_file("$self->{directory}/$segment.prx" => pack('(w)*', @proxs)); | |||||||||||||||||||
| 150 | 257 | $tis->break_ref; | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 257 | ▶ | sub _write_norms { | ||||||||||||||||||
|
Subroutine
_write_norms: called CC=3 SCAR=11.4
| |||||||||||||||||||||
| 154 | 257 | my ($self, $doc, $segment) = @_; | |||||||||||||||||||
| 155 | 257 | for my $field (grep $_->is_indexed, $doc->fields) { | |||||||||||||||||||
| 156 | 370 | my $fn = $self->{field_infos}->field_number($field->name); | |||||||||||||||||||
| 157 | 0 | ▶ | warn "Couldn't find field @{[ $field->name ]} in list [ @{[ map | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 0 | $_->name, $self->{field_infos}->fields]}]" unless $fn >= 0; | |||||||||||||||||||
| 159 | 370 | my $norm = | |||||||||||||||||||
| 160 | Plucene::Store::OutputStream->new("$self->{directory}/$segment.f$fn"); | ||||||||||||||||||||
| 161 | 370 | my $val = $self->{field_lengths}[$fn]; | |||||||||||||||||||
| 162 | 370 | my $norm_val = Plucene::Search::Similarity->norm($val); | |||||||||||||||||||
| 163 | 370 | $norm->print(chr($norm_val)); | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | package Plucene::Index::Posting; | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 17 | ▶ | use base 'Class::Accessor::Fast'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 17 | __PACKAGE__->mk_accessors(qw( term freq positions )); | |||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 17 | 1; | |||||||||||||||||||