1 package Koha
::Z3950Responder
::ZebraSession
;
3 # Copyright ByWater Solutions 2016
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use base
qw( Koha::Z3950Responder::Session );
30 Koha::Z3950Responder::ZebraSession
34 Zebra-specific session class that uses C<Koha::Session> as the base class.
38 =head2 INSTANCE METHODS
42 my ($resultset, $hits) = $self->_start_search( $args, $self->{server}->{num_to_prefetch} );
44 Connect to Zebra and do the search
49 my ( $self, $args, $num_to_prefetch, $in_retry ) = @_;
51 my $database = $args->{DATABASES
}->[0];
52 my ( $connection, $results );
55 $connection = C4
::Context
->Zconn(
56 # We're depending on the caller to have done some validation.
57 $database eq 'biblios' ?
'biblioserver' : 'authorityserver',
58 0 # No, no async, doesn't really help much for single-server searching
61 $results = $connection->search_pqf( $args->{QUERY
} );
63 $self->log_debug(' retry successful') if ($in_retry);
66 die $@
if ( ref($@
) ne 'ZOOM::Exception' );
68 if ( $@
->diagset() eq 'ZOOM' && $@
->code() == 10004 && !$in_retry ) {
69 $self->log_debug(' upstream server lost connection, retrying');
70 return $self->_start_search( $args, $num_to_prefetch, 1 );
73 $self->_set_error_from_zoom( $args, $@
);
77 my $hits = $results ?
$results->size() : -1;
79 database
=> $database,
80 connection
=> $connection,
82 query
=> $args->{QUERY
},
86 return ( $resultset, $hits );
91 my $record = $self->_fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} );
93 Fetch a record from Zebra. Caches records in session to avoid too many fetches.
98 my ( $self, $resultset, $args, $index, $num_to_prefetch ) = @_;
103 if ( !$resultset->{results
}->record_immediate( $index ) ) {
104 my $start = $num_to_prefetch ?
int( $index / $num_to_prefetch ) * $num_to_prefetch : $index;
106 if ( $start + $num_to_prefetch >= $resultset->{results
}->size() ) {
107 $num_to_prefetch = $resultset->{results
}->size() - $start;
110 $self->log_debug(" fetch uncached, fetching $num_to_prefetch records starting at $start");
112 $resultset->{results
}->records( $start, $num_to_prefetch, 0 );
115 $record = $resultset->{results
}->record_immediate( $index )->raw();
118 die $@
if ( ref($@
) ne 'ZOOM::Exception' );
119 $self->_set_error_from_zoom( $args, $@
);
128 Callback that is called when a session is terminated
133 my ( $self, $args ) = @_;
135 foreach my $resultset ( values %{ $self->{resultsets
} } ) {
136 $resultset->{results
}->destroy();
140 =head3 _set_error_from_zoom
142 $self->_set_error_from_zoom( $args, $@ );
144 Log and set error code and diagnostic message from a ZOOM exception
148 sub _set_error_from_zoom
{
149 my ( $self, $args, $exception ) = @_;
151 $self->set_error( $args, $self->ERR_TEMPORARY_ERROR, 'Cannot connect to upstream server' );
153 "Zebra upstream error: " .
154 $exception->message() . " (" .
155 $exception->code() . ") " .
156 ( $exception->addinfo() // '' ) . " " .
157 $exception->diagset()