blib/lib/KinoSearch1/Search/SearchClient.pm

filter: Statementstmt 100% 73 / 73 Branchbran 68.1% 15 / 22 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 18 / 18 Podpod 11.1% 1 / 9 totaltotal 82.5% 109 / 132 CC 14 SCAR 10.5
CC
14
Cov
89%
_rpc
5.1
search_hit_collector
5.0
init_instance
4.2
CRAP 2.9
« blib/lib/KinoSearch1/Index/FieldsReader.pm ↑ Summary blib/lib/KinoSearch1/Searcher.pm »
1 package KinoSearch1::Search::SearchClient;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use KinoSearch1::Util::ToolSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use base qw( KinoSearch1::Searcher );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Storable qw( nfreeze thaw );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 __PACKAGE__->init_instance_vars(
11 # params/members
12 analyzer => undef,
13 peer_address => undef,
14 password => undef,
15 # members
16 similarity => undef,
17 );
18 }
19  
20 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶sub init_instance {
Subroutine init_instance: called CC=4 SCAR=14.4
Pod documented
23 1 my $self = shift;
24  
25 1 ▶ $self->{similarity} ||= KinoSearch1::Search::Similarity->new;
Condition33%1/3
$self->{"similarity"} ||= "KinoSearch1::Search::Similarity"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"similarity"} ||= "KinoSearch1::Search::Similarity"->new
$self->{"similarity"} "KinoSearch1::Search::Similarity"->new
Truth table33%1/3
A ||= B
A= $self->{"similarity"}B= "KinoSearch1::Search::Similarity"->new
ABresult
1X1
011
000
26 1 $self->{field_sims} = {};
27  
28 # establish a connection
29 my $sock = IO::Socket::INET->new(
30 PeerAddr => $self->{peer_address},
31 1 Proto => 'tcp',
32 );
33 1 ▶ confess("No socket: $!") unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 1 1
34 1 $sock->autoflush(1);
35 1 $self->{sock} = $sock;
36  
37 # verify password
38 1 print $sock "$self->{password}\n";
39 1 chomp( my $response = <$sock> );
40 1 ▶ confess("Failed to connect: '$response'") unless $response =~ /accept/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $response =~ /accept/i 0 1 1
41 }
42  
43 =for comment
44  
45 Make a remote procedure call. For every call that does not close/terminate
46 the socket connection, expect a response back that's been serialized using
47 Storable.
48  
49 =cut
50  
51 17 ▶sub _rpc {
Subroutine _rpc: called CC=5 SCAR=16.2
52 17 my ( $self, $method, $args ) = @_;
53 17 my $sock = $self->{sock};
54  
55 17 my $serialized = nfreeze($args);
56 17 my $packed_len = pack( 'N', bytes::length($serialized) );
57 17 print $sock "$method\n$packed_len$serialized";
58  
59 # bail out if we're either closing or shutting down the server remotely
60 17 ▶ return if $method eq 'done';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "done" 1 16 17
61 16 ▶ return if $method eq 'terminate';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "terminate" 1 15 16
62  
63 # decode response
64 15 $sock->read( $packed_len, 4 );
65 15 my $arg_len = unpack( 'N', $packed_len );
66 15 my $check_val = read( $sock, $serialized, $arg_len );
67 15 ▶ confess("Tried to read $arg_len bytes, got $check_val")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg_len and $check_val == $arg_len 0 15 15
Condition33%1/3
defined $arg_len and $check_val == $arg_len
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $arg_len and $check_val == $arg_len
defined $arg_len $check_val == $arg_len
Truth table33%1/3
A and B
A= defined $arg_lenB= $check_val == $arg_len
ABresult
0X0
100
111
68 unless ( defined $arg_len and $check_val == $arg_len );
69 15 return thaw($serialized);
70 }
71  
72 3 ▶sub get_field_names {
Subroutine get_field_names: called CC=1 SCAR=0.0
Pod undocumented
73 3 my $self = shift;
74 3 return $self->_rpc( 'get_field_names', {} );
75 }
76  
77 2 my %search_hit_collector_args = (
78 hit_collector => undef,
79 weight => undef,
80 filter => undef,
81 sort_spec => undef,
82 );
83  
84 3 ▶sub search_hit_collector {
Subroutine search_hit_collector: called CC=5 SCAR=16.2
Pod undocumented
85 3 my $self = shift;
86 3 ▶ confess kerror() unless verify_args( \%search_hit_collector_args, @_ );
Branch: 1 branch
BranchTrueFalseTotal
unless verify_args(\%search_hit_collector_args, @_) 0 3 3
87 3 my %args = ( %search_hit_collector_args, @_ );
88 3 ▶ confess("remote filtered search not supported") if defined $args{filter};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"filter"} 0 3 3
89  
90 # replace the HitCollector with a size rather than serialize it
91 3 my $collector = delete $args{hit_collector};
92 3 ▶ if ( a_isa_b( $collector, "KinoSearch1::Search::OffsetCollector" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (a_isa_b($collector, 'KinoSearch1::Search::OffsetCollector')) { } 1 2 3
93 1 $args{num_wanted} = $collector->get_storage->get_max_size;
94 }
95 else {
96 2 $args{num_wanted} = $collector->get_max_size;
97 }
98  
99 # Make the remote call, which returns a hashref of doc => score pairs.
100 # Accumulate hits into the HitCollector if the query is valid.
101 3 my $score_pairs = $self->_rpc( 'search_hit_collector', \%args );
102 3 ▶ while ( my ( $doc, $score ) = each %$score_pairs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$score_pairs) 5 3 8
103 5 $collector->collect( $doc, $score );
104 }
105 }
106  
107 1 ▶sub terminate {
Subroutine terminate: called CC=1 SCAR=0.0
Pod undocumented
108 1 my $self = shift;
109 1 return $self->_rpc( 'terminate', {} );
110 }
111  
112 1 ▶sub fetch_doc {
Subroutine fetch_doc: called CC=1 SCAR=0.0
Pod undocumented
113 1 my ( $self, $doc_num ) = @_;
114 1 return $self->_rpc( 'fetch_doc', { doc_num => $doc_num } );
115 }
116  
117 3 ▶sub max_doc {
Subroutine max_doc: called CC=1 SCAR=0.0
Pod undocumented
118 3 my $self = shift;
119 3 return $self->_rpc( 'max_doc', {} );
120 }
121  
122 4 ▶sub doc_freq {
Subroutine doc_freq: called CC=1 SCAR=0.0
Pod undocumented
123 4 my ( $self, $term ) = @_;
124 4 return $self->_rpc( 'doc_freq', { term => $term } );
125 }
126  
127 1 ▶sub doc_freqs {
Subroutine doc_freqs: called CC=1 SCAR=0.0
Pod undocumented
128 1 my ( $self, $terms ) = @_;
129 1 return $self->_rpc( 'doc_freqs', { terms => $terms } );
130 }
131  
132 1 ▶sub close {
Subroutine close: called CC=2 SCAR=7.0
Pod undocumented
133 1 my $self = shift;
134 1 $self->_rpc( 'done', {} );
135 1 my $sock = $self->{sock};
136 1 ▶ close $sock or confess("Error when closing socket: $!");
Branch: 1 branch
BranchTrueFalseTotal
CORE::close $sock or confess("Error when closing socket: $!") 1 0 1
137 1 undef $self->{sock};
138 }
139  
140 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
141 1 my $self = shift;
142 1 ▶ $self->close if defined $self->{sock};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"sock"} 1 0 1
143 }
144  
145 2 1;
146  
147 __END__
« blib/lib/KinoSearch1/Index/FieldsReader.pm ↑ Summary blib/lib/KinoSearch1/Searcher.pm »