+ zebra_register_close (zh->service, zh->reg);
+ zh->reg = 0;
+
+ yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
+ ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
+ zh->records_processed, zh->records_inserted,
+ zh->records_updated, zh->records_deleted);
+
+ status->processed = (int) zh->records_processed;
+ status->inserted = (int) zh->records_inserted;
+ status->updated = (int) zh->records_updated;
+ status->deleted = (int) zh->records_deleted;
+
+ zebra_get_state (zh, &val, &seqno);
+ if (val != 'd')
+ {
+ BFiles bfs = bfs_create (rval, zh->path_reg);
+ yaz_log (YLOG_DEBUG, "deleting shadow val=%c", val);
+ bf_commitClean (bfs, rval);
+ bfs_destroy (bfs);
+ }
+ if (!rval)
+ seqno++;
+ zebra_set_state (zh, 'o', seqno);
+
+ zebra_unlock (zh->lock_shadow);
+ zebra_unlock (zh->lock_normal);
+
+ }
+#if HAVE_SYS_TIMES_H
+ times (&zh->tms2);
+ yaz_log (log_level, "user/system: %ld/%ld",
+ (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
+ (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
+
+ status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
+ status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
+#endif
+ return ZEBRA_OK;
+}
+
+int zebra_repository_update (ZebraHandle zh, const char *path)
+{
+ ASSERTZH;
+ assert(path);
+ zh->errCode = 0;
+ yaz_log (log_level, "updating %s", path);
+ repositoryUpdate (zh, path);
+ return zh->errCode;
+}
+
+int zebra_repository_delete (ZebraHandle zh, const char *path)
+{
+ ASSERTZH;
+ assert(path);
+ zh->errCode = 0;
+ yaz_log (log_level, "deleting %s", path);
+ repositoryDelete (zh, path);
+ return zh->errCode;
+}
+
+int zebra_repository_show (ZebraHandle zh, const char *path)
+{
+ ASSERTZH;
+ assert(path);
+ yaz_log(log_level, "zebra_repository_show");
+ zh->errCode = 0;
+ repositoryShow (zh, path);
+ return zh->errCode;
+}
+
+static int zebra_commit_ex(ZebraHandle zh, int clean_only)
+{
+ int seqno;
+ char val;
+ const char *rval;
+ BFiles bfs;
+ ASSERTZH;
+ zh->errCode = 0;
+
+ zebra_select_default_database(zh);
+ if (!zh->res)
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ return -1;
+ }
+ rval = res_get (zh->res, "shadow");
+ if (!rval)
+ {
+ yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
+ return 0;
+ }
+
+ zebra_lock_w (zh->lock_normal);
+ zebra_lock_r (zh->lock_shadow);
+
+ bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
+
+ zebra_get_state (zh, &val, &seqno);
+
+ if (rval && *rval)
+ bf_cache (bfs, rval);
+ if (bf_commitExists (bfs))
+ {
+ if (clean_only)
+ zebra_set_state (zh, 'd', seqno);
+ else
+ {
+ zebra_set_state (zh, 'c', seqno);
+
+ yaz_log (YLOG_DEBUG, "commit start");
+ bf_commitExec (bfs);
+#ifndef WIN32
+ sync ();
+#endif
+ }
+ yaz_log (YLOG_DEBUG, "commit clean");
+ bf_commitClean (bfs, rval);
+ seqno++;
+ zebra_set_state (zh, 'o', seqno);
+ }
+ else
+ {
+ yaz_log (log_level, "nothing to commit");
+ }
+ bfs_destroy (bfs);
+
+ zebra_unlock (zh->lock_shadow);
+ zebra_unlock (zh->lock_normal);
+ return 0;
+}
+
+ZEBRA_RES zebra_clean(ZebraHandle zh)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_clean");
+ return zebra_commit_ex(zh, 1);
+}
+
+ZEBRA_RES zebra_commit(ZebraHandle zh)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_commit");
+ return zebra_commit_ex(zh, 0);
+}
+
+ZEBRA_RES zebra_init(ZebraHandle zh)
+{
+ const char *rval;
+ BFiles bfs = 0;
+ ASSERTZH;
+ yaz_log(log_level, "zebra_init");
+ zh->errCode = 0;
+
+ zebra_select_default_database(zh);
+ if (!zh->res)
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ return ZEBRA_FAIL;
+ }
+ rval = res_get (zh->res, "shadow");
+
+ bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
+ if (!bfs)
+ return ZEBRA_FAIL;
+ if (rval && *rval)
+ bf_cache (bfs, rval);
+
+ bf_reset (bfs);
+ bfs_destroy (bfs);
+ zebra_set_state (zh, 'o', 0);
+ return ZEBRA_OK;
+}
+
+ZEBRA_RES zebra_compact(ZebraHandle zh)
+{
+ BFiles bfs;
+ ASSERTZH;
+ yaz_log(log_level, "zebra_compact");
+ zh->errCode = 0;
+ if (!zh->res)
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ return ZEBRA_FAIL;
+ }
+ bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
+ inv_compact (bfs);
+ bfs_destroy (bfs);
+ return ZEBRA_OK;
+}
+
+void zebra_result(ZebraHandle zh, int *code, char **addinfo)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_result");
+ *code = zh->errCode;
+ *addinfo = zh->errString;
+}
+
+void zebra_shadow_enable(ZebraHandle zh, int value)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_shadow_enable");
+ zh->errCode = 0;
+ zh->shadow_enable = value;
+}
+
+ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
+{
+ ASSERTZH;
+ assert(encoding);
+ yaz_log(log_level, "zebra_octet_term_encoding");
+ zh->errCode = 0;
+
+ if (zh->iconv_to_utf8 != 0)
+ yaz_iconv_close(zh->iconv_to_utf8);
+ if (zh->iconv_from_utf8 != 0)
+ yaz_iconv_close(zh->iconv_from_utf8);
+
+ zh->iconv_to_utf8 =
+ yaz_iconv_open ("UTF-8", encoding);
+ if (zh->iconv_to_utf8 == 0)
+ yaz_log (YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
+ zh->iconv_from_utf8 =
+ yaz_iconv_open (encoding, "UTF-8");
+ if (zh->iconv_to_utf8 == 0)
+ yaz_log (YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
+
+ return ZEBRA_OK;
+}
+
+ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_record_encoding");
+ zh->errCode = 0;
+ xfree(zh->record_encoding);
+ zh->record_encoding = 0;
+ if (encoding)
+ zh->record_encoding = xstrdup (encoding);
+ return ZEBRA_OK;
+}
+
+void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
+{
+ ASSERTZH;
+ assert(name);
+ assert(value);
+ yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
+ zh->errCode = 0;
+ res_set(zh->res, name, value);
+}
+
+const char *zebra_get_resource(ZebraHandle zh,
+ const char *name, const char *defaultvalue)
+{
+ const char *v;
+ ASSERTZH;
+ assert(name);
+ assert(defaultvalue);
+ v = res_get_def (zh->res, name, (char *)defaultvalue);
+ zh->errCode = 0;
+ yaz_log(log_level, "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_level, "zebra_trans_no");
+ return zh->trans_no;
+}
+
+int zebra_get_shadow_enable (ZebraHandle zh)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_get_shadow_enable");
+ return zh->shadow_enable;
+}
+
+void zebra_set_shadow_enable (ZebraHandle zh, int value)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_set_shadow_enable %d",value);
+ zh->shadow_enable = value;
+}
+
+/* Used by Perl API.. Added the record buffer dup to zebra_records_retrieve
+ so that it's identicical to the original api_records_retrieve */
+void api_records_retrieve (ZebraHandle zh, ODR stream,
+ const char *setname, Z_RecordComposition *comp,
+ oid_value input_format, int num_recs,
+ ZebraRetrievalRecord *recs)
+{
+ zebra_records_retrieve(zh, stream, setname, comp, input_format,
+ num_recs, recs);
+}
+
+/* ---------------------------------------------------------------------------
+ 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)
+{
+ SYSNO sysno = 0;
+ return zebra_update_record(zh, 0, &sysno, 0, 0, buf, buf_size, 0);
+}
+
+ZEBRA_RES zebra_insert_record (ZebraHandle zh,
+ const char *recordType,
+ SYSNO *sysno, const char *match,
+ const char *fname,
+ const char *buf, int buf_size, int force_update)
+{
+ ZEBRA_RES res;
+ ASSERTZH;
+ assert(sysno);
+ assert(buf);
+ yaz_log(log_level, "zebra_insert_record sysno=" ZINT_FORMAT, *sysno);
+
+ if (buf_size < 1)
+ buf_size = strlen(buf);
+
+ if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ 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;
+}
+
+ZEBRA_RES zebra_update_record (ZebraHandle zh,
+ const char *recordType,
+ SYSNO* sysno, const char *match,
+ const char *fname,
+ const char *buf, int buf_size,
+ int force_update)
+{
+ ZEBRA_RES res;
+ ASSERTZH;
+ assert(sysno);
+ assert(buf);
+
+ yaz_log(log_level, "zebra_update_record sysno=" ZINT_FORMAT, *sysno);
+
+ if (buf_size < 1) buf_size = strlen(buf);
+
+ if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ 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;
+}
+
+ZEBRA_RES zebra_delete_record (ZebraHandle zh,
+ const char *recordType,
+ SYSNO *sysno, const char *match,
+ const char *fname,
+ const char *buf, int buf_size,
+ int force_update)
+{
+ ZEBRA_RES res;
+ ASSERTZH;
+ assert(sysno);
+ assert(buf);
+ yaz_log(log_level, "zebra_delete_record sysno=" ZINT_FORMAT, *sysno);
+
+ if (buf_size < 1) buf_size = strlen(buf);
+
+ if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ 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
+*/
+
+ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
+ const char *setname, zint *numhits)
+{
+ zint hits = 0;
+ ZEBRA_RES res = ZEBRA_OK;
+ Z_RPNQuery *query;
+ ODR odr = odr_createmem(ODR_ENCODE);
+ ASSERTZH;
+ assert(pqf_query);
+ assert(setname);
+
+ yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
+
+ query = p_query_rpn (odr, PROTO_Z3950, pqf_query);
+
+ if (!query)
+ {
+ yaz_log (YLOG_WARN, "bad query %s\n", pqf_query);
+ zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
+ res = ZEBRA_FAIL;
+ }
+ else
+ res = zebra_search_RPN(zh, odr, query, setname, &hits);
+
+ odr_destroy(odr);
+
+ yaz_log(log_level, "Hits: " ZINT_FORMAT, hits);
+
+ if (numhits)
+ *numhits = hits;
+
+ return res;
+}
+
+/* ---------------------------------------------------------------------------
+ Sort - a simplified interface, with optional read locks.
+*/
+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;
+ ASSERTZH;
+ assert(stream);
+ assert(sort_spec);
+ assert(output_setname);
+ assert(input_setnames);
+ sort_sequence = yaz_sort_spec (stream, sort_spec);
+ yaz_log(log_level, "sort (FIXME) ");
+ if (!sort_sequence)
+ {
+ yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
+ zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
+ 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;
+}
+
+/* ---------------------------------------------------------------------------
+ Get BFS for Zebra system (to make alternative storage methods)
+*/
+struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
+{
+ if (zh && zh->reg)
+ return zh->reg->bfs;
+ return 0;
+}
+
+
+/* ---------------------------------------------------------------------------
+ Set limit for search/scan
+*/
+ZEBRA_RES zebra_set_limit(ZebraHandle zh, int exclude_flag, zint *ids)
+{
+ ASSERTZH;
+ zebra_limit_destroy(zh->m_limit);
+ zh->m_limit = zebra_limit_create(exclude_flag, ids);
+ return ZEBRA_OK;