[Koha-patches] [PATCH 2/2] Bug 12347: Performance improvements in Search.pm

Tomas Cohen Arazi tomascohen at gmail.com
Tue Jun 3 01:09:25 CEST 2014


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 | 51 +++++++++++++++++++++------------------------------
 1 file changed, 21 insertions(+), 30 deletions(-)

diff --git a/C4/Search.pm b/C4/Search.pm
index 748bb1c..beef11d 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,10 @@ 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];
+                            # we skip bad records
+                            next if ( !defined $marc_record );
 
                             my @used_datas = ();
 
@@ -528,6 +531,7 @@ sub getRecords {
                                 # Removed when as_string fixed
                                 my @subfields = $subfield_letters =~ /./sg;
 
+                                # Check $tag_num is a valid field for $marc_record
                                 my @fields = $marc_record->field($tag_num);
                                 foreach my $field (@fields) {
                                     my $data = $field->as_string( $subfield_letters, $facet->{sep} );
@@ -1715,22 +1719,9 @@ 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];
+        # we skip bad records
+        next if ( !defined $marcrecord );
 
         my $fw = $scan
              ? undef
-- 
1.9.1



More information about the Koha-patches mailing list