+ ZebraMetaRecord *poset;
+ int i;
+ ZEBRA_RES ret = ZEBRA_OK;
+ zint *pos_array;
+ ASSERTZH;
+ assert(stream);
+ assert(setname);
+ assert(recs);
+ assert(num_recs>0);
+
+ yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
+
+ if (!zh->res)
+ {
+ zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
+ setname);
+ return ZEBRA_FAIL;
+ }
+
+ if (zebra_begin_read (zh) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+
+ pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
+ for (i = 0; i<num_recs; i++)
+ pos_array[i] = recs[i].position;
+ poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
+ if (!poset)
+ {
+ yaz_log (YLOG_DEBUG, "zebraPosSetCreate error");
+ zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
+ setname);
+ ret = ZEBRA_FAIL;
+ }
+ 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;
+ }
+ else if (poset[i].sysno)
+ {
+ char *buf;
+ int len;
+ zebra_snippets *hit_snippet = zebra_snippets_create();
+
+ zebra_snippets_hit_vector(zh, setname, poset[i].sysno,
+ hit_snippet);
+
+ recs[i].errCode =
+ zebra_record_fetch(zh, poset[i].sysno, poset[i].score,
+ hit_snippet,
+ stream, input_format, comp,
+ &recs[i].format, &buf, &len,
+ &recs[i].base, &recs[i].errString);
+
+ recs[i].len = len;
+ if (len > 0)
+ {
+ recs[i].buf = (char*) odr_malloc(stream, len);
+ memcpy(recs[i].buf, buf, len);
+ }
+ else
+ recs[i].buf = buf;
+ recs[i].score = poset[i].score;
+ recs[i].sysno = poset[i].sysno;
+ zebra_snippets_destroy(hit_snippet);
+ }
+ else
+ {
+ /* only need to set it once */
+ if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
+ {
+ zebra_setError_zint(zh,
+ YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
+ pos_array[i]);
+ ret = ZEBRA_FAIL;
+ break;
+ }
+ recs[i].buf = 0; /* no record and no error issued */
+ recs[i].len = 0;
+ recs[i].errCode = 0;
+ recs[i].format = VAL_NONE;
+ recs[i].sysno = 0;
+ }
+ }
+ zebra_meta_records_destroy(zh, poset, num_recs);
+ }
+ zebra_end_read (zh);
+ xfree(pos_array);
+ return ret;
+}
+
+ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
+ int *position,
+ int *num_entries, ZebraScanEntry **entries,
+ int *is_partial,
+ const char *setname)
+{
+ YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
+ Z_AttributesPlusTerm *zapt;
+ int *attributeSet;
+ ZEBRA_RES res;
+
+ if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
+ {
+ res = ZEBRA_FAIL;
+ zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
+ }
+ else
+ res = zebra_scan(zh, stream, zapt, VAL_BIB1,
+ position, num_entries, entries, is_partial,
+ setname);
+ yaz_pqf_destroy (pqf_parser);
+ return res;
+}
+
+ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
+ oid_value attributeset,
+ int *position,
+ int *num_entries, ZebraScanEntry **entries,
+ int *is_partial,
+ const char *setname)
+{
+ ZEBRA_RES res;
+ RSET limit_rset = 0;
+ ASSERTZH;
+ assert(stream);
+ assert(zapt);
+ assert(position);
+ assert(num_entries);
+ assert(is_partial);
+ assert(entries);
+ yaz_log(log_level, "zebra_scan");
+
+ if (zebra_begin_read (zh) == ZEBRA_FAIL)
+ {
+ *entries = 0;
+ *num_entries = 0;
+ return ZEBRA_FAIL;
+ }
+ if (setname)
+ {
+ limit_rset = resultSetRef(zh, setname);
+ if (!limit_rset)
+ {
+ zebra_setError(zh,
+ YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
+ setname);
+ zebra_end_read (zh);
+ return ZEBRA_FAIL;
+ }
+ }
+ res = rpn_scan (zh, stream, zapt, attributeset,
+ zh->num_basenames, zh->basenames, position,
+ num_entries, entries, is_partial, limit_rset, 0);
+ zebra_end_read (zh);
+ return res;
+}
+
+ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream,
+ int num_input_setnames, const char **input_setnames,
+ const char *output_setname,
+ Z_SortKeySpecList *sort_sequence,
+ int *sort_status)
+{
+ ZEBRA_RES res;
+ ASSERTZH;
+ assert(stream);
+ assert(num_input_setnames>0);
+ assert(input_setnames);
+ assert(sort_sequence);
+ assert(sort_status);
+ yaz_log(log_level, "zebra_sort");
+
+ if (zebra_begin_read(zh) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
+ output_setname, sort_sequence, sort_status);
+ zebra_end_read(zh);
+ return res;
+}
+
+int zebra_deleteResultSet(ZebraHandle zh, int function,
+ int num_setnames, char **setnames,
+ int *statuses)
+{
+ int i, status;
+ ASSERTZH;
+ assert(statuses);
+ yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames);
+
+ if (zebra_begin_read(zh))
+ return Z_DeleteStatus_systemProblemAtTarget;
+ switch (function)
+ {
+ case Z_DeleteResultSetRequest_list:
+ assert(num_setnames>0);
+ assert(setnames);
+ resultSetDestroy (zh, num_setnames, setnames, statuses);
+ break;
+ case Z_DeleteResultSetRequest_all:
+ resultSetDestroy (zh, -1, 0, statuses);
+ break;
+ }
+ zebra_end_read (zh);
+ status = Z_DeleteStatus_success;
+ for (i = 0; i<num_setnames; i++)
+ if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
+ status = statuses[i];
+ return status;
+}
+
+int zebra_errCode (ZebraHandle zh)
+{
+ if (zh)
+ {
+ yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
+ return zh->errCode;
+ }
+ yaz_log(log_level, "zebra_errCode: o");
+ return 0;
+}
+
+const char *zebra_errString (ZebraHandle zh)
+{
+ const char *e = 0;
+ if (zh)
+ e= diagbib1_str (zh->errCode);
+ yaz_log(log_level, "zebra_errString: %s",e);
+ return e;
+}
+
+char *zebra_errAdd (ZebraHandle zh)
+{
+ char *a = 0;
+ if (zh)
+ a= zh->errString;
+ yaz_log(log_level, "zebra_errAdd: %s",a);
+ return a;
+}
+
+ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass)
+{
+ const char *p;
+ const char *astring;
+ char u[40];
+ ZebraService zs;
+
+ ASSERTZH;
+
+ zs= zh->service;
+
+ sprintf(u, "perm.%.30s", user ? user : "anonymous");
+ p = res_get(zs->global_res, u);
+ xfree(zh->user_perm);
+ zh->user_perm = xstrdup(p ? p : "r");
+
+ /* Determine database access list */
+ astring = res_get(zs->dbaccess, user ? user : "anonymous");
+ if (astring)
+ zh->dbaccesslist = xstrdup(astring);
+ else
+ zh->dbaccesslist = 0;
+
+ /* users that don't require a password .. */
+ if (zh->user_perm && strchr(zh->user_perm, 'a'))
+ return ZEBRA_OK;
+
+ if (!zs->passwd_db || !passwd_db_auth (zs->passwd_db, user, pass))
+ return ZEBRA_OK;
+ return ZEBRA_FAIL;
+}
+
+ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database,
+ const char *record_type)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s",
+ database, record_type);
+ if (zebra_select_database(zh, database) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ return zebra_begin_trans(zh, 1);
+}
+
+ZEBRA_RES zebra_admin_import_end (ZebraHandle zh)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_admin_import_end");
+ return zebra_end_trans(zh);
+}
+
+ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
+{
+ ZEBRA_RES res = ZEBRA_OK;
+ SYSNO sysno;
+ int i;
+ ASSERTZH;
+ yaz_log(log_level, "zebra_admin_import_segment");
+
+ for (i = 0; i<segment->num_segmentRecords; i++)
+ {
+ Z_NamePlusRecord *npr = segment->segmentRecords[i];
+
+ if (npr->which == Z_NamePlusRecord_intermediateFragment)
+ {
+ Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
+ if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
+ {
+ Odr_oct *oct = fragment->u.notExternallyTagged;
+ sysno = 0;
+
+ if (zebra_update_record(zh,
+ 0, /* record Type */
+ &sysno,
+ 0, /* match */
+ 0, /* fname */
+ (const char *) oct->buf, oct->len,
+ 0) == ZEBRA_FAIL)
+ res = ZEBRA_FAIL;
+ }
+ }
+ }
+ return res;
+}
+
+ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
+ const char *rec_buf,
+ size_t rec_len,
+ const char *recid_buf, size_t recid_len,
+ int action)
+ /* 1 = insert. Fail it already exists */
+ /* 2 = replace. Fail it does not exist */
+ /* 3 = delete. Fail if does not exist */
+ /* 4 = update. Insert/replace */
+{
+ ZEBRA_RES res;
+ SYSNO sysno = 0;
+ char *rinfo = 0;
+ char recid_z[256];
+ int db_ord;
+ ASSERTZH;
+ assert(action>0 && action <=4);
+ assert(rec_buf);
+
+ yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action);
+
+ if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z))
+ {
+ zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
+ "no record ID or empty record ID");
+ return ZEBRA_FAIL;
+ }
+
+ memcpy (recid_z, recid_buf, recid_len);
+ recid_z[recid_len] = 0;
+
+ if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+
+ db_ord = zebraExplain_get_database_ord(zh->reg->zei);
+ rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord, recid_z);
+ if (rinfo)
+ {
+ if (action == 1) /* fail if insert */
+ {
+ zebra_end_trans(zh);
+ zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
+ "Cannot insert record: already exist");
+ return ZEBRA_FAIL;
+ }
+
+ memcpy (&sysno, rinfo+1, sizeof(sysno));
+ }
+ else
+ {
+ if (action == 2 || action == 3) /* fail if delete or update */
+ {
+ zebra_end_trans(zh);
+ zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
+ "Cannot delete/update record: does not exist");
+ return ZEBRA_FAIL;
+ }
+ action = 1; /* make it an insert (if it's an update).. */
+ }
+ res = buffer_extract_record (zh, rec_buf, rec_len,
+ action == 3 ? 1 : 0 /* delete flag */,
+ 0, /* test mode */
+ 0, /* recordType */
+ &sysno,
+ 0, /* match */
+ 0, /* fname */
+ 0, /* force update */
+ 1 /* allow update */
+ );
+ if (res == ZEBRA_FAIL)
+ {
+ zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
+ "Unable to parse record");
+ }
+ if (action == 1)
+ {
+ dict_insert_ord(zh->reg->matchDict, db_ord, recid_z,
+ sizeof(sysno), &sysno);
+ }
+ else if (action == 3)
+ {
+ dict_delete_ord(zh->reg->matchDict, db_ord, recid_z);
+ }
+ zebra_end_trans(zh);
+ return res;
+}
+
+int delete_w_handle(const char *info, void *handle)
+{
+ ZebraHandle zh = (ZebraHandle) handle;
+ ISAM_P pos;
+ ASSERTZH;
+
+ if (*info == sizeof(pos))
+ {
+ memcpy (&pos, info+1, sizeof(pos));
+ isamb_unlink(zh->reg->isamb, pos);
+ }
+ return 0;
+}
+
+static int delete_SU_handle(void *handle, int ord)
+{
+ ZebraHandle zh = (ZebraHandle) handle;
+ char ord_buf[20];
+ int ord_len;
+
+ ord_len = key_SU_encode (ord, ord_buf);
+ ord_buf[ord_len] = '\0';
+
+ assert (zh->reg->isamb);
+ dict_delete_subtree(zh->reg->dict, ord_buf,
+ zh, delete_w_handle);
+ return 0;
+}
+
+ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
+{
+ ZEBRA_RES ret = ZEBRA_OK;
+ ASSERTZH;
+ yaz_log(log_level, "zebra_drop_database %s", db);
+
+ if (zebra_select_database (zh, db) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ if (zebra_begin_trans (zh, 1) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ if (zh->reg->isamb)
+ {
+ int db_ord;
+ zebraExplain_curDatabase (zh->reg->zei, db);
+ db_ord = zebraExplain_get_database_ord(zh->reg->zei);
+ dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
+ 0 /* handle */, 0 /* func */);
+ zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
+ zebraExplain_removeDatabase(zh->reg->zei, zh);
+ }
+ else
+ {
+ yaz_log(YLOG_WARN, "drop database only supported for isam:b");
+ zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
+ "drop database only supported for isam:b");
+ ret = ZEBRA_FAIL;
+ }
+ zebra_end_trans (zh);
+ return ret;
+}
+
+ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db)
+{
+ ASSERTZH;
+ yaz_log(log_level, "zebra_create_database %s", db);
+ assert(db);
+
+ if (zebra_select_database (zh, db) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+ if (zebra_begin_trans (zh, 1))
+ return ZEBRA_FAIL;
+
+ /* announce database */
+ if (zebraExplain_newDatabase (zh->reg->zei, db, 0
+ /* explainDatabase */))
+ {
+ zebra_end_trans (zh);
+ zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
+ return ZEBRA_FAIL;
+ }
+ return zebra_end_trans (zh);
+}
+
+int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
+ const char *input_str, int input_len,
+ char *output_str, int output_len)
+{
+ WRBUF wrbuf;
+ ASSERTZH;
+ assert(input_str);
+ assert(output_str);
+ yaz_log(log_level, "zebra_string_norm ");
+
+ if (!zh->reg->zebra_maps)
+ return -1;
+ wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "",
+ input_str, input_len);
+ if (!wrbuf)
+ return -2;
+ if (wrbuf_len(wrbuf) >= output_len)
+ return -3;
+ if (wrbuf_len(wrbuf))
+ memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
+ output_str[wrbuf_len(wrbuf)] = '\0';
+ return wrbuf_len(wrbuf);
+}
+
+static void zebra_set_state (ZebraHandle zh, int val, int seqno)
+{
+ char state_fname[256];
+ char *fname;
+ long p = getpid();
+ FILE *f;
+ ASSERTZH;
+ yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno);
+
+ sprintf (state_fname, "state.%s.LCK", zh->reg_name);
+ fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
+ f = fopen (fname, "w");
+
+ yaz_log (YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
+ fprintf (f, "%c %d %ld\n", val, seqno, p);
+ fclose (f);
+ xfree(fname);
+}
+
+static void zebra_get_state (ZebraHandle zh, char *val, int *seqno)
+{
+ char state_fname[256];
+ char *fname;
+ FILE *f;
+
+ ASSERTZH;
+ yaz_log(log_level, "zebra_get_state ");
+
+ sprintf (state_fname, "state.%s.LCK", zh->reg_name);
+ fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname);
+ f = fopen (fname, "r");
+ *val = 'o';
+ *seqno = 0;
+
+ if (f)
+ {
+ fscanf (f, "%c %d", val, seqno);
+ fclose (f);
+ }
+ xfree(fname);
+}
+
+ZEBRA_RES zebra_begin_read (ZebraHandle zh)
+{
+ return zebra_begin_trans(zh, 0);
+}
+
+ZEBRA_RES zebra_end_read (ZebraHandle zh)
+{
+ return zebra_end_trans(zh);
+}
+
+static void read_res_for_transaction(ZebraHandle zh)
+{
+ const char *group = res_get(zh->res, "group");
+ const char *v;
+ /* FIXME - do we still use groups ?? */
+
+ zh->m_group = group;
+ v = res_get_prefix(zh->res, "followLinks", group, "1");
+ zh->m_follow_links = atoi(v);
+
+ zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
+ zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
+
+ v = res_get_prefix(zh->res, "storeKeys", group, "1");
+ zh->m_store_keys = atoi(v);
+
+ v = res_get_prefix(zh->res, "storeData", group, "1");
+ zh->m_store_data = atoi(v);
+
+ v = res_get_prefix(zh->res, "explainDatabase", group, "0");
+ zh->m_explain_database = atoi(v);
+
+ v = res_get_prefix(zh->res, "openRW", group, "1");
+ zh->m_flag_rw = atoi(v);
+
+ v = res_get_prefix(zh->res, "fileVerboseLimit", group, "100000");
+ zh->m_file_verbose_limit = atoi(v);
+}
+
+ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
+{
+ ASSERTZH;
+ zebra_select_default_database(zh);
+ if (!zh->res)
+ {
+ zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "zebra_begin_trans: no database selected");
+ return ZEBRA_FAIL;
+ }
+ ASSERTZHRES;
+ yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
+
+ if (zh->user_perm)
+ {
+ if (rw && !strchr(zh->user_perm, 'w'))
+ {
+ zebra_setError(
+ zh,
+ YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
+ 0);
+ return ZEBRA_FAIL;
+ }
+ }
+
+ assert (zh->res);
+ if (rw)
+ {
+ int pass;
+ int seqno = 0;
+ char val = '?';
+ const char *rval = 0;
+
+ (zh->trans_no++);
+ if (zh->trans_w_no)
+ {
+ read_res_for_transaction(zh);
+ return 0;
+ }
+ if (zh->trans_no != 1)
+ {
+ zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "zebra_begin_trans: no write trans within read");
+ return ZEBRA_FAIL;
+ }
+ if (zh->reg)
+ {
+ resultSetInvalidate (zh);
+ zebra_register_close (zh->service, zh->reg);
+ }
+ zh->trans_w_no = zh->trans_no;
+
+ zh->records_inserted = 0;
+ zh->records_updated = 0;
+ zh->records_deleted = 0;
+ zh->records_processed = 0;
+
+#if HAVE_SYS_TIMES_H
+ times (&zh->tms1);
+#endif
+ /* lock */
+ if (zh->shadow_enable)
+ rval = res_get (zh->res, "shadow");
+
+ for (pass = 0; pass < 2; pass++)
+ {
+ if (rval)
+ {
+ zebra_lock_r (zh->lock_normal);
+ zebra_lock_w (zh->lock_shadow);
+ }
+ else
+ {
+ zebra_lock_w (zh->lock_normal);
+ zebra_lock_w (zh->lock_shadow);
+ }
+
+ zebra_get_state (zh, &val, &seqno);
+ if (val == 'c')
+ {
+ yaz_log (YLOG_WARN, "previous transaction didn't finish commit");
+ zebra_unlock (zh->lock_shadow);
+ zebra_unlock (zh->lock_normal);
+ zebra_commit (zh);
+ continue;
+ }
+ else if (val == 'd')
+ {
+ if (rval)
+ {
+ BFiles bfs = bfs_create (res_get (zh->res, "shadow"),
+ zh->path_reg);
+ yaz_log (YLOG_WARN, "previous transaction didn't reach commit");
+ bf_commitClean (bfs, rval);
+ bfs_destroy (bfs);
+ }
+ else
+ {
+ yaz_log (YLOG_WARN, "your previous transaction didn't finish");
+ }
+ }
+ break;
+ }
+ if (pass == 2)
+ {
+ yaz_log (YLOG_FATAL, "zebra_begin_trans couldn't finish commit");
+ abort();
+ return ZEBRA_FAIL;
+ }
+ zebra_set_state (zh, 'd', seqno);
+
+ zh->reg = zebra_register_open(zh->service, zh->reg_name,
+ 1, rval ? 1 : 0, zh->res,
+ zh->path_reg);
+ if (zh->reg)
+ zh->reg->seqno = seqno;
+ else
+ {
+ zebra_set_state (zh, 'o', seqno);
+
+ zebra_unlock (zh->lock_shadow);
+ zebra_unlock (zh->lock_normal);
+
+ zh->trans_no--;
+ zh->trans_w_no = 0;
+
+ zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "zebra_begin_trans: cannot open register");
+ yaz_log(YLOG_FATAL, "%s", zh->errString);
+ return ZEBRA_FAIL;
+ }
+ zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
+ }
+ else
+ {
+ int dirty = 0;
+ char val;
+ int seqno;
+
+ (zh->trans_no)++;
+
+ if (zh->trans_no != 1)
+ {
+ zebra_flush_reg (zh);
+ return ZEBRA_OK;
+ }
+#if HAVE_SYS_TIMES_H
+ times (&zh->tms1);
+#endif
+ if (!zh->res)
+ {
+ (zh->trans_no)--;
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ return ZEBRA_FAIL;
+ }
+ if (!zh->lock_normal || !zh->lock_shadow)
+ {
+ (zh->trans_no)--;
+ zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ return ZEBRA_FAIL;
+ }
+ zebra_get_state (zh, &val, &seqno);
+ if (val == 'd')
+ val = 'o';
+
+ if (!zh->reg)
+ dirty = 1;
+ else if (seqno != zh->reg->seqno)
+ {
+ yaz_log (YLOG_DEBUG, "reopen seqno cur/old %d/%d",
+ seqno, zh->reg->seqno);
+ dirty = 1;
+ }
+ else if (zh->reg->last_val != val)
+ {
+ yaz_log (YLOG_DEBUG, "reopen last cur/old %d/%d",
+ val, zh->reg->last_val);
+ dirty = 1;
+ }
+ if (!dirty)
+ return ZEBRA_OK;
+
+ if (val == 'c')
+ zebra_lock_r (zh->lock_shadow);
+ else
+ zebra_lock_r (zh->lock_normal);
+
+ if (zh->reg)
+ {
+ resultSetInvalidate (zh);
+ zebra_register_close (zh->service, zh->reg);
+ }
+ zh->reg = zebra_register_open(zh->service, zh->reg_name,
+ 0, val == 'c' ? 1 : 0,
+ zh->res, zh->path_reg);
+ if (!zh->reg)
+ {
+ zebra_unlock (zh->lock_normal);
+ zebra_unlock (zh->lock_shadow);
+ zh->trans_no--;
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ return ZEBRA_FAIL;
+ }
+ zh->reg->last_val = val;
+ zh->reg->seqno = seqno;
+ }
+ read_res_for_transaction(zh);
+ return ZEBRA_OK;
+}
+
+ZEBRA_RES zebra_end_trans (ZebraHandle zh)
+{
+ ZebraTransactionStatus dummy;
+ ASSERTZH;
+ yaz_log(log_level, "zebra_end_trans");
+ return zebra_end_transaction(zh, &dummy);
+}
+
+ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status)
+{
+ char val;
+ int seqno;
+ const char *rval;
+
+ ASSERTZH;
+ assert(status);
+ yaz_log(log_level, "zebra_end_transaction");
+
+ status->processed = 0;
+ status->inserted = 0;
+ status->updated = 0;
+ status->deleted = 0;
+ status->utime = 0;
+ status->stime = 0;
+
+ if (!zh->res || !zh->reg)
+ {
+ zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "zebra_end_trans: no open transaction");
+ return ZEBRA_FAIL;
+ }
+ if (zh->trans_no != zh->trans_w_no)
+ {
+ zh->trans_no--;
+ if (zh->trans_no != 0)
+ return ZEBRA_OK;
+
+ /* release read lock */
+
+ zebra_unlock (zh->lock_normal);
+ zebra_unlock (zh->lock_shadow);
+ }
+ else
+ { /* release write lock */
+ zh->trans_no--;
+ zh->trans_w_no = 0;
+
+ yaz_log (YLOG_DEBUG, "zebra_end_trans");
+ rval = res_get (zh->res, "shadow");
+
+ zebraExplain_runNumberIncrement (zh->reg->zei, 1);
+
+ zebra_flush_reg (zh);
+
+ resultSetInvalidate (zh);
+
+ 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);
+ yaz_log (log_level, "updating %s", path);
+ repositoryUpdate (zh, path);
+ return 0;
+}
+
+int zebra_repository_delete (ZebraHandle zh, const char *path)
+{
+ ASSERTZH;
+ assert(path);
+ yaz_log (log_level, "deleting %s", path);
+ repositoryDelete (zh, path);
+ return 0;
+}
+
+int zebra_repository_show (ZebraHandle zh, const char *path)
+{
+ ASSERTZH;
+ assert(path);
+ yaz_log(log_level, "zebra_repository_show");
+ repositoryShow (zh, path);
+ return 0;
+}
+
+static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
+{
+ int seqno;
+ char val;
+ const char *rval;
+ BFiles bfs;
+ ASSERTZH;
+
+ zebra_select_default_database(zh);
+ if (!zh->res)
+ {
+ zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
+ return ZEBRA_FAIL;
+ }
+ rval = res_get (zh->res, "shadow");
+ if (!rval)
+ {
+ yaz_log (YLOG_WARN, "Cannot perform commit - No shadow area defined");
+ return ZEBRA_OK;
+ }
+
+ 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 ZEBRA_OK;
+}
+
+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");
+
+ zebra_select_default_database(zh);
+ if (!zh->res)
+ {
+ zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+ "cannot select default database");
+ return ZEBRA_FAIL;
+ }
+ rval = res_get (zh->res, "shadow");
+
+ bfs = bfs_create (res_get (zh->res, "register"), zh->path_reg);
+ if (!bfs)
+ {
+ zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
+ 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");
+ 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->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 %s", encoding);
+
+ 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");
+ 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);
+ 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);
+ v = res_get_def (zh->res, name, (char *)defaultvalue);
+ 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)
+{
+ return zebra_update_record(zh, 0, 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(buf);
+
+ yaz_log(log_level, "zebra_update_record");
+ if (sysno)
+ yaz_log(log_level, " 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 *hits)
+{
+ zint lhits = 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, &lhits);
+
+ odr_destroy(odr);
+
+ yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
+
+ if (hits)
+ *hits = lhits;
+
+ 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 complement_flag, zint *ids)
+{
+ ASSERTZH;
+ zebra_limit_destroy(zh->m_limit);
+ zh->m_limit = zebra_limit_create(complement_flag, ids);
+ return ZEBRA_OK;
+}
+
+/*
+ Set Error code + addinfo
+*/
+void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
+{
+ zh->errCode = code;
+ nmem_reset(zh->nmem_error);
+ zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
+}
+
+void zebra_setError_zint(ZebraHandle zh, int code, zint i)
+{
+ char vstr[60];
+ sprintf(vstr, ZINT_FORMAT, i);
+
+ zh->errCode = code;
+ nmem_reset(zh->nmem_error);
+ zh->errString = nmem_strdup(zh->nmem_error, vstr);
+}