| 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 | |||||||||||||||||||||||||||||
| 18 | package LucyX::Index::ZlibDocWriter; | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use base qw( Lucy::Index::DataWriter ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Compress::Zlib qw( compress ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Lucy::Util::StringHelper qw( cat_bytes ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use Clownfish; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | no bytes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | our $VERSION = '0.008000'; | |||||||||||||||||||||||||||
| 29 | 2 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Inside-out member vars. | ||||||||||||||||||||||||||||
| 32 | 2 | our %ix_out; | |||||||||||||||||||||||||||
| 33 | 2 | our %dat_out; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Inherit constructor. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 4 | ▶ | sub _lazy_init { | ||||||||||||||||||||||||||
|
Subroutine
_lazy_init: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 38 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # Get outstreams. Skip past non-doc #0. | ||||||||||||||||||||||||||||
| 41 | 4 | my $folder = $self->get_folder; | |||||||||||||||||||||||||||
| 42 | 4 | my $ix_file = $self->get_segment->get_name . "/zdocs.ix"; | |||||||||||||||||||||||||||
| 43 | 4 | my $dat_file = $self->get_segment->get_name . "/zdocs.dat"; | |||||||||||||||||||||||||||
| 44 | 4 | ▶ | $ix_out{$$self} = $folder->open_out($ix_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | or confess Clownfish->error; | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | $dat_out{$$self} = $folder->open_out($dat_file) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | or confess Clownfish->error; | ||||||||||||||||||||||||||||
| 48 | 4 | $ix_out{$$self}->write_i64(0); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 13 | ▶ | sub add_inverted_doc { | ||||||||||||||||||||||||||
|
Subroutine
add_inverted_doc: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 13 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 53 | 13 | ▶ | _lazy_init($self) unless $ix_out{$$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 13 | my $inverter = $args{inverter}; | |||||||||||||||||||||||||||
| 55 | 13 | my $ix_out = $ix_out{$$self}; | |||||||||||||||||||||||||||
| 56 | 13 | my $dat_out = $dat_out{$$self}; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Check doc id. | ||||||||||||||||||||||||||||
| 59 | 13 | my $expected = $ix_out->tell / 8; | |||||||||||||||||||||||||||
| 60 | confess("Expected doc id $expected, got '$args{doc_id}'") | ||||||||||||||||||||||||||||
| 61 | 13 | ▶ | unless $args{doc_id} == $expected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 13 | my $to_compress = ""; | |||||||||||||||||||||||||||
| 64 | 13 | my $count = 0; | |||||||||||||||||||||||||||
| 65 | 13 | my $schema = $self->get_schema; | |||||||||||||||||||||||||||
| 66 | 13 | $inverter->iterate; | |||||||||||||||||||||||||||
| 67 | 13 | ▶ | while ( $inverter->next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 52 | ▶ | next unless $inverter->get_type->stored; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 39 | my $name = $inverter->get_field_name; | |||||||||||||||||||||||||||
| 70 | 39 | my $value = $inverter->get_value; | |||||||||||||||||||||||||||
| 71 | 39 | cat_bytes( $to_compress, pack( "w", bytes::length($name) ) ); | |||||||||||||||||||||||||||
| 72 | 39 | cat_bytes( $to_compress, $name ); | |||||||||||||||||||||||||||
| 73 | 39 | cat_bytes( $to_compress, pack( "w", bytes::length($value) ) ); | |||||||||||||||||||||||||||
| 74 | 39 | cat_bytes( $to_compress, $value ); | |||||||||||||||||||||||||||
| 75 | 39 | $count++; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | # Prepend count of fields to store in this Doc. | ||||||||||||||||||||||||||||
| 78 | 13 | $to_compress = pack( "w", $count ) . $to_compress; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Write file pointer to index file. Write compressed serialized string to | ||||||||||||||||||||||||||||
| 81 | # main file. | ||||||||||||||||||||||||||||
| 82 | 13 | $ix_out->write_i64( $dat_out->tell ); | |||||||||||||||||||||||||||
| 83 | 13 | $dat_out->print( compress($to_compress) ); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 3 | ▶ | sub add_segment { | ||||||||||||||||||||||||||
|
Subroutine
add_segment: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 3 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 88 | 3 | my $seg_reader = $args{reader}; | |||||||||||||||||||||||||||
| 89 | 3 | my $doc_map = $args{doc_map}; | |||||||||||||||||||||||||||
| 90 | 3 | my $doc_max = $seg_reader->doc_max; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Bail if the supplied segment is empty. */ | ||||||||||||||||||||||||||||
| 93 | 3 | ▶ | return unless $doc_max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 3 | ▶ | _lazy_init($self) unless $ix_out{$$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 3 | my $ix_out = $ix_out{$$self}; | |||||||||||||||||||||||||||
| 97 | 3 | my $dat_out = $dat_out{$$self}; | |||||||||||||||||||||||||||
| 98 | 3 | my $doc_reader = $seg_reader->obtain("Lucy::Index::DocReader"); | |||||||||||||||||||||||||||
| 99 | 3 | ▶ | confess("Not a ZlibDocReader") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($doc_reader) and $doc_reader->isa("LucyX::Index::ZlibDocReader")
MC/DC0%0/2
builtin::blessed($doc_reader) and $doc_reader->isa("LucyX::Index::ZlibDocReader")
builtin::blessed($doc_reader)
$doc_reader->isa("LucyX::Index::ZlibDocReader")
Truth table33%1/3
A and B
A= builtin::blessed($doc_reader)B= $doc_reader->isa("LucyX::Index::ZlibDocReader")
| |||||||||||||||||||||||||||||
| 100 | unless ( blessed($doc_reader) | ||||||||||||||||||||||||||||
| 101 | and $doc_reader->isa("LucyX::Index::ZlibDocReader") ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | ▶ | for ( my $i = 1; $i <= $doc_max; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 24 | ▶ | next unless $doc_map->get($i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 21 | my $buf; | |||||||||||||||||||||||||||
| 106 | 21 | $doc_reader->read_record( $i, \$buf ); | |||||||||||||||||||||||||||
| 107 | 21 | $ix_out->write_i64( $dat_out->tell ); | |||||||||||||||||||||||||||
| 108 | 21 | $dat_out->print($buf); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 4 | ▶ | sub finish { | ||||||||||||||||||||||||||
|
Subroutine
finish: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 4 | my $ix_out = $ix_out{$$self}; | |||||||||||||||||||||||||||
| 115 | 4 | my $dat_out = $dat_out{$$self}; | |||||||||||||||||||||||||||
| 116 | 4 | ▶ | if ($ix_out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | # Write one extra file pointer so that we can always derive record | ||||||||||||||||||||||||||||
| 118 | # length. | ||||||||||||||||||||||||||||
| 119 | 4 | $ix_out->write_i64( $dat_out->tell ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # Close streams and store metadata. | ||||||||||||||||||||||||||||
| 122 | 4 | $ix_out->close; | |||||||||||||||||||||||||||
| 123 | 4 | $dat_out->close; | |||||||||||||||||||||||||||
| 124 | 4 | my $segment = $self->get_segment; | |||||||||||||||||||||||||||
| 125 | 4 | $segment->store_metadata( | |||||||||||||||||||||||||||
| 126 | key => 'zdocs', | ||||||||||||||||||||||||||||
| 127 | metadata => $self->metadata, | ||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 4 | ▶ | sub format {1} | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 4 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 4 | delete $ix_out{$$self}; | |||||||||||||||||||||||||||
| 137 | 4 | delete $dat_out{$$self}; | |||||||||||||||||||||||||||
| 138 | 4 | $self->SUPER::DESTROY; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 2 | 1; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||