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