c9eac7f48d8687a8538f50ab748dde1d08e477da
[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.10 2000-09-06 08:59:36 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     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     /* Each argument to main will be a query */
140     for (argno = 1; argno < argc; argno++)
141     {
142         /* parse the query and generate an RPN structure */
143         Z_RPNQuery *query = p_query_rpn (odr_input, PROTO_Z3950, argv[argno]);
144         char setname[64];
145         int errCode;
146         int i;
147         const char *errString;
148         char *errAdd;
149         ZebraRetrievalRecord *records;
150         int noOfRecordsToFetch;
151
152         /* bad query? */
153         if (!query)
154         {
155             logf (LOG_WARN, "bad query %s\n", argv[argno]);
156             odr_reset (odr_input);
157             continue;
158         }
159         else
160         {
161             char out_str[100];
162             int r;
163 #if 1
164             r = zebra_string_norm (zh, 'w',
165                                    argv[argno], strlen(argv[argno]),
166                                    out_str, sizeof(out_str));
167             if (r >= 0)
168             {
169                 printf ("norm: '%s'\n", out_str);
170             }
171             else
172             {
173                 printf ("norm fail: %d\n", r);
174             }
175 #endif
176
177         }
178         /* result set name will be called 1,2, etc */
179         sprintf (setname, "%d", argno);
180
181         /* fire up the search */
182         zebra_search_rpn (zh, odr_input, odr_output, query, 1, &base, setname);
183         
184         /* status ... */
185         errCode = zebra_errCode (zh);
186         errString = zebra_errString (zh);
187         errAdd = zebra_errAdd (zh);
188         
189         /* error? */
190         if (errCode)
191         {
192             printf ("Zebra Search Error %d %s %s\n",
193                     errCode, errString, errAdd ? errAdd : "");
194             continue;
195         }
196         /* ok ... */
197         printf ("Zebra Search gave %d hits\n", zebra_hits (zh));
198         
199         /* Deterimine number of records to fetch ... */
200         if (zebra_hits(zh) > 10)
201             noOfRecordsToFetch = 10;
202         else
203             noOfRecordsToFetch = zebra_hits(zh);
204
205         /* reset our memory - we've finished dealing with search */
206         odr_reset (odr_input);
207         odr_reset (odr_output);
208
209         /* prepare to fetch ... */
210         records = odr_malloc (odr_input, sizeof(*records) * noOfRecordsToFetch);
211         /* specify position of each record to fetch */
212         /* first one is numbered 1 and NOT 0 */
213         for (i = 0; i<noOfRecordsToFetch; i++)
214             records[i].position = i+1;
215         /* fetch them and request for GRS-1 records */
216         zebra_records_retrieve (zh, odr_input, setname, NULL, VAL_SUTRS,
217                                 noOfRecordsToFetch, records);
218
219         /* status ... */
220         errCode = zebra_errCode (zh);
221         errString = zebra_errString (zh);
222         errAdd = zebra_errAdd (zh);
223         
224         /* error ? */
225         if (errCode)
226         {
227             printf ("Zebra Search Error %d %s %s\n",
228                     errCode, errString, errAdd ? errAdd : "");
229         }
230         else
231         {
232             /* inspect each record in result */
233             for (i = 0; i<noOfRecordsToFetch; i++)
234             {
235                 printf ("Record %d\n", i+1);
236                 /* error when fetching this record? */
237                 if (records[i].errCode)
238                 {
239                     printf ("  Error %d\n", records[i].errCode);
240                     continue;
241                 }
242                 /* GRS-1 record ? */
243                 if (records[i].format == VAL_GRS1)
244                 {
245                     Z_GenericRecord *grs_record =
246                         (Z_GenericRecord *) records[i].buf;
247                     printf ("  GRS-1\n");
248                     display_grs1(grs_record, 0);
249                 }
250                 else if (records[i].format == VAL_SUTRS)
251                 {
252                     printf ("  SUTRS\n");
253                     printf ("%.*s", records[i].len, records[i].buf);
254                 }
255                 /* some other record we don't handle yet... */
256                 else
257                 {
258                     printf ("  Other record (ignored)\n");
259                 }
260             }
261         }
262         /* reset our memory - we've finished dealing with present */
263         odr_reset (odr_input); 
264         odr_reset (odr_output);
265     }
266     odr_destroy (odr_input);
267     odr_destroy (odr_output);
268     zebra_close (zh);
269     zebra_stop (zs);
270     return 0;
271 }