| 1 | package Plucene::Index::TermInfosWriter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Plucene::Index::TermInfosWriter - write to the term infos file | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my $writer = Plucene::Index::TermInfosWriter->new( | ||||||||||||||||||||||||||||
| 10 | $dir_name, $segment, $field_infos); | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | $writer->add(Plucene::Index::Term $term, | ||||||||||||||||||||||||||||
| 13 | Plucene::Index::TermInfo $term_info); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | $writer->write_term(Plucene::Index::Term $term); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | This will allow for the writing and adding to a term infos file for a | ||||||||||||||||||||||||||||
| 20 | particular segment. It also writes the term infos index. | ||||||||||||||||||||||||||||
| 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 constant INDEX_INTERVAL => 128; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 19 | ▶ | use Carp qw(confess carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 19 | ▶ | use Plucene::Store::OutputStream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 19 | ▶ | use Plucene::Index::Term; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 19 | ▶ | use Plucene::Index::TermInfo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 new | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | my $writer = Plucene::Index::TermInfosWriter->new( | ||||||||||||||||||||||||||||
| 40 | $dir_name, $segment, $field_infos); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | This will create a new Plucene::Index::TermInfosWriter object. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 594 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 594 | my ($class, $d, $segment, $fis, $is_i) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 594 | ▶ | my $self = bless { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | field_infos => $fis, | ||||||||||||||||||||||||||||
| 51 | is_index => $is_i, | ||||||||||||||||||||||||||||
| 52 | size => 0, | ||||||||||||||||||||||||||||
| 53 | last_term => Plucene::Index::Term->new({ field => "", text => "" }), | ||||||||||||||||||||||||||||
| 54 | last_ti => Plucene::Index::TermInfo->new, | ||||||||||||||||||||||||||||
| 55 | last_index_ptr => 0, | ||||||||||||||||||||||||||||
| 56 | output => Plucene::Store::OutputStream->new( | ||||||||||||||||||||||||||||
| 57 | "$d/$segment.ti" . ($is_i ? "i" : "s") | ||||||||||||||||||||||||||||
| 58 | ), | ||||||||||||||||||||||||||||
| 59 | }, $class; | ||||||||||||||||||||||||||||
| 60 | 594 | ▶ | confess("No field_infos!") unless $self->{field_infos}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 594 | $self->{output}->write_int(0); # Will be filled in when DESTROYed | |||||||||||||||||||||||||||
| 62 | 594 | ▶ | if (!$is_i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 297 | $self->{other} = $class->new($d, $segment, $fis, 1); | |||||||||||||||||||||||||||
| 64 | 297 | $self->{other}->{other} = $self; # My enemy's enemy is my friend | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 594 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 break_ref | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | This will break a circular reference. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Damned circular references. | ||||||||||||||||||||||||||||
| 76 | 297 | ▶ | sub break_ref { undef shift->{other} } | ||||||||||||||||||||||||||
|
Subroutine
break_ref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 add | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | $writer->add(Plucene::Index::Term $term, | ||||||||||||||||||||||||||||
| 81 | Plucene::Index::TermInfo $term_info); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | This will add the term and term info to the term infos file. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 46707 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 46707 | my ($self, $term, $ti) = @_; | |||||||||||||||||||||||||||
| 89 | 19 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | carp sprintf "Can't add out-of-order term %s lt %s (%s lt %s)", $term->text, | ||||||||||||||||||||||||||||
| 91 | $self->{last_term}->text, $term->field, $self->{last_term}->{field} | ||||||||||||||||||||||||||||
| 92 | 46707 | ▶ | if !$self->{is_index} && $term->lt($self->{last_term}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->{"is_index"} and $term->lt($self->{"last_term"})
MC/DC0%0/2
not $self->{"is_index"} and $term->lt($self->{"last_term"})
not $self->{"is_index"}
$term->lt($self->{"last_term"})
Truth table66%2/3
not(A) and B
A= not $self->{"is_index"}B= $term->lt($self->{"last_term"})
| |||||||||||||||||||||||||||||
| 93 | carp "Frequency pointer out of order" | ||||||||||||||||||||||||||||
| 94 | 46707 | ▶ | if $ti->freq_pointer < $self->{last_ti}->freq_pointer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | carp "Proximity pointer out of order" | ||||||||||||||||||||||||||||
| 96 | 46707 | ▶ | if $ti->prox_pointer < $self->{last_ti}->prox_pointer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | $self->{other}->add($self->{last_term}, $self->{last_ti}) | ||||||||||||||||||||||||||||
| 99 | if !$self->{is_index} | ||||||||||||||||||||||||||||
| 100 | 46707 | ▶ | and (($self->{size} % INDEX_INTERVAL) == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"is_index"} and $self->{"size"} % 128 == 0
MC/DC100%2/2
not $self->{"is_index"} and $self->{"size"} % 128 == 0
not $self->{"is_index"}
$self->{"size"} % 128 == 0
Truth table100%3/3
not(A) and B
A= not $self->{"is_index"}B= $self->{"size"} % 128 == 0
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 46707 | $self->write_term($term); | |||||||||||||||||||||||||||
| 103 | 46707 | $self->{output}->write_vint($ti->doc_freq); | |||||||||||||||||||||||||||
| 104 | $self->{output} | ||||||||||||||||||||||||||||
| 105 | 46707 | ->write_vlong($ti->freq_pointer - $self->{last_ti}->freq_pointer); | |||||||||||||||||||||||||||
| 106 | $self->{output} | ||||||||||||||||||||||||||||
| 107 | 46707 | ->write_vlong($ti->prox_pointer - $self->{last_ti}->prox_pointer); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 46707 | ▶ | if ($self->{is_index}) { # I bet Tony will think about subclassing | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | # at this point | ||||||||||||||||||||||||||||
| 111 | $self->{output}->write_vlong( | ||||||||||||||||||||||||||||
| 112 | 635 | $self->{other}->{output}->tell - $self->{last_index_pointer}); | |||||||||||||||||||||||||||
| 113 | 635 | $self->{last_index_pointer} = $self->{other}->{output}->tell; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 46707 | $self->{last_ti} = $ti->clone; | |||||||||||||||||||||||||||
| 117 | 46707 | $self->{size}++; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 write_term | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | $writer->write_term(Plucene::Index::Term $term); | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | This will write the term to the term infos file. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 46707 | ▶ | sub write_term { | ||||||||||||||||||||||||||
|
Subroutine
write_term: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 46707 | my ($self, $term) = @_; | |||||||||||||||||||||||||||
| 130 | 46707 | ▶ | my $text = $term->text || ""; | ||||||||||||||||||||||||||
|
Condition100%2/2
$term->text || ''
MC/DC100%1/1
$term->text || ''
$term->text
Truth table100%2/2
A
A= $term->text
| |||||||||||||||||||||||||||||
| 131 | 19 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Find longest common prefix | ||||||||||||||||||||||||||||
| 134 | 46707 | ($text ^ $self->{last_term}->text) =~ /^(\0*)/; | |||||||||||||||||||||||||||
| 135 | 46707 | my $start = length $1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 46707 | $self->{output}->write_vint($start); | |||||||||||||||||||||||||||
| 138 | 46707 | $self->{output}->write_string(substr($text, $start)); | |||||||||||||||||||||||||||
| 139 | $self->{output} | ||||||||||||||||||||||||||||
| 140 | 46707 | ->write_vint($self->{field_infos}->field_number($term->field)); | |||||||||||||||||||||||||||
| 141 | 46707 | $self->{last_term} = $term; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 594 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 145 | 594 | my $self = shift; | |||||||||||||||||||||||||||
| 146 | 594 | $self->{output}->seek(0, 0); | |||||||||||||||||||||||||||
| 147 | 594 | $self->{output}->write_int($self->{size}); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 19 | 1; | |||||||||||||||||||||||||||