blib/lib/KinoSearch1/Index/FieldsWriter.pm

filter: Statementstmt 100% 54 / 54 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 25.0% 1 / 4 totaltotal 92.1% 70 / 76 CC 9 SCAR 10.3
CC
9
Cov
95%
add_segment
5.0
init_instance
3.0
add_doc
3.0
CRAP 2.8
« blib/lib/KinoSearch1/Searcher.pm ↑ Summary blib/lib/KinoSearch1/Index/CompoundFileWriter.pm »
1 package KinoSearch1::Index::FieldsWriter;
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 );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 __PACKAGE__->init_instance_vars(
9 # constructor params / members
10 invindex => undef,
11 seg_name => undef,
12 # members
13 fdata_stream => undef,
14 findex_stream => undef,
15 );
16 }
17 34 ▶use Compress::Zlib qw( compress );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 62 ▶sub init_instance {
Subroutine init_instance: called CC=3 SCAR=11.0
Pod documented
20 62 my $self = shift;
21 62 my $invindex = $self->{invindex};
22  
23 # open an index stream and a data stream.
24 62 my $fdx_file = "$self->{seg_name}.fdx";
25 62 my $fdt_file = "$self->{seg_name}.fdt";
26 62 for ( $fdx_file, $fdt_file, ) {
27 124 ▶ $invindex->delete_file($_) if $invindex->file_exists($_);
Branch: 1 branch
BranchTrueFalseTotal
if $invindex->file_exists($_) 0 124 124
28 }
29 62 $self->{findex_stream} = $invindex->open_outstream($fdx_file);
30 62 $self->{fdata_stream} = $invindex->open_outstream($fdt_file);
31 }
32  
33 14017 ▶sub add_doc {
Subroutine add_doc: called CC=3 SCAR=11.0
Pod undocumented
34 14017 my ( $self, $doc ) = @_;
35  
36 # record the data stream's current file pointer in the index.
37 14017 $self->{findex_stream}->lu_write( 'Q', $self->{fdata_stream}->tell );
38  
39 # only store fields marked as "stored"
40 14017 my @stored = sort { $a->get_field_num <=> $b->get_field_num }
41 grep $_->get_stored, $doc->get_fields;
42  
43 # add the number of stored fields in the Doc
44 14017 my @to_write = ( scalar @stored );
45  
46 # add flag bits and value for each stored field
47 14017 for (@stored) {
48 15390 push @to_write, ( $_->get_field_num, $_->get_fdt_bits );
49 15390 ▶ push @to_write, $_->get_compressed
Branch: 1 branch
BranchTrueFalseTotal
$_->get_compressed ? : 2 15388 15390
50 ? compress( $_->get_value )
51 : $_->get_value;
52 15390 push @to_write, $_->get_tv_string;
53 }
54  
55 # write out data
56 14017 my $lu_template = 'V' . ( 'VaTT' x scalar @stored );
57 14017 $self->{fdata_stream}->lu_write( $lu_template, @to_write );
58 }
59  
60 16 ▶sub add_segment {
Subroutine add_segment: called CC=5 SCAR=16.2
Pod undocumented
61 16 my ( $self, $seg_reader, $doc_map, $field_num_map ) = @_;
62 my ( $findex_stream, $fdata_stream )
63 16 = @{$self}{qw( findex_stream fdata_stream )};
64 16 my $fields_reader = $seg_reader->get_fields_reader;
65  
66 16 my $max = $seg_reader->max_doc;
67 16 ▶ return unless $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $max 0 16 16
68 16 $max -= 1;
69 16 for my $orig ( 0 .. $max ) {
70 # if the doc isn't deleted, copy it to the new seg
71 10138 ▶ next unless defined $doc_map->get($orig);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc_map->get($orig) 0 10138 10138
72  
73 # write pointer
74 10138 $findex_stream->lu_write( 'Q', $fdata_stream->tell );
75  
76 # retrieve all fields
77 10138 my ( $num_fields, $all_data ) = $fields_reader->fetch_raw($orig);
78  
79 # write number of fields
80 10138 $fdata_stream->lu_write( 'V', $num_fields );
81  
82 # write data for each field
83 10138 for ( 1 .. $num_fields ) {
84 10138 my ( $field_num, @some_data ) = splice( @$all_data, 0, 4 );
85 10138 $fdata_stream->lu_write( 'VaTT', $field_num_map->get($field_num),
86 @some_data );
87 }
88 }
89 }
90  
91 62 ▶sub finish {
Subroutine finish: called CC=1 SCAR=0.0
Pod undocumented
92 62 my $self = shift;
93 62 $self->{fdata_stream}->close;
94 62 $self->{findex_stream}->close;
95 }
96  
97 34 1;
98  
99 __END__
« blib/lib/KinoSearch1/Searcher.pm ↑ Summary blib/lib/KinoSearch1/Index/CompoundFileWriter.pm »