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   936 use strict;
  2         1  
  2         43  
17 2     2   6 use warnings;
  2         2  
  2         54  
18              
19             package LucyX::Index::ByteBufDocWriter;
20 2     2   5 use base qw( Lucy::Index::DataWriter );
  2         2  
  2         382  
21             our $VERSION = '0.006000_002';
22             $VERSION = eval $VERSION;
23 2     2   10 use Carp;
  2         2  
  2         83  
24 2     2   5 use Scalar::Util qw( blessed );
  2         3  
  2         62  
25 2     2   445 use bytes;
  2         8  
  2         6  
26 2     2   44 no bytes;
  2         2  
  2         4  
27              
28             # Inside-out member vars.
29             our %field;
30             our %width;
31             our %outstream;
32              
33             sub new {
34 4     4 1 420 my ( $either, %args ) = @_;
35 4         7 my $width = delete $args{width};
36 4         5 my $field = delete $args{field};
37 4         31 my $self = $either->SUPER::new(%args);
38 4 50       146 confess("Missing required param 'width'") unless defined $width;
39 4 50       5 confess("Missing required param 'field'") unless defined $field;
40 4 50       7 if ( $width < 1 ) { confess("'width' must be at least 1") }
  0         0  
41 4         12 $field{$$self} = $field;
42 4         7 $width{$$self} = $width;
43 4         8 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         11 my $folder = $self->get_folder;
51 4         24 my $filename = $self->get_segment->get_name . "/bytebufdocs.dat";
52 4 50       48 my $outstream = $outstream{$$self} = $folder->open_out($filename)
53             or confess Clownfish->error;
54 4         8 my $nulls = "\0" x $width{$$self};
55 4         12 $outstream->print($nulls);
56              
57 4         13 return $outstream;
58             }
59              
60             sub add_inverted_doc {
61 13     13 0 285 my ( $self, %args ) = @_;
62 13   66     30 my $outstream = $outstream{$$self} || _lazy_init($self);
63 13         38 my $fields = $args{inverter}->get_doc->get_fields;
64 13         13 my $width = $width{$$self};
65 13         11 my $field = $field{$$self};
66 13 50       25 if ( bytes::length( $fields->{$field} ) != $width ) {
67 0         0 confess("Width of '$fields->{$field}' not $width");
68             }
69 13         841 $outstream->print( $fields->{$field} );
70             }
71              
72             sub add_segment {
73 3     3 1 243 my ( $self, %args ) = @_;
74 3         4 my $seg_reader = $args{reader};
75 3         4 my $doc_map = $args{doc_map};
76 3         7 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     11 my $outstream = $outstream{$$self} || _lazy_init($self);
82 3         12 my $doc_reader = $seg_reader->obtain("Lucy::Index::DocReader");
83 3 50 33     24 confess("Not a ByteBufDocReader")
84             unless ( blessed($doc_reader)
85             and $doc_reader->isa("LucyX::Index::ByteBufDocReader") );
86              
87 3         8 for ( my $i = 1; $i <= $doc_max; $i++ ) {
88 24 100       54 next unless $doc_map->get($i);
89 21         12 my $buf;
90 21         30 $doc_reader->read_record( $i, \$buf );
91 21         251 $outstream->print($buf);
92             }
93             }
94              
95             sub finish {
96 4     4 1 61 my $self = shift;
97 4         5 my $outstream = $outstream{$$self};
98 4 50       9 if ($outstream) {
99 4         12 $outstream->close;
100 4         10 my $segment = $self->get_segment;
101 4         19 $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   372 my $self = shift;
112 4         8 delete $field{$$self};
113 4         4 delete $width{$$self};
114 4         11 delete $outstream{$$self};
115 4         147 $self->SUPER::DESTROY;
116             }
117              
118             1;
119              
120             __END__