| 1 | package Plucene::Index::FieldInfos; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Plucene::Index::FieldInfos - a collection of FieldInfo objects | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | my $fis = Plucene::Index::FieldInfos->new($dir_name); | ||||||||||||||||||||||||||||
| 10 | my $fis = Plucene::Index::FieldInfos->new($dir_name, $file); | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | $fis->add(Plucene::Document $doc, $indexed); | ||||||||||||||||||||||||||||
| 13 | $fis->add(Plucene::Index::FieldInfos $other_fis, $indexed); | ||||||||||||||||||||||||||||
| 14 | $fis->add($name, $indexed); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | $fis->write($path); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my @fields = $fis->fields; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my $field_number = $fis->field_number($name); | ||||||||||||||||||||||||||||
| 21 | my $field_info = $fis->field_info($name); | ||||||||||||||||||||||||||||
| 22 | my $field_name = $fis->field_name($number); | ||||||||||||||||||||||||||||
| 23 | my $num_fields = $fis->size; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | This is a collection of field info objects, which happen to live in | ||||||||||||||||||||||||||||
| 29 | the field infos file. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 19 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 19 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 19 | ▶ | use Class::Struct 'Plucene::Index::FieldInfo' => | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 19 | [ name => '$', is_indexed => '$', number => '$' ]; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 new | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | my $fis = Plucene::Index::FieldInfos->new($dir_name); | ||||||||||||||||||||||||||||
| 46 | my $fis = Plucene::Index::FieldInfos->new($dir_name, $file); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | This will create a new Plucene::Index::FieldInfos object with the passed | ||||||||||||||||||||||||||||
| 49 | directory and optional filename. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 880 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 880 | my ($class, $dir, $file) = @_; | |||||||||||||||||||||||||||
| 55 | 880 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 56 | 880 | ▶ | $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | ? $self->_read("$dir/$file") | ||||||||||||||||||||||||||||
| 58 | : $self->_add_internal("", 0); | ||||||||||||||||||||||||||||
| 59 | 880 | return $self; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 add | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | $fis->add(Plucene::Document $doc, $indexed); | ||||||||||||||||||||||||||||
| 65 | $fis->add(Plucene::Index::FieldInfos $other_fis, $indexed); | ||||||||||||||||||||||||||||
| 66 | $fis->add($name, $indexed); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | This will add the fields from a Plucene::Document or a | ||||||||||||||||||||||||||||
| 69 | Plucene::Index::FieldsInfos to the field infos file. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | It is also possible to pass the name of a field and have it added | ||||||||||||||||||||||||||||
| 72 | to the file. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1574 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 1574 | my ($self, $obj, $indexed) = @_; | |||||||||||||||||||||||||||
| 78 | 1574 | ▶ | if ( UNIVERSAL::isa($obj, "Plucene::Document") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
UNIVERSAL::isa($obj, "Plucene::Document") or UNIVERSAL::isa($obj, "Plucene::Index::FieldInfos")
MC/DC100%2/2
UNIVERSAL::isa($obj, "Plucene::Document") or UNIVERSAL::isa($obj, "Plucene::Index::FieldInfos")
UNIVERSAL::isa($obj, "Plucene::Document")
UNIVERSAL::isa($obj, "Plucene::Index::FieldInfos")
Truth table100%3/3
A or B
A= UNIVERSAL::isa($obj, "Plucene::Document")B= UNIVERSAL::isa($obj, "Plucene::Index::FieldInfos")
| |||||||||||||||||||||||||||||
| 79 | or UNIVERSAL::isa($obj, "Plucene::Index::FieldInfos")) { | ||||||||||||||||||||||||||||
| 80 | 531 | $self->add($_->name, $_->is_indexed) for $obj->fields; | |||||||||||||||||||||||||||
| 81 | 531 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 1043 | ▶ | confess "Don't yet know how to handle a $obj" if ref $obj; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1043 | my $name = $obj; # For clarity. :) | |||||||||||||||||||||||||||
| 85 | 1043 | my $fi = $self->field_info($name); | |||||||||||||||||||||||||||
| 86 | 1043 | ▶ | $fi | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | ? $fi->is_indexed($indexed) | ||||||||||||||||||||||||||||
| 88 | : $self->_add_internal($name, $indexed); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2323 | ▶ | sub _add_internal { | ||||||||||||||||||||||||||
|
Subroutine
_add_internal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 2323 | my ($self, $name, $indexed) = @_; | |||||||||||||||||||||||||||
| 93 | my $fi = Plucene::Index::FieldInfo->new( | ||||||||||||||||||||||||||||
| 94 | name => $name, | ||||||||||||||||||||||||||||
| 95 | is_indexed => $indexed, | ||||||||||||||||||||||||||||
| 96 | 2323 | number => $#{ $self->{bynumber} } + 1, | |||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | 2323 | push @{ $self->{bynumber} }, $fi; | |||||||||||||||||||||||||||
| 99 | 2323 | $self->{byname}{$name} = $fi; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 field_number | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | my $field_number = $fis->field_number($name); | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | This will return the field number of the field with $name. If there is | ||||||||||||||||||||||||||||
| 107 | no match, then -1 is returned. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 48263 | ▶ | sub field_number { | ||||||||||||||||||||||||||
|
Subroutine
field_number: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 48263 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 113 | 48263 | ▶ | return -1 unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 48263 | ▶ | my $field = $self->{byname}{$name} or return -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 48263 | return $field->number; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 fields | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | my @fields = $fis->fields; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | This will return all the fields. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1170 | ▶ | sub fields { return @{ $_[0]->{bynumber} } } | ||||||||||||||||||||||||||
|
Subroutine
fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 field_info | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | my $field_info = $fis->field_info($name); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | This will return the field info for the field called $name. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Please ensure nothing in the code tries passing this a number. :( | ||||||||||||||||||||||||||||
| 137 | 1043 | ▶ | sub field_info { $_[0]->{byname}{ $_[1] } } | ||||||||||||||||||||||||||
|
Subroutine
field_info: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 field_name | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | my $field_name = $fis->field_name($number); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | This will return the field name for the field whose number is $number. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 77510 | ▶ | sub field_name { $_[0]->{bynumber}[ $_[1] ]->name } | ||||||||||||||||||||||||||
|
Subroutine
field_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 size | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | my $num_fields = $fis->size; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | This returns the number of field info objects. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub size { scalar $_[0]->fields } | ||||||||||||||||||||||||||
|
Subroutine
size: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 write | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | $fis->write($path); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | This will write the field info objects to $path. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # Called by DocumentWriter->add_document and | ||||||||||||||||||||||||||||
| 168 | # SegmentMerger->merge_fields | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 296 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 296 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 172 | 296 | my @fi = @{ $self->{bynumber} }; | |||||||||||||||||||||||||||
| 173 | 296 | my $template = "w" . ("w/a*C" x @fi); | |||||||||||||||||||||||||||
| 174 | my $packed = pack $template, scalar(@fi), | ||||||||||||||||||||||||||||
| 175 | 296 | ▶ | map { $_->name => ($_->is_indexed ? 1 : 0) } @fi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 296 | write_file($file => $packed); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 583 | ▶ | sub _read { | ||||||||||||||||||||||||||
|
Subroutine
_read: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 180 | 583 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||
| 181 | 583 | my @fields = unpack "w/(w/aC)", read_file($filename); | |||||||||||||||||||||||||||
| 182 | 583 | ▶ | while (my ($field, $indexed) = splice @fields, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 1591 | $self->_add_internal($field => $indexed); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 19 | 1; | |||||||||||||||||||||||||||