Solr.pm

filter: Statementstmt 46.1% 143 / 310 Branchbran 31.7% 68 / 214 Conditioncond 41.1% 35 / 85 MC/DCmcdc 17.3% 9 / 52 Subroutinesub 52.0% 26 / 50 Podpod 92.8% 26 / 28 totaltotal 41.5% 307 / 739 CC 140 SCAR 40.3
CC
140
Cov
40%
extractDocument
240.0
delete
156.0
request
132.0
CRAP 56.4
« Solr/JSON.pm ↑ Summary Solr/XML.pm »
1 # This code is part of Perl distribution Apache-Solr version 1.12.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2012-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Apache::Solr;{
13 6 our $VERSION = '1.12';
14 }
15  
16  
17 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 ▶use Apache::Solr::Tables;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use Log::Report qw/solr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 6 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use Encode qw/encode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use MIME::Types ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 6 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 LATEST_SOLR_VERSION => '9.8', # newest support by this module
33 ENETDOWN => 100, # error codes may not be available on all platforms
34 ENETUNREACH => 101, # so cannot use Errno.
35 6 };
36  
37 # overrule this when your host has a different unique field
38 6 our $uniqueKey = 'id';
39 6 my $mimetypes = MIME::Types->new;
40 6 my $http_agent;
41  
42 sub _to_bool($)
43 22 ▶{ my $b = shift;
Subroutine _to_bool: called CC=5 SCAR=16.8
44 22 ▶ !defined $b ? undef : ($b && $b ne 'false' && $b ne 'off') ? 'true' : 'false';
Branch: 2 branches
BranchTrueFalseTotal
!defined($b) ? : 0 22 22
$b && $b ne 'false' && $b ne 'off' ? : 20 2 22
Condition66%4/6
$b and $b ne "false" and $b ne "off"
!ll&&!rl&&r
2020
$b and $b ne "false"
!ll&&!rl&&r
2020
MC/DC33%1/3
$b and $b ne "false" and $b ne "off"
$b $b ne "false" $b ne "off"
Truth table50%2/4
A and B and C
A= $bB= $b ne "false"C= $b ne "off"
ABCresult
0XX0
10X0
1100
1111
45 }
46  
47 #--------------------
48  
49 sub new(@)
50 4 ▶{ my ($class, %args) = @_;
Subroutine new: called CC=6 SCAR=19.0
Pod documented
51 4 ▶ if($class eq __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 2 2 4
52 2 ▶ { my $format = delete $args{format} || 'XML';
Condition50%1/2
delete $args{'format'} || 'XML'
l!l
02
MC/DC0%0/1
delete $args{'format'} || 'XML'
delete $args{'format'}
Truth table50%1/2
A
A= delete $args{'format'}
Aresult
11
00
53 2 ▶ $format eq 'XML' || $format eq 'JSON'
Branch: 1 branch
BranchTrueFalseTotal
$format eq "XML" or $format eq "JSON" or &panic("unknown communication format '${format}' for solr") 2 0 2
Condition33%1/3
$format eq "XML" or $format eq "JSON"
l!l&&r!l&&!r
200
MC/DC0%0/2
$format eq "XML" or $format eq "JSON"
$format eq "XML" $format eq "JSON"
Truth table33%1/3
A or B
A= $format eq "XML"B= $format eq "JSON"
ABresult
1X1
011
000
54 or panic "unknown communication format '$format' for solr";
55 2 $class .= '::' . $format;
56 2 ▶ eval "require $class"; panic $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
57 }
58 4 (bless {}, $class)->init(\%args)
59 }
60  
61 sub init($)
62 4 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=8 SCAR=23.4
Pod undocumented
63 4 $self->server($args->{server});
64 4 $self->{AS_core} = $args->{core};
65 4 ▶ $self->{AS_commit} = exists $args->{autocommit} ? $args->{autocommit} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'autocommit'} ? : 0 4 4
66 4 ▶ $self->{AS_sversion} = $args->{server_version} || LATEST_SOLR_VERSION;
Condition50%1/2
$args->{'server_version'} || '9.8'
l!l
04
MC/DC0%0/1
$args->{'server_version'} || '9.8'
$args->{'server_version'}
Truth table50%1/2
A
A= $args->{'server_version'}
Aresult
11
00
67 4 ▶ $self->{AS_retry_wait} = $args->{retry_wait} // 5; # seconds
Condition50%1/2
$args->{"retry_wait"} // 5
l!l
04
MC/DC0%0/1
$args->{"retry_wait"} // 5
$args->{"retry_wait"}
Truth table50%1/2
A
A= $args->{"retry_wait"}
Aresult
11
00
68 4 ▶ $self->{AS_retry_max} = $args->{retry_max} // 60;
Condition50%1/2
$args->{"retry_max"} // 60
l!l
04
MC/DC0%0/1
$args->{"retry_max"} // 60
$args->{"retry_max"}
Truth table50%1/2
A
A= $args->{"retry_max"}
Aresult
11
00
69 4 ▶ $self->{AS_underscore} = exists $args->{field_key_simplify} ? $args->{field_key_simplify} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'field_key_simplify'} ? : 0 4 4
70  
71 $http_agent = $self->{AS_agent} =
72 4 ▶ $args->{agent} || $http_agent || LWP::UserAgent->new(keep_alive=>1);
Condition33%2/6
$args->{'agent'} || $http_agent || 'LWP::UserAgent'->new('keep_alive', 1)
l!l&&r!l&&!r
040
$args->{"agent"} or $http_agent
l!l&&r!l&&!r
004
MC/DC0%0/3
$args->{'agent'} || $http_agent || 'LWP::UserAgent'->new('keep_alive', 1)
$args->{"agent"} $http_agent 'LWP::UserAgent'->new('keep_alive', 1)
Truth table25%1/4
A or B || C
A= $args->{"agent"}B= $http_agentC= 'LWP::UserAgent'->new('keep_alive', 1)
ABCresult
1XX1
01X1
0011
0000
73  
74 4 weaken $http_agent;
75 4 $self;
76 }
77  
78 #--------------------
79  
80 1 ▶sub core(;$) { my $s = shift; @_ ? $s->{AS_core} = shift : $s->{AS_core} }
Subroutine core: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
81 0 ▶sub autocommit(;$) { my $s = shift; @_ ? $s->{AS_commit} = shift : $s->{AS_commit} }
Subroutine autocommit: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
82 0 ▶sub agent() { $_[0]->{AS_agent} }
Subroutine agent: not called CC=1 SCAR=6.9
Pod documented
83 26 ▶sub serverVersion() { $_[0]->{AS_sversion} }
Subroutine serverVersion: called CC=1 SCAR=0.0
Pod documented
84  
85  
86 sub server(;$)
87 5 ▶{ my ($self, $uri) = @_;
Subroutine server: called CC=4 SCAR=14.6
Pod documented
88 5 ▶ $uri or return $self->{AS_server};
Branch: 1 branch
BranchTrueFalseTotal
$uri or return $self->{"AS_server"} 4 1 5
89 4 ▶ $uri = URI->new($uri)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($uri) and $uri->isa("URI") 4 0 4
Condition33%1/3
builtin::blessed($uri) and $uri->isa("URI")
!ll&&!rl&&r
400
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("URI")
builtin::blessed($uri) $uri->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("URI")
ABresult
0X0
100
111
90 unless blessed $uri && $uri->isa('URI');
91 4 $self->{AS_server} = $uri;
92 }
93  
94  
95 127 ▶sub fieldKeySimplify() { $_[0]->{AS_underscore} }
Subroutine fieldKeySimplify: called CC=1 SCAR=0.0
Pod documented
96  
97 #--------------------
98  
99 sub select(@)
100 0 ▶{ my $self = shift;
Subroutine select: not called CC=3 SCAR=24.8
Pod documented
101 0 ▶ my $args = @_ && ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
102 0 $self->_select($args, scalar $self->expandSelect(@_));
103 }
104 0 ▶sub _select($$) {panic "not extended"}
Subroutine _select: not called CC=1 SCAR=6.9
105  
106  
107 sub queryTerms(@)
108 0 ▶{ my $self = shift;
Subroutine queryTerms: not called CC=1 SCAR=6.9
Pod documented
109 0 $self->_terms(scalar $self->expandTerms(@_));
110 }
111 0 ▶sub _terms(@) {panic "not implemented"}
Subroutine _terms: not called CC=1 SCAR=6.9
112  
113 #--------------------
114  
115 sub addDocument($%)
116 0 ▶{ my ($self, $docs, %args) = @_;
Subroutine addDocument: not called CC=10 SCAR=47.0
Pod documented
117 0 ▶ $docs = [ $docs ] if ref $docs ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $docs ne "ARRAY" 0 0 0
118  
119 0 my $sv = $self->serverVersion;
120  
121 0 my (%attrs, %params);
122 0 ▶ $params{commit} = _to_bool(exists $args{commit} ? $args{commit} : $self->autocommit);
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'commit'} ? : 0 0 0
123  
124 0 ▶ if(my $cw = $args{commitWithin})
Branch: 1 branch
BranchTrueFalseTotal
if (my $cw = $args{"commitWithin"}) 0 0 0
125 0 ▶ { if($sv lt '3.4') { $attrs{commit} = 'true' }
Branch: 1 branch
BranchTrueFalseTotal
if ($sv lt '3.4') { } 0 0 0
126 0 else { $attrs{commitWithin} = int($cw * 1000) }
127 }
128  
129 $attrs{overwrite} = _to_bool delete $args{overwrite}
130 0 ▶ if exists $args{overwrite};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"overwrite"} 0 0 0
131  
132 0 foreach my $depr (qw/allowDups overwritePending overwriteCommitted/)
133 0 ▶ { if(exists $args{$depr})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$depr}) 0 0 0
134 0 ▶ { if($sv ge '4.0') { $self->removed("add($depr)"); delete $args{$depr} }
Branch: 2 branches
BranchTrueFalseTotal
if ($sv ge '4.0') { } 0 0 0
elsif ($sv ge '1.0') { } 0 0 0
135 0 elsif($sv ge '1.0') { $self->deprecated("add($depr)") }
136 0 else { $attrs{$depr} = _to_bool delete $args{$depr} }
137 }
138 }
139  
140 0 $self->_add($docs, \%attrs, \%params);
141 }
142  
143  
144 sub commit(%)
145 0 ▶{ my ($self, %args) = @_;
Subroutine commit: not called CC=9 SCAR=45.0
Pod documented
146 0 my $sv = $self->serverVersion;
147  
148 0 my %attrs;
149 0 ▶ if(exists $args{waitFlush})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"waitFlush"}) 0 0 0
150 0 ▶ { if($sv ge '4.0')
Branch: 2 branches
BranchTrueFalseTotal
if ($sv ge '4.0') { } 0 0 0
elsif ($sv ge '1.4') { } 0 0 0
151 0 { $self->removed("commit(waitFlush)"); delete $args{waitFlush} }
152 0 elsif($sv ge '1.4') { $self->deprecated("commit(waitFlush)") }
153 0 else { $attrs{waitFlush} = _to_bool delete $args{waitFlush} }
154 }
155  
156 $attrs{waitSearcher} = _to_bool delete $args{waitSearcher}
157 0 ▶ if exists $args{waitSearcher};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"waitSearcher"} 0 0 0
158  
159 0 ▶ if(exists $args{softCommit})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"softCommit"}) 0 0 0
160 0 ▶ { if($sv lt '4.0') { $self->ignored("commit(softCommit)") }
Branch: 1 branch
BranchTrueFalseTotal
if ($sv lt '4.0') { } 0 0 0
161 0 else { $attrs{softCommit} = _to_bool delete $args{softCommit} }
162 }
163  
164 0 ▶ if(exists $args{expungeDeletes})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"expungeDeletes"}) 0 0 0
165 0 ▶ { if($sv lt '1.4') { $self->ignored("commit(expungeDeletes)") }
Branch: 1 branch
BranchTrueFalseTotal
if ($sv lt '1.4') { } 0 0 0
166 0 else { $attrs{expungeDeletes} = _to_bool delete $args{expungeDeletes} }
167 }
168  
169 0 $self->_commit(\%attrs);
170 }
171 0 ▶sub _commit($) {panic "not implemented"}
Subroutine _commit: not called CC=1 SCAR=6.9
172  
173  
174 sub optimize(%)
175 0 ▶{ my ($self, %args) = @_;
Subroutine optimize: not called CC=9 SCAR=45.0
Pod documented
176 0 my $sv = $self->serverVersion;
177  
178 0 my %attrs;
179 0 ▶ if(exists $args{waitFlush})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"waitFlush"}) 0 0 0
180 0 ▶ { if($sv ge '4.0') { $self->removed("commit(waitFlush)"); delete $args{waitFlush} }
Branch: 2 branches
BranchTrueFalseTotal
if ($sv ge '4.0') { } 0 0 0
elsif ($sv ge '1.4') { } 0 0 0
181 0 elsif($sv ge '1.4') { $self->deprecated("optimize(waitFlush)") }
182 0 else { $attrs{waitFlush} = _to_bool delete $args{waitFlush} }
183 }
184  
185 $attrs{waitSearcher} = _to_bool delete $args{waitSearcher}
186 0 ▶ if exists $args{waitSearcher};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"waitSearcher"} 0 0 0
187  
188 0 ▶ if(exists $args{softCommit})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"softCommit"}) 0 0 0
189 0 ▶ { if($sv lt '4.0') { $self->ignored("optimize(softCommit)") }
Branch: 1 branch
BranchTrueFalseTotal
if ($sv lt '4.0') { } 0 0 0
190 0 else { $attrs{softCommit} = _to_bool delete $args{softCommit} }
191 }
192  
193 0 ▶ if(exists $args{maxSegments})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"maxSegments"}) 0 0 0
194 0 ▶ { if($sv lt '1.3') { $self->ignored("optimize(maxSegments)") }
Branch: 1 branch
BranchTrueFalseTotal
if ($sv lt '1.3') { } 0 0 0
195 0 else { $attrs{maxSegments} = delete $args{maxSegments} }
196 }
197  
198 0 $self->_optimize(\%attrs);
199 }
200 0 ▶sub _optimize($) {panic "not implemented"}
Subroutine _optimize: not called CC=1 SCAR=6.9
201  
202  
203 sub delete(%)
204 0 ▶{ my ($self, %args) = @_;
Subroutine delete: not called CC=12 SCAR=50.5
Pod documented
205  
206 0 my %attrs;
207 0 ▶ $attrs{commit} = _to_bool(exists $args{commit} ? $args{commit} : $self->autocommit);
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'commit'} ? : 0 0 0
208  
209 0 ▶ if(exists $args{fromPending})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"fromPending"}) 0 0 0
210 0 { $self->deprecated("delete(fromPending)");
211 0 $attrs{fromPending} = _to_bool delete $args{fromPending};
212 }
213 0 ▶ if(exists $args{fromCommitted})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"fromCommitted"}) 0 0 0
214 0 { $self->deprecated("delete(fromCommitted)");
215 0 $attrs{fromCommitted} = _to_bool delete $args{fromCommitted};
216 }
217  
218 0 my @which;
219 0 ▶ if(my $id = $args{id})
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $args{"id"}) 0 0 0
220 0 ▶ { push @which, map +(id => $_), ref $id eq 'ARRAY' ? @$id : $id;
Branch: 1 branch
BranchTrueFalseTotal
ref $id eq 'ARRAY' ? : 0 0 0
221 }
222 0 ▶ if(my $q = $args{query})
Branch: 1 branch
BranchTrueFalseTotal
if (my $q = $args{"query"}) 0 0 0
223 0 ▶ { push @which, map +(query => $_), ref $q eq 'ARRAY' ? @$q : $q;
Branch: 1 branch
BranchTrueFalseTotal
ref $q eq 'ARRAY' ? : 0 0 0
224 }
225 0 ▶ @which or return;
Branch: 1 branch
BranchTrueFalseTotal
@which or return 0 0 0
226  
227 # JSON calls do not accept multiple ids at once (it seems in 4.0)
228 0 my $result;
229 0 ▶ if($self->serverVersion ge '1.4' && !$self->isa('Apache::Solr::JSON'))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->serverVersion ge '1.4' and not $self->isa('Apache::Solr::JSON')) { } 0 0 0
Condition0%0/3
$self->serverVersion ge "1.4" and not $self->isa("Apache::Solr::JSON")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->serverVersion ge "1.4" and not $self->isa("Apache::Solr::JSON")
$self->serverVersion ge "1.4" not $self->isa("Apache::Solr::JSON")
Truth table0%0/3
A and not(B)
A= $self->serverVersion ge "1.4"B= not $self->isa("Apache::Solr::JSON")
ABresult
0X0
100
111
230 0 { $result = $self->_delete(\%attrs, \@which);
231 }
232 else
233 { # old servers accept only one id or query per delete
234 0 ▶ $result = $self->_delete(\%attrs, [splice @which, 0, 2]) while @which;
Branch: 1 branch
BranchTrueFalseTotal
@which and do { $result = $self->_delete(\%attrs, [splice(@which, 0, 2)]) } 0 0 0
235 }
236 0 $result;
237 }
238 0 ▶sub _delete(@) {panic "not implemented"}
Subroutine _delete: not called CC=1 SCAR=6.9
239  
240  
241 sub rollback()
242 0 ▶{ my $self = shift;
Subroutine rollback: not called CC=2 SCAR=17.9
Pod documented
243 0 ▶ $self->serverVersion ge '1.4'
Branch: 1 branch
BranchTrueFalseTotal
$self->serverVersion ge "1.4" or &error(&__x("rollback not supported by your solr server version.")) 0 0 0
244 or error __x"rollback not supported by your solr server version.";
245  
246 0 $self->_rollback;
247 }
248  
249  
250 sub extractDocument(@)
251 0 ▶{ my $self = shift;
Subroutine extractDocument: not called CC=15 SCAR=54.8
Pod documented
252  
253 0 ▶ $self->serverVersion ge '1.4'
Branch: 1 branch
BranchTrueFalseTotal
$self->serverVersion ge "1.4" or &error(&__x("extractDocument() requires Solr v1.4 or higher.")) 0 0 0
254 or error __x"extractDocument() requires Solr v1.4 or higher.";
255  
256 0 my %p = $self->expandExtract(@_);
257 0 my $data;
258  
259 # expand* changes '_' into '.'
260 0 my $ct = delete $p{'content.type'};
261 0 my $fn = delete $p{file};
262 0 ▶ $p{'resource.name'} ||= $fn if $fn && !ref $fn;
Branch: 1 branch
BranchTrueFalseTotal
if $fn and not ref $fn 0 0 0
Condition0%0/6
$fn and not ref $fn
!ll&&!rl&&r
000
$p{"resource.name"} ||= $fn
l!l&&r!l&&!r
000
MC/DC0%0/2
$fn and not ref $fn
$fn not ref $fn
MC/DC0%0/2
$p{"resource.name"} ||= $fn
$p{"resource.name"} $fn
Truth table0%0/3
A and not(B)
A= $fnB= not ref $fn
ABresult
0X0
100
111
Truth table0%0/3
A ||= B
A= $p{"resource.name"}B= $fn
ABresult
1X1
011
000
263  
264 $p{commit} = _to_bool $self->autocommit
265 0 ▶ unless exists $p{commit};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"commit"} 0 0 0
266  
267 0 ▶ if(defined $p{string})
Branch: 2 branches
BranchTrueFalseTotal
if (defined $p{'string'}) { } 0 0 0
elsif ($fn) { } 0 0 0
268 { # try to avoid copying the data, which can be huge
269 $data = $ct =~ m!^text/!i
270 0 ▶ ? \encode(utf8 => (ref $p{string} eq 'SCALAR' ? ${$p{string}} : $p{string}))
Branch: 1 branch
BranchTrueFalseTotal
ref $p{'string'} eq 'SCALAR' ? : 0 0 0
271 0 ▶ : (ref $p{string} eq 'SCALAR' ? $p{string} : \$p{string} );
Branch: 2 branches
BranchTrueFalseTotal
$ct =~ m[^text/]i ? : 0 0 0
ref $p{'string'} eq 'SCALAR' ? : 0 0 0
272  
273 0 delete $p{string};
274 }
275 elsif($fn)
276 0 { local $/;
277 0 ▶ if(ref $fn eq 'GLOB') { $data = \<$fn> }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fn eq 'GLOB') { } 0 0 0
278 else
279 0 { local *IN;
280 0 ▶ open IN, '<:raw', $fn or fault __x"Cannot read document from {fn}", fn => $fn;
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<:raw", $fn or &fault(&__x("Cannot read document from {fn}", "fn", $fn)) 0 0 0
281 0 $data = \<IN>;
282 0 ▶ close IN or fault __x"Read error for document {fn}", fn => $fn;
Branch: 1 branch
BranchTrueFalseTotal
close IN or &fault(&__x("Read error for document {fn}", "fn", $fn)) 0 0 0
283 0 ▶ $ct ||= $mimetypes->mimeTypeOf($fn);
Condition0%0/3
$ct ||= $mimetypes->mimeTypeOf($fn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$ct ||= $mimetypes->mimeTypeOf($fn)
$ct $mimetypes->mimeTypeOf($fn)
Truth table0%0/3
A ||= B
A= $ctB= $mimetypes->mimeTypeOf($fn)
ABresult
1X1
011
000
284 }
285 }
286 else
287 0 { error __x"extract requires document as file or string.";
288 }
289  
290 0 $self->_extract([%p], $data, $ct);
291 }
292  
293 0 ▶sub _extract($) { panic "not implemented" }
Subroutine _extract: not called CC=1 SCAR=6.9
294  
295 #--------------------
296  
297 sub _core_admin($@)
298 0 ▶{ my ($self, $action, $params) = @_;
Subroutine _core_admin: not called CC=2 SCAR=17.9
299 0 ▶ $params->{core} ||= $self->core;
Condition0%0/3
$params->{"core"} ||= $self->core
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{"core"} ||= $self->core
$params->{"core"} $self->core
Truth table0%0/3
A ||= B
A= $params->{"core"}B= $self->core
ABresult
1X1
011
000
300  
301 0 my $endpoint = $self->endpoint('cores', core => 'admin', params => $params);
302 0 my @params = %$params;
303 0 my $result = Apache::Solr::Result->new(params => [ %$params ], endpoint => $endpoint, core => $self);
304  
305 0 $self->request($endpoint, $result);
306 0 $result;
307 }
308  
309  
310 sub coreStatus(%)
311 0 ▶{ my ($self, %args) = @_;
Subroutine coreStatus: not called CC=1 SCAR=6.9
Pod documented
312 0 $self->_core_admin('STATUS', \%args);
313 }
314  
315  
316 sub coreReload(%)
317 0 ▶{ my ($self, %args) = @_;
Subroutine coreReload: not called CC=1 SCAR=6.9
Pod documented
318 0 $self->_core_admin('RELOAD', \%args);
319 }
320  
321  
322 sub coreUnload($%)
323 0 ▶{ my ($self, %args) = @_;
Subroutine coreUnload: not called CC=1 SCAR=6.9
Pod documented
324 0 $self->_core_admin('UNLOAD', \%args);
325 }
326  
327 #--------------------
328  
329 sub _calling_sub()
330 1 ▶{ for(my $i=0;$i <10; $i++)
Subroutine _calling_sub: called CC=4 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 4 0 4
331 4 { my $sub = (caller $i)[3];
332 4 ▶ return $sub if !$sub || index($sub, 'Apache::Solr::') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $sub or index($sub, "Apache::Solr::") == -1 1 3 4
Condition66%2/3
not $sub or index($sub, "Apache::Solr::") == -1
l!l&&r!l&&!r
013
MC/DC50%1/2
not $sub or index($sub, "Apache::Solr::") == -1
not $sub index($sub, "Apache::Solr::") == -1
Truth table66%2/3
not(A) or B
A= not $subB= index($sub, "Apache::Solr::") == -1
ABresult
1X1
011
000
333 }
334 }
335  
336 sub _simpleExpand($$$)
337 26 ▶{ my ($self, $p, $prefix) = @_;
Subroutine _simpleExpand: called CC=14 SCAR=26.9
338 26 ▶ my @p = ref $p eq 'HASH' ? %$p : @$p;
Branch: 1 branch
BranchTrueFalseTotal
ref $p eq 'HASH' ? : 15 11 26
339 26 my $sv = $self->serverVersion;
340  
341 26 my @t;
342 26 ▶ while(@p)
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 90 26 116
343 90 { my ($k, $v) = (shift @p, shift @p);
344 90 ▶ $k =~ s/_/./g if $self->fieldKeySimplify;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fieldKeySimplify 90 0 90
345 90 ▶ $k = $prefix.$k if defined $prefix && index($k, $prefix)!=0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix and index($k, $prefix) != 0 43 47 90
Condition100%3/3
defined $prefix and index($k, $prefix) != 0
!ll&&!rl&&r
45243
MC/DC100%2/2
defined $prefix and index($k, $prefix) != 0
defined $prefix index($k, $prefix) != 0
Truth table100%3/3
A and B
A= defined $prefixB= index($k, $prefix) != 0
ABresult
0X0
100
111
346 90 ▶ my $param = $k =~ m/^f\.[^\.]+\.(.*)/ ? $1 : $k;
Branch: 1 branch
BranchTrueFalseTotal
$k =~ /^f\.[^\.]+\.(.*)/ ? : 2 88 90
347  
348 90 my ($dv, $iv);
349 90 ▶ if(($dv = $deprecated{$param}) && $sv ge $dv)
Branch: 2 branches
BranchTrueFalseTotal
if ($dv = $Apache::Solr::deprecated{$param} and $sv ge $dv) { } 1 89 90
elsif ($iv = $Apache::Solr::introduced{$param} and $iv gt $sv) { } 0 169 169
Condition66%2/3
$dv = $Apache::Solr::deprecated{$param} and $sv ge $dv
!ll&&!rl&&r
8901
MC/DC50%1/2
$dv = $Apache::Solr::deprecated{$param} and $sv ge $dv
$dv = $Apache::Solr::deprecated{$param} $sv ge $dv
Truth table66%2/3
A and B
A= $dv = $Apache::Solr::deprecated{$param}B= $sv ge $dv
ABresult
0X0
100
111
350 1 { my $command = _calling_sub;
351 1 $self->deprecated("$command($param) since $dv");
352 }
353 ▶ elsif(($iv = $introduced{$param}) && $iv gt $sv)
Condition66%2/3
$iv = $Apache::Solr::introduced{$param} and $iv gt $sv
!ll&&!rl&&r
8090
MC/DC0%0/2
$iv = $Apache::Solr::introduced{$param} and $iv gt $sv
$iv = $Apache::Solr::introduced{$param} $iv gt $sv
Truth table66%2/3
A and B
A= $iv = $Apache::Solr::introduced{$param}B= $iv gt $sv
ABresult
0X0
100
111
354 0 { my $command = _calling_sub;
355 0 $self->ignored("$command($param) introduced in $iv");
356 0 next;
357 }
358  
359 push @t, $k => $boolparams{$param} ? _to_bool($_) : $_
360 90 ▶ for ref $v eq 'ARRAY' ? @$v : $v;
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 7 83 90
$Apache::Solr::boolparams{$param} ? : 22 75 97
361 }
362 26 @t;
363 }
364  
365  
366 sub expandTerms(@)
367 1 ▶{ my $self = shift;
Subroutine expandTerms: called CC=3 SCAR=11.4
Pod documented
368 1 ▶ my $p = @_==1 ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
369 1 my @t = $self->_simpleExpand($p, 'terms.');
370 1 ▶ wantarray ? @t : \@t;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
371 }
372  
373  
374 sub _expand_flatten($$)
375 4 ▶{ my ($self, $v, $prefix) = @_;
Subroutine _expand_flatten: called CC=3 SCAR=11.0
376 4 ▶ my @l = ref $v eq 'HASH' ? %$v : @$v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 4 0 4
377 4 my @s;
378 4 ▶ push @s, $prefix.(shift @l) => (shift @l) while @l;
Branch: 1 branch
BranchTrueFalseTotal
@l and do { push @s, $prefix . shift(@l), shift @l } 4 4 8
379 4 @s;
380 }
381  
382 sub expandExtract(@)
383 1 ▶{ my $self = shift;
Subroutine expandExtract: called CC=12 SCAR=26.7
Pod documented
384 0 ▶ my @p = @_==1 ? @{(shift)} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 1 1
385 1 my @s;
386 1 ▶ while(@p)
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 10 1 11
387 10 { my ($k, $v) = (shift @p, shift @p);
388 10 ▶ if(!ref $v || ref $v eq 'SCALAR')
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $v or ref $v eq 'SCALAR') { } 6 4 10
elsif ($k eq 'literal' or $k eq 'literals') { } 2 2 4
elsif ($k eq 'fmap' or $k eq 'boost' or $k eq 'resource') { } 2 0 2
Condition66%2/3
not ref $v or ref $v eq "SCALAR"
l!l&&r!l&&!r
604
MC/DC50%1/2
not ref $v or ref $v eq "SCALAR"
not ref $v ref $v eq "SCALAR"
Truth table66%2/3
not(A) or B
A= not ref $vB= ref $v eq "SCALAR"
ABresult
1X1
011
000
389 6 { push @s, $k => $v }
390 ▶ elsif($k eq 'literal' || $k eq 'literals')
Condition100%3/3
$k eq "literal" or $k eq "literals"
l!l&&r!l&&!r
112
MC/DC100%2/2
$k eq "literal" or $k eq "literals"
$k eq "literal" $k eq "literals"
Truth table100%3/3
A or B
A= $k eq "literal"B= $k eq "literals"
ABresult
1X1
011
000
391 2 { push @s, $self->_expand_flatten($v, 'literal.') }
392 ▶ elsif($k eq 'fmap' || $k eq 'boost' || $k eq 'resource')
Condition50%3/6
$k eq "fmap" or $k eq "boost" or $k eq "resource"
l!l&&r!l&&!r
200
$k eq "fmap" or $k eq "boost"
l!l&&r!l&&!r
110
MC/DC0%0/3
$k eq "fmap" or $k eq "boost" or $k eq "resource"
$k eq "fmap" $k eq "boost" $k eq "resource"
Truth table50%2/4
A or B or C
A= $k eq "fmap"B= $k eq "boost"C= $k eq "resource"
ABCresult
1XX1
01X1
0011
0000
393 2 { push @s, $self->_expand_flatten($v, "$k.") }
394 0 else { panic "unknown set '$k'" }
395 }
396  
397 1 ▶ my @t = @s ? $self->_simpleExpand(\@s) : ();
Branch: 1 branch
BranchTrueFalseTotal
@s ? : 1 0 1
398 1 ▶ wantarray ? @t : \@t;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
399 }
400  
401  
402 # probably more config later, currently only one column
403 # "also-per-field" means, not only $set.$more, but also f.$field.$set.$more
404 6 my %sets = ( #also-per-field?
405 expand => [0],
406 facet => [1],
407 hl => [1],
408 mlt => [0],
409 stats => [0],
410 suggest => [0],
411 group => [0],
412 );
413  
414 sub expandSelect(@)
415 9 ▶{ my $self = shift;
Subroutine expandSelect: called CC=13 SCAR=27.2
Pod documented
416 9 my @s;
417 9 my (@flat, %seen_set);
418 9 ▶ while(@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 37 9 46
419 37 { my ($k, $v) = (shift, shift);
420 37 ▶ $k =~ s/_/./g if $self->fieldKeySimplify;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fieldKeySimplify 37 0 37
421 37 my @p = split /\./, $k;
422  
423 # fields are $set.$more or f.$field.$set.$more
424 37 ▶ my $per_field = $p[0] eq 'f' && @p > 2;
Condition66%2/3
$p[0] eq 'f' && @p > 2
!ll&&!rl&&r
3502
MC/DC50%1/2
$p[0] eq 'f' && @p > 2
$p[0] eq 'f' @p > 2
Truth table66%2/3
A && B
A= $p[0] eq 'f'B= @p > 2
ABresult
0X0
100
111
425 37 ▶ my ($set, $more) = $per_field ? @p[2,3] : @p[0,1];
Branch: 1 branch
BranchTrueFalseTotal
$per_field ? : 2 35 37
426  
427 37 ▶ if(my $def = $sets{$set})
Branch: 2 branches
BranchTrueFalseTotal
if (my $def = $sets{$set}) { } 15 22 37
elsif (ref $v eq 'HASH') { } 0 22 22
428 15 { $seen_set{$set} = 1;
429 15 ▶ !$per_field || $def->[0]
Branch: 1 branch
BranchTrueFalseTotal
not $per_field or $def->[0] or &error(&__x("set {set} cannot be used per field, in {field}.", "set", $set, "field", $k)) 15 0 15
Condition66%2/3
not $per_field or $def->[0]
l!l&&r!l&&!r
1320
MC/DC0%0/2
not $per_field or $def->[0]
not $per_field $def->[0]
Truth table66%2/3
not(A) or B
A= not $per_fieldB= $def->[0]
ABresult
1X1
011
000
430 or error __x"set {set} cannot be used per field, in {field}.", set => $set, field => $k;
431  
432 15 ▶ if(ref $v eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq 'HASH') { } 15 0 15
elsif ($more) { } 0 0 0
433 15 ▶ { !$more or error __x"field {field} is not simple for a set.", field => $k;
Branch: 1 branch
BranchTrueFalseTotal
$more and &error(&__x("field {field} is not simple for a set.", "field", $k)) 0 15 15
434 15 push @s, $self->_simpleExpand($v, "$k.");
435 }
436 elsif($more) # skip $set=true for now
437 0 { push @flat, $k => $v;
438 }
439 }
440 elsif(ref $v eq 'HASH')
441 0 { error __x"unknown field set {set}.", set => $set;
442 }
443 else
444 22 { push @flat, $k => $v;
445 }
446 }
447 9 push @flat, %seen_set;
448 9 unshift @s, $self->_simpleExpand(\@flat);
449 9 ▶ wantarray ? @s : \@s;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 8 9
450 }
451  
452  
453 sub deprecated($)
454 1 ▶{ my ($self, $msg) = @_;
Subroutine deprecated: called CC=2 SCAR=7.1
Pod documented
455 1 ▶ return if $self->{AS_depr_msg}{$msg}++; # report only once
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"AS_depr_msg"}{$msg}++ 0 1 1
456 1 warning __x"deprecated solr {message}", message => $msg;
457 }
458  
459  
460 sub ignored($)
461 0 ▶{ my ($self, $msg) = @_;
Subroutine ignored: not called CC=2 SCAR=17.9
Pod documented
462 0 ▶ return if $self->{AS_ign_msg}{$msg}++; # report only once
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"AS_ign_msg"}{$msg}++ 0 0 0
463 0 warning __x"ignored solr {message}", message => $msg;
464 }
465  
466  
467 sub removed($)
468 0 ▶{ my ($self, $msg) = @_;
Subroutine removed: not called CC=2 SCAR=17.9
Pod documented
469 0 ▶ return if $self->{AS_rem_msg}{$msg}++; # report only once
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"AS_rem_msg"}{$msg}++ 0 0 0
470 0 warning __x"removed solr {message}", message => $msg;
471 }
472  
473  
474 #--------------------
475  
476 sub endpoint($@)
477 1 ▶{ my ($self, $action, %args) = @_;
Subroutine endpoint: called CC=9 SCAR=23.4
Pod documented
478 1 ▶ my $core = $args{core} || $self->core;
Condition33%1/3
$args{'core'} || $self->core
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'core'} || $self->core
$args{'core'} $self->core
Truth table33%1/3
A || B
A= $args{'core'}B= $self->core
ABresult
1X1
011
000
479 1 my $take = $self->server->clone; # URI
480 1 ▶ $take->path($take->path . (defined $core ? "/$core" : '') . "/$action");
Branch: 1 branch
BranchTrueFalseTotal
defined $core ? : 1 0 1
481  
482 # make parameters ordered
483 1 ▶ my $params = $args{params} || [];
Condition50%1/2
$args{'params'} || []
l!l
10
MC/DC0%0/1
$args{'params'} || []
$args{'params'}
Truth table50%1/2
A
A= $args{'params'}
Aresult
11
00
484 1 ▶ $params = [ %$params ] if ref $params eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $params eq "HASH" 0 1 1
485 1 ▶ @$params or return $take;
Branch: 1 branch
BranchTrueFalseTotal
@$params or return $take 1 0 1
486  
487 # remove paramers with undefined value
488 1 my @p = @$params;
489 1 my @params;
490 1 ▶ while(@p)
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 3 1 4
491 3 ▶ { push @params, $p[0] => $p[1] if defined $p[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $p[1] 3 0 3
492 3 shift @p, shift @p;
493 }
494  
495 1 ▶ $take->query_form(@params) if @params;
Branch: 1 branch
BranchTrueFalseTotal
if @params 1 0 1
496 1 $take;
497 }
498  
499  
500 sub request($$;$$)
501 0 ▶{ my ($self, $url, $result, $body, $body_ct) = @_;
Subroutine request: not called CC=11 SCAR=48.8
Pod documented
502  
503 0 my $req;
504 0 ▶ if($body)
Branch: 1 branch
BranchTrueFalseTotal
if ($body) { } 0 0 0
505 { # request with 'form' payload
506 0 ▶ $req = HTTP::Request->new(
Branch: 1 branch
BranchTrueFalseTotal
ref $body eq 'SCALAR' ? : 0 0 0
507 POST => $url,
508 [ Content_Type => $body_ct
509 , Content_Disposition => 'form-data; name="content"'
510 ],
511 (ref $body eq 'SCALAR' ? $$body : $body)
512 );
513 }
514 else
515 { # request without payload
516 0 $req = HTTP::Request->new(GET => $url);
517 }
518  
519 0 $result->request($req);
520  
521 0 my $resp;
522 0 my $retries = $self->{AS_retry_max};
523 0 my $wait = $self->{AS_retry_wait};
524 0 my $start = time;
525  
526 0 ▶ while($retries--)
Branch: 1 branch
BranchTrueFalseTotal
if ($retries--) 0 0 0
527 0 { $resp = $self->agent->request($req);
528 0 $result->response($resp);
529 0 my $dec = $result->decoded($self->decodeResponse($resp));
530  
531 0 ▶ last if $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->is_success 0 0 0
532  
533 0 ▶ if($resp->code==500)
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->code == 500) 0 0 0
534 0 { $! = ENETDOWN; # HTTP(500) -> unix error
535 0 alert __x"Solr request failed with {code}, {retries} retries left",
536 code => $resp->code, retries => $retries, result => $result;
537 0 ▶ sleep $wait if $wait && $retries; # let remote settle a bit
Branch: 1 branch
BranchTrueFalseTotal
if $wait and $retries 0 0 0
Condition0%0/3
$wait and $retries
!ll&&!rl&&r
000
MC/DC0%0/2
$wait and $retries
$wait $retries
Truth table0%0/3
A and B
A= $waitB= $retries
ABresult
0X0
100
111
538 0 next;
539 }
540  
541 0 ▶ error __x"Solr request failed with: {err}",
Condition0%0/3
$result->solrError || $result->httpError
l!l&&r!l&&!r
000
MC/DC0%0/2
$result->solrError || $result->httpError
$result->solrError $result->httpError
Truth table0%0/3
A || B
A= $result->solrErrorB= $result->httpError
ABresult
1X1
011
000
542 err => ($result->solrError || $result->httpError), result => $result;
543 }
544  
545 0 ▶ unless($resp->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->is_success) 0 0 0
546 0 ▶ { $! = $resp->code==500 ? ENETDOWN : ENETUNREACH;
Branch: 1 branch
BranchTrueFalseTotal
$resp->code == 500 ? : 0 0 0
547 fault __x"Solr request failed after {elapse} seconds after {retries} retries",
548 0 elapse => time - $start, retries => $self->{AS_retry_max} - $retries -1, result => $result;
549 }
550  
551 0 $resp;
552 }
553  
554 0 ▶sub decodeResponse($) { undef }
Subroutine decodeResponse: not called CC=1 SCAR=6.9
Pod undocumented
555  
556 #--------------------
557  
558 6 1;
« Solr/JSON.pm ↑ Summary Solr/XML.pm »