| 1 | # Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||
| 2 | # contributor license agreements. See the NOTICE file distributed with | ||||||||||
| 3 | # this work for additional information regarding copyright ownership. | ||||||||||
| 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||
| 5 | # (the "License"); you may not use this file except in compliance with | ||||||||||
| 6 | # the License. You may obtain a copy of the License at | ||||||||||
| 7 | # | ||||||||||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||
| 9 | # | ||||||||||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||||||||||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||
| 13 | # See the License for the specific language governing permissions and | ||||||||||
| 14 | # limitations under the License. | ||||||||||
| 15 | |||||||||||
| 16 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | package LucyX::Index::ZlibDocReader; | ||||||||||
| 20 | 2 | ▶ | use base qw( Lucy::Index::DocReader ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | our $VERSION = '0.008000'; | |||||||||
| 22 | 2 | $VERSION = eval $VERSION; | |||||||||
| 23 | 2 | ▶ | use Lucy::Util::StringHelper qw( utf8_valid utf8_flag_on ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | ▶ | use Compress::Zlib qw( uncompress ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | # Inside-out member vars. | ||||||||||
| 28 | 2 | our %ix_in; | |||||||||
| 29 | 2 | our %dat_in; | |||||||||
| 30 | 2 | our %binary_fields; | |||||||||
| 31 | |||||||||||
| 32 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=6 SCAR=18.3
Pod
documented
| |||||||||||
| 33 | 6 | my ( $either, %args ) = @_; | |||||||||
| 34 | 6 | my $self = $either->SUPER::new(%args); | |||||||||
| 35 | |||||||||||
| 36 | # Validate metadata. Only open streams if the segment has data we | ||||||||||
| 37 | # recognize. | ||||||||||
| 38 | 6 | my $segment = $self->get_segment; | |||||||||
| 39 | 6 | my $metadata = $segment->fetch_metadata("zdocs"); | |||||||||
| 40 | 6 | ▶ | if ($metadata) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 6 | ▶ | if ( $metadata->{format} != 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | confess("Unrecognized format: '$metadata->{format}'"); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | # Open InStreams. | ||||||||||
| 46 | 6 | my $dat_filename = $segment->get_name . "/zdocs.dat"; | |||||||||
| 47 | 6 | my $ix_filename = $segment->get_name . "/zdocs.ix"; | |||||||||
| 48 | 6 | my $folder = $self->get_folder; | |||||||||
| 49 | 6 | ▶ | $ix_in{$$self} = $folder->open_in($ix_filename) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | or confess Clownfish->error; | ||||||||||
| 51 | 6 | ▶ | $dat_in{$$self} = $folder->open_in($dat_filename) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | or confess Clownfish->error; | ||||||||||
| 53 | |||||||||||
| 54 | # Remember which fields are binary. | ||||||||||
| 55 | 6 | my $schema = $self->get_schema; | |||||||||
| 56 | 6 | my $bin_fields = $binary_fields{$$self} = {}; | |||||||||
| 57 | $bin_fields->{$_} = 1 | ||||||||||
| 58 | 6 | for grep { $schema->fetch_type($_)->binary } | |||||||||
| 59 | 6 | @{ $schema->all_fields }; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 6 | return $self; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 3 | ▶ | sub fetch_doc { | ||||||||
|
Subroutine
fetch_doc: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 66 | 3 | my ( $self, $doc_id ) = @_; | |||||||||
| 67 | 3 | my $dat_in = $dat_in{$$self}; | |||||||||
| 68 | 3 | my $ix_in = $ix_in{$$self}; | |||||||||
| 69 | 3 | my $bin_fields = $binary_fields{$$self}; | |||||||||
| 70 | |||||||||||
| 71 | # Bail if no data in segment. | ||||||||||
| 72 | 3 | ▶ | return unless $ix_in; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | # Read index information. | ||||||||||
| 75 | 3 | $ix_in->seek( $doc_id * 8 ); | |||||||||
| 76 | 3 | my $start = $ix_in->read_i64; | |||||||||
| 77 | 3 | my $len = $ix_in->read_i64 - $start; | |||||||||
| 78 | 3 | my $compressed; | |||||||||
| 79 | |||||||||||
| 80 | # Read main data. | ||||||||||
| 81 | 3 | $dat_in->seek($start); | |||||||||
| 82 | 3 | $dat_in->read( $compressed, $len ); | |||||||||
| 83 | 3 | my $inflated = uncompress($compressed); | |||||||||
| 84 | 3 | my $num_fields = unpack( "w", $inflated ); | |||||||||
| 85 | 3 | my $pack_template = "w "; | |||||||||
| 86 | 3 | $pack_template .= "w/a*" x ( $num_fields * 2 ); | |||||||||
| 87 | 3 | my ( undef, %fields ) = unpack( $pack_template, $inflated ); | |||||||||
| 88 | |||||||||||
| 89 | # Turn on UTF-8 flag for string fields. | ||||||||||
| 90 | 3 | for my $field ( keys %fields ) { | |||||||||
| 91 | 9 | ▶ | next if $bin_fields->{$field}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 6 | utf8_flag_on( $fields{$field} ); | |||||||||
| 93 | confess("Invalid UTF-8 read for doc $doc_id field '$field'") | ||||||||||
| 94 | 6 | ▶ | unless utf8_valid( $fields{$field} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 3 | return Lucy::Document::HitDoc->new( | |||||||||
| 98 | fields => \%fields, | ||||||||||
| 99 | doc_id => $doc_id, | ||||||||||
| 100 | ); | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 21 | ▶ | sub read_record { | ||||||||
|
Subroutine
read_record: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 104 | 21 | my ( $self, $doc_id, $buf ) = @_; | |||||||||
| 105 | 21 | my $dat_in = $dat_in{$$self}; | |||||||||
| 106 | 21 | my $ix_in = $ix_in{$$self}; | |||||||||
| 107 | 21 | my $bin_fields = $binary_fields{$$self}; | |||||||||
| 108 | |||||||||||
| 109 | 21 | ▶ | if ($ix_in) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 21 | $ix_in->seek( $doc_id * 8 ); | |||||||||
| 111 | 21 | my $start = $ix_in->read_i64; | |||||||||
| 112 | 21 | my $len = $ix_in->read_i64 - $start; | |||||||||
| 113 | 21 | $dat_in->seek($start); | |||||||||
| 114 | 21 | $dat_in->read( $$buf, $len ); | |||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 6 | ▶ | sub close { | ||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 119 | 6 | my $self = shift; | |||||||||
| 120 | 6 | delete $ix_in{$$self}; | |||||||||
| 121 | 6 | delete $dat_in{$$self}; | |||||||||
| 122 | 6 | delete $binary_fields{$$self}; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 6 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 126 | 6 | my $self = shift; | |||||||||
| 127 | 6 | delete $ix_in{$$self}; | |||||||||
| 128 | 6 | delete $dat_in{$$self}; | |||||||||
| 129 | 6 | delete $binary_fields{$$self}; | |||||||||
| 130 | 6 | $self->SUPER::DESTROY; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 2 | 1; | |||||||||
| 134 | |||||||||||
| 135 | __END__ | ||||||||||