3 package Koha::Z3950Responder::ZebraSession;
5 # Copyright ByWater Solutions 2016
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 use base qw( Koha::Z3950Responder::Session );
32 Koha::Z3950Responder::ZebraSession
36 Zebra-specific session class that uses C<Koha::Session> as the base class.
40 =head2 INSTANCE METHODS
44 my ($resultset, $hits) = $self->_start_search( $args, $self->{server}->{num_to_prefetch} );
46 Connect to Zebra and do the search
51 my ( $self, $args, $num_to_prefetch, $in_retry ) = @_;
53 my $database = $args->{DATABASES}->[0];
54 my ( $connection, $results );
57 $connection = C4::Context->Zconn(
58 # We're depending on the caller to have done some validation.
59 $database eq 'biblios' ? 'biblioserver' : 'authorityserver',
60 0 # No, no async, doesn't really help much for single-server searching
63 $results = $connection->search_pqf( $args->{QUERY} );
65 $self->log_debug(' retry successful') if ($in_retry);
68 die $@ if ( ref($@) ne 'ZOOM::Exception' );
70 if ( $@->diagset() eq 'ZOOM' && $@->code() == 10004 && !$in_retry ) {
71 $self->log_debug(' upstream server lost connection, retrying');
72 return $self->_start_search( $args, $num_to_prefetch, 1 );
75 $self->_set_error_from_zoom( $args, $@ );
79 my $hits = $results ? $results->size() : -1;
81 database => $database,
82 connection => $connection,
84 query => $args->{QUERY},
88 return ( $resultset, $hits );
93 my $record = $self->_fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} );
95 Fetch a record from Zebra. Caches records in session to avoid too many fetches.
100 my ( $self, $resultset, $args, $index, $num_to_prefetch ) = @_;
105 if ( !$resultset->{results}->record_immediate( $index ) ) {
106 my $start = $num_to_prefetch ? int( $index / $num_to_prefetch ) * $num_to_prefetch : $index;
108 if ( $start + $num_to_prefetch >= $resultset->{results}->size() ) {
109 $num_to_prefetch = $resultset->{results}->size() - $start;
112 $self->log_debug(" fetch uncached, fetching $num_to_prefetch records starting at $start");
114 $resultset->{results}->records( $start, $num_to_prefetch, 0 );
117 $record = $resultset->{results}->record_immediate( $index )->raw();
120 die $@ if ( ref($@) ne 'ZOOM::Exception' );
121 $self->_set_error_from_zoom( $args, $@ );
130 Callback that is called when a session is terminated
135 my ( $self, $args ) = @_;
137 foreach my $resultset ( values %{ $self->{resultsets} } ) {
138 $resultset->{results}->destroy();
142 =head3 _set_error_from_zoom
144 $self->_set_error_from_zoom( $args, $@ );
146 Log and set error code and diagnostic message from a ZOOM exception
150 sub _set_error_from_zoom {
151 my ( $self, $args, $exception ) = @_;
153 $self->set_error( $args, $self->ERR_TEMPORARY_ERROR, 'Cannot connect to upstream server' );
155 "Zebra upstream error: " .
156 $exception->message() . " (" .
157 $exception->code() . ") " .
158 ( $exception->addinfo() // '' ) . " " .
159 $exception->diagset()