changed output to be non-cascarding when using -n switch
[yaz-moved-to-github.git] / zoom / zoomtst7.c
1 /* $Id: zoomtst7.c,v 1.16 2007-01-03 08:42:17 adam Exp $  */
2
3 /** \file zoomtst7.c
4     \brief Mix of operations
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <assert.h>
10
11 #include <yaz/xmalloc.h>
12 #include <yaz/nmem.h>
13 #include <yaz/log.h>
14 #include <yaz/zoom.h>
15
16 int main(int argc, char **argv)
17 {
18     int block;
19     int i, j;
20     ZOOM_connection z;
21     ZOOM_resultset r[10];  /* and result sets .. */
22     ZOOM_options o;
23
24     nmem_init ();
25     o = ZOOM_options_create ();
26
27     z = ZOOM_connection_new ("localhost", 9999);
28     if (ZOOM_connection_error (z, 0, 0))
29     {
30         printf ("error - couldn't connect?\n");
31         exit (1);
32     }
33         
34     ZOOM_connection_destroy (z);
35
36     for (block = 0; block < 3; block++)
37     {
38         switch (block)
39         {
40         case 0:
41             printf ("blocking - not calling ZOOM_events\n");
42             break;
43         case 1:
44             printf ("blocking - calling ZOOM_events\n");
45             break;
46         case 2:
47             printf ("non-blocking - calling ZOOM_events\n");
48             break;
49         }
50         if (block > 1)
51             ZOOM_options_set (o, "async", "1");
52         for (i = 0; i<10; i++)
53         {
54             char host[40];
55
56             printf ("session %2d", i);
57             sprintf (host, "localhost:9999/%d", i);
58             z = ZOOM_connection_create (o);
59             ZOOM_connection_connect (z, host, 0);
60             
61             for (j = 0; j < 10; j++)
62             {
63                 ZOOM_record recs[2];
64                 char query[40];
65                 ZOOM_query s = ZOOM_query_create ();
66                 
67                 sprintf (query, "i%dr%d", i, j);
68                 
69                 if (ZOOM_query_prefix (s, query))
70                 {
71                     printf ("bad PQF: %s\n", query);
72                     exit (2);
73                 }
74                 ZOOM_options_set (o, "start", "0");
75                 ZOOM_options_set (o, "count", "0");
76                 
77                 r[j] = ZOOM_connection_search (z, s); /* non-piggy */
78                 
79                 ZOOM_resultset_records (r[j], recs, 0, 2);  /* first two */
80                 
81                 ZOOM_resultset_records (r[j], recs, 1, 2);  /* third */
82
83                 ZOOM_resultset_records (r[j], recs, 0, 0);  /* ignored */
84
85                 if (ZOOM_resultset_size (r[j]) > 2)
86                 {
87                     if (!recs[0])
88                     {
89                         fprintf (stderr, "\nrecord missing\n");
90                         exit (1);
91                     }
92                 }
93                 
94                 ZOOM_query_destroy (s);
95
96                 printf (".");
97                 if (block > 0)
98                     while (ZOOM_event (1, &z))
99                         ;
100             }
101             for (j = 0; j<i; j++)
102                 ZOOM_resultset_destroy (r[j]);
103             ZOOM_connection_destroy (z);
104             for (; j < 10; j++)
105                 ZOOM_resultset_destroy (r[j]);
106             printf ("10 searches, 20 presents done\n");
107
108         }
109
110         for (i = 0; i<1; i++)
111         {
112             ZOOM_query q = ZOOM_query_create ();
113             char host[40];
114
115             printf ("session %2d", i+10);
116             sprintf (host, "localhost:9999/%d", i);
117             z = ZOOM_connection_create (o);
118             ZOOM_connection_connect (z, host, 0);
119             
120             for (j = 0; j < 10; j++)
121             {
122                 char query[40];
123                 
124                 sprintf (query, "i%dr%d", i, j);
125                 
126                 ZOOM_options_set (o, "count", "0");
127                 
128                 r[j] = ZOOM_connection_search_pqf (z, query);
129
130                 printf (".");
131                 if (block > 0)
132                     while (ZOOM_event (1, &z))
133                         ;
134             }
135
136             ZOOM_connection_destroy (z);
137             
138             for (j = 0; j < 10; j++)
139             {
140                 ZOOM_resultset_records (r[j], 0, 0, 1);
141             }
142             for (j = 0; j < 10; j++)
143                 ZOOM_resultset_destroy (r[j]);
144             ZOOM_query_destroy (q);
145             printf ("10 searches, 10 ignored presents done\n");
146         }
147
148
149         for (i = 0; i<1; i++)
150         {
151             char host[40];
152             ZOOM_scanset scan = 0;
153
154             printf ("session %2d", i);
155             sprintf (host, "localhost:9999/%d", i);
156             z = ZOOM_connection_create (o);
157             ZOOM_connection_connect (z, host, 0);
158
159             scan = ZOOM_connection_scan (z, "@attr 1=4 a");
160             if (block > 0)
161                 while (ZOOM_event (1, &z))
162                     ;
163             printf (" scan size = %ld\n", (long) ZOOM_scanset_size(scan));
164             for (j = 0; j<ZOOM_scanset_size (scan); j++)
165             {
166                 int occur, len;
167                 const char *term;
168                 term = ZOOM_scanset_term (scan, j, &occur, &len);
169                 if (term)
170                     printf ("%d %.*s %d\n", j, len, term, occur);
171                 
172             }
173             ZOOM_scanset_destroy (scan);
174             ZOOM_connection_destroy (z);
175         }
176
177     }
178     ZOOM_options_destroy (o);
179     exit (0);
180 }
181
182 /*
183  * Local variables:
184  * c-basic-offset: 4
185  * indent-tabs-mode: nil
186  * End:
187  * vim: shiftwidth=4 tabstop=8 expandtab
188  */
189