/usr/share/perl5/Plucene/Index/SegmentsReader.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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | package Plucene::Index::SegmentsReader;
=head1 NAME
Plucene::Index::SegmentsReader - reads the segments
=head1 SYNOPSIS
my $segs_reader = Plucene::Index::SegmentsReader
->new($dir, Plucene::Index::SegmentReader @readers);
my $num_docs = $segs_reader->num_docs;
my $doc = $segs_reader->document($id);
my $norms = $seg_reader->norms($field);
my $doc_freq = $segs_reader->doc_freq($term);
my Plucene::Index::SegmentsTermEnum $term_enum
= $segs_reader->terms($term);
my Plucene::Index::SegmentsTermDocs $term_docs
= $segs_reader->term_docs;
my Plucene::Index::SegmentsTermPositions $term_positions
= $segs_reader->term_positions;
if ($segs_reader->is_deleted($id)) { ... }
=head1 DESCRIPTION
This is the segments reader class.
=head1 METHODS
=cut
use strict;
use warnings;
use List::Util qw(sum);
use Plucene::Index::SegmentsTermEnum;
use base qw(Plucene::Index::Reader Class::Accessor::Fast);
__PACKAGE__->mk_accessors(qw(max_doc));
use Memoize;
memoize("norms"); # Saves messing with normsCache
=head2 new
my $segs_reader = Plucene::Index::SegmentsReader
->new($dir, Plucene::Index::SegmentReader @readers);
This will create a new Plucene::Index::SegmentsReader object with the passed
directory and Plucene::Index::SegmentReader objects.
=cut
sub new {
my ($class, $dir, @readers) = @_;
my $self = $class->SUPER::new($dir);
$self->{readers} = \@readers;
$self->{max_doc} = 0;
for my $reader (@readers) {
push @{ $self->{starts} }, $self->{max_doc};
$self->{max_doc} += $reader->max_doc;
}
return $self;
}
=head2 num_docs
my $num_docs = $segs_reader->num_docs;
This will return the number of documents in all the segments in the Reader.
=cut
sub num_docs {
my $self = shift;
return $self->{num_docs} if exists $self->{num_docs};
return $self->{num_docs} = sum(map $_->num_docs, @{ $self->{readers} });
}
=head2 document
my $doc = $segs_reader->document($id);
This will return the document at the passed document id.
=cut
sub document {
my ($self, $n) = @_;
my $i = $self->_reader_index($n);
return $self->{readers}[$i]->document($n - $self->{starts}[$i]);
}
=head2 is_deleted
if ($segs_reader->is_deleted($id)) { ... }
=cut
sub is_deleted {
my ($self, $n) = @_;
my $i = $self->_reader_index($n);
return $self->{readers}[$i]->is_deleted($n - $self->{starts}[$i]);
}
sub _do_delete {
my ($self, $n) = @_;
delete $self->{num_docs}; # Invalidate cache
my $i = $self->_reader_index($n);
return $self->{readers}[$i]->_do_delete($n - $self->{starts}[$i]);
}
sub _do_close {
my $self = shift;
$_->close for @{ $self->{readers} };
}
sub _reader_index {
my ($self, $n) = @_;
my ($lo, $hi) = (0, $#{ $self->{readers} });
# Binary search
while ($hi >= $lo) {
my $mid = int(($lo + $hi) / 2);
my $mid_val = $self->{starts}[$mid];
if ($n < $mid_val) { $hi = $mid - 1; }
elsif ($n > $mid_val) { $lo = $mid + 1; }
else { return $mid; }
}
return $hi;
}
=head2 norms
my $norms = $seg_reader->norms($field);
This returns the norms for the passed field.
=cut
sub norms {
my ($self, $field) = @_;
my $bytes = "\0" x $self->max_doc;
for my $i (0 .. $#{ $self->{readers} }) {
my $norm = $self->{readers}[$i]->norms($field);
substr($bytes, $self->{starts}[$i], length $norm) = $norm;
}
return $bytes;
}
=head2 terms
my Plucene::Index::SegmentsTermEnum $term_enum
= $segs_reader->terms($term);
This will return the Plucene::Index::SegmentsTermEnum onject for the
passed in term.
=cut
sub terms {
my ($self, $term) = @_;
return Plucene::Index::SegmentsTermEnum->new($self->{readers},
$self->{starts}, $term);
}
=head2 doc_freq
my $doc_freq = $segs_reader->doc_freq($term);
This returns the number of documents containing the passed term.
=cut
sub doc_freq {
my ($self, $term) = @_;
return sum map $_->doc_freq($term), @{ $self->{readers} };
}
=head2 term_docs
my Plucene::Index::SegmentsTermDocs $term_docs
= $segs_reader->term_docs;
This will return the Plucene::Index::SegmentsTermDocs object.
=cut
sub term_docs {
my $self = shift;
my $term = shift;
my $docs =
Plucene::Index::SegmentsTermDocs->new($self->{readers}, $self->{starts});
if ($term) { $docs->seek($term) }
return $docs;
}
=head2 term_positions
my Plucene::Index::SegmentsTermPositions $term_positions
= $segs_reader->term_positions;
This will return the Plucene::Index::SegmentsTermPositions object.
=cut
sub term_positions {
my $self = shift;
my $term = shift;
my $pos =
Plucene::Index::SegmentsTermPositions->new($self->{readers},
$self->{starts});
if ($term) { $pos->seek($term) }
return $pos;
}
package Plucene::Index::SegmentsTermDocs;
sub new {
my ($class, $readers, $starts) = @_;
bless {
readers => $readers,
starts => $starts,
seg_term_docs => [],
base => 0,
pointer => 0,
current => undef,
}, $class;
}
sub doc {
my $self = shift;
return $self->{base} + $self->{current}->doc;
}
sub freq { return shift->{current}->freq; }
sub seek {
my ($self, $term) = @_;
$self->{term} = $term;
$self->{base} = 0;
$self->{pointer} = 0;
$self->{current} = undef;
}
sub _at_end { $_[0]->{pointer} >= @{ $_[0]->{readers} } }
sub _set_base_and_advance {
my $self = shift;
$self->{base} = $self->{starts}[ $self->{pointer} ];
$self->{current} = $self->term_docs($self->{pointer}++);
}
sub next {
my $self = shift;
return 1 if $self->{current} && $self->{current}->next;
unless ($self->_at_end) {
$self->_set_base_and_advance;
return $self->next;
}
return 0;
}
sub read {
my ($self) = @_;
my ($docs, $freqs) = ([], []);
while (1) {
# Get a .current, somehow
while (!$self->{current}) {
goto done if $self->_at_end; # Don't fall off
$self->_set_base_and_advance;
}
my ($new_docs, $new_freqs) = $self->{current}->read($docs, $freqs);
if (!scalar @$new_docs) {
# It's empty
undef $self->{current};
} else {
# Correct the doc positions to the appropriate base
$_ += $self->{base} for @$new_docs;
push @$docs, @$new_docs;
push @$freqs, @$new_freqs;
goto done;
}
}
done: return ($docs, $freqs);
}
sub skip_to {
my ($self, $target) = @_;
$self->next || return while $target > $self->doc;
return 1;
}
sub term_docs {
my ($self, $i) = @_;
return unless $self->{term};
$self->{seg_term_docs}[$i] = $self->term_docs_r($self->{readers}[$i])
unless exists $self->{seg_term_docs}[$i];
my $result = $self->{seg_term_docs}[$i];
$result->seek($self->{term});
return $result;
}
sub term_docs_r {
my ($self, $reader) = @_;
return $reader->term_docs;
}
package Plucene::Index::SegmentsTermPositions;
use base 'Plucene::Index::SegmentsTermDocs';
sub term_docs_r {
my ($self, $reader) = @_;
return $reader->term_positions;
}
sub next_position {
return shift->{current}->next_position;
}
1;
|