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