| 1 | package KinoSearch1::Index::FieldsReader; | ||||||||||
| 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 ANALYZED => "\x01"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 34 | ▶ | use constant BINARY => "\x02"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 34 | ▶ | use constant COMPRESSED => "\x04"; | ||||||||
|
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 | @EXPORT_OK = qw( ANALYZED BINARY COMPRESSED ); | |||||||||
| 15 | 34 | __PACKAGE__->init_instance_vars( | |||||||||
| 16 | # constructor params / members | ||||||||||
| 17 | finfos => undef, | ||||||||||
| 18 | fdata_stream => undef, | ||||||||||
| 19 | findex_stream => undef, | ||||||||||
| 20 | # members | ||||||||||
| 21 | size => undef, | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 34 | ▶ | use Compress::Zlib qw( uncompress ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 34 | ▶ | use KinoSearch1::Document::Field; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 34 | ▶ | use KinoSearch1::Document::Doc; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | 95 | ▶ | sub init_instance { | ||||||||
|
Subroutine
init_instance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | 95 | my $self = shift; | |||||||||
| 32 | |||||||||||
| 33 | # derive the number of documents in the segment | ||||||||||
| 34 | 95 | $self->{size} = $self->{findex_stream}->length / 8; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | # Return number of documents in segment. | ||||||||||
| 38 | 1070 | ▶ | sub get_size { $_[0]->{size} } | ||||||||
|
Subroutine
get_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 39 | |||||||||||
| 40 | # Retrieve raw field data from files. Either the data will be turned into | ||||||||||
| 41 | # full-on Field and Doc objects by fetch_doc, or it will be passed on mostly | ||||||||||
| 42 | # intact when merging segments (field numbers will be modified). | ||||||||||
| 43 | 10184 | ▶ | sub fetch_raw { | ||||||||
|
Subroutine
fetch_raw: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 44 | 10184 | my ( $self, $doc_num ) = @_; | |||||||||
| 45 | my ( $findex_stream, $fdata_stream ) | ||||||||||
| 46 | 10184 | = @{$self}{ 'findex_stream', 'fdata_stream' }; | |||||||||
| 47 | |||||||||||
| 48 | # get data file pointer from index | ||||||||||
| 49 | 10184 | $findex_stream->seek( $doc_num * 8 ); | |||||||||
| 50 | 10184 | my $start = $findex_stream->lu_read('Q'); | |||||||||
| 51 | |||||||||||
| 52 | # retrieve one doc's worth of field data | ||||||||||
| 53 | 10184 | $fdata_stream->seek($start); | |||||||||
| 54 | 10184 | my $num_fields = $fdata_stream->lu_read('V'); | |||||||||
| 55 | 10184 | my $template = 'VaTT' x $num_fields; | |||||||||
| 56 | 10184 | my @raw = $fdata_stream->lu_read($template); | |||||||||
| 57 | 10184 | return ( $num_fields, \@raw ); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # Given a doc_num, rebuild a Doc object from the fields that were | ||||||||||
| 61 | # stored. | ||||||||||
| 62 | 46 | ▶ | sub fetch_doc { | ||||||||
|
Subroutine
fetch_doc: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||
| 63 | 46 | my ( $self, $doc_num ) = @_; | |||||||||
| 64 | 46 | my $finfos = $self->{finfos}; | |||||||||
| 65 | |||||||||||
| 66 | # start a new Doc object, read in data | ||||||||||
| 67 | 46 | my $doc = KinoSearch1::Document::Doc->new; | |||||||||
| 68 | 46 | my ( $num_fields, $data ) = $self->fetch_raw($doc_num); | |||||||||
| 69 | |||||||||||
| 70 | # docode stored data and build up the Doc object Field by Field. | ||||||||||
| 71 | 46 | for ( 1 .. $num_fields ) { | |||||||||
| 72 | 120 | my ( $field_num, $bits, $string, $tv_string ) | |||||||||
| 73 | = splice( @$data, 0, 4 ); | ||||||||||
| 74 | |||||||||||
| 75 | # decode fnm bits | ||||||||||
| 76 | 120 | ▶ | my $analyzed = ( $bits & ANALYZED ) eq ANALYZED ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 120 | ▶ | my $binary = ( $bits & BINARY ) eq BINARY ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 120 | ▶ | my $compressed = ( $bits & COMPRESSED ) eq COMPRESSED ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | |||||||||||
| 80 | # create a field object, merging in the FieldInfo data, and add it | ||||||||||
| 81 | 120 | my $finfo = $finfos->info_by_num($field_num); | |||||||||
| 82 | 120 | ▶ | my $field = KinoSearch1::Document::Field->new( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | %$finfo, | ||||||||||
| 84 | field_num => $field_num, | ||||||||||
| 85 | analyzed => $analyzed, | ||||||||||
| 86 | binary => $binary, | ||||||||||
| 87 | compressed => $compressed, | ||||||||||
| 88 | fdt_bits => $bits, | ||||||||||
| 89 | value => $compressed ? uncompress($string) : $string, | ||||||||||
| 90 | tv_string => $tv_string, | ||||||||||
| 91 | ); | ||||||||||
| 92 | 120 | $doc->add_field($field); | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 46 | return $doc; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | sub decode_fdt_bits { | ||||||||
|
Subroutine
decode_fdt_bits: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 99 | 0 | my ( undef, $field, $bits ) = @_; | |||||||||
| 100 | 0 | $field->set_analyzed( ( $bits & ANALYZED ) eq ANALYZED ); | |||||||||
| 101 | 0 | $field->set_binary( ( $bits & BINARY ) eq BINARY ); | |||||||||
| 102 | 0 | $field->set_compressed( ( $bits & COMPRESSED ) eq COMPRESSED ); | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 128 | ▶ | sub encode_fdt_bits { | ||||||||
|
Subroutine
encode_fdt_bits: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||
| 106 | 128 | my ( undef, $field ) = @_; | |||||||||
| 107 | 128 | my $bits = "\0"; | |||||||||
| 108 | 128 | for ($bits) { | |||||||||
| 109 | 128 | ▶ | $_ |= ANALYZED if $field->get_analyzed; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 128 | ▶ | $_ |= BINARY if $field->get_binary; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 128 | ▶ | $_ |= COMPRESSED if $field->get_compressed; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | } | ||||||||||
| 113 | 128 | return $bits; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | 39 | ▶ | sub close { | ||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 117 | 39 | my $self = shift; | |||||||||
| 118 | 39 | $self->{findex_stream}->close; | |||||||||
| 119 | 39 | $self->{fdata_stream}->close; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 34 | 1; | |||||||||
| 123 | |||||||||||
| 124 | __END__ | ||||||||||