File Coverage

blib/lib/LucyX/Index/ByteBufDocWriter.pm
Criterion Covered Total %
statement 70 72 97.2
branch 10 18 55.5
condition 5 9 55.5
subroutine 14 14 100.0
pod 4 5 80.0
total 103 118 87.2


line stmt bran cond sub pod time code
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     2   1075 use strict;
  2         2  
  2         48  
17 2     2   6 use warnings;
  2         2  
  2         62  
18              
19             package LucyX::Index::ByteBufDocWriter;
20 2     2   6 use base qw( Lucy::Index::DataWriter );
  2         2  
  2         406  
21             our $VERSION = '0.006000_001';
22             $VERSION = eval $VERSION;
23 2     2   6 use Carp;
  2         3  
  2         81  
24 2     2   6 use Scalar::Util qw( blessed );
  2         2  
  2         65  
25 2     2   480 use bytes;
  2         8  
  2         9  
26 2     2   46 no bytes;
  2         1  
  2         5  
27              
28             # Inside-out member vars.
29             our %field;
30             our %width;
31             our %outstream;
32              
33             sub new {
34 4     4 1 426 my ( $either, %args ) = @_;
35 4         6 my $width = delete $args{width};
36 4         6 my $field = delete $args{field};
37 4         31 my $self = $either->SUPER::new(%args);
38 4 50       145 confess("Missing required param 'width'") unless defined $width;
39 4 50       6 confess("Missing required param 'field'") unless defined $field;
40 4 50       9 if ( $width < 1 ) { confess("'width' must be at least 1") }
  0         0  
41 4         12 $field{$$self} = $field;
42 4         5 $width{$$self} = $width;
43 4         10 return $self;
44             }
45              
46             sub _lazy_init {
47 4     4   5 my $self = shift;
48              
49             # Get outstream. Skip past non-doc #0.
50 4         12 my $folder = $self->get_folder;
51 4         23 my $filename = $self->get_segment->get_name . "/bytebufdocs.dat";
52 4 50       52 my $outstream = $outstream{$$self} = $folder->open_out($filename)
53             or confess Clownfish->error;
54 4         7 my $nulls = "\0" x $width{$$self};
55 4         15 $outstream->print($nulls);
56              
57 4         12 return $outstream;
58             }
59              
60             sub add_inverted_doc {
61 13     13 0 288 my ( $self, %args ) = @_;
62 13   66     39 my $outstream = $outstream{$$self} || _lazy_init($self);
63 13         43 my $fields = $args{inverter}->get_doc->get_fields;
64 13         13 my $width = $width{$$self};
65 13         14 my $field = $field{$$self};
66 13 50       23 if ( bytes::length( $fields->{$field} ) != $width ) {
67 0         0 confess("Width of '$fields->{$field}' not $width");
68             }
69 13         915 $outstream->print( $fields->{$field} );
70             }
71              
72             sub add_segment {
73 3     3 1 246 my ( $self, %args ) = @_;
74 3         4 my $seg_reader = $args{reader};
75 3         3 my $doc_map = $args{doc_map};
76 3         8 my $doc_max = $seg_reader->doc_max;
77              
78             # Bail if the supplied segment is empty. */
79 3 50       6 return unless $doc_max;
80              
81 3   66     13 my $outstream = $outstream{$$self} || _lazy_init($self);
82 3         15 my $doc_reader = $seg_reader->obtain("Lucy::Index::DocReader");
83 3 50 33     22 confess("Not a ByteBufDocReader")
84             unless ( blessed($doc_reader)
85             and $doc_reader->isa("LucyX::Index::ByteBufDocReader") );
86              
87 3         9 for ( my $i = 1; $i <= $doc_max; $i++ ) {
88 24 100       51 next unless $doc_map->get($i);
89 21         23 my $buf;
90 21         30 $doc_reader->read_record( $i, \$buf );
91 21         257 $outstream->print($buf);
92             }
93             }
94              
95             sub finish {
96 4     4 1 64 my $self = shift;
97 4         7 my $outstream = $outstream{$$self};
98 4 50       8 if ($outstream) {
99 4         11 $outstream->close;
100 4         9 my $segment = $self->get_segment;
101 4         20 $segment->store_metadata(
102             key => 'bytebufdocs',
103             metadata => $self->metadata
104             );
105             }
106             }
107              
108 4     4 1 759 sub format {1}
109              
110             sub DESTROY {
111 4     4   413 my $self = shift;
112 4         9 delete $field{$$self};
113 4         4 delete $width{$$self};
114 4         11 delete $outstream{$$self};
115 4         157 $self->SUPER::DESTROY;
116             }
117              
118             1;
119              
120             __END__