11 #include <yaz/diagbib1.h>
15 #include "zebra_perl.h"
16 #include "zebra_api_ext.h"
18 #include <yaz/pquery.h>
19 #include <yaz/sortspec.h>
21 void data1_print_tree(data1_handle dh, data1_node *n, FILE *out) {
22 data1_pr_tree(dh, n, stdout);
25 int zebra_get_shadow_enable (ZebraHandle zh) {
26 return (zh->shadow_enable);
29 void zebra_set_shadow_enable (ZebraHandle zh, int value) {
30 zh->shadow_enable = value;
33 void init_recordGroup (recordGroup *rg) {
35 rg->databaseName = NULL;
38 rg->recordType = NULL;
39 rg->flagStoreData = -1;
40 rg->flagStoreKeys = -1;
42 rg->databaseNamePath = 0;
43 rg->explainDatabase = 0;
44 rg->fileVerboseLimit = 100000;
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,
57 if (!rGroup->groupName || !*rGroup->groupName)
60 sprintf (gprefix, "%s.", rGroup->groupName);
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);
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);
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);
87 if (!rGroup->databaseName)
88 rGroup->databaseName = "Default";
90 /* determine if explain database */
91 sprintf (ext_res, "%sexplainDatabase", gprefix);
92 rGroup->explainDatabase =
93 atoi (res_get_def (zh->res, ext_res, "0"));
96 if (rGroup->flagStoreData == -1) {
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);
104 rGroup->flagStoreData = atoi (sval);
106 if (rGroup->flagStoreData == -1) rGroup->flagStoreData = 0;
109 if (rGroup->flagStoreKeys == -1) {
112 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
113 sval = res_get (zh->res, ext_res);
115 sprintf (ext_res, "%sstoreKeys", gprefix);
116 sval = res_get (zh->res, ext_res);
118 if (!sval) sval = res_get (zh->res, "storeKeys");
119 if (sval) rGroup->flagStoreKeys = atoi (sval);
121 if (rGroup->flagStoreKeys == -1) rGroup->flagStoreKeys = 0;
125 /* ---------------------------------------------------------------------------
126 Record insert(=update), delete
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
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)
142 if (buf_size < 1) buf_size = strlen(buf);
144 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
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)
160 if (buf_size < 1) buf_size = strlen(buf);
162 res=bufferExtractRecord (zh, buf, buf_size, rGroup,
170 /* ---------------------------------------------------------------------------
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)
177 void zebra_search_RPN (ZebraHandle zh, ODR decode, ODR stream,
178 Z_RPNQuery *query, const char *setname, int *hits)
183 if (zebra_begin_read (zh))
185 resultSetAddRPN (zh, decode, stream, query,
186 zh->num_basenames, zh->basenames, setname);
193 int zebra_search_PQF (ZebraHandle zh,
194 ODR odr_input, ODR odr_output,
195 const char *pqf_query,
201 query = p_query_rpn (odr_input, PROTO_Z3950, pqf_query);
204 logf (LOG_WARN, "bad query %s\n", pqf_query);
205 odr_reset (odr_input);
208 zebra_search_RPN (zh, odr_input, odr_output, query, setname, &hits);
210 odr_reset (odr_input);
211 odr_reset (odr_output);
216 /* ---------------------------------------------------------------------------
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
223 void record_retrieve(RetrievalObj *ro,
225 RetrievalRecord *res,
231 RetrievalRecordBuf *buf =
232 (RetrievalRecordBuf *) odr_malloc(stream, sizeof(*buf));
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;
240 res->buf->len = ro->records[i].len;
241 res->buf->buf = ro->records[i].buf;
245 /* most of the code here was copied from yaz-client */
246 void records_retrieve(ZebraHandle zh,
250 const char *a_schema,
251 const char *a_format,
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];
267 if (from < 1) from = 1;
268 if (from > to) to = from;
269 res->noOfRecords = to - from + 1;
271 res->records = odr_malloc (stream,
272 sizeof(*res->records) * (res->noOfRecords));
274 for (i = 0; i<res->noOfRecords; i++) res->records[i].position = from+i;
276 if (!a_eset || !*a_eset) {
279 strcpy(what, a_eset);
280 esn.which = Z_ElementSetNames_generic;
281 esn.u.generic = what;
282 elementSetNames = &esn;
285 if (!a_schema || !*a_schema) {
288 schema = oid_getvalbyname (a_schema);
289 if (schema == VAL_NONE) {
290 logf(LOG_WARN,"unknown schema '%s'",a_schema);
295 if (!a_format || !*a_format) {
296 recordsyntax = VAL_SUTRS;
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;
305 if (schema != VAL_NONE) {
308 prefschema.proto = PROTO_Z3950;
309 prefschema.oclass = CLASS_SCHEMA;
310 prefschema.value = schema;
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;
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));
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));
331 if (!elementSetNames) {
332 compo.u.complex->generic->elementSpec = 0;
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;
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;
346 else if (elementSetNames) {
347 compo.which = Z_RecordComp_simple;
348 compo.u.simple = elementSetNames;
351 if (compo.which == -1) {
352 api_records_retrieve (zh, stream, setname,
355 res->noOfRecords, res->records);
357 api_records_retrieve (zh, stream, setname,
360 res->noOfRecords, res->records);
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)
378 zh->errString = odr_strdup (stream, setname);
384 if (zebra_begin_read (zh))
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);
393 logf (LOG_DEBUG, "zebraPosSetCreate error");
395 zh->errString = nmem_strdup (stream->mem, setname);
399 for (i = 0; i<num_recs; i++)
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;
410 else if (poset[i].sysno)
412 /* changed here ??? CHECK ??? */
415 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
416 stream, input_format, comp,
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;
429 sprintf (num_str, "%d", pos_array[i]);
431 zh->errString = odr_strdup (stream, num_str);
436 zebraPosSetDestroy (zh, poset, num_recs);
443 /* ---------------------------------------------------------------------------
444 Sort - a simplified interface, with optional read locks.
446 int sort (ZebraHandle zh,
448 const char *sort_spec,
449 const char *output_setname,
450 const char **input_setnames
453 int num_input_setnames = 0;
455 Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
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++;
461 if (zebra_begin_read (zh))
464 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
465 output_setname, sort_sequence, &sort_status);
468 return (sort_status);