11 #include <yaz/diagbib1.h>
15 #include "zebra_perl.h"
16 #include "zebra_api_ext.h"
18 #include <yaz/pquery.h>
20 #include <yaz/sortspec.h>
22 void data1_print_tree(data1_handle dh, data1_node *n, FILE *out) {
23 data1_pr_tree(dh, n, stdout);
26 int zebra_get_shadow_enable (ZebraHandle zh) {
27 return (zh->shadow_enable);
30 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
31 zh->shadow_enable = value;
34 void init_recordGroup (recordGroup *rg) {
36 rg->databaseName = NULL;
39 rg->recordType = NULL;
40 rg->flagStoreData = -1;
41 rg->flagStoreKeys = -1;
43 rg->databaseNamePath = 0;
44 rg->explainDatabase = 0;
45 rg->fileVerboseLimit = 100000;
50 /* This is from extract.c... it seems useful, when extract_rec_in mem is
51 called... and in general... Should be moved to somewhere else */
52 void res_get_recordGroup (ZebraHandle zh,
58 if (!rGroup->groupName || !*rGroup->groupName)
61 sprintf (gprefix, "%s.", rGroup->groupName);
63 /* determine file type - depending on extension */
64 if (!rGroup->recordType) {
65 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
66 if (!(rGroup->recordType = res_get (zh->res, ext_res))) {
67 sprintf (ext_res, "%srecordType", gprefix);
68 rGroup->recordType = res_get (zh->res, ext_res);
71 /* determine match criteria */
72 if (!rGroup->recordId) {
73 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
74 if (!(rGroup->recordId = res_get (zh->res, ext_res))) {
75 sprintf (ext_res, "%srecordId", gprefix);
76 rGroup->recordId = res_get (zh->res, ext_res);
80 /* determine database name */
81 if (!rGroup->databaseName) {
82 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
83 if (!(rGroup->databaseName = res_get (zh->res, ext_res))) {
84 sprintf (ext_res, "%sdatabase", gprefix);
85 rGroup->databaseName = res_get (zh->res, ext_res);
88 if (!rGroup->databaseName)
89 rGroup->databaseName = "Default";
91 /* determine if explain database */
92 sprintf (ext_res, "%sexplainDatabase", gprefix);
93 rGroup->explainDatabase =
94 atoi (res_get_def (zh->res, ext_res, "0"));
97 if (rGroup->flagStoreData == -1) {
99 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
100 if (!(sval = res_get (zh->res, ext_res))) {
101 sprintf (ext_res, "%sstoreData", gprefix);
102 sval = res_get (zh->res, ext_res);
105 rGroup->flagStoreData = atoi (sval);
107 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
110 if (rGroup->flagStoreKeys == -1) {
113 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
114 sval = res_get (zh->res, ext_res);
116 sprintf (ext_res, "%sstoreKeys", gprefix);
117 sval = res_get (zh->res, ext_res);
119 if (!sval) sval = res_get (zh->res, "storeKeys");
120 if (sval) rGroup->flagStoreKeys = atoi (sval);
122 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
126 int zebra_trans_processed(ZebraTransactionStatus s) {
127 return (s.processed);
130 /* ---------------------------------------------------------------------------
131 Record insert(=update), delete
133 If sysno is provided, then it's used to identify the reocord.
134 If not, and match_criteria is provided, then sysno is guessed
135 If not, and a record is provided, then sysno is got from there
138 int zebra_update_record (ZebraHandle zh,
139 struct recordGroup *rGroup,
140 const char *recordType,
141 int sysno, const char *match, const char *fname,
142 const char *buf, int buf_size)
147 if (buf_size < 1) buf_size = strlen(buf);
149 zebra_begin_trans(zh);
150 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
160 int zebra_delete_record (ZebraHandle zh,
161 struct recordGroup *rGroup,
162 const char *recordType,
163 int sysno, const char *match, const char *fname,
164 const char *buf, int buf_size)
168 if (buf_size < 1) buf_size = strlen(buf);
170 zebra_begin_trans(zh);
171 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
181 /* ---------------------------------------------------------------------------
184 zebra_search_RPN is the same as zebra_search_rpn, except that read locking
185 is not mandatory. (it's repeatable now, also in zebraapi.c)
188 void zebra_search_RPN (ZebraHandle zh, ODR decode, ODR stream,
189 Z_RPNQuery *query, const char *setname, int *hits)
194 if (zebra_begin_read (zh))
196 resultSetAddRPN (zh, decode, stream, query,
197 zh->num_basenames, zh->basenames, setname);
204 int zebra_search_PQF (ZebraHandle zh,
205 ODR odr_input, ODR odr_output,
206 const char *pqf_query,
212 query = p_query_rpn (odr_input, PROTO_Z3950, pqf_query);
215 logf (LOG_WARN, "bad query %s\n", pqf_query);
216 odr_reset (odr_input);
219 zebra_search_RPN (zh, odr_input, odr_output, query, setname, &hits);
221 odr_reset (odr_input);
222 odr_reset (odr_output);
227 int zebra_cql2pqf (cql_transform_t ct,
228 const char *query, char *res, int len) {
232 CQL_parser cp = cql_parser_create();
234 if (status = cql_transform_error(ct, &addinfo)) {
235 logf (LOG_WARN,"Transform error %d %s\n", status, addinfo ? addinfo : "");
239 if (status = cql_parser_string(cp, query))
242 if (status = cql_transform_buf(ct, cql_parser_result(cp), res, len)) {
243 logf (LOG_WARN,"Transform error %d %s\n", status, addinfo ? addinfo : "");
247 logf (LOG_LOG,"PQF:%s",res);
252 void zebra_scan_PQF (ZebraHandle zh,
255 const char *pqf_query)
257 Z_AttributesPlusTerm *zapt;
260 oid_value attributeset;
261 ZebraScanEntry *entries;
265 "scan req: pos:%d, num:%d, partial:%d",
266 so->position, so->num_entries, so->is_partial);
268 zapt = p_query_scan (stream, PROTO_Z3950, &attrsetid, pqf_query);
270 oidname = yaz_z3950oid_to_str (attrsetid, &class);
271 logf (LOG_DEBUG, "Attributreset: %s", oidname);
272 attributeset = oid_getvalbyname(oidname);
275 logf (LOG_WARN, "bad query %s\n", pqf_query);
280 so->entries = (ScanEntry *)
281 odr_malloc (stream, sizeof(so->entries) * (so->num_entries));
284 zebra_scan (zh, stream, zapt, attributeset,
285 &so->position, &so->num_entries,
286 (ZebraScanEntry **) &so->entries, &so->is_partial);
289 "scan res: pos:%d, num:%d, partial:%d",
290 so->position, so->num_entries, so->is_partial);
293 ScanEntry *getScanEntry(ScanObj *so, int pos) {
294 return (&so->entries[pos-1]);
297 /* ---------------------------------------------------------------------------
299 2 phase retrieval - I didn't manage to return array of blessed references
300 to wrapped structures... it's feasible, but I'll need some time
304 void record_retrieve(RetrievalObj *ro,
306 RetrievalRecord *res,
312 RetrievalRecordBuf *buf =
313 (RetrievalRecordBuf *) odr_malloc(stream, sizeof(*buf));
315 res->errCode = ro->records[i].errCode;
316 res->errString = ro->records[i].errString;
317 res->position = ro->records[i].position;
318 res->base = ro->records[i].base;
319 res->format = ro->records[i].format;
321 res->buf->len = ro->records[i].len;
322 res->buf->buf = ro->records[i].buf;
326 /* most of the code here was copied from yaz-client */
327 void records_retrieve(ZebraHandle zh,
331 const char *a_schema,
332 const char *a_format,
337 static enum oid_value recordsyntax = VAL_SUTRS;
338 static enum oid_value schema = VAL_NONE;
339 static Z_ElementSetNames *elementSetNames = 0;
340 static Z_RecordComposition compo;
341 static Z_ElementSetNames esn;
342 static char what[100];
348 if (from < 1) from = 1;
349 if (from > to) to = from;
350 res->noOfRecords = to - from + 1;
352 res->records = odr_malloc (stream,
353 sizeof(*res->records) * (res->noOfRecords));
355 for (i = 0; i<res->noOfRecords; i++) res->records[i].position = from+i;
357 if (!a_eset || !*a_eset) {
360 strcpy(what, a_eset);
361 esn.which = Z_ElementSetNames_generic;
362 esn.u.generic = what;
363 elementSetNames = &esn;
366 if (!a_schema || !*a_schema) {
369 schema = oid_getvalbyname (a_schema);
370 if (schema == VAL_NONE) {
371 logf(LOG_WARN,"unknown schema '%s'",a_schema);
376 if (!a_format || !*a_format) {
377 recordsyntax = VAL_SUTRS;
379 recordsyntax = oid_getvalbyname (a_format);
380 if (recordsyntax == VAL_NONE) {
381 logf(LOG_WARN,"unknown record syntax '%s', using SUTRS",a_schema);
382 recordsyntax = VAL_SUTRS;
386 if (schema != VAL_NONE) {
389 prefschema.proto = PROTO_Z3950;
390 prefschema.oclass = CLASS_SCHEMA;
391 prefschema.value = schema;
393 compo.which = Z_RecordComp_complex;
394 compo.u.complex = (Z_CompSpec *)
395 odr_malloc(stream, sizeof(*compo.u.complex));
396 compo.u.complex->selectAlternativeSyntax = (bool_t *)
397 odr_malloc(stream, sizeof(bool_t));
398 *compo.u.complex->selectAlternativeSyntax = 0;
400 compo.u.complex->generic = (Z_Specification *)
401 odr_malloc(stream, sizeof(*compo.u.complex->generic));
402 compo.u.complex->generic->which = Z_Schema_oid;
403 compo.u.complex->generic->schema.oid = (Odr_oid *)
404 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
405 if (!compo.u.complex->generic->schema.oid)
407 /* OID wasn't a schema! Try record syntax instead. */
408 prefschema.oclass = CLASS_RECSYN;
409 compo.u.complex->generic->schema.oid = (Odr_oid *)
410 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
412 if (!elementSetNames)
413 compo.u.complex->generic->elementSpec = 0;
416 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
417 odr_malloc(stream, sizeof(Z_ElementSpec));
418 compo.u.complex->generic->elementSpec->which =
419 Z_ElementSpec_elementSetName;
420 compo.u.complex->generic->elementSpec->u.elementSetName =
421 elementSetNames->u.generic;
423 compo.u.complex->num_dbSpecific = 0;
424 compo.u.complex->dbSpecific = 0;
425 compo.u.complex->num_recordSyntax = 0;
426 compo.u.complex->recordSyntax = 0;
428 else if (elementSetNames) {
429 compo.which = Z_RecordComp_simple;
430 compo.u.simple = elementSetNames;
433 if (compo.which == -1) {
434 api_records_retrieve (zh, stream, setname,
437 res->noOfRecords, res->records);
439 api_records_retrieve (zh, stream, setname,
442 res->noOfRecords, res->records);
447 int zebra_trans_no (ZebraHandle zh) {
448 return (zh->trans_no);
451 /* almost the same as zebra_records_retrieve ... but how did it work?
452 I mean for multiple records ??? CHECK ??? */
453 void api_records_retrieve (ZebraHandle zh, ODR stream,
454 const char *setname, Z_RecordComposition *comp,
455 oid_value input_format, int num_recs,
456 ZebraRetrievalRecord *recs)
464 zh->errString = odr_strdup (stream, setname);
470 if (zebra_begin_read (zh))
473 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
474 for (i = 0; i<num_recs; i++)
475 pos_array[i] = recs[i].position;
476 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
479 logf (LOG_DEBUG, "zebraPosSetCreate error");
481 zh->errString = nmem_strdup (stream->mem, setname);
485 for (i = 0; i<num_recs; i++)
490 recs[i].format = VAL_SUTRS;
491 recs[i].len = strlen(poset[i].term);
492 recs[i].buf = poset[i].term;
493 recs[i].base = poset[i].db;
496 else if (poset[i].sysno)
498 /* changed here ??? CHECK ??? */
501 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
502 stream, input_format, comp,
507 recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
508 memcpy(recs[i].buf, b, recs[i].len);
509 recs[i].errString = NULL;
515 sprintf (num_str, "%d", pos_array[i]);
517 zh->errString = odr_strdup (stream, num_str);
522 zebraPosSetDestroy (zh, poset, num_recs);
529 /* ---------------------------------------------------------------------------
530 Sort - a simplified interface, with optional read locks.
532 int sort (ZebraHandle zh,
534 const char *sort_spec,
535 const char *output_setname,
536 const char **input_setnames
539 int num_input_setnames = 0;
541 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
543 /* we can do this, since the typemap code for char** will
544 put a NULL at the end of list */
545 while (input_setnames[num_input_setnames]) num_input_setnames++;
547 if (zebra_begin_read (zh))
550 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
551 output_setname, sort_sequence, &sort_status);
554 return (sort_status);