+ yaz_log(LOG_API,"zebra_get_resource %s:%s",name,v);
+ return v;
+}
+
+/* moved from zebra_api_ext.c by pop */
+/* FIXME: Should this really be public??? -Heikki */
+
+int zebra_trans_no (ZebraHandle zh)
+{
+ ASSERTZH;
+ yaz_log(LOG_API,"zebra_trans_no");
+ return zh->trans_no;
+}
+
+int zebra_get_shadow_enable (ZebraHandle zh)
+{
+ yaz_log(LOG_API,"zebra_get_shadow_enable");
+ return (zh->shadow_enable);
+}
+
+int zebra_set_shadow_enable (ZebraHandle zh, int value)
+{
+ yaz_log(LOG_API,"zebra_set_shadow_enable %d",value);
+ zh->shadow_enable = value;
+ return 0;
+}
+
+/* almost the same as zebra_records_retrieve ... but how did it work?
+ I mean for multiple records ??? CHECK ??? */
+void api_records_retrieve (ZebraHandle zh, ODR stream,
+ const char *setname, Z_RecordComposition *comp,
+ oid_value input_format, int num_recs,
+ ZebraRetrievalRecord *recs)
+{
+ ZebraPosSet poset;
+ int i, *pos_array;
+ yaz_log(LOG_API,"api_records_retrieve s=%s n=%d",setname,num_recs);
+
+ if (!zh->res)
+ {
+ zh->errCode = 30;
+ zh->errString = odr_strdup (stream, setname);
+ return;
+ }
+
+ zh->errCode = 0;
+
+ if (zebra_begin_read (zh))
+ return;
+
+ pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
+ for (i = 0; i<num_recs; i++)
+ pos_array[i] = recs[i].position;
+ poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
+ if (!poset)
+ {
+ logf (LOG_DEBUG, "zebraPosSetCreate error");
+ zh->errCode = 30;
+ zh->errString = nmem_strdup (stream->mem, setname);
+ }
+ else
+ {
+ for (i = 0; i<num_recs; i++)
+ {
+ if (poset[i].term)
+ {
+ recs[i].errCode = 0;
+ recs[i].format = VAL_SUTRS;
+ recs[i].len = strlen(poset[i].term);
+ recs[i].buf = poset[i].term;
+ recs[i].base = poset[i].db;
+ recs[i].sysno = 0;
+
+ }
+ else if (poset[i].sysno)
+ {
+ /* changed here ??? CHECK ??? */
+ char *b;
+ recs[i].errCode =
+ zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
+ stream, input_format, comp,
+ &recs[i].format,
+ &b,
+ &recs[i].len,
+ &recs[i].base);
+ recs[i].buf = (char *) odr_malloc(stream,recs[i].len);
+ memcpy(recs[i].buf, b, recs[i].len);
+ recs[i].errString = 0; /* Hmmm !!! we should get this */
+ recs[i].sysno = poset[i].sysno;
+ recs[i].score = poset[i].score;
+ }
+ else
+ {
+ char num_str[20];
+
+ sprintf (num_str, "%d", pos_array[i]);
+ zh->errCode = 13;
+ zh->errString = odr_strdup (stream, num_str);
+ break;
+ }
+
+ }
+ zebraPosSetDestroy (zh, poset, num_recs);
+ }
+ zebra_end_read (zh);
+ xfree (pos_array);
+}
+
+
+/* ---------------------------------------------------------------------------
+ Record insert(=update), delete
+
+ If sysno is provided, then it's used to identify the record.
+ If not, and match_criteria is provided, then sysno is guessed
+ If not, and a record is provided, then sysno is got from there
+NOTE: Now returns 0 at success and updates sysno, which is an int*
+ 20-jun-2003 Heikki
+*/
+
+int zebra_add_record(ZebraHandle zh,
+ const char *buf, int buf_size)
+{
+ int sysno = 0;
+ return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
+}
+
+int zebra_insert_record (ZebraHandle zh,
+ const char *recordType,
+ int *sysno, const char *match, const char *fname,
+ const char *buf, int buf_size)
+{
+ int res;
+ yaz_log(LOG_API,"zebra_insert_record sysno=%d", *sysno);
+
+ if (buf_size < 1) buf_size = strlen(buf);
+
+ zebra_begin_trans(zh, 1);
+ res = buffer_extract_record (zh, buf, buf_size,
+ 0, /* delete_flag */
+ 0, /* test_mode */
+ recordType,
+ sysno,
+ match, fname,
+ 0,
+ 0); /* allow_update */
+ zebra_end_trans(zh);
+ return res;
+}
+
+int zebra_update_record (ZebraHandle zh,
+ const char *recordType,
+ int* sysno, const char *match, const char *fname,
+ const char *buf, int buf_size,
+ int force_update)
+{
+ int res;
+
+ yaz_log(LOG_API,"zebra_update_record sysno=%d", *sysno);
+
+ if (buf_size < 1) buf_size = strlen(buf);
+
+ zebra_begin_trans(zh, 1);
+ res = buffer_extract_record (zh, buf, buf_size,
+ 0, /* delete_flag */
+ 0, /* test_mode */
+ recordType,
+ sysno,
+ match, fname,
+ force_update,
+ 1); /* allow_update */
+ zebra_end_trans(zh);
+ return res;
+}
+
+int zebra_delete_record (ZebraHandle zh,
+ const char *recordType,
+ int *sysno, const char *match, const char *fname,
+ const char *buf, int buf_size,
+ int force_update)
+{
+ int res;
+ yaz_log(LOG_API,"zebra_delete_record sysno=%d", *sysno);
+
+ if (buf_size < 1) buf_size = strlen(buf);
+
+ zebra_begin_trans(zh, 1);
+ res = buffer_extract_record (zh, buf, buf_size,
+ 1, /* delete_flag */
+ 0, /* test_mode */
+ recordType,
+ sysno,
+ match,fname,
+ force_update,
+ 1); /* allow_update */
+ zebra_end_trans(zh);
+ return res;
+}
+
+/* ---------------------------------------------------------------------------
+ Searching
+*/
+
+int zebra_search_PQF (ZebraHandle zh, const char *pqf_query,
+ const char *setname, int *numhits)
+{
+ int hits = 0;
+ int res=-1;
+ Z_RPNQuery *query;
+ ODR odr = odr_createmem(ODR_ENCODE);
+
+ yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query);
+
+ query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
+
+ if (!query)
+ yaz_log (LOG_WARN, "bad query %s\n", pqf_query);
+ else
+ res=zebra_search_RPN (zh, odr, query, setname, &hits);
+
+ odr_destroy(odr);
+
+ yaz_log(LOG_API,"Hits: %d",hits);
+
+ if (numhits)
+ *numhits=hits;
+
+ return res;
+}
+
+/* ---------------------------------------------------------------------------
+ Sort - a simplified interface, with optional read locks.
+ FIXME - This is a horrible name, will conflict with half the applications
+*/
+int zebra_sort_by_specstr (ZebraHandle zh,
+ ODR stream,
+ const char *sort_spec,
+ const char *output_setname,
+ const char **input_setnames)
+{
+ int num_input_setnames = 0;
+ int sort_status = 0;
+ Z_SortKeySpecList *sort_sequence = yaz_sort_spec (stream, sort_spec);
+ yaz_log(LOG_API,"sort (FIXME) ");
+ if (!sort_sequence)
+ {
+ logf(LOG_WARN,"invalid sort specs '%s'", sort_spec);
+ zh->errCode = 207;
+ return -1;
+ }
+
+ /* we can do this, since the perl typemap code for char** will
+ put a NULL at the end of list */
+ while (input_setnames[num_input_setnames]) num_input_setnames++;
+
+ if (zebra_begin_read (zh))
+ return -1;
+
+ resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
+ output_setname, sort_sequence, &sort_status);
+
+ zebra_end_read(zh);
+ return sort_status;