async and multi-target Z search .... yes, I am that indefferent to it
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sat, 15 Dec 2007 02:21:56 +0000 (02:21 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Sat, 15 Dec 2007 02:21:56 +0000 (02:21 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@8213 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
Open-ILS/src/perlmods/OpenILS/Utils/ZClient.pm

index 71bc3ae..c6ad36b 100755 (executable)
@@ -109,10 +109,35 @@ sub do_class_search {
        my $auth                        = shift;
        my $args                        = shift;
 
+       if (ref($$args{service}) =~ /ARRAY/o) {
+               $$args{service} = [$$args{service}];
+               $$args{username} = [$$args{username}];
+               $$args{password} = [$$args{password}];
+       }
+
+       $$args{async} = 1;
+
        $$args{query} = 
                compile_query('and', $$args{service}, $$args{search});
 
-       return $self->do_service_search( $conn, $auth, $args );
+       my @results;
+       for (var $i = 0; $i < @{$$args{service}}; i++) {
+               my %tmp_args = %$args;
+               $tmp_args{service} = $$args{service}[$i];
+               $tmp_args{username} = $$args{username}[$i];
+               $tmp_args{password} = $$args{password}[$i];
+               $results[$i] = $self->do_service_search( $conn, $auth, \%tmp_args );
+       }
+
+       my @records;
+       while ((my $index = OpenILS::Utils::ZClient::event( \@results )) != 0) {
+               my $ev = $results[$index - 1]->last_event();
+               if ($ev == OpenILS::Utils::ZClient::Event::END) {
+                       my $munged = process_results( $results[$index - 1], $limit, $offset );
+                       $$munged{service} = $$args{service}[$index];
+                       $conn->respond($munged);
+               }
+       }
 }
 
 
@@ -152,6 +177,7 @@ sub do_search {
        my $port                = $$args{port} or return undef;
        my $db          = $$args{db}    or return undef;
        my $query       = $$args{query} or return undef;
+       my $async       = $$args{async} || 0;
 
        my $limit       = $$args{limit} || 10;
        my $offset      = $$args{offset} || 0;
@@ -167,6 +193,7 @@ sub do_search {
                $host, $port,
                databaseName                            => $db, 
                user                                                    => $username,
+               async                                                   => $async,
                password                                                => $password,
                preferredRecordSyntax   => $output, 
        );
@@ -195,6 +222,8 @@ sub do_search {
 
        $logger->info("z3950: search [$query] took ".(time - $start)." seconds");
 
+       return $result if ($async);
+
        my $munged = process_results($results, $limit, $offset);
        $munged->{query} = $query;
 
index 137d1c5..54e4930 100644 (file)
@@ -45,6 +45,20 @@ sub search {
        return OpenILS::Utils::ZClient::ResultSet->new( $r );
 }
 
+sub event {
+       my $list = shift;
+       if ($imp_class eq 'Net::Z3950') {
+               if (defined $$list[0]{_async_index}) {  
+                       return 0 if ($$list[0]{_async_index} == @$list);
+                       return ++$$list[0]{_async_index};
+               } else {
+                       return $$list[0]{_async_index} = 1;
+               }
+       }
+
+       return ZOOM::event([map { ($_->{result}) } @$list]);
+}
+
 *{__PACKAGE__ . '::search_pqf'} = \&search; 
 
 sub AUTOLOAD {
@@ -84,6 +98,12 @@ sub record {
        return  OpenILS::Utils::ZClient::Record->new( $r );
 }
 
+sub last_event {
+       my $self = shift;
+       return OpenILS::Utils::ZClient::Event::EVENT_END if ($imp_class eq 'Net::Z3950');
+       $self->{result}->last_event();
+}
+
 sub AUTOLOAD {
        my $self = shift;
 
@@ -129,6 +149,20 @@ sub AUTOLOAD {
        return $self->{record}->$method( @_ );
 }
 
+#-------------------------------------------------------------------------------
+package OpenILS::Utils::ZClient::Event;
+
+sub NONE { 0 }
+sub CONNECT { 1 }
+sub SEND_DATA { 2 }
+sub RECV_DATA { 3 }
+sub TIMEOUT { 4 }
+sub UNKNOWN { 5 }
+sub SEND_APDU { 6 }
+sub RECV_APDU { 7 }
+sub RECV_RECORD { 8 }
+sub RECV_SEARCH { 9 }
+sub END { 10 }
 
 1;