X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=index%2Fzebraapi.c;h=ce5615425ae517a07be32c736df65b4d862ef0aa;hb=c154b6ebab028dc512c43403864a8e4a7fc2b178;hp=5352457ae28fb7d59d5d5f01ba5b1916d1d3f27b;hpb=89895e559c05c44e538a732b1e072e28413ddc1f;p=idzebra-moved-to-github.git diff --git a/index/zebraapi.c b/index/zebraapi.c index 5352457..ce56154 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.167 2005-05-09 10:16:13 adam Exp $ +/* $Id: zebraapi.c,v 1.183 2005-08-19 09:21:34 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -27,7 +27,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include -#else +#endif +#if HAVE_UNISTD_H #include #endif @@ -38,6 +39,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include +#define DEFAULT_APPROX_LIMIT 2000000000 + /* simple asserts to validate the most essential input args */ #define ASSERTZH assert(zh && zh->service) #define ASSERTZHRES assert(zh && zh->service && zh->res) @@ -69,7 +72,7 @@ static void zebra_flush_reg (ZebraHandle zh) { ASSERTZH; yaz_log(log_level, "zebra_flush_reg"); - zh->errCode = 0; + zebra_clearError(zh); zebraExplain_flush (zh->reg->zei, zh); extract_flushWriteKeys (zh, 1 /* final */); @@ -115,6 +118,7 @@ ZebraHandle zebra_open (ZebraService zs) zh->num_basenames = 0; zh->basenames = 0; + zh->approx_limit = DEFAULT_APPROX_LIMIT; zh->trans_no = 0; zh->trans_w_no = 0; @@ -122,6 +126,7 @@ ZebraHandle zebra_open (ZebraService zs) zh->lock_shadow = 0; zh->shadow_enable = 1; + zh->m_staticrank = 0; default_encoding = res_get_def(zs->global_res, "encoding", "ISO-8859-1"); @@ -149,6 +154,8 @@ ZebraHandle zebra_open (ZebraService zs) zh->m_limit = zebra_limit_create(1, 0); + zh->nmem_error = nmem_create(); + return zh; } @@ -166,33 +173,39 @@ ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res) log_level = yaz_log_module_level("zebraapi"); log_level_initialized = 1; } - - yaz_log(log_level, "zebra_start %s",configName); - assert(configName); + + yaz_log(YLOG_LOG, "zebra_start %s %s", ZEBRAVER, + configName ? configName : ""); if ((res = res_open (configName, def_res, over_res))) { + const char *passwd_plain = 0; + const char *passwd_encrypt = 0; ZebraService zh = xmalloc(sizeof(*zh)); - yaz_log (YLOG_DEBUG, "Read resources `%s'", configName); - zh->global_res = res; - zh->configName = xstrdup(configName); zh->sessions = 0; zebra_chdir (zh); zebra_mutex_cond_init (&zh->session_lock); - if (!res_get (zh->global_res, "passwd")) + passwd_plain = res_get (zh->global_res, "passwd"); + passwd_encrypt = res_get (zh->global_res, "passwd.c"); + + if (!passwd_plain && !passwd_encrypt) zh->passwd_db = NULL; else { - zh->passwd_db = passwd_db_open (); + zh->passwd_db = passwd_db_open(); if (!zh->passwd_db) yaz_log (YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed"); else - passwd_db_file (zh->passwd_db, - res_get (zh->global_res, "passwd")); + { + if (passwd_plain) + passwd_db_file_plain(zh->passwd_db, passwd_plain); + if (passwd_encrypt) + passwd_db_file_crypt(zh->passwd_db, passwd_encrypt); + } } zh->path_root = res_get (zh->global_res, "root"); zh->nmem = nmem_create(); @@ -222,22 +235,16 @@ Dict dict_open_res (BFiles bfs, const char *name, int cache, int rw, { int page_size = 4096; char resource_str[200]; - const char *v; sprintf (resource_str, "dict.%.100s.pagesize", name); assert(bfs); assert(name); - v = res_get(res, resource_str); - if (v) - { - page_size = atoi(v); + if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK) yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s", page_size, name); - } return dict_open(bfs, name, cache, rw, compact_flag, page_size); } - static struct zebra_register *zebra_register_open (ZebraService zs, const char *name, int rw, int useshadow, Res res, @@ -266,15 +273,27 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name, reg->dh = data1_createx (DATA1_FLAG_XML); if (!reg->dh) + { + xfree(reg); return 0; + } reg->bfs = bfs_create (res_get (res, "register"), reg_path); if (!reg->bfs) { data1_destroy(reg->dh); + xfree(reg); return 0; } if (useshadow) - bf_cache (reg->bfs, res_get (res, "shadow")); + { + if (bf_cache (reg->bfs, res_get (res, "shadow")) == ZEBRA_FAIL) + { + bfs_destroy(reg->bfs); + data1_destroy(reg->dh); + xfree(reg); + return 0; + } + } getcwd(cwd, sizeof(cwd)-1); profilePath = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH); @@ -295,6 +314,9 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name, reg->sortKeys.buf = 0; reg->sortKeys.buf_max = 0; +#if NATTR + reg->sortKeys.codec_handle = iscz1_start(); +#endif reg->records = 0; reg->dict = 0; @@ -308,8 +330,9 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name, reg->key_file_no = 0; reg->ptr_i = 0; - zebraRankInstall (reg, rank1_class); - zebraRankInstall (reg, rankzv_class); + zebraRankInstall (reg, rank_1_class); + zebraRankInstall (reg, rank_zv_class); + zebraRankInstall (reg, rank_static_class); recordCompression = res_get_def (res, "recordCompression", "none"); if (!strcmp (recordCompression, "none")) @@ -406,7 +429,7 @@ ZEBRA_RES zebra_admin_shutdown (ZebraHandle zh) { ASSERTZH; yaz_log(log_level, "zebra_admin_shutdown"); - zh->errCode = 0; + zebra_clearError(zh); zebra_mutex_cond_lock (&zh->service->session_lock); zh->service->stop_flag = 1; @@ -419,7 +442,7 @@ ZEBRA_RES zebra_admin_start (ZebraHandle zh) ZebraService zs; ASSERTZH; yaz_log(log_level, "zebra_admin_start"); - zh->errCode = 0; + zebra_clearError(zh); zs = zh->service; zebra_mutex_cond_lock (&zs->session_lock); zebra_mutex_cond_unlock (&zs->session_lock); @@ -459,6 +482,10 @@ static void zebra_register_close (ZebraService zs, struct zebra_register *reg) xfree(reg->keys.buf); if (reg->keys.codec_handle) iscz1_stop(reg->keys.codec_handle); +#if NATTR + if (reg->sortKeys.codec_handle) + iscz1_stop(reg->sortKeys.codec_handle); +#endif xfree(reg->key_buf); xfree(reg->name); xfree(reg); @@ -483,7 +510,6 @@ ZEBRA_RES zebra_stop(ZebraService zs) recTypeClass_destroy(zs->record_classes); nmem_destroy(zs->nmem); res_close (zs->global_res); - xfree(zs->configName); xfree(zs); return ZEBRA_OK; } @@ -540,6 +566,8 @@ ZEBRA_RES zebra_close (ZebraHandle zh) zebra_limit_destroy(zh->m_limit); + nmem_destroy(zh->nmem_error); + xfree(zh->path_reg); xfree(zh); return ZEBRA_OK; @@ -667,6 +695,19 @@ static void zebra_select_register (ZebraHandle zh, const char *new_reg) zebra_close_res(zh); } } + if (zh->res) + { + int approx = 0; + if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK) + zebra_set_approx_limit(zh, approx); + } + if (zh->res) + { + if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK) + yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank); + else + yaz_log(YLOG_LOG, "static rank unset"); + } } void map_basenames_func (void *vp, const char *name, const char *value) @@ -726,8 +767,6 @@ void map_basenames (ZebraHandle zh, ODR stream, yaz_log(log_level, "map_basenames "); assert(stream); - zh->errCode = 0; - info.zh = zh; info.num_bases = *num_bases; @@ -757,7 +796,6 @@ ZEBRA_RES zebra_select_database (ZebraHandle zh, const char *basename) ASSERTZH; yaz_log(log_level, "zebra_select_database %s",basename); assert(basename); - zh->errCode = 0; return zebra_select_databases (zh, 1, &basename); } @@ -842,6 +880,14 @@ ZEBRA_RES zebra_select_databases (ZebraHandle zh, int num_bases, return ZEBRA_OK; } +ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit) +{ + if (approx_limit == 0) + approx_limit = DEFAULT_APPROX_LIMIT; + zh->approx_limit = approx_limit; + return ZEBRA_OK; +} + ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query, const char *setname, zint *hits) { @@ -852,7 +898,7 @@ ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query, assert(hits); assert(setname); yaz_log(log_level, "zebra_search_rpn"); - zh->errCode = 0; + zebra_clearError(zh); zh->hits = 0; *hits = 0; @@ -866,11 +912,11 @@ ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query, return r; } -ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, - const char *setname, - Z_RecordComposition *comp, - oid_value input_format, int num_recs, - ZebraRetrievalRecord *recs) +ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream, + const char *setname, + Z_RecordComposition *comp, + oid_value input_format, int num_recs, + ZebraRetrievalRecord *recs) { ZebraMetaRecord *poset; int i; @@ -883,17 +929,15 @@ ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, assert(num_recs>0); yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs); - zh->errCode = 0; + zebra_clearError(zh); if (!zh->res) { - zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST; - zh->errString = odr_strdup(stream, setname); + zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, + setname); return ZEBRA_FAIL; } - zh->errCode = 0; - if (zebra_begin_read (zh) == ZEBRA_FAIL) return ZEBRA_FAIL; @@ -904,8 +948,8 @@ ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, if (!poset) { yaz_log (YLOG_DEBUG, "zebraPosSetCreate error"); - zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST; - zh->errString = nmem_strdup (stream->mem, setname); + zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, + setname); ret = ZEBRA_FAIL; } else @@ -924,11 +968,18 @@ ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, { 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) { @@ -939,16 +990,24 @@ ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, recs[i].buf = buf; recs[i].score = poset[i].score; recs[i].sysno = poset[i].sysno; + zebra_snippets_destroy(hit_snippet); } else { - char num_str[20]; - - sprintf (num_str, ZINT_FORMAT, pos_array[i]); - zh->errCode = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE; - zh->errString = odr_strdup (stream, num_str); - ret = ZEBRA_FAIL; - break; + /* 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); @@ -961,7 +1020,8 @@ ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query, int *position, int *num_entries, ZebraScanEntry **entries, - int *is_partial) + int *is_partial, + const char *setname) { YAZ_PQF_Parser pqf_parser = yaz_pqf_create (); Z_AttributesPlusTerm *zapt; @@ -975,18 +1035,21 @@ ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query, } else res = zebra_scan(zh, stream, zapt, VAL_BIB1, - position, num_entries, entries, is_partial); + 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) +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); @@ -995,16 +1058,28 @@ ZEBRA_RES zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, assert(is_partial); assert(entries); yaz_log(log_level, "zebra_scan"); - zh->errCode = 0; + zebra_clearError(zh); 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, 0, 0); + num_entries, entries, is_partial, limit_rset, 0); zebra_end_read (zh); return res; } @@ -1015,6 +1090,7 @@ ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream, Z_SortKeySpecList *sort_sequence, int *sort_status) { + ZEBRA_RES res; ASSERTZH; assert(stream); assert(num_input_setnames>0); @@ -1022,12 +1098,13 @@ ZEBRA_RES zebra_sort (ZebraHandle zh, ODR stream, assert(sort_sequence); assert(sort_status); yaz_log(log_level, "zebra_sort"); - zh->errCode = 0; - if (zebra_begin_read (zh) == ZEBRA_FAIL) + zebra_clearError(zh); + if (zebra_begin_read(zh) == ZEBRA_FAIL) return ZEBRA_FAIL; - resultSetSort (zh, stream->mem, num_input_setnames, input_setnames, - output_setname, sort_sequence, sort_status); - return zebra_end_read(zh); + 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, @@ -1038,7 +1115,7 @@ int zebra_deleteResultSet(ZebraHandle zh, int function, ASSERTZH; assert(statuses); yaz_log(log_level, "zebra_deleteResultSet n=%d",num_setnames); - zh->errCode = 0; + zebra_clearError(zh);; if (zebra_begin_read(zh)) return Z_DeleteStatus_systemProblemAtTarget; switch (function) @@ -1073,7 +1150,7 @@ int zebra_errCode (ZebraHandle zh) const char *zebra_errString (ZebraHandle zh) { - const char *e=""; + const char *e = 0; if (zh) e= diagbib1_str (zh->errCode); yaz_log(log_level, "zebra_errString: %s",e); @@ -1082,7 +1159,7 @@ const char *zebra_errString (ZebraHandle zh) char *zebra_errAdd (ZebraHandle zh) { - char *a=""; + char *a = 0; if (zh) a= zh->errString; yaz_log(log_level, "zebra_errAdd: %s",a); @@ -1105,7 +1182,7 @@ ZEBRA_RES zebra_auth (ZebraHandle zh, const char *user, const char *pass) ZebraService zs; ASSERTZH; - zh->errCode = 0; + zebra_clearError(zh); zs= zh->service; sprintf(u, "perm.%.30s", user ? user : "anonymous"); @@ -1128,7 +1205,7 @@ ZEBRA_RES zebra_admin_import_begin (ZebraHandle zh, const char *database, ASSERTZH; yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s", database, record_type); - zh->errCode = 0; + zebra_clearError(zh); if (zebra_select_database(zh, database) == ZEBRA_FAIL) return ZEBRA_FAIL; return zebra_begin_trans(zh, 1); @@ -1138,7 +1215,7 @@ ZEBRA_RES zebra_admin_import_end (ZebraHandle zh) { ASSERTZH; yaz_log(log_level, "zebra_admin_import_end"); - zh->errCode = 0; + zebra_clearError(zh); return zebra_end_trans(zh); } @@ -1149,7 +1226,7 @@ ZEBRA_RES zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment) int i; ASSERTZH; yaz_log(log_level, "zebra_admin_import_segment"); - zh->errCode = 0; + zebra_clearError(zh); for (i = 0; inum_segmentRecords; i++) { Z_NamePlusRecord *npr = segment->segmentRecords[i]; @@ -1195,7 +1272,7 @@ ZEBRA_RES zebra_admin_exchange_record (ZebraHandle zh, assert(rec_buf); yaz_log(log_level, "zebra_admin_exchange_record ac=%d", action); - zh->errCode = 0; + zebra_clearError(zh); if (!recid_buf || recid_len <= 0 || recid_len >= sizeof(recid_z)) return ZEBRA_FAIL; @@ -1277,20 +1354,20 @@ static int delete_SU_handle(void *handle, int ord) return 0; } -ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database) +ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db) { ZEBRA_RES ret = ZEBRA_OK; ASSERTZH; - yaz_log(log_level, "zebra_drop_database"); - zh->errCode = 0; + yaz_log(log_level, "zebra_drop_database %s", db); + zebra_clearError(zh); - if (zebra_select_database (zh, database) == ZEBRA_FAIL) + 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) { - zebraExplain_curDatabase (zh->reg->zei, database); + zebraExplain_curDatabase (zh->reg->zei, db); zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle); zebraExplain_removeDatabase(zh->reg->zei, zh); @@ -1304,25 +1381,24 @@ ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database) return ret; } -ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database) +ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *db) { ASSERTZH; - yaz_log(log_level, "zebra_create_database %s", database); - assert(database); - zh->errCode = 0; + yaz_log(log_level, "zebra_create_database %s", db); + assert(db); + zebra_clearError(zh); - if (zebra_select_database (zh, database) == ZEBRA_FAIL) + 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, database, 0 + if (zebraExplain_newDatabase (zh->reg->zei, db, 0 /* explainDatabase */)) { zebra_end_trans (zh); - zh->errCode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED; - zh->errString = "database already exist"; + zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db); return ZEBRA_FAIL; } return zebra_end_trans (zh); @@ -1337,7 +1413,7 @@ int zebra_string_norm (ZebraHandle zh, unsigned reg_id, assert(input_str); assert(output_str); yaz_log(log_level, "zebra_string_norm "); - zh->errCode = 0; + zebra_clearError(zh); if (!zh->reg->zebra_maps) return -1; wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, "", @@ -1360,7 +1436,7 @@ static void zebra_set_state (ZebraHandle zh, int val, int seqno) FILE *f; ASSERTZH; yaz_log(log_level, "zebra_set_state v=%d seq=%d", val, seqno); - zh->errCode = 0; + zebra_clearError(zh); sprintf (state_fname, "state.%s.LCK", zh->reg_name); fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname); @@ -1380,7 +1456,7 @@ static void zebra_get_state (ZebraHandle zh, char *val, int *seqno) ASSERTZH; yaz_log(log_level, "zebra_get_state "); - zh->errCode = 0; + zebra_clearError(zh); sprintf (state_fname, "state.%s.LCK", zh->reg_name); fname = zebra_mk_fname (res_get(zh->res, "lockDir"), state_fname); f = fopen (fname, "r"); @@ -1440,8 +1516,8 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) zebra_select_default_database(zh); if (!zh->res) { - zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; - zh->errString = "zebra_begin_trans: no database selected"; + zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, + "zebra_begin_trans: no database selected"); return ZEBRA_FAIL; } ASSERTZHRES; @@ -1451,9 +1527,10 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) { if (rw && !strchr(zh->user_perm, 'w')) { - zh->errCode = - YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE; - zh->errString = 0; + zebra_setError( + zh, + YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE, + 0); return ZEBRA_FAIL; } } @@ -1474,8 +1551,8 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) } if (zh->trans_no != 1) { - zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; - zh->errString = "zebra_begin_trans: write trans not allowed within read trans"; + zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, + "zebra_begin_trans: no write trans within read"); return ZEBRA_FAIL; } if (zh->reg) @@ -1485,7 +1562,7 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) } zh->trans_w_no = zh->trans_no; - zh->errCode = 0; + zebra_clearError(zh); zh->records_inserted = 0; zh->records_updated = 0; @@ -1561,9 +1638,9 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) zh->trans_no--; zh->trans_w_no = 0; - zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; - zh->errString = "zebra_begin_trans: cannot open register"; - yaz_log(YLOG_FATAL, zh->errString); + zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, + "zebra_begin_trans: cannot open register"); + yaz_log(YLOG_FATAL, "%s", zh->errString); return ZEBRA_FAIL; } } @@ -1580,7 +1657,7 @@ ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) zebra_flush_reg (zh); return ZEBRA_OK; } - zh->errCode = 0; + zebra_clearError(zh); #if HAVE_SYS_TIMES_H times (&zh->tms1); #endif @@ -1672,8 +1749,8 @@ ZEBRA_RES zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status) if (!zh->res || !zh->reg) { - zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR; - zh->errString = "zebra_end_trans: no open transaction"; + 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) @@ -1746,7 +1823,7 @@ int zebra_repository_update (ZebraHandle zh, const char *path) { ASSERTZH; assert(path); - zh->errCode = 0; + zebra_clearError(zh); yaz_log (log_level, "updating %s", path); repositoryUpdate (zh, path); return zh->errCode; @@ -1756,7 +1833,7 @@ int zebra_repository_delete (ZebraHandle zh, const char *path) { ASSERTZH; assert(path); - zh->errCode = 0; + zebra_clearError(zh); yaz_log (log_level, "deleting %s", path); repositoryDelete (zh, path); return zh->errCode; @@ -1767,7 +1844,7 @@ int zebra_repository_show (ZebraHandle zh, const char *path) ASSERTZH; assert(path); yaz_log(log_level, "zebra_repository_show"); - zh->errCode = 0; + zebra_clearError(zh); repositoryShow (zh, path); return zh->errCode; } @@ -1779,7 +1856,7 @@ static int zebra_commit_ex(ZebraHandle zh, int clean_only) const char *rval; BFiles bfs; ASSERTZH; - zh->errCode = 0; + zebra_clearError(zh); zebra_select_default_database(zh); if (!zh->res) @@ -1853,19 +1930,23 @@ ZEBRA_RES zebra_init(ZebraHandle zh) BFiles bfs = 0; ASSERTZH; yaz_log(log_level, "zebra_init"); - zh->errCode = 0; + zebra_clearError(zh); zebra_select_default_database(zh); if (!zh->res) { - zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE; + 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); @@ -1880,7 +1961,7 @@ ZEBRA_RES zebra_compact(ZebraHandle zh) BFiles bfs; ASSERTZH; yaz_log(log_level, "zebra_compact"); - zh->errCode = 0; + zebra_clearError(zh); if (!zh->res) { zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE; @@ -1904,7 +1985,7 @@ void zebra_shadow_enable(ZebraHandle zh, int value) { ASSERTZH; yaz_log(log_level, "zebra_shadow_enable"); - zh->errCode = 0; + zebra_clearError(zh); zh->shadow_enable = value; } @@ -1913,7 +1994,7 @@ 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; + zebra_clearError(zh); if (zh->iconv_to_utf8 != 0) yaz_iconv_close(zh->iconv_to_utf8); @@ -1936,7 +2017,7 @@ ZEBRA_RES zebra_record_encoding (ZebraHandle zh, const char *encoding) { ASSERTZH; yaz_log(log_level, "zebra_record_encoding"); - zh->errCode = 0; + zebra_clearError(zh); xfree(zh->record_encoding); zh->record_encoding = 0; if (encoding) @@ -1950,7 +2031,7 @@ 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); - zh->errCode = 0; + zebra_clearError(zh); res_set(zh->res, name, value); } @@ -1960,9 +2041,8 @@ const char *zebra_get_resource(ZebraHandle zh, const char *v; ASSERTZH; assert(name); - assert(defaultvalue); v = res_get_def (zh->res, name, (char *)defaultvalue); - zh->errCode = 0; + zebra_clearError(zh); yaz_log(log_level, "zebra_get_resource %s:%s", name, v); return v; } @@ -2112,9 +2192,9 @@ ZEBRA_RES zebra_delete_record (ZebraHandle zh, */ ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query, - const char *setname, zint *numhits) + const char *setname, zint *hits) { - zint hits = 0; + zint lhits = 0; ZEBRA_RES res = ZEBRA_OK; Z_RPNQuery *query; ODR odr = odr_createmem(ODR_ENCODE); @@ -2133,14 +2213,14 @@ ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query, res = ZEBRA_FAIL; } else - res = zebra_search_RPN(zh, odr, query, setname, &hits); + res = zebra_search_RPN(zh, odr, query, setname, &lhits); odr_destroy(odr); - yaz_log(log_level, "Hits: " ZINT_FORMAT, hits); + yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits); - if (numhits) - *numhits = hits; + if (hits) + *hits = lhits; return res; } @@ -2205,3 +2285,24 @@ ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids) 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); +} +