« blib/lib/KinoSearch1/Highlight/Highlighter.pm
↑ Summary
blib/lib/KinoSearch1/QueryParser/QueryParser.pm »
| 1 | package KinoSearch1::Search::SearchServer; | ||||||||||||||||||||||||||||
| 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::Util::Class ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | __PACKAGE__->init_instance_vars( | |||||||||||||||||||||||||||
| 9 | # params/members | ||||||||||||||||||||||||||||
| 10 | searchable => undef, | ||||||||||||||||||||||||||||
| 11 | port => undef, | ||||||||||||||||||||||||||||
| 12 | password => undef, | ||||||||||||||||||||||||||||
| 13 | # members | ||||||||||||||||||||||||||||
| 14 | sock => undef, | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Storable qw( nfreeze thaw ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub init_instance { | ||||||||||||||||||||||||||
|
Subroutine
init_instance: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | confess("Missing required param 'password'") | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | unless defined $self->{password}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # establish a listening socket | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | confess("Invalid port") unless $self->{port} =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | my $sock = IO::Socket::INET->new( | ||||||||||||||||||||||||||||
| 32 | LocalPort => $self->{port}, | ||||||||||||||||||||||||||||
| 33 | 0 | Proto => 'tcp', | |||||||||||||||||||||||||||
| 34 | Listen => SOMAXCONN, | ||||||||||||||||||||||||||||
| 35 | Reuse => 1, | ||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | confess("No socket: $!") unless $sock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $sock->autoflush(1); | |||||||||||||||||||||||||||
| 39 | 0 | $self->{sock} = $sock; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | my %dispatch = ( | |||||||||||||||||||||||||||
| 43 | get_field_names => \&do_get_field_names, | ||||||||||||||||||||||||||||
| 44 | max_doc => \&do_max_doc, | ||||||||||||||||||||||||||||
| 45 | doc_freq => \&do_doc_freq, | ||||||||||||||||||||||||||||
| 46 | doc_freqs => \&do_doc_freqs, | ||||||||||||||||||||||||||||
| 47 | search_hit_collector => \&do_search_hit_collector, | ||||||||||||||||||||||||||||
| 48 | fetch_doc => \&do_fetch_doc, | ||||||||||||||||||||||||||||
| 49 | terminate => undef, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub serve { | ||||||||||||||||||||||||||
|
Subroutine
serve: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 54 | 0 | my $main_sock = $self->{sock}; | |||||||||||||||||||||||||||
| 55 | 0 | my $read_set = IO::Select->new($main_sock); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | while ( my @ready = $read_set->can_read ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | for my $readhandle (@ready) { | |||||||||||||||||||||||||||
| 59 | # if this is the main handle, we have a new client, so accept | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( $readhandle == $main_sock ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | my $client_sock = $main_sock->accept; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # verify password | ||||||||||||||||||||||||||||
| 64 | 0 | my $pass = <$client_sock>; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | chomp($pass) if defined $pass; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ( defined($pass) && $pass eq $self->{password} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $pass and $pass eq $self->{"password"}
MC/DC0%0/2
defined $pass and $pass eq $self->{"password"}
defined $pass
$pass eq $self->{"password"}
Truth table0%0/3
A and B
A= defined $passB= $pass eq $self->{"password"}
| |||||||||||||||||||||||||||||
| 67 | 0 | $read_set->add($client_sock); | |||||||||||||||||||||||||||
| 68 | 0 | print $client_sock "accepted\n"; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 0 | print $client_sock "password incorrect\n"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | # otherwise it's a client sock, so process the request | ||||||||||||||||||||||||||||
| 75 | else { | ||||||||||||||||||||||||||||
| 76 | 0 | my $client_sock = $readhandle; | |||||||||||||||||||||||||||
| 77 | 0 | my ( $check_val, $buf, $len, $method, $args ); | |||||||||||||||||||||||||||
| 78 | 0 | chomp( $method = <$client_sock> ); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # if "done", the client's closing | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( $method eq 'done' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 82 | 0 | $read_set->remove($client_sock); | |||||||||||||||||||||||||||
| 83 | 0 | $client_sock->close; | |||||||||||||||||||||||||||
| 84 | 0 | next; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | # remote signal to close the server | ||||||||||||||||||||||||||||
| 87 | elsif ( $method eq 'terminate' ) { | ||||||||||||||||||||||||||||
| 88 | 0 | $read_set->remove($client_sock); | |||||||||||||||||||||||||||
| 89 | 0 | $client_sock->close; | |||||||||||||||||||||||||||
| 90 | 0 | $main_sock->close; | |||||||||||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | # sanity check the method name | ||||||||||||||||||||||||||||
| 94 | elsif ( !$dispatch{$method} ) { | ||||||||||||||||||||||||||||
| 95 | 0 | print $client_sock "ERROR: Bad method name: $method\n"; | |||||||||||||||||||||||||||
| 96 | 0 | next; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # process the method call | ||||||||||||||||||||||||||||
| 100 | 0 | read( $client_sock, $buf, 4 ); | |||||||||||||||||||||||||||
| 101 | 0 | $len = unpack( 'N', $buf ); | |||||||||||||||||||||||||||
| 102 | 0 | read( $client_sock, $buf, $len ); | |||||||||||||||||||||||||||
| 103 | 0 | my $response = $dispatch{$method}->( $self, thaw($buf) ); | |||||||||||||||||||||||||||
| 104 | 0 | my $frozen = nfreeze($response); | |||||||||||||||||||||||||||
| 105 | 0 | my $packed_len = pack( 'N', bytes::length($frozen) ); | |||||||||||||||||||||||||||
| 106 | 0 | print $client_sock $packed_len . $frozen; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub do_get_field_names { | ||||||||||||||||||||||||||
|
Subroutine
do_get_field_names: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 114 | 0 | return $self->{searchable}->get_field_names(%$args); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub do_doc_freq { | ||||||||||||||||||||||||||
|
Subroutine
do_doc_freq: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | my $doc_freq = $self->{searchable}->doc_freq( $args->{term} ); | |||||||||||||||||||||||||||
| 120 | 0 | return { doc_freq => $doc_freq }; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub do_doc_freqs { | ||||||||||||||||||||||||||
|
Subroutine
do_doc_freqs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 125 | 0 | return $self->{searchable}->doc_freqs( $args->{terms} ); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub do_search_hit_collector { | ||||||||||||||||||||||||||
|
Subroutine
do_search_hit_collector: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 129 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | confess("remote filtered search not supported") | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if defined $args->{filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | my $collector = KinoSearch1::Search::HitQueueCollector->new( | ||||||||||||||||||||||||||||
| 134 | 0 | size => $args->{num_wanted} ); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $scorer = $args->{weight}->scorer( $self->{searchable}->get_reader ); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ( defined $scorer ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | $scorer->score_batch( | ||||||||||||||||||||||||||||
| 140 | hit_collector => $collector, | ||||||||||||||||||||||||||||
| 141 | end => $self->{searchable}->max_doc, | ||||||||||||||||||||||||||||
| 142 | 0 | ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | my $hit_queue = $collector->get_hit_queue; | |||||||||||||||||||||||||||
| 145 | 0 | my $hit_docs = $hit_queue->hits; | |||||||||||||||||||||||||||
| 146 | 0 | my %score_docs; | |||||||||||||||||||||||||||
| 147 | 0 | $score_docs{ $_->get_id } = $_->get_score for @$hit_docs; | |||||||||||||||||||||||||||
| 148 | 0 | return \%score_docs; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub do_max_doc { | ||||||||||||||||||||||||||
|
Subroutine
do_max_doc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 152 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 153 | 0 | my $max_doc = $self->{searchable}->max_doc; | |||||||||||||||||||||||||||
| 154 | 0 | return { max_doc => $max_doc }; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub do_fetch_doc { | ||||||||||||||||||||||||||
|
Subroutine
do_fetch_doc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 159 | 0 | return $self->{searchable}->fetch_doc( $args->{doc_num} ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | 1; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||