| 1 | package Plucene::Index::TermInfosReader; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Plucene::Index::TermInfosReader - read the term infos file | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my $reader = Plucene::Index::TermInfosReader->new( | ||||||||||||||||||||||||||||
| 10 | $dir_name, $segment, $fis); | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | my Plucene::Index::TermInfo $term_info = | ||||||||||||||||||||||||||||
| 13 | $reader->get(Plucene::Index::Term $term); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | my Plucene::Index::SegmentTermEnum $enum = | ||||||||||||||||||||||||||||
| 16 | $reader->terms(Plucene::Index::Term $term); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | This reads a term infos file. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 METHODS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 19 | ▶ | use Memoize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 19 | ▶ | use Carp qw/confess/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 19 | ▶ | use Plucene::Index::SegmentTermEnum; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 19 | ▶ | use Plucene::Index::TermInfosWriter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 19 | ▶ | use Plucene::Store::InputStream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 new | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | my $reader = Plucene::Index::TermInfosReader->new( | ||||||||||||||||||||||||||||
| 40 | $dir_name, $segment, $fis); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | This will create a new Plucene::Index::TermInfosReader object with | ||||||||||||||||||||||||||||
| 43 | the passed directory name, segment name and field infos. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 584 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 584 | my ($class, $dir, $seg, $fis) = @_; | |||||||||||||||||||||||||||
| 49 | 584 | my $file = "$dir/$seg.tis"; | |||||||||||||||||||||||||||
| 50 | 584 | ▶ | confess("$file is already open!") unless -s $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 584 | my $self = bless { | |||||||||||||||||||||||||||
| 53 | directory => $dir, | ||||||||||||||||||||||||||||
| 54 | segment => $seg, | ||||||||||||||||||||||||||||
| 55 | field_infos => $fis, | ||||||||||||||||||||||||||||
| 56 | enum => Plucene::Index::SegmentTermEnum->new( | ||||||||||||||||||||||||||||
| 57 | Plucene::Store::InputStream->new($file), | ||||||||||||||||||||||||||||
| 58 | $fis, 0 | ||||||||||||||||||||||||||||
| 59 | ), | ||||||||||||||||||||||||||||
| 60 | }, $class; | ||||||||||||||||||||||||||||
| 61 | 584 | $self->{size} = $self->{enum}->size; | |||||||||||||||||||||||||||
| 62 | 584 | $self->_read_index; | |||||||||||||||||||||||||||
| 63 | 584 | return $self; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 584 | ▶ | sub _read_index { | ||||||||||||||||||||||||||
|
Subroutine
_read_index: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 67 | 584 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | my $index_enum = Plucene::Index::SegmentTermEnum->new( | ||||||||||||||||||||||||||||
| 69 | Plucene::Store::InputStream->new( | ||||||||||||||||||||||||||||
| 70 | "$self->{directory}/$self->{segment}.tii"), | ||||||||||||||||||||||||||||
| 71 | $self->{field_infos}, | ||||||||||||||||||||||||||||
| 72 | 584 | 1 | |||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | 584 | my $size = $index_enum->size; | |||||||||||||||||||||||||||
| 75 | 584 | $self->{index_terms} = []; | |||||||||||||||||||||||||||
| 76 | 584 | $self->{index_infos} = []; | |||||||||||||||||||||||||||
| 77 | 584 | $self->{index_pointers} = []; | |||||||||||||||||||||||||||
| 78 | 584 | ▶ | for (my $i = 0 ; $index_enum->next ; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 5707 | $self->{index_terms}->[$i] = $index_enum->term; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Need to clone here. | ||||||||||||||||||||||||||||
| 82 | $self->{index_infos}->[$i] = | ||||||||||||||||||||||||||||
| 83 | 5707 | Plucene::Index::TermInfo->new({ %{ $index_enum->term_info } }); | |||||||||||||||||||||||||||
| 84 | 5707 | $self->{index_pointers}->[$i] = $index_enum->index_pointer; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 19 | memoize('_get_index_offset'); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 392 | ▶ | sub _get_index_offset { | ||||||||||||||||||||||||||
|
Subroutine
_get_index_offset: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 91 | 392 | my ($self, $term) = @_; | |||||||||||||||||||||||||||
| 92 | 392 | my $lo = 0; | |||||||||||||||||||||||||||
| 93 | 392 | my $hi = $#{ $self->{index_terms} }; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 392 | ▶ | while ($hi >= $lo) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1459 | my $mid = ($lo + $hi) >> 1; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Terms are comparable, hooray | ||||||||||||||||||||||||||||
| 99 | 1459 | my $delta = $term->_cmp($self->{index_terms}->[$mid]); | |||||||||||||||||||||||||||
| 100 | 1459 | ▶ | if ($delta < 0) { $hi = $mid - 1; } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 101 | 611 | elsif ($delta > 0) { $lo = $mid + 1; } | |||||||||||||||||||||||||||
| 102 | 3 | else { return $mid } | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 389 | return $hi; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 get | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | my Plucene::Index::TermInfo $term_info = | ||||||||||||||||||||||||||||
| 110 | $reader->get(Plucene::Index::Term $term); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 779 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 779 | my ($self, $term) = @_; | |||||||||||||||||||||||||||
| 116 | 779 | ▶ | return unless $self->{size}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 779 | $self->_seek_enum($self->_get_index_offset($term)); | |||||||||||||||||||||||||||
| 118 | 779 | return $self->_scan_enum($term); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1054 | ▶ | sub _seek_enum { | ||||||||||||||||||||||||||
|
Subroutine
_seek_enum: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 1054 | my ($self, $offset) = @_; | |||||||||||||||||||||||||||
| 123 | $self->{enum}->seek( | ||||||||||||||||||||||||||||
| 124 | $self->{index_pointers}->[$offset], | ||||||||||||||||||||||||||||
| 125 | $offset * Plucene::Index::TermInfosWriter::INDEX_INTERVAL() - 1, | ||||||||||||||||||||||||||||
| 126 | $self->{index_terms}->[$offset], | ||||||||||||||||||||||||||||
| 127 | 1054 | $self->{index_infos}->[$offset]); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 779 | ▶ | sub _scan_enum { | ||||||||||||||||||||||||||
|
Subroutine
_scan_enum: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 131 | 779 | my ($self, $term) = @_; | |||||||||||||||||||||||||||
| 132 | 779 | ▶ | 1 while $term->gt($self->{enum}->term) && $self->{enum}->next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$term->gt($self->{"enum"}->term) and $self->{"enum"}->next
MC/DC50%1/2
$term->gt($self->{"enum"}->term) and $self->{"enum"}->next
$term->gt($self->{"enum"}->term)
$self->{"enum"}->next
Truth table66%2/3
A and B
A= $term->gt($self->{"enum"}->term)B= $self->{"enum"}->next
| |||||||||||||||||||||||||||||
| 133 | return $self->{enum}->term_info | ||||||||||||||||||||||||||||
| 134 | if $self->{enum}->term | ||||||||||||||||||||||||||||
| 135 | 779 | ▶ | and $self->{enum}->term->eq($term); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"enum"}->term and $self->{"enum"}->term->eq($term)
MC/DC50%1/2
$self->{"enum"}->term and $self->{"enum"}->term->eq($term)
$self->{"enum"}->term
$self->{"enum"}->term->eq($term)
Truth table66%2/3
A and B
A= $self->{"enum"}->termB= $self->{"enum"}->term->eq($term)
| |||||||||||||||||||||||||||||
| 136 | 240 | return; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 get_int / get_position | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | These are never called. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub get_int { } | ||||||||||||||||||||||||||
|
Subroutine
get_int: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub get_position { } | ||||||||||||||||||||||||||
|
Subroutine
get_position: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 terms | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | my Plucene::Index::SegmentTermEnum $enum = | ||||||||||||||||||||||||||||
| 151 | $reader->terms(Plucene::Index::Term $term); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | This will return the Plucene::Index::SegmentTermEnum for the passed-in | ||||||||||||||||||||||||||||
| 154 | Plucene::Index::Term. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 285 | ▶ | sub terms { | ||||||||||||||||||||||||||
|
Subroutine
terms: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 285 | my ($self, $term) = @_; | |||||||||||||||||||||||||||
| 160 | 285 | ▶ | $term ? $self->get($term) : $self->_seek_enum(0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 285 | $self->{enum}->clone; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 19 | 1; | |||||||||||||||||||||||||||