+ 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;
+
+ yaz_log(log_level, "zebra_compact");
+ ZEBRA_CHECK_HANDLE(zh);
+ 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;
+}
+
+#define ZEBRA_CHECK_DICT 1
+#define ZEBRA_CHECK_ISAM 2
+
+static ZEBRA_RES zebra_record_check(ZebraHandle zh, Record rec,
+ zint *no_keys, int message_limit,
+ unsigned flags,
+ zint *no_long_dict_entries,
+ zint *no_failed_dict_lookups,
+ zint *no_invalid_keys,
+ zint *no_invalid_dict_infos,
+ zint *no_invalid_isam_entries)
+{
+ ZEBRA_RES res = ZEBRA_OK;
+ zebra_rec_keys_t keys = zebra_rec_keys_open();
+ zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
+ rec->size[recInfo_delKeys], 0);
+
+ *no_keys = 0;
+ if (!zebra_rec_keys_rewind(keys))
+ {
+ ;
+ }
+ else
+ {
+ size_t slen;
+ const char *str;
+ struct it_key key_in;
+ NMEM nmem = nmem_create();
+
+ while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
+ {
+ int do_fail = 0;
+ int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
+ char ord_buf[IT_MAX_WORD+20];
+ int ord_len = key_SU_encode(ord, ord_buf);
+ char *info = 0;
+
+ (*no_keys)++;
+
+ if (key_in.len < 2 || key_in.len > IT_KEY_LEVEL_MAX)
+ {
+ res = ZEBRA_FAIL;
+ (*no_invalid_keys)++;
+ if (*no_invalid_keys <= message_limit)
+ {
+ do_fail = 1;
+ yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
+ ": unexpected key length %d",
+ rec->sysno, key_in.len);
+ }
+ }
+ if (ord_len + slen >= sizeof(ord_buf)-1)
+ {
+ res = ZEBRA_FAIL;
+ (*no_long_dict_entries)++;
+ if (*no_long_dict_entries <= message_limit)
+ {
+ do_fail = 1;
+ /* so bad it can not fit into our ord_buf */
+ yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
+ ": long dictionary entry %d + %d",
+ rec->sysno, ord_len, (int) slen);
+ }
+ continue;
+ }
+ memcpy(ord_buf + ord_len, str, slen);
+ ord_buf[ord_len + slen] = '\0';
+ if (ord_len + slen >= IT_MAX_WORD)
+ {
+ res = ZEBRA_FAIL;
+ (*no_long_dict_entries)++;
+ if (*no_long_dict_entries <= message_limit)
+ {
+ do_fail = 1;
+ yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
+ ": long dictionary entry %d + %d",
+ rec->sysno, (int) ord_len, (int) slen);
+ }
+ }
+ if ((flags & ZEBRA_CHECK_DICT) == 0)
+ continue;
+ info = dict_lookup(zh->reg->dict, ord_buf);
+ if (!info)
+ {
+ res = ZEBRA_FAIL;
+ (*no_failed_dict_lookups)++;
+ if (*no_failed_dict_lookups <= message_limit)
+ {
+ do_fail = 1;
+ yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
+ ": term do not exist in dictionary", rec->sysno);
+ }
+ }
+ else if (flags & ZEBRA_CHECK_ISAM)
+ {
+ ISAM_P pos;
+
+ if (*info != sizeof(pos))
+ {
+ res = ZEBRA_FAIL;
+ (*no_invalid_dict_infos)++;
+ if (*no_invalid_dict_infos <= message_limit)
+ {
+ do_fail = 1;
+ yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
+ ": long dictionary entry %d + %d",
+ rec->sysno, (int) ord_len, (int) slen);
+ }
+ }
+ else
+ {
+ int scope = 1;
+ memcpy(&pos, info+1, sizeof(pos));
+ if (zh->reg->isamb)
+ {
+ ISAMB_PP ispt = isamb_pp_open(zh->reg->isamb, pos,
+ scope);
+ if (!ispt)
+ {
+ res = ZEBRA_FAIL;
+ (*no_invalid_isam_entries)++;
+ if (*no_invalid_isam_entries <= message_limit)
+ {
+ do_fail = 1;
+ yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
+ ": isamb_pp_open entry " ZINT_FORMAT
+ " not found",
+ rec->sysno, pos);
+ }
+ }
+ else if (zh->m_staticrank)
+ {
+ isamb_pp_close(ispt);
+ }
+ else
+ {
+ struct it_key until_key;
+ struct it_key isam_key;
+ int r;
+ int i = 0;
+
+ until_key.len = key_in.len - 1;
+ for (i = 0; i < until_key.len; i++)
+ until_key.mem[i] = key_in.mem[i+1];
+
+ if (until_key.mem[0] == 0)
+ until_key.mem[0] = rec->sysno;
+ r = isamb_pp_forward(ispt, &isam_key, &until_key);
+ if (r != 1)
+ {
+ res = ZEBRA_FAIL;
+ (*no_invalid_isam_entries)++;
+ if (*no_invalid_isam_entries <= message_limit)
+ {
+ do_fail = 1;
+ yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
+ ": isamb_pp_forward " ZINT_FORMAT
+ " returned no entry",
+ rec->sysno, pos);
+ }
+ }
+ else
+ {
+ int cmp = key_compare(&until_key, &isam_key);
+ if (cmp != 0)
+ {
+ res = ZEBRA_FAIL;
+ (*no_invalid_isam_entries)++;
+ if (*no_invalid_isam_entries
+ <= message_limit)
+ {
+ do_fail = 1;
+ yaz_log(YLOG_WARN, "Record "
+ ZINT_FORMAT
+ ": isamb_pp_forward "
+ ZINT_FORMAT
+ " returned different entry",
+ rec->sysno, pos);
+
+ key_logdump_txt(YLOG_LOG,
+ &until_key,
+ "until");
+
+ key_logdump_txt(YLOG_LOG,
+ &isam_key,
+ "isam");
+
+ }
+ }
+ }
+ isamb_pp_close(ispt);
+ }
+
+ }
+ }
+ }
+ if (do_fail)
+ {
+ zebra_it_key_str_dump(zh, &key_in, str,
+ slen, nmem, YLOG_LOG);
+ nmem_reset(nmem);
+ }
+ }
+ nmem_destroy(nmem);
+ }
+ zebra_rec_keys_close(keys);
+ return res;
+}
+
+ZEBRA_RES zebra_register_check(ZebraHandle zh, const char *spec)
+{
+ ZEBRA_RES res = ZEBRA_FAIL;
+ unsigned flags = 0;
+ int message_limit = 10;
+
+ if (!spec || *spec == '\0'
+ || !strcmp(spec, "dict") || !strcmp(spec, "default"))
+ flags = ZEBRA_CHECK_DICT;
+ else if (!strcmp(spec, "isam") || !strcmp(spec, "full"))
+ flags = ZEBRA_CHECK_DICT|ZEBRA_CHECK_ISAM;
+ else if (!strcmp(spec, "quick"))
+ flags = 0;
+ else
+ return ZEBRA_FAIL;
+
+ yaz_log(YLOG_LOG, "zebra_register_check begin flags=%u message_limit=%d",
+ flags, message_limit);
+ if (zebra_begin_read(zh) == ZEBRA_OK)
+ {
+ zint no_records_total = 0;
+ zint no_records_fail = 0;
+ zint total_keys = 0;
+
+ if (zh->reg)
+ {
+ Record rec = rec_get_root(zh->reg->records);
+
+ zint no_long_dict_entries = 0;
+ zint no_failed_dict_lookups = 0;
+ zint no_invalid_keys = 0;
+ zint no_invalid_dict_infos = 0;
+ zint no_invalid_isam_entries = 0;
+
+ res = ZEBRA_OK;
+ while (rec)
+ {
+ Record r1;
+ zint no_keys;
+
+ if (zebra_record_check(zh, rec, &no_keys, message_limit,
+ flags,
+ &no_long_dict_entries,
+ &no_failed_dict_lookups,
+ &no_invalid_keys,
+ &no_invalid_dict_infos,
+ &no_invalid_isam_entries
+ )
+ != ZEBRA_OK)
+ {
+ res = ZEBRA_FAIL;
+ no_records_fail++;
+ }
+
+ r1 = rec_get_next(zh->reg->records, rec);
+ rec_free(&rec);
+ rec = r1;
+ no_records_total++;
+ total_keys += no_keys;
+ }
+ yaz_log(YLOG_LOG, "records total: " ZINT_FORMAT,
+ no_records_total);
+ yaz_log(YLOG_LOG, "records fail: " ZINT_FORMAT,
+ no_records_fail);
+ yaz_log(YLOG_LOG, "total keys: " ZINT_FORMAT,
+ total_keys);
+ yaz_log(YLOG_LOG, "long dict entries: " ZINT_FORMAT,
+ no_long_dict_entries);
+ if (flags & ZEBRA_CHECK_DICT)
+ {
+ yaz_log(YLOG_LOG, "failed dict lookups: " ZINT_FORMAT,
+ no_failed_dict_lookups);
+ yaz_log(YLOG_LOG, "invalid dict infos: " ZINT_FORMAT,
+ no_invalid_dict_infos);
+ }
+ if (flags & ZEBRA_CHECK_ISAM)
+ yaz_log(YLOG_LOG, "invalid isam entries: " ZINT_FORMAT,
+ no_invalid_isam_entries);
+ }
+ zebra_end_read(zh);
+ }
+ yaz_log(YLOG_LOG, "zebra_register_check end ret=%d", res);
+ return res;
+}
+
+void zebra_result(ZebraHandle zh, int *code, char **addinfo)
+{
+ yaz_log(log_level, "zebra_result");
+ if (zh)
+ {
+ *code = zh->errCode;
+ *addinfo = zh->errString;
+ }
+ else
+ {
+ *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+ *addinfo ="ZebraHandle is NULL";
+ }
+}
+
+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)
+{
+ yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
+ ZEBRA_CHECK_HANDLE(zh);
+ assert(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)
+{
+ yaz_log(log_level, "zebra_record_encoding");
+ ZEBRA_CHECK_HANDLE(zh);
+ 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)
+{
+ assert(name);
+ assert(value);
+ yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
+ ASSERTZH;
+ 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)
+{
+ yaz_log(log_level, "zebra_trans_no");
+ ASSERTZH;
+ return zh->trans_no;
+}
+
+int zebra_get_shadow_enable(ZebraHandle zh)
+{
+ yaz_log(log_level, "zebra_get_shadow_enable");
+ ASSERTZH;
+ return zh->shadow_enable;
+}
+
+void zebra_set_shadow_enable(ZebraHandle zh, int value)
+{
+ yaz_log(log_level, "zebra_set_shadow_enable %d",value);
+ ASSERTZH;
+ zh->shadow_enable = value;
+}
+
+ZEBRA_RES zebra_add_record(ZebraHandle zh,
+ const char *buf, int buf_size)
+{
+ return zebra_update_record(zh, action_update,
+ 0 /* record type */,
+ 0 /* sysno */ ,
+ 0 /* match */,
+ 0 /* fname */,
+ buf, buf_size);
+}
+
+ZEBRA_RES zebra_update_record(ZebraHandle zh,
+ enum zebra_recctrl_action_t action,
+ const char *recordType,
+ zint *sysno, const char *match,
+ const char *fname,
+ const char *buf, int buf_size)
+{
+ ZEBRA_RES res;
+
+ ZEBRA_CHECK_HANDLE(zh);
+
+ 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 = zebra_buffer_extract_record(zh, buf, buf_size,
+ action,
+ recordType,
+ sysno,
+ match,
+ fname);
+ if (zebra_end_trans(zh) != ZEBRA_OK)
+ {
+ yaz_log(YLOG_WARN, "zebra_end_trans failed");
+ res = ZEBRA_FAIL;
+ }
+ 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;
+
+
+ ZEBRA_CHECK_HANDLE(zh);
+
+ odr = odr_createmem(ODR_ENCODE);
+
+ assert(pqf_query);
+ assert(setname);
+
+ yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
+
+ query = p_query_rpn(odr, 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;
+
+ ZEBRA_CHECK_HANDLE(zh);
+ 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);