Prog/Indexer.pm

filter: Statementstmt 40.2% 29 / 72 Branchbran 4.1% 1 / 24 Conditioncond 19.0% 4 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 66.6% 8 / 12 Podpod 100% 4 / 4 totaltotal 31.9% 46 / 144 CC 20 SCAR 31.5
CC
20
Cov
29%
start
42.0
_verify_swish3_config
42.0
process
30.0
CRAP 23.4
« Prog/Aggregator/Mail.pm ↑ Summary Prog/Native/Searcher.pm »
1 package SWISH::Prog::Indexer;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use base qw( SWISH::Prog::Class );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use SWISH::Prog::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 our $VERSION = '0.75';
11  
12 12 __PACKAGE__->mk_accessors(
13 qw( invindex config count clobber flush started test_mode ));
14  
15 =pod
16  
17 =head1 NAME
18  
19 SWISH::Prog::Indexer - base indexer class
20  
21 =head1 SYNOPSIS
22  
23 use SWISH::Prog::Indexer;
24 my $indexer = SWISH::Prog::Indexer->new(
25 invindex => SWISH::Prog::InvIndex->new,
26 config => SWISH::Prog::Config->new,
27 count => 0,
28 clobber => 1,
29 flush => 10000,
30 started => time()
31 );
32 $indexer->start;
33 for my $doc (@list_of_docs) {
34 $indexer->process($doc);
35 }
36 $indexer->finish;
37
38 =head1 DESCRIPTION
39  
40 SWISH::Prog::Indexer is a base class implementing the simplest of indexing
41 APIs. It is intended to be subclassed, along with InvIndex, for each
42 IR backend library.
43  
44 =head1 METHODS
45  
46 =head2 new( I<params> )
47  
48 Constructor. See the SYNOPSIS for default options.
49  
50 I<params> may include the following keys, each of which is also an
51 accessor method:
52  
53 =over
54  
55 =item clobber
56  
57 Overrite any existing InvIndex.
58  
59 =item config
60  
61 A SWISH::Prog::Config object or file name.
62  
63 =item flush
64  
65 The number of indexed docs at which in-memory changes
66 should be written to disk.
67  
68 =item invindex
69  
70 A SWISH::Prog::InvIndex object.
71  
72 =item test_mode
73  
74 Dry run mode, just prints info on stderr but does not
75 build index.
76  
77 =back
78  
79 =head2 init
80  
81 Override base method to initialize object.
82  
83 =cut
84  
85 8 ▶sub init {
Subroutine init: called CC=6 SCAR=23.5
Pod documented
86 8 my $self = shift;
87 8 $self->SUPER::init(@_);
88 8 ▶ if ( exists $self->{config}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"config"} and defined $self->{"config"} and not builtin::blessed($self->{"config"}) and not $self->{"config"} =~ /<swish>|\.xml$/) 0 8 8
Condition33%3/9
exists $self->{"config"} and defined $self->{"config"} and not builtin::blessed($self->{"config"}) and not $self->{"config"} =~ /<swish>|\.xml$/
!ll&&!rl&&r
800
exists $self->{"config"} and defined $self->{"config"} and not builtin::blessed($self->{"config"})
!ll&&!rl&&r
800
exists $self->{"config"} and defined $self->{"config"}
!ll&&!rl&&r
800
MC/DC0%0/4
exists $self->{"config"} and defined $self->{"config"} and not builtin::blessed($self->{"config"}) and not $self->{"config"} =~ /<swish>|\.xml$/
exists $self->{"config"} defined $self->{"config"} not builtin::blessed($self->{"config"}) not $self->{"config"} =~ /<swish>|\.xml$/
Truth table20%1/5
A and B and not(C) and not(D)
A= exists $self->{"config"}B= defined $self->{"config"}C= not builtin::blessed($self->{"config"})D= not $self->{"config"} =~ /<swish>|\.xml$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
89 and defined $self->{config}
90 and !blessed( $self->{config} )
91 and $self->{config} !~ m/<swish>|\.xml$/ )
92 {
93 $self->{config}
94 0 = $self->verify_isa_swish_prog_config( $self->{config} );
95 }
96 8 ▶ $self->{config} ||= SWISH::Prog::Config->new;
Condition33%1/3
$self->{"config"} ||= "SWISH::Prog::Config"->new
l!l&&r!l&&!r
080
MC/DC0%0/2
$self->{"config"} ||= "SWISH::Prog::Config"->new
$self->{"config"} "SWISH::Prog::Config"->new
Truth table33%1/3
A ||= B
A= $self->{"config"}B= "SWISH::Prog::Config"->new
ABresult
1X1
011
000
97 8 return $self;
98 }
99  
100 =head2 start
101  
102 Opens the invindex() object and sets the started() time to time().
103  
104 Subclasses should always call SUPER::start() if they override
105 this method since it provides sanity checking on the InvIndex.
106  
107 =cut
108  
109 0 ▶sub start {
Subroutine start: not called CC=6 SCAR=37.4
Pod documented
110 0 my $self = shift;
111 0 ▶ if ( !defined $self->invindex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->invindex) 0 0 0
112 0 croak "Missing invindex object";
113 }
114 0 my $invindex = $self->invindex;
115 0 ▶ if ( !blessed($invindex)
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($invindex) and $invindex->can("open")) 0 0 0
Condition0%0/3
builtin::blessed($invindex) and $invindex->can("open")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($invindex) and $invindex->can("open")
builtin::blessed($invindex) $invindex->can("open")
Truth table0%0/3
A and B
A= builtin::blessed($invindex)B= $invindex->can("open")
ABresult
0X0
100
111
116 or !$invindex->can('open') )
117 {
118 0 croak "Invalid invindex $invindex: "
119 . "either not blessed object or does not implement 'open' method";
120 }
121  
122 # sanity check. if this is an existing index
123 # does our Format match what already exists?
124 0 my $meta;
125 0 eval { $meta = $invindex->meta; };
126 0 ▶ if ( !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
127 0 my $format = $meta->Index->{Format};
128 0 ▶ if ( !$self->isa( 'SWISH::Prog::' . $format . '::Indexer' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->isa('SWISH::Prog::' . $format . '::Indexer')) 0 0 0
129 0 croak "Fatal error: found existing invindex '$invindex' "
130 . "with format $format.\n"
131 . "You tried to open it with "
132 . ref($self);
133 }
134  
135 }
136 0 $self->invindex->open;
137 0 $self->{started} = time();
138 0 $self->invindex->path->file('swish_last_start')->touch();
139 }
140  
141 =head2 process( I<doc> )
142  
143 I<doc> should be a SWISH::Prog::Doc-derived object.
144  
145 process() should implement whatever the particular IR library
146 API requires.
147  
148 =cut
149  
150 0 ▶sub process {
Subroutine process: not called CC=5 SCAR=34.0
Pod documented
151 0 my $self = shift;
152 0 my $doc = shift;
153 0 ▶ unless ( $doc && blessed($doc) && $doc->isa('SWISH::Prog::Doc') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc and builtin::blessed($doc) and $doc->isa("SWISH::Prog::Doc")) 0 0 0
Condition0%0/6
$doc and builtin::blessed($doc) and $doc->isa("SWISH::Prog::Doc")
!ll&&!rl&&r
000
$doc and builtin::blessed($doc)
!ll&&!rl&&r
000
MC/DC0%0/3
$doc and builtin::blessed($doc) and $doc->isa("SWISH::Prog::Doc")
$doc builtin::blessed($doc) $doc->isa("SWISH::Prog::Doc")
Truth table0%0/4
A and B and C
A= $docB= builtin::blessed($doc)C= $doc->isa("SWISH::Prog::Doc")
ABCresult
0XX0
10X0
1100
1111
154 0 croak "SWISH::Prog::Doc object required";
155 }
156  
157 0 ▶ $self->start unless $self->started;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->started 0 0 0
158  
159 0 $self->{count}++;
160  
161 0 return $doc;
162 }
163  
164 =head2 finish
165  
166 Closes the invindex().
167  
168 =cut
169  
170 0 ▶sub finish {
Subroutine finish: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172 0 $self->invindex->close;
173 }
174  
175 =head2 count
176  
177 Returns the number of documents processed.
178  
179 =head2 started
180  
181 The time at which the Indexer object was created. Returns a Unix epoch
182 integer.
183  
184 =cut
185  
186 # NOTE in _verify_swish3_config() below,
187 # if config is already in swish3 format, must
188 # override param value with SWISH::Prog::Config object
189 # after adding to SWISH::3::Config object so that the
190 # aggregator using this Indexer is happy.
191  
192 0 ▶sub _verify_swish3_config {
Subroutine _verify_swish3_config: not called CC=6 SCAR=37.4
193 0 my $self = shift;
194  
195 0 ▶ if ( !exists $self->{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"config"}) 0 0 0
196 0 return;
197 }
198  
199 #carp dump $self->{config};
200  
201 # isa object
202 0 ▶ if ( blessed( $self->{config} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($self->{'config'})) { } 0 0 0
elsif ($self->{'config'} =~ /<swish>|[\n\r]/) { } 0 0 0
elsif (-r $self->{'config'}) { } 0 0 0
203 $self->{config}
204 0 = $self->verify_isa_swish_prog_config( $self->{config} );
205 0 my $swish_3_config = $self->{config}->ver2_to_ver3();
206 0 $self->{s3}->config->add($swish_3_config);
207 }
208  
209 # xml string
210 elsif ( $self->{config} =~ m/<swish>|[\n\r]/ ) {
211 0 $self->{s3}->config->add( $self->{config} );
212 0 $self->{config} = SWISH::Prog::Config->new();
213 }
214  
215 # file
216 elsif ( -r $self->{config} ) {
217  
218 # swish3 format
219 0 ▶ if ( $self->{config} =~ m/\.xml/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'config'} =~ /\.xml/) { } 0 0 0
220 0 $self->{s3}->config->add( $self->{config} );
221 0 $self->{config} = SWISH::Prog::Config->new();
222 }
223  
224 # swish2 format
225 else {
226 $self->{config}
227 0 = $self->verify_isa_swish_prog_config( $self->{config} );
228 0 my $swish_3_config = $self->{config}->ver2_to_ver3();
229 0 $self->{s3}->config->add($swish_3_config);
230 }
231  
232 }
233  
234 # no support
235 else {
236 0 croak
237 "Unsupported config format (not a XML string, filename or SWISH::Prog::Config object): $self->{config}";
238 }
239  
240 0 return $self->{config};
241 }
242 12 1;
243  
244 __END__
« Prog/Aggregator/Mail.pm ↑ Summary Prog/Native/Searcher.pm »