Z39 Batch ML
authorBill Erickson <berick@esilibrary.com>
Thu, 31 Jan 2013 20:46:27 +0000 (15:46 -0500)
committerBill Erickson <berick@esilibrary.com>
Fri, 15 Feb 2013 16:09:45 +0000 (11:09 -0500)
Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Z3950.pm

index de2f225..48baa89 100644 (file)
@@ -14,6 +14,7 @@ use OpenSRF::EX qw(:try);
 use OpenSRF::MultiSession;
 use OpenILS::Utils::ModsParser;
 use OpenSRF::Utils::SettingsClient;
+use OpenSRF::Utils::JSON;
 use OpenILS::Application::AppUtils;
 use OpenSRF::Utils::Logger qw/$logger/;
 use OpenILS::Utils::CStoreEditor q/:funcs/;
@@ -392,7 +393,7 @@ sub process_results {
     my $res = {};
     my $count = $$res{count} = $results->size;
 
-    $logger->info("z3950: search returned $count hits");
+    $logger->info("z3950: '$service' search returned $count hits");
 
     my $tend = $limit + $offset;
 
@@ -511,7 +512,7 @@ __PACKAGE__->register_method(
             q/Object containing status information about the on-going search
             and queue operation. 
             {
-                bre_count : $num, -- number of bibs to search against
+                bre_count    : $num, -- number of bibs to search against
                 search_count : $num,
                 search_complete  : $num,
                 queue_count  : $num
@@ -625,11 +626,12 @@ sub send_and_queue_bucket_searches {
 
         # there will be one response per z-source
         for my $resp (@{$req->{response}}) {
-            next unless $resp->content;
-
-            my $result = $resp->content;
             $response->{search_complete}++;
 
+            $logger->info("z3950 got bucket response for request " .
+                OpenSRF::Utils::JSON->perl2JSON($req->{params}));
+
+            my $result = $resp->content or next;
             my $service = $result->{service};
 
             for my $rec (@{$result->{records}}) {
@@ -645,49 +647,68 @@ sub send_and_queue_bucket_searches {
     my $multi_ses = OpenSRF::MultiSession->new(
         app             => 'open-ils.search',
         cap             => $max_parallel,
+        timeout         => 120,
         success_handler => $handle_search_result
     );
 
-    # fire a Z39 search for each distinct combination of z-sources
+    # compile and send the z3950-compatible search blobs
+    my @searches;
     for my $bre_id (keys %$z_searches) {
 
-        # build a search blob for each distinct combination of z-sources
-        my @searches;
+        # one search blob per distinct set of Z sources for this record
+        my @rec_searches;
 
         for my $z_index (keys %{$z_searches->{$bre_id}}) {
             my $search = $z_searches->{$bre_id}{$z_index};
 
+            $logger->info("z39 search piece [$bre_id]: " . $search->{z_index_name});
+
             my ($blob) = grep { 
                 # array compare, weee
                 @{$_->{service}} ~~ @{$search->{z_source}} 
-            } @searches;
+            } @rec_searches;
 
             if ($blob) {
+
                 $blob->{search}{$search->{z_index_name}} = 
                     $search->{bib_value};
+
             } else {
+
                 $blob = {
                     service => $search->{z_source},
-                    $search->{z_index_name} => $search->{bib_value},
+                    search => {
+                        $search->{z_index_name} => $search->{bib_value}
+                    },
                     limit => $search_limit,
                     offset => 0
                 };
-                push(@searches, $blob);
+                push(@rec_searches, $blob);
             }
-
-            # search count is incremented by one for every 
-            # service we sent a search blob to
-            $response->{search_count}++ for @{$blob->{service}};
         }
 
-        $multi_ses->request(
-            'open-ils.search.z3950.search_class', 
-            $e->authtoken, $_) for @searches;
+        push(@searches, $_) for @rec_searches;
     }
 
+
+
+    # multi-session blocks new requests once it reaches max parallel,
+    # so we need to calculate the number of searches up front 
+    # for accurate reporting
+    $response->{search_count} += scalar(@{$_->{service}}) for @searches;
     $conn->respond($response); # searches in flight
-    $multi_ses->session_wait(1);
 
+    for my $search (@searches) {
+
+        $logger->info("z39 firing bucket search " .
+            OpenSRF::Utils::JSON->perl2JSON($search));
+
+        # toss it onto the multi-pile
+        my $req = $multi_ses->request(
+            'open-ils.search.z3950.search_class', $e->authtoken, $search);
+    }
+
+    $multi_ses->session_wait(1);
     $response->{queue} = $queue;
     $conn->respond($response);
 }