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