Implemtation of ZOOM Facet API in zoom shell
[yaz-moved-to-github.git] / zoom / zoomtst6.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdio.h>
7 #include <yaz/nmem.h>
8 #include <yaz/xmalloc.h>
9
10 #include <yaz/zoom.h>
11
12 static void display_records (const char *tname, ZOOM_resultset r)
13 {
14     /* OK, no major errors. Look at the result count */
15     int pos;
16     printf ("%s: %ld hits\n", tname, (long) ZOOM_resultset_size(r));
17     /* go through all records at target */
18     for (pos = 0; pos < 4; pos++)
19     {
20         ZOOM_record rec = ZOOM_resultset_record (r, pos);
21         if (rec)
22         {
23             /* get database for record and record itself at pos */
24             const char *db = ZOOM_record_get (rec, "database", 0);
25             int len;
26             const char *render = ZOOM_record_get (rec, "render", &len);
27             /* if rec is non-null, we got a record for display */
28             if (rec)
29             {
30                 printf ("%d %s\n", pos+1, (db ? db : "unknown"));
31                 if (render)
32                 {
33                     if (fwrite (render, 1, len, stdout) != (size_t) len)
34                         printf("write to stdout failed\n");
35                 }
36                 printf ("\n");
37             }
38         }
39     }
40 }
41
42 int main(int argc, char **argv)
43 {
44     int i;
45     int no = argc-3;
46     ZOOM_connection z[500];  /* allow at most 500 connections */
47     ZOOM_resultset r1[500];  /* and result sets .. */
48     ZOOM_resultset r2[500];  /* and result sets .. */
49     ZOOM_query q;
50     ZOOM_options o;
51
52     o = ZOOM_options_create ();
53     if (argc < 4)
54     {
55         fprintf (stderr, "usage:\n%s target1 .. targetN query1 query2\n",
56                  *argv);
57         exit (1);
58     }
59     if (no > 500)
60         no = 500;
61
62     ZOOM_options_set (o, "async", "1");
63
64     /* get 3 (at most) records from beginning */
65     ZOOM_options_set (o, "count", "3");
66
67     ZOOM_options_set (o, "preferredRecordSyntax", "sutrs");
68     ZOOM_options_set (o, "elementSetName", "B");
69
70     /* create query */
71     q = ZOOM_query_create ();
72     if (ZOOM_query_prefix (q, argv[argc-2]))
73     {
74         printf ("bad PQF: %s\n", argv[argc-2]);
75         exit (2);
76     }
77     /* connect - and search all */
78     for (i = 0; i<no; i++)
79     {
80         z[i] = ZOOM_connection_create (o);
81         ZOOM_connection_connect (z[i], argv[i+1], 0);
82         r1[i] = ZOOM_connection_search (z[i], q);
83     }
84     if (ZOOM_query_prefix (q, argv[argc-1]))
85     {
86         printf ("bad prefix query: %s\n", argv[argc-1]);
87         exit (2);
88     }
89     /* queue second search */
90     for (i = 0; i<no; i++)
91         r2[i] = ZOOM_connection_search (z[i], q);
92
93     /* fetch 5th record from first result set as well */
94     for (i = 0; i<no; i++)
95         ZOOM_resultset_records (r1[i], 0, 4, 1);
96
97     /* network I/O */
98     while (ZOOM_event (no, z))
99         ;
100
101     /* handle errors */
102     for (i = 0; i<no; i++)
103     {
104         int error;
105         const char *errmsg, *addinfo;
106         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
107             fprintf (stderr, "%s error: %s (%d) %s\n",
108                      ZOOM_connection_option_get(z[i], "host"),
109                      errmsg, error, addinfo);
110         else
111         {
112             display_records (ZOOM_connection_option_get(z[i], "host"), r1[i]);
113             display_records (ZOOM_connection_option_get(z[i], "host"), r2[i]);
114         }
115     }
116     /* destroy stuff and exit */
117     ZOOM_query_destroy (q);
118     for (i = 0; i<no; i++)
119     {
120         ZOOM_connection_destroy (z[i]);
121         ZOOM_resultset_destroy (r1[i]);
122         ZOOM_resultset_destroy (r2[i]);
123     }
124     ZOOM_options_destroy(o);
125     exit (0);
126 }
127 /*
128  * Local variables:
129  * c-basic-offset: 4
130  * c-file-style: "Stroustrup"
131  * indent-tabs-mode: nil
132  * End:
133  * vim: shiftwidth=4 tabstop=8 expandtab
134  */
135