X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=index%2Fzebraapi.c;h=9ff2d7c63a632d1d1047831a729aa379994f551d;hb=0193e3e218a53df6c3011e2f16f73b1af9a7e19e;hp=9ebb98016eac1087a3da6640da98c0d9cdb2daea;hpb=f967c55541a4adb26f43fc78f6bb7b98ebba6a32;p=idzebra-moved-to-github.git diff --git a/index/zebraapi.c b/index/zebraapi.c index 9ebb980..9ff2d7c 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.166 2005-05-09 10:04:30 adam Exp $ +/* $Id: zebraapi.c,v 1.172 2005-05-31 17:10:06 adam Exp $ Copyright (C) 1995-2005 Index Data ApS @@ -69,7 +69,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 */); @@ -147,7 +147,9 @@ ZebraHandle zebra_open (ZebraService zs) zh->store_data_buf = 0; - zh->m_limit = zebra_limit_create(0, 0); + zh->m_limit = zebra_limit_create(1, 0); + + zh->nmem_error = nmem_create(); return zh; } @@ -167,11 +169,13 @@ ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res) log_level_initialized = 1; } - yaz_log(log_level, "zebra_start %s",configName); + yaz_log(YLOG_LOG, "zebra_start %s %s",configName, ZEBRAVER); assert(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); @@ -183,16 +187,23 @@ ZebraService zebra_start_res (const char *configName, Res def_res, Res over_res) 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(); @@ -266,15 +277,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); @@ -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); @@ -540,6 +563,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; @@ -726,8 +751,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 +780,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); } @@ -852,7 +874,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 +888,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 +905,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 +924,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,6 +944,9 @@ ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, { char *buf; int len; + + zebra_get_hit_vector(zh, setname, poset[i].sysno); + recs[i].errCode = zebra_record_fetch(zh, poset[i].sysno, poset[i].score, stream, input_format, comp, @@ -942,11 +965,10 @@ ZEBRA_RES zebra_records_retrieve (ZebraHandle zh, ODR stream, } 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); + if (ret == ZEBRA_OK) /* only need to set it once */ + zebra_setError_zint(zh, + YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE, + pos_array[i]); ret = ZEBRA_FAIL; break; } @@ -995,7 +1017,7 @@ 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; @@ -1015,6 +1037,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 +1045,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 +1062,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 +1097,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 +1106,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 +1129,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 +1152,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 +1162,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 +1173,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 +1219,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; @@ -1282,7 +1306,7 @@ ZEBRA_RES zebra_drop_database (ZebraHandle zh, const char *database) ZEBRA_RES ret = ZEBRA_OK; ASSERTZH; yaz_log(log_level, "zebra_drop_database"); - zh->errCode = 0; + zebra_clearError(zh); if (zebra_select_database (zh, database) == ZEBRA_FAIL) return ZEBRA_FAIL; @@ -1309,7 +1333,7 @@ ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database) ASSERTZH; yaz_log(log_level, "zebra_create_database %s", database); assert(database); - zh->errCode = 0; + zebra_clearError(zh); if (zebra_select_database (zh, database) == ZEBRA_FAIL) return ZEBRA_FAIL; @@ -1321,8 +1345,7 @@ ZEBRA_RES zebra_create_database (ZebraHandle zh, const char *database) /* 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, database); return ZEBRA_FAIL; } return zebra_end_trans (zh); @@ -1337,7 +1360,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 +1383,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 +1403,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 +1463,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 +1474,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 +1498,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 +1509,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 +1585,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 +1604,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 +1696,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 +1770,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 +1780,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 +1791,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 +1803,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 +1877,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 +1908,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 +1932,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 +1941,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 +1964,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 +1978,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); } @@ -1962,7 +1990,7 @@ const char *zebra_get_resource(ZebraHandle zh, 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; } @@ -2198,10 +2226,31 @@ struct BFiles_struct *zebra_get_bfs(ZebraHandle zh) /* --------------------------------------------------------------------------- Set limit for search/scan */ -ZEBRA_RES zebra_set_limit(ZebraHandle zh, int exclude_flag, zint *ids) +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(exclude_flag, 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); +} +