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