9bfe1af18e8056e77b88ebec206fe185c850e9d5
[idzebra-moved-to-github.git] / index / apitest.c
1 /*
2  * Copyright (C) 1995-2000, Index Data
3  * All rights reserved.
4  *
5  * $Header: /home/cvsroot/idis/index/Attic/apitest.c,v 1.11 2002-04-04 14:14:13 adam Exp $
6  */
7
8 #include <stdio.h>
9
10 #include <yaz/log.h>
11 #include <yaz/pquery.h>
12 #include "zebraapi.h"
13
14 /* Small routine to display GRS-1 record variants ... */
15 /* Copied verbatim from yaz/client/client.c */
16 static void display_variant(Z_Variant *v, int level)
17 {
18     int i;
19
20     for (i = 0; i < v->num_triples; i++)
21     {
22         printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
23             *v->triples[i]->type);
24         if (v->triples[i]->which == Z_Triple_internationalString)
25             printf(",value=%s\n", v->triples[i]->value.internationalString);
26         else
27             printf("\n");
28     }
29 }
30  
31 /* Small routine to display a GRS-1 record ... */
32 /* Copied verbatim from yaz/client/client.c */
33 static void display_grs1(Z_GenericRecord *r, int level)
34 {
35     int i;
36
37     if (!r)
38         return;
39     for (i = 0; i < r->num_elements; i++)
40     {
41         Z_TaggedElement *t;
42
43         printf("%*s", level * 4, "");
44         t = r->elements[i];
45         printf("(");
46         if (t->tagType)
47             printf("%d,", *t->tagType);
48         else
49             printf("?,");
50         if (t->tagValue->which == Z_StringOrNumeric_numeric)
51             printf("%d) ", *t->tagValue->u.numeric);
52         else
53             printf("%s) ", t->tagValue->u.string);
54         if (t->content->which == Z_ElementData_subtree)
55         {
56             printf("\n");
57             display_grs1(t->content->u.subtree, level+1);
58         }
59         else if (t->content->which == Z_ElementData_string)
60             printf("%s\n", t->content->u.string);
61         else if (t->content->which == Z_ElementData_numeric)
62             printf("%d\n", *t->content->u.numeric);
63         else if (t->content->which == Z_ElementData_oid)
64         {
65             int *ip = t->content->u.oid;
66             oident *oent;
67
68             if ((oent = oid_getentbyoid(t->content->u.oid)))
69                 printf("OID: %s\n", oent->desc);
70             else
71             {
72                 printf("{");
73                 while (ip && *ip >= 0)
74                     printf(" %d", *(ip++));
75                 printf(" }\n");
76             }
77         }
78         else if (t->content->which == Z_ElementData_noDataRequested)
79             printf("[No data requested]\n");
80         else if (t->content->which == Z_ElementData_elementEmpty)
81             printf("[Element empty]\n");
82         else if (t->content->which == Z_ElementData_elementNotThere)
83             printf("[Element not there]\n");
84         else
85             printf("??????\n");
86         if (t->appliedVariant)
87             display_variant(t->appliedVariant, level+1);
88         if (t->metaData && t->metaData->supportedVariants)
89         {
90             int c;
91
92             printf("%*s---- variant list\n", (level+1)*4, "");
93             for (c = 0; c < t->metaData->num_supportedVariants; c++)
94             {
95                 printf("%*svariant #%d\n", (level+1)*4, "", c);
96                 display_variant(t->metaData->supportedVariants[c], level + 2);
97             }
98         }
99     }
100 }
101
102 /* Small test main to illustrate the use of the C api */
103 int main (int argc, char **argv)
104 {
105     /* odr is a handle to memory assocated with RETURNED data from
106        various functions */
107     ODR odr_input, odr_output;
108     
109     /* zs is our Zebra Service - decribes whole server */
110     ZebraService zs;
111
112     /* zh is our Zebra Handle - describes database session */
113     ZebraHandle zh;
114     
115     /* the database we specify in our example */
116     const char *base = "Default";
117     int argno;
118
119     nmem_init ();
120
121     yaz_log_init_file("apitest.log");
122
123     odr_input = odr_createmem (ODR_DECODE);    
124     odr_output = odr_createmem (ODR_ENCODE);    
125     
126     zs = zebra_start ("zebra.cfg");
127     if (!zs)
128     {
129         printf ("zebra_start failed; missing zebra.cfg?\n");
130         exit (1);
131     }
132     /* open Zebra */
133     zh = zebra_open (zs);
134     if (!zh)
135     {
136         printf ("zebras_open failed\n");
137         exit (1);
138     }
139     zebra_select_databases (zh, 1, &base);
140     /* Each argument to main will be a query */
141     for (argno = 1; argno < argc; argno++)
142     {
143         /* parse the query and generate an RPN structure */
144         Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
145         char setname[64];
146         int errCode;
147         int i;
148         int hits;
149         char *errString;
150         ZebraRetrievalRecord *records;
151         int noOfRecordsToFetch;
152
153         /* bad query? */
154         if (!query)
155         {
156             logf (LOG_WARN, "bad query %s\n", argv[argno]);
157             odr_reset (odr_input);
158             continue;
159         }
160         else
161         {
162             char out_str[100];
163             int r;
164 #if 1
165             r = zebra_string_norm (zh, 'w',
166                                    argv[argno], strlen(argv[argno]),
167                                    out_str, sizeof(out_str));
168             if (r >= 0)
169             {
170                 printf ("norm: '%s'\n", out_str);
171             }
172             else
173             {
174                 printf ("norm fail: %d\n", r);
175             }
176 #endif
177
178         }
179         /* result set name will be called 1,2, etc */
180         sprintf (setname, "%d", argno);
181
182         /* fire up the search */
183         zebra_search_rpn (zh, odr_input, odr_output, query, setname, &hits);
184         
185         /* status ... */
186         zebra_result (zh, &errCode, &errString);
187         
188         /* error? */
189         if (errCode)
190         {
191             printf ("Zebra Search Error %d %s\n",
192                     errCode, errString);
193             continue;
194         }
195         /* ok ... */
196         printf ("Zebra Search gave %d hits\n", hits);
197         
198         /* Deterimine number of records to fetch ... */
199         if (hits > 10)
200             noOfRecordsToFetch = 10;
201         else
202             noOfRecordsToFetch = hits;
203
204         /* reset our memory - we've finished dealing with search */
205         odr_reset (odr_input);
206         odr_reset (odr_output);
207
208         /* prepare to fetch ... */
209         records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
210         /* specify position of each record to fetch */
211         /* first one is numbered 1 and NOT 0 */
212         for (i = 0; i<noOfRecordsToFetch; i++)
213             records[i].position = i+1;
214         /* fetch them and request for GRS-1 records */
215         zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
216                                 noOfRecordsToFetch, records);
217
218         /* status ... */
219
220         zebra_result (zh, &errCode, &errString);
221
222         /* error ? */
223         if (errCode)
224         {
225             printf ("Zebra Search Error %d %s\n",
226                     errCode, errString);
227         }
228         else
229         {
230             /* inspect each record in result */
231             for (i = 0; i<noOfRecordsToFetch; i++)
232             {
233                 printf ("Record %d\n", i+1);
234                 /* error when fetching this record? */
235                 if (records[i].errCode)
236                 {
237                     printf ("  Error %d\n", records[i].errCode);
238                     continue;
239                 }
240                 /* GRS-1 record ? */
241                 if (records[i].format == VAL_GRS1)
242                 {
243                     Z_GenericRecord *grs_record =
244                         (Z_GenericRecord *) records[i].buf;
245                     printf ("  GRS-1\n");
246                     display_grs1(grs_record, 0);
247                 }
248                 else if (records[i].format == VAL_SUTRS)
249                 {
250                     printf ("  SUTRS\n");
251                     printf ("%.*s", records[i].len, records[i].buf);
252                 }
253                 /* some other record we don't handle yet... */
254                 else
255                 {
256                     printf ("  Other record (ignored)\n");
257                 }
258             }
259         }
260         /* reset our memory - we've finished dealing with present */
261         odr_reset (odr_input); 
262         odr_reset (odr_output);
263     }
264     odr_destroy (odr_input);
265     odr_destroy (odr_output);
266     zebra_close (zh);
267     zebra_stop (zs);
268     return 0;
269 }