From e4e3462e71cfc1dbeb1a955423c94249ef1e8b84 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 26 May 2014 15:44:54 -0300 Subject: [PATCH] Bug 12347: Performance improvements in Search.pm Short: some costly stuff is done twice, this patch refactors that to make it more reasonable. Long: Bug 11096 introduced a noticeable overhead to the facet building stage of the getRecords code. This patch leverages it by propagating the MARC::Record objects created upon facet building, and reusing them in the C4::Search::searchResults function. To test: - Search for a term on master - Apply the patch - Same results Unit tests reflecting API change in a separate patch. Regards To+ Sponsored-by: Universidad Nacional de Cordoba --- C4/Search.pm | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 748bb1c..f41be72 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -485,18 +485,27 @@ sub getRecords { $tmprecord->append_fields($tmptitle); $tmprecord->append_fields($tmpauthor); } - $results_hash->{'RECORDS'}[$j] = - $tmprecord->as_usmarc(); + $results_hash->{'RECORDS'}[$j] = $tmprecord; } # not an index scan else { - $record = $results[ $i - 1 ]->record($j)->raw(); - # warn "RECORD $j:".$record; + $record = new_record_from_zebra ( + 'biblioserver', + $results[ $i - 1 ]->record($j)->raw() + ); + + if ( ! defined $record ) { + warn "ERROR DECODING RECORD - $@: " . + $results[ $i - 1 ]->record($j)->raw(); + next; + } + $results_hash->{'RECORDS'}[$j] = $record; } } + $results_hashref->{ $servers[ $i - 1 ] } = $results_hash; # Fill the facets while we're looping, but only for the biblioserver and not for a scan @@ -507,16 +516,8 @@ sub getRecords { for my $facet (@$facets) { for ( my $j = 0 ; $j < $jmax ; $j++ ) { - my $marc_record = new_record_from_zebra ( - 'biblioserver', - $results[ $i - 1 ]->record($j)->raw() - ); - - if ( ! defined $marc_record ) { - warn "ERROR DECODING RECORD - $@: " . - $results[ $i - 1 ]->record($j)->raw(); - next; - } + my $marc_record = + $results_hashref->{ $servers[ $i - 1 ] }->{'RECORDS'}[$j]; my @used_datas = (); @@ -528,7 +529,8 @@ sub getRecords { # Removed when as_string fixed my @subfields = $subfield_letters =~ /./sg; - my @fields = $marc_record->field($tag_num); + # Check $tag_num is a valid field for $marc_record + my @fields = eval { $marc_record->field($tag_num) }; foreach my $field (@fields) { my $data = $field->as_string( $subfield_letters, $facet->{sep} ); @@ -1715,22 +1717,7 @@ sub searchResults { # loop through all of the records we've retrieved for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) { - my $marcrecord; - if ($scan) { - # For Scan searches we built USMARC data - $marcrecord = MARC::Record->new_from_usmarc( $marcresults->[$i]); - } else { - # Normal search, render from Zebra's output - $marcrecord = new_record_from_zebra( - 'biblioserver', - $marcresults->[$i] - ); - - if ( ! defined $marcrecord ) { - warn "ERROR DECODING RECORD - $@: " . $marcresults->[$i]; - next; - } - } + my $marcrecord = $marcresults->[$i]; my $fw = $scan ? undef -- 1.9.1