No major changes here... Do we need this at all?
[idzebra-moved-to-github.git] / perl / zebra_api_ext.c
1 #include <assert.h>
2 #include <stdio.h>
3 #ifdef WIN32
4 #include <io.h>
5 #include <process.h>
6 #include <direct.h>
7 #else
8 #include <unistd.h>
9 #endif
10
11 #include <yaz/diagbib1.h>
12 #include "index.h"
13 #include <charmap.h>
14 #include <data1.h>
15 #include "zebra_perl.h"
16 #include "zebra_api_ext.h"
17 #include "yaz/log.h"
18 #include <yaz/pquery.h>
19 #include <yaz/sortspec.h>
20
21 void data1_print_tree(data1_handle dh, data1_node *n, FILE *out) {
22   data1_pr_tree(dh, n, stdout);
23 }
24
25 int zebra_get_shadow_enable (ZebraHandle zh) {
26   return (zh->shadow_enable);
27 }
28
29 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
30   zh->shadow_enable = value;
31 }
32
33 void init_recordGroup (recordGroup *rg) {
34     rg->groupName = NULL;
35     rg->databaseName = NULL;
36     rg->path = NULL;
37     rg->recordId = NULL;
38     rg->recordType = NULL;
39     rg->flagStoreData = -1;
40     rg->flagStoreKeys = -1;
41     rg->flagRw = 1;
42     rg->databaseNamePath = 0;
43     rg->explainDatabase = 0;
44     rg->fileVerboseLimit = 100000;
45     rg->followLinks = -1;
46 }
47
48
49 /* This is from extract.c... it seems useful, when extract_rec_in mem is 
50    called... and in general... Should be moved to somewhere else */
51 void res_get_recordGroup (ZebraHandle zh,
52                           recordGroup *rGroup,
53                           const char *ext) {
54   char gprefix[128];
55   char ext_res[128];
56     
57   if (!rGroup->groupName || !*rGroup->groupName)
58     *gprefix = '\0';
59   else
60     sprintf (gprefix, "%s.", rGroup->groupName);
61   
62   /* determine file type - depending on extension */
63   if (!rGroup->recordType) {
64     sprintf (ext_res, "%srecordType.%s", gprefix, ext);
65     if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
66       sprintf (ext_res, "%srecordType", gprefix);
67       rGroup->recordType = res_get (zh->res, ext_res);
68     }
69   }
70   /* determine match criteria */
71   if (!rGroup->recordId) {
72     sprintf (ext_res, "%srecordId.%s", gprefix, ext);
73     if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
74       sprintf (ext_res, "%srecordId", gprefix);
75       rGroup->recordId = res_get (zh->res, ext_res);
76     }
77   }
78   
79   /* determine database name */
80   if (!rGroup->databaseName) {
81     sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
82     if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
83       sprintf (ext_res, "%sdatabase", gprefix);
84       rGroup->databaseName = res_get (zh->res, ext_res);
85     }
86   }
87   if (!rGroup->databaseName)
88     rGroup->databaseName = "Default";
89
90   /* determine if explain database */
91   sprintf (ext_res, "%sexplainDatabase", gprefix);
92   rGroup->explainDatabase =
93     atoi (res_get_def (zh->res, ext_res, "0"));
94
95   /* storeData */
96   if (rGroup->flagStoreData == -1) {
97     const char *sval;
98     sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
99     if (!(sval = res_get (zh->res, ext_res))) {
100       sprintf (ext_res, "%sstoreData", gprefix);
101       sval = res_get (zh->res, ext_res);
102     }
103     if (sval)
104       rGroup->flagStoreData = atoi (sval);
105   }
106   if (rGroup->flagStoreData == -1)  rGroup->flagStoreData = 0;
107
108   /* storeKeys */
109   if (rGroup->flagStoreKeys == -1)  {
110     const char *sval;
111     
112     sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
113     sval = res_get (zh->res, ext_res);
114     if (!sval) {
115       sprintf (ext_res, "%sstoreKeys", gprefix);
116       sval = res_get (zh->res, ext_res);
117     }
118     if (!sval)  sval = res_get (zh->res, "storeKeys");
119     if (sval) rGroup->flagStoreKeys = atoi (sval);
120   }
121   if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
122   
123 }
124
125 /* ---------------------------------------------------------------------------
126   Record insert(=update), delete 
127
128   If sysno is provided, then it's used to identify the reocord.
129   If not, and match_criteria is provided, then sysno is guessed
130   If not, and a record is provided, then sysno is got from there
131
132 */
133
134 int zebra_update_record (ZebraHandle zh, 
135                          struct recordGroup *rGroup, 
136                          int sysno, const char *match, const char *fname,
137                          const char *buf, int buf_size)
138
139 {
140     int res;
141
142     if (buf_size < 1) buf_size = strlen(buf);
143
144     res=bufferExtractRecord (zh, buf, buf_size, rGroup, 
145                              0, // delete_flag
146                              0, // test_mode, 
147                              &sysno,
148                              match, fname);    
149   
150     return sysno;
151 }
152
153 int zebra_delete_record (ZebraHandle zh, 
154                          struct recordGroup *rGroup, 
155                          int sysno, const char *match, const char *fname,
156                          const char *buf, int buf_size)
157 {
158     int res;
159
160     if (buf_size < 1) buf_size = strlen(buf);
161
162     res=bufferExtractRecord (zh, buf, buf_size, rGroup, 
163                              1, // delete_flag
164                              0, // test_mode, 
165                              &sysno,
166                              match,fname);    
167     return sysno;
168 }
169
170 /* ---------------------------------------------------------------------------
171   Searching 
172
173   zebra_search_RPN is the same as zebra_search_rpn, except that read locking
174   is not mandatory. (it's repeatable now, also in zebraapi.c)
175 */
176
177 void zebra_search_RPN (ZebraHandle zh, ODR decode, ODR stream,
178                        Z_RPNQuery *query, const char *setname, int *hits)
179 {
180     zh->hits = 0;
181     *hits = 0;
182
183     if (zebra_begin_read (zh))
184         return;
185     resultSetAddRPN (zh, decode, stream, query, 
186                      zh->num_basenames, zh->basenames, setname);
187
188         zebra_end_read (zh);
189
190     *hits = zh->hits;
191 }
192
193 int zebra_search_PQF (ZebraHandle zh, 
194                       ODR odr_input, ODR odr_output, 
195                       const char *pqf_query,
196                       const char *setname)
197
198 {
199   int hits;
200   Z_RPNQuery *query;
201   query = p_query_rpn (odr_input, PROTO_Z3950, pqf_query);
202
203   if (!query) {
204     logf (LOG_WARN, "bad query %s\n", pqf_query);
205     odr_reset (odr_input);
206     return(0);
207   }
208   zebra_search_RPN (zh, odr_input, odr_output, query, setname, &hits);
209
210   odr_reset (odr_input);
211   odr_reset (odr_output);
212   
213   return(hits);
214 }
215
216 /* ---------------------------------------------------------------------------
217   Record retrieval 
218   2 phase retrieval - I didn't manage to return array of blessed references
219   to wrapped structures... it's feasible, but I'll need some time 
220   / pop - 2002-11-17
221 */
222
223 void record_retrieve(RetrievalObj *ro,
224                      ODR stream,
225                      RetrievalRecord *res,
226                      int pos) 
227 {
228   int i = pos - 1;
229
230
231   RetrievalRecordBuf *buf = 
232     (RetrievalRecordBuf *) odr_malloc(stream, sizeof(*buf));  
233
234   res->errCode    = ro->records[i].errCode;
235   res->errString  = ro->records[i].errString;
236   res->position   = ro->records[i].position;
237   res->base       = ro->records[i].base;
238   res->format     = ro->records[i].format;
239   res->buf        = buf;
240   res->buf->len   = ro->records[i].len;
241   res->buf->buf   = ro->records[i].buf;
242
243 }
244
245 /* most of the code here was copied from yaz-client */
246 void records_retrieve(ZebraHandle zh,
247                       ODR stream,
248                       const char *setname,
249                       const char *a_eset, 
250                       const char *a_schema,
251                       const char *a_format,
252                       int from,
253                       int to,
254                       RetrievalObj *res) 
255 {
256   static enum oid_value recordsyntax = VAL_SUTRS;
257   static enum oid_value schema = VAL_NONE;
258   static Z_ElementSetNames *elementSetNames = 0; 
259   static Z_RecordComposition compo;
260   static Z_ElementSetNames esn;
261   static char what[100];
262   int i;
263   int oid[OID_SIZE];
264
265   compo.which = -1;
266
267   if (from < 1) from = 1;
268   if (from > to) to = from;
269   res->noOfRecords = to - from + 1;
270
271   res->records = odr_malloc (stream, 
272                              sizeof(*res->records) * (res->noOfRecords));  
273
274   for (i = 0; i<res->noOfRecords; i++) res->records[i].position = from+i;
275
276   if (!a_eset || !*a_eset) {
277     elementSetNames = 0;
278   } else {
279     strcpy(what, a_eset);
280     esn.which = Z_ElementSetNames_generic;
281     esn.u.generic = what;
282     elementSetNames = &esn;
283   }
284
285   if (!a_schema || !*a_schema) {
286     schema = VAL_NONE;
287   } else {
288     schema = oid_getvalbyname (a_schema);
289     if (schema == VAL_NONE) {
290       logf(LOG_WARN,"unknown schema '%s'",a_schema);
291     }
292   }
293
294   
295   if (!a_format || !*a_format) {
296     recordsyntax = VAL_SUTRS;
297   } else {
298     recordsyntax = oid_getvalbyname (a_format);
299     if (recordsyntax == VAL_NONE) {
300       logf(LOG_WARN,"unknown record syntax '%s', using SUTRS",a_schema);
301       recordsyntax = VAL_SUTRS;
302     }
303   }
304
305   if (schema != VAL_NONE) {
306     oident prefschema;
307
308     prefschema.proto  = PROTO_Z3950;
309     prefschema.oclass = CLASS_SCHEMA;
310     prefschema.value  = schema;
311
312     compo.which = Z_RecordComp_complex;
313     compo.u.complex = (Z_CompSpec *)
314       odr_malloc(stream, sizeof(*compo.u.complex));
315     compo.u.complex->selectAlternativeSyntax = (bool_t *) 
316       odr_malloc(stream, sizeof(bool_t));
317     *compo.u.complex->selectAlternativeSyntax = 0;
318
319     compo.u.complex->generic = (Z_Specification *)
320       odr_malloc(stream, sizeof(*compo.u.complex->generic));
321     compo.u.complex->generic->schema = (Odr_oid *)
322       odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
323
324     if (!compo.u.complex->generic->schema) {
325       /* OID wasn't a schema! Try record syntax instead. */
326       prefschema.oclass = CLASS_RECSYN;
327       compo.u.complex->generic->schema = (Odr_oid *)
328         odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
329     }
330
331     if (!elementSetNames) {
332       compo.u.complex->generic->elementSpec = 0;
333     } else {
334       compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
335         odr_malloc(stream, sizeof(Z_ElementSpec));
336       compo.u.complex->generic->elementSpec->which =
337         Z_ElementSpec_elementSetName;
338       compo.u.complex->generic->elementSpec->u.elementSetName =
339         elementSetNames->u.generic;
340     }
341     compo.u.complex->num_dbSpecific = 0;
342     compo.u.complex->dbSpecific = 0;
343     compo.u.complex->num_recordSyntax = 0;
344     compo.u.complex->recordSyntax = 0;
345   } 
346   else if (elementSetNames) {
347     compo.which = Z_RecordComp_simple;
348     compo.u.simple = elementSetNames;
349   }
350
351   if (compo.which == -1) {
352     api_records_retrieve (zh, stream, setname, 
353                             NULL, 
354                             recordsyntax,
355                             res->noOfRecords, res->records);
356   } else {
357     api_records_retrieve (zh, stream, setname, 
358                             &compo, 
359                             recordsyntax,
360                             res->noOfRecords, res->records);
361   }
362
363 }
364
365 /* almost the same as zebra_records_retrieve ... but how did it work? 
366    I mean for multiple records ??? CHECK ??? */
367 void api_records_retrieve (ZebraHandle zh, ODR stream,
368                            const char *setname, Z_RecordComposition *comp,
369                            oid_value input_format, int num_recs,
370                            ZebraRetrievalRecord *recs)
371 {
372     ZebraPosSet poset;
373     int i, *pos_array;
374
375     if (!zh->res)
376     {
377         zh->errCode = 30;
378         zh->errString = odr_strdup (stream, setname);
379         return;
380     }
381     
382     zh->errCode = 0;
383
384     if (zebra_begin_read (zh))
385         return;
386
387     pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
388     for (i = 0; i<num_recs; i++)
389         pos_array[i] = recs[i].position;
390     poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
391     if (!poset)
392     {
393         logf (LOG_DEBUG, "zebraPosSetCreate error");
394         zh->errCode = 30;
395         zh->errString = nmem_strdup (stream->mem, setname);
396     }
397     else
398     {
399         for (i = 0; i<num_recs; i++)
400         {
401             if (poset[i].term)
402             {
403                 recs[i].errCode = 0;
404                 recs[i].format = VAL_SUTRS;
405                 recs[i].len = strlen(poset[i].term);
406                 recs[i].buf = poset[i].term;
407                 recs[i].base = poset[i].db;
408             
409             }
410             else if (poset[i].sysno)
411             {
412               /* changed here ??? CHECK ??? */
413               char *b;
414                 recs[i].errCode =
415                     zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
416                                         stream, input_format, comp,
417                                         &recs[i].format, 
418                                         &b,
419                                         &recs[i].len,
420                                         &recs[i].base);
421                 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
422                 memcpy(recs[i].buf, b, recs[i].len);
423                 recs[i].errString = NULL;
424             }
425             else
426             {
427                 char num_str[20];
428
429                 sprintf (num_str, "%d", pos_array[i]);  
430                 zh->errCode = 13;
431                 zh->errString = odr_strdup (stream, num_str);
432                 break;
433             }
434
435         }
436         zebraPosSetDestroy (zh, poset, num_recs);
437     }
438     zebra_end_read (zh);
439     xfree (pos_array);
440 }
441
442
443 /* ---------------------------------------------------------------------------
444   Sort - a simplified interface, with optional read locks.
445 */
446 int sort (ZebraHandle zh, 
447           ODR stream,
448           const char *sort_spec,
449           const char *output_setname,
450           const char **input_setnames
451           ) 
452 {
453   int num_input_setnames = 0;
454   int sort_status = 0;
455   Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
456
457   /* we can do this, since the typemap code for char** will 
458      put a NULL at the end of list */
459     while (input_setnames[num_input_setnames]) num_input_setnames++;
460
461     if (zebra_begin_read (zh))
462         return;
463
464     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
465                    output_setname, sort_sequence, &sort_status);
466
467     zebra_end_read(zh);
468     return (sort_status);
469 }