blib/lib/KinoSearch1/Index/FieldInfos.pm

filter: Statementstmt 96.6% 115 / 119 Branchbran 80.7% 21 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 96.1% 25 / 26 Podpod 6.2% 1 / 16 totaltotal 86.6% 162 / 187 CC 20 SCAR 9.1
CC
20
Cov
94%
encode_fnm_bits
5.0
read_infos
4.0
consolidate
4.0
CRAP 2.5
« blib/lib/KinoSearch1/Search/MultiSearcher.pm ↑ Summary blib/lib/KinoSearch1/Highlight/SimpleHTMLEncoder.pm »
1 package KinoSearch1::Index::FieldInfos;
2 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use KinoSearch1::Util::ToolSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use base qw( KinoSearch1::Util::Class Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶use constant INDEXED => "\x01";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use constant VECTORIZED => "\x02";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use constant OMIT_NORMS => "\x10";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 34 our @EXPORT_OK;
12  
13 34 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 __PACKAGE__->init_instance_vars(
15 # members
16 by_name => undef,
17 by_num => undef,
18 from_file => 0,
19 );
20 34 __PACKAGE__->ready_get_set(qw( from_file ));
21  
22 34 @EXPORT_OK = qw(
23 INDEXED
24 VECTORIZED
25 OMIT_NORMS
26 );
27 }
28  
29 34 ▶use KinoSearch1::Document::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 260 ▶sub init_instance {
Subroutine init_instance: called CC=1 SCAR=0.0
Pod documented
32 260 my $self = shift;
33 260 $self->{by_name} = {};
34 260 $self->{by_num} = [];
35 }
36  
37 100 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod undocumented
38 100 my $self = shift;
39 100 my $evil_twin = __PACKAGE__->new;
40 100 $evil_twin->{from_file} = $self->{from_file};
41 100 my @by_num;
42 my %by_name;
43 100 for my $finfo ( @{ $self->{by_num} } ) {
44 182 my $dupe = $finfo->clone;
45 182 push @by_num, $dupe;
46 182 $by_name{ $finfo->get_name } = $dupe;
47 }
48 100 $evil_twin->{by_num} = \@by_num;
49 100 $evil_twin->{by_name} = \%by_name;
50 100 return $evil_twin;
51 }
52  
53 # Add a user-supplied Field object to the collection.
54 158 ▶sub add_field {
Subroutine add_field: called CC=3 SCAR=11.2
Pod undocumented
55 158 my ( $self, $field ) = @_;
56 158 ▶ croak("Not a KinoSearch1::Document::Field")
Branch: 1 branch
BranchTrueFalseTotal
unless a_isa_b($field, "KinoSearch1::Document::Field") 0 158 158
57 unless a_isa_b( $field, 'KinoSearch1::Document::Field' );
58  
59 # don't mod Field objects for segments that are read back in
60 croak("Can't update FieldInfos that were read in from file")
61 158 ▶ if $self->{from_file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"from_file"} 0 158 158
62  
63 # add the field
64 158 my $fieldname = $field->get_name;
65 158 $self->{by_name}{$fieldname} = $field;
66 158 $self->_assign_field_nums;
67 }
68  
69 # Return the number of fields in the segment.
70 208 ▶sub size { scalar @{ $_[0]->{by_num} } }
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
71  
72 # Return a list of the Field objects.
73 339 ▶sub get_infos { @{ $_[0]->{by_num} } }
Subroutine get_infos: called CC=1 SCAR=0.0
Pod undocumented
74  
75 # Given a fieldname, return its number.
76 2157 ▶sub get_field_num {
Subroutine get_field_num: called CC=2 SCAR=6.9
Pod undocumented
77 2157 my ( $self, $name ) = @_;
78 return undef
79 2157 ▶ unless exists $self->{by_name}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"by_name"}{$name} 46 2111 2157
80 2111 my $num = $self->{by_name}{$name}->get_field_num;
81 2111 return $num;
82 }
83  
84 # Given a fieldname, return its FieldInfo.
85 1 ▶sub info_by_name { $_[0]->{by_name}{ $_[1] } }
Subroutine info_by_name: called CC=1 SCAR=0.0
Pod undocumented
86  
87 # Given a field number, return its fieldInfo.
88 146 ▶sub info_by_num { $_[0]->{by_num}[ $_[1] ] }
Subroutine info_by_num: called CC=1 SCAR=0.0
Pod undocumented
89  
90 # Given the field number (new, not original), return the name of the field.
91 10 ▶sub field_name {
Subroutine field_name: called CC=2 SCAR=7.0
Pod undocumented
92 10 my ( $self, $num ) = @_;
93 10 my $name = $self->{by_num}[$num]->get_name;
94 10 ▶ croak("Don't know about field number $num")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 10 10
95 unless defined $name;
96 10 return $name;
97 }
98  
99 # Sort all the fields lexically by name and assign ascending numbers.
100 173 ▶sub _assign_field_nums {
Subroutine _assign_field_nums: called CC=3 SCAR=11.0
101 173 my $self = shift;
102 173 ▶ confess("Can't _assign_field_nums when from_file") if $self->{from_file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"from_file"} 0 173 173
103  
104 # assign field nums according to lexical order of field names
105 173 @{ $self->{by_num} }
106 173 = sort { $a->get_name cmp $b->get_name } values %{ $self->{by_name} };
107 173 my $inc = 0;
108 173 $_->set_field_num( $inc++ ) for @{ $self->{by_num} };
109 }
110  
111 # Decode an existing .fnm file.
112 100 ▶sub read_infos {
Subroutine read_infos: called CC=4 SCAR=13.9
Pod undocumented
113 100 my ( $self, $instream ) = @_;
114 100 my ( $by_name, $by_num ) = @{$self}{qw( by_name by_num )};
115  
116 # set flag indicating that this FieldInfos object has been read in
117 100 $self->{from_file} = 1;
118  
119 # read in infos from stream
120 100 my $num_fields = $instream->lu_read('V');
121 100 my @names_and_bits = $instream->lu_read( 'Ta' x $num_fields );
122 100 my $field_num = 0;
123 100 ▶ while ( $field_num < $num_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_num < $num_fields) 247 100 347
124 247 my ( $name, $bits ) = splice( @names_and_bits, 0, 2 );
125 247 ▶ my $info = KinoSearch1::Document::Field->new(
Branch: 2 branches
BranchTrueFalseTotal
("$bits" & "\cA") eq "\cA" ? : 229 18 247
("$bits" & "\cB") eq "\cB" ? : 243 4 247
126 field_num => $field_num,
127 name => $name,
128 indexed => ( "$bits" & INDEXED ) eq INDEXED ? 1 : 0,
129 vectorized => ( "$bits" & VECTORIZED ) eq VECTORIZED ? 1 : 0,
130 fnm_bits => $bits,
131 );
132 247 $by_name->{$name} = $info;
133 # order of storage implies lexical order by name and field number
134 247 push @$by_num, $info;
135 247 $field_num++;
136 }
137 }
138  
139 # Write .fnm file.
140 64 ▶sub write_infos {
Subroutine write_infos: called CC=2 SCAR=6.9
Pod undocumented
141 64 my ( $self, $outstream ) = @_;
142  
143 64 $outstream->lu_write( 'V', scalar @{ $self->{by_num} } );
144 64 for my $finfo ( @{ $self->{by_num} } ) {
145 162 $outstream->lu_write( 'Ta', $finfo->get_name, $finfo->get_fnm_bits, );
146 }
147 }
148  
149 # Merge two FieldInfos objects, redefining fields as necessary and generating
150 # new field numbers.
151 15 ▶sub consolidate {
Subroutine consolidate: called CC=4 SCAR=13.9
Pod undocumented
152 15 my ( $self, @others ) = @_;
153 15 my $infos = $self->{by_name};
154  
155 # Make *this* finfos the master FieldInfos object
156 15 for my $other (@others) {
157 36 ▶ while ( my ( $name, $other_finfo ) = each %{ $other->{by_name} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$other{"by_name"};}) 48 36 84
158 48 ▶ if ( exists $infos->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $infos->{$name}) { } 28 20 48
159 28 $infos->{$name} = $other_finfo->breed_with( $infos->{$name} );
160 }
161 else {
162 20 $infos->{$name} = $other_finfo->clone;
163 }
164 }
165 }
166  
167 15 $self->_assign_field_nums;
168 }
169  
170 # Generate a mapping of field numbers between two FieldInfos objects. Should
171 # be called by the superset.
172 16 ▶sub generate_field_num_map {
Subroutine generate_field_num_map: called CC=2 SCAR=6.9
Pod undocumented
173 16 my ( $self, $other ) = @_;
174 16 my $map = '';
175 16 for my $other_finfo ( @{ $other->{by_num} } ) {
176 18 my $orig_finfo = $self->{by_name}{ $other_finfo->get_name };
177 18 $map .= pack( 'I', $orig_finfo->get_field_num );
178 }
179 16 return KinoSearch1::Util::IntMap->new( \$map );
180 }
181  
182 158 ▶sub encode_fnm_bits {
Subroutine encode_fnm_bits: called CC=5 SCAR=16.1
Pod undocumented
183 158 my ( undef, $field ) = @_;
184 158 my $bits = "\0";
185 158 for ($bits) {
186 158 ▶ $_ |= INDEXED if $field->get_indexed;
Branch: 1 branch
BranchTrueFalseTotal
if $field->get_indexed 154 4 158
187 158 ▶ $_ |= VECTORIZED if $field->get_vectorized;
Branch: 1 branch
BranchTrueFalseTotal
if $field->get_vectorized 154 4 158
188 158 ▶ $_ |= OMIT_NORMS if $field->get_omit_norms;
Branch: 1 branch
BranchTrueFalseTotal
if $field->get_omit_norms 0 158 158
189 }
190 158 return $bits;
191 }
192  
193 0 ▶sub decode_fnm_bits {
Subroutine decode_fnm_bits: not called CC=1 SCAR=6.9
Pod undocumented
194 0 my ( undef, $field, $bits ) = @_;
195 0 $field->set_indexed( ( $bits & INDEXED ) eq INDEXED );
196 0 $field->set_vectorized( ( $bits & VECTORIZED ) eq VECTORIZED );
197 0 $field->set_omit_norms( ( $bits & OMIT_NORMS ) eq OMIT_NORMS );
198 }
199  
200 39 ▶sub close { }
Subroutine close: called CC=1 SCAR=0.0
Pod undocumented
201  
202 34 1;
203  
204 __END__
« blib/lib/KinoSearch1/Search/MultiSearcher.pm ↑ Summary blib/lib/KinoSearch1/Highlight/SimpleHTMLEncoder.pm »