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