9 #include <yaz/yaz-util.h>
10 #include <yaz/proto.h>
13 #include <yaz/pquery.h>
15 #include "zebra_perl.h"
16 #include "../recctrl/perlread.h"
23 yaz_log_init_prefix ("ZebraPerl");
24 yaz_log (LOG_LOG, "Zebra API initialized");
29 yaz_log (LOG_LOG, "Zebra API destroyed");
32 /* Logging facilities from yaz */
33 void logMsg (int level, const char *message) {
34 logf(level, "%s", message);
37 /* debug tool for data1... maybe should be moved to data1.
38 perl can't really pass filehandles */
39 void data1_print_tree(data1_handle dh, data1_node *n) {
40 data1_pr_tree(dh, n, stdout);
43 /* ---------------------------------------------------------------------------
45 2 phase retrieval - I didn't manage to return array of blessed references
46 to wrapped structures... it's feasible, but I'll need some time
50 void record_retrieve(RetrievalObj *ro,
58 RetrievalRecordBuf *buf =
59 (RetrievalRecordBuf *) odr_malloc(stream, sizeof(*buf));
61 res->errCode = ro->records[i].errCode;
62 if (ro->records[i].errString) {
63 res->errString = odr_strdup(stream, ro->records[i].errString);
67 res->position = ro->records[i].position;
68 res->base = odr_strdup(stream, ro->records[i].base);
69 res->format = (char *)
70 yaz_z3950_oid_value_to_str(ro->records[i].format, CLASS_RECSYN);
72 res->buf->len = ro->records[i].len;
73 res->buf->buf = ro->records[i].buf;
74 res->score = ro->records[i].score;
75 res->sysno = ro->records[i].sysno;
81 /* most of the code here was copied from yaz-client */
82 void records_retrieve(ZebraHandle zh,
92 static enum oid_value recordsyntax = VAL_SUTRS;
93 static enum oid_value schema = VAL_NONE;
94 static Z_ElementSetNames *elementSetNames = 0;
95 static Z_RecordComposition compo;
96 static Z_ElementSetNames esn;
97 static char what[100];
103 if (from < 1) from = 1;
104 if (from > to) to = from;
105 res->noOfRecords = to - from + 1;
107 res->records = odr_malloc (stream,
108 sizeof(*res->records) * (res->noOfRecords));
110 for (i = 0; i<res->noOfRecords; i++) res->records[i].position = from+i;
112 if (!a_eset || !*a_eset) {
115 strcpy(what, a_eset);
116 esn.which = Z_ElementSetNames_generic;
117 esn.u.generic = what;
118 elementSetNames = &esn;
121 if (!a_schema || !*a_schema) {
124 schema = oid_getvalbyname (a_schema);
125 if (schema == VAL_NONE) {
126 logf(LOG_WARN,"unknown schema '%s'",a_schema);
131 if (!a_format || !*a_format) {
132 recordsyntax = VAL_SUTRS;
134 recordsyntax = oid_getvalbyname (a_format);
135 if (recordsyntax == VAL_NONE) {
136 logf(LOG_WARN,"unknown record syntax '%s', using SUTRS",a_schema);
137 recordsyntax = VAL_SUTRS;
141 if (schema != VAL_NONE) {
144 prefschema.proto = PROTO_Z3950;
145 prefschema.oclass = CLASS_SCHEMA;
146 prefschema.value = schema;
148 compo.which = Z_RecordComp_complex;
149 compo.u.complex = (Z_CompSpec *)
150 odr_malloc(stream, sizeof(*compo.u.complex));
151 compo.u.complex->selectAlternativeSyntax = (bool_t *)
152 odr_malloc(stream, sizeof(bool_t));
153 *compo.u.complex->selectAlternativeSyntax = 0;
155 compo.u.complex->generic = (Z_Specification *)
156 odr_malloc(stream, sizeof(*compo.u.complex->generic));
157 compo.u.complex->generic->which = Z_Schema_oid;
158 compo.u.complex->generic->schema.oid = (Odr_oid *)
159 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
160 if (!compo.u.complex->generic->schema.oid)
162 /* OID wasn't a schema! Try record syntax instead. */
163 prefschema.oclass = CLASS_RECSYN;
164 compo.u.complex->generic->schema.oid = (Odr_oid *)
165 odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
167 if (!elementSetNames)
168 compo.u.complex->generic->elementSpec = 0;
171 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
172 odr_malloc(stream, sizeof(Z_ElementSpec));
173 compo.u.complex->generic->elementSpec->which =
174 Z_ElementSpec_elementSetName;
175 compo.u.complex->generic->elementSpec->u.elementSetName =
176 elementSetNames->u.generic;
178 compo.u.complex->num_dbSpecific = 0;
179 compo.u.complex->dbSpecific = 0;
180 compo.u.complex->num_recordSyntax = 0;
181 compo.u.complex->recordSyntax = 0;
183 else if (elementSetNames) {
184 compo.which = Z_RecordComp_simple;
185 compo.u.simple = elementSetNames;
188 if (compo.which == -1) {
189 api_records_retrieve (zh, stream, setname,
192 res->noOfRecords, res->records);
194 api_records_retrieve (zh, stream, setname,
197 res->noOfRecords, res->records);
202 int zebra_cql2pqf (cql_transform_t ct,
203 const char *query, char *res, int len) {
206 const char *addinfo = "";
207 CQL_parser cp = cql_parser_create();
209 if (status = cql_parser_string(cp, query)) {
210 cql_parser_destroy(cp);
214 if (cql_transform_buf(ct, cql_parser_result(cp), res, len)) {
215 status = cql_transform_error(ct, &addinfo);
216 logf (LOG_WARN,"Transform error %d %s\n", status, addinfo ? addinfo : "");
217 cql_parser_destroy(cp);
221 cql_parser_destroy(cp);
225 void zebra_scan_PQF (ZebraHandle zh,
228 const char *pqf_query)
230 Z_AttributesPlusTerm *zapt;
233 oid_value attributeset;
234 ZebraScanEntry *entries;
238 "scan req: pos:%d, num:%d, partial:%d",
239 so->position, so->num_entries, so->is_partial);
241 zapt = p_query_scan (stream, PROTO_Z3950, &attrsetid, pqf_query);
243 oidname = yaz_z3950oid_to_str (attrsetid, &class);
244 logf (LOG_DEBUG, "Attributreset: %s", oidname);
245 attributeset = oid_getvalbyname(oidname);
248 logf (LOG_WARN, "bad query %s\n", pqf_query);
253 so->entries = (scanEntry *)
254 odr_malloc (stream, sizeof(so->entries) * (so->num_entries));
257 zebra_scan (zh, stream, zapt, attributeset,
258 &so->position, &so->num_entries,
259 (ZebraScanEntry **) &so->entries, &so->is_partial);
262 "scan res: pos:%d, num:%d, partial:%d",
263 so->position, so->num_entries, so->is_partial);
266 scanEntry *getScanEntry(ScanObj *so, int pos) {
267 return (&so->entries[pos-1]);
271 void Filter_store_buff (struct perl_context *context, char *buff, size_t len)
279 XPUSHs(context->filterRef);
280 XPUSHs(sv_2mortal(newSVpv(buff, len)));
282 call_method("_store_buff", 0);
290 /* The "file" manipulation function wrappers */
291 int grs_perl_readf(struct perl_context *context, size_t len)
294 char *buf = (char *) xmalloc (len+1);
295 r = (*context->readf)(context->fh, buf, len);
296 if (r > 0) Filter_store_buff (context, buf, r);
301 int grs_perl_readline(struct perl_context *context)
304 char *buf = (char *) xmalloc (4096);
307 while ((r = (*context->readf)(context->fh,p,1)) && (p-buf < 4095)) {
309 if (*(p-1) == 10) break;
314 if (p != buf) Filter_store_buff (context, buf, p - buf);
319 char grs_perl_getc(struct perl_context *context)
323 if ((r = (*context->readf)(context->fh,p,1))) {
330 off_t grs_perl_seekf(struct perl_context *context, off_t offset)
332 return ((*context->seekf)(context->fh, offset));
335 off_t grs_perl_tellf(struct perl_context *context)
337 return ((*context->tellf)(context->fh));
340 void grs_perl_endf(struct perl_context *context, off_t offset)
342 (*context->endf)(context->fh, offset);
345 /* Get pointers from the context. Easyer to wrap this by SWIG */
346 data1_handle *grs_perl_get_dh(struct perl_context *context)
348 return(&context->dh);
351 NMEM *grs_perl_get_mem(struct perl_context *context)
353 return(&context->mem);
356 /* Set the result in the context */
357 void grs_perl_set_res(struct perl_context *context, data1_node *n)