remove stale $Id$
[irspy-moved-to-github.git] / bin / test-pod.pl
1 #!/usr/bin/perl -w
2 #
3 # Run like this:
4 #       YAZ_LOG=pod perl -I ../lib test-pod.pl "bagel.indexdata.com/gils" "bagel.indexdata.com/marc"
5
6 use strict;
7 use warnings;
8
9 use ZOOM::Pod;
10
11 if (@ARGV == 0) {
12     printf STDERR "Usage: $0 <target1> [<target2> ...]\n";
13     exit 1;
14 }
15
16 ZOOM::Log::mask_str("appl");
17 my $pod = new ZOOM::Pod(@ARGV);
18 $pod->option(elementSetName => "b");
19 $pod->callback(ZOOM::Event::RECV_SEARCH, \&completed_search);
20 $pod->callback(ZOOM::Event::RECV_RECORD, \&got_record);
21 #$pod->callback(exception => \&exception_thrown);
22 $pod->search_pqf("the");
23 my $err = $pod->wait({});
24 die "$pod->wait() failed with error $err" if $err;
25
26 sub completed_search {
27     my($conn, $arg, $rs, $event) = @_;
28
29     my $host = $conn->option("host");
30     print "$host : found ", $rs->size(), " records\n";
31     my %state = (next_to_show => 0, next_to_fetch => 0);
32     request_records($conn, $rs, \%state, 2);
33     $arg->{$host} = \%state;
34     return 0;
35 }
36
37 sub got_record {
38     my($conn, $arg, $rs, $event) = @_;
39
40     my $host = $conn->option("host");
41     my $state = $arg->{$host};
42
43     {
44         # Sanity-checking assertions.  These should be impossible
45         my $ns = $state->{next_to_show};
46         my $nf = $state->{next_to_fetch};
47         if ($ns > $nf) {
48             die "next_to_show > next_to_fetch ($ns > $nf)";
49         } elsif ($ns == $nf) {
50             die "next_to_show == next_to_fetch ($ns)";
51         }
52     }
53
54     my $i = $state->{next_to_show}++;
55     my $rec = $rs->record($i);
56     print "$host: record $i is ", render_record($rec), "\n";
57     request_records($conn, $rs, $state, 3)
58         if $i == $state->{next_to_fetch}-1;
59
60     return 0;
61 }
62
63 sub exception_thrown {
64     my($conn, $arg, $rs, $exception) = @_;
65     print "Uh-oh!  $exception\n";
66     return 0;
67 }
68
69 sub request_records {
70     my($conn, $rs, $state, $count) = @_;
71
72     my $i = $state->{next_to_fetch};
73     ZOOM::Log::log("appl", "requesting $count records from $i for ",
74                    $conn->option("host"));
75     $rs->records($i, $count, 0);
76     $state->{next_to_fetch} += $count;
77 }
78
79 sub render_record {
80     my($rec) = @_;
81
82     return "undefined" if !defined $rec;
83     return "'" . $rec->render() . "'";
84 }