From 104beff061317519411d779c7c84419ccdfc70e1 Mon Sep 17 00:00:00 2001 From: Heikki Levanto Date: Fri, 20 Jun 2003 14:21:23 +0000 Subject: [PATCH] Cleaning zebraapi (return conventions etc) More details in zebrash (perl interface still broken because of this!) --- include/zebraapi.h | 56 ++++++++++--------- index/index.h | 4 +- index/invstat.c | 7 +-- index/zebraapi.c | 152 ++++++++++++++++++++++++++++++++++------------------ index/zebrash.c | 89 ++++++++++++++++++++++-------- test/api/t2.c | 6 +-- test/api/t3.c | 4 +- test/api/t4.c | 4 +- 8 files changed, 211 insertions(+), 111 deletions(-) diff --git a/include/zebraapi.h b/include/zebraapi.h index 64de9eb..d619baf 100644 --- a/include/zebraapi.h +++ b/include/zebraapi.h @@ -1,4 +1,4 @@ -/* $Id: zebraapi.h,v 1.7 2003-06-18 11:46:33 adam Exp $ +/* $Id: zebraapi.h,v 1.8 2003-06-20 14:21:23 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003 Index Data Aps @@ -20,6 +20,12 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +/* Return codes: + * Most functions return an int. Unix-like, 0 means OK, + * non-zero means an error. The error info should be available + * via zebra_errCode and friends. + */ + #ifndef ZEBRAAPI_H #define ZEBRAAPI_H @@ -93,7 +99,7 @@ typedef struct zebra_service *ZebraService; YAZ_EXPORT ZebraService zebra_start (const char *configName); /* Close the whole Zebra */ -YAZ_EXPORT void zebra_stop (ZebraService zs); +YAZ_EXPORT int zebra_stop (ZebraService zs); /* Open a ZebraHandle */ @@ -103,7 +109,7 @@ YAZ_EXPORT void zebra_stop (ZebraService zs); YAZ_EXPORT ZebraHandle zebra_open (ZebraService zs); /* Close handle */ -YAZ_EXPORT void zebra_close (ZebraHandle zh); +YAZ_EXPORT int zebra_close (ZebraHandle zh); /********* * Error handling @@ -119,9 +125,11 @@ YAZ_EXPORT const char *zebra_errString (ZebraHandle zh); YAZ_EXPORT char *zebra_errAdd (ZebraHandle zh); /* get the result code and addinfo from zh */ -YAZ_EXPORT void zebra_result (ZebraHandle zh, int *code, char **addinfo); +YAZ_EXPORT int zebra_result (ZebraHandle zh, int *code, char **addinfo); /* FIXME - why is this needed?? -H */ +/* clear them error things */ +YAZ_EXPORT void zebra_clearError(ZebraHandle zh); /************** * Searching @@ -129,14 +137,14 @@ YAZ_EXPORT void zebra_result (ZebraHandle zh, int *code, char **addinfo); /* Search using PQF Query */ YAZ_EXPORT int zebra_search_PQF (ZebraHandle zh, const char *pqf_query, - const char *setname); + const char *setname, int *numhits); /* Search using RPN Query */ -YAZ_EXPORT void zebra_search_RPN (ZebraHandle zh, ODR o, Z_RPNQuery *query, +YAZ_EXPORT int zebra_search_RPN (ZebraHandle zh, ODR o, Z_RPNQuery *query, const char *setname, int *hits); /* Retrieve record(s) */ -YAZ_EXPORT void zebra_records_retrieve (ZebraHandle zh, ODR stream, +YAZ_EXPORT int zebra_records_retrieve (ZebraHandle zh, ODR stream, const char *setname, Z_RecordComposition *comp, oid_value input_format, int num_recs, ZebraRetrievalRecord *recs); @@ -148,7 +156,7 @@ YAZ_EXPORT int zebra_deleleResultSet(ZebraHandle zh, int function, /* Browse */ -YAZ_EXPORT void zebra_scan (ZebraHandle zh, ODR stream, +YAZ_EXPORT int zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, oid_value attributeset, int *position, int *num_entries, @@ -175,21 +183,21 @@ YAZ_EXPORT int zebra_string_norm (ZebraHandle zh, unsigned reg_id, * Admin */ -YAZ_EXPORT void zebra_create_database (ZebraHandle zh, const char *db); +YAZ_EXPORT int zebra_create_database (ZebraHandle zh, const char *db); -YAZ_EXPORT void zebra_admin_shutdown (ZebraHandle zh); -YAZ_EXPORT void zebra_admin_start (ZebraHandle zh); +YAZ_EXPORT int zebra_admin_shutdown (ZebraHandle zh); +YAZ_EXPORT int zebra_admin_start (ZebraHandle zh); -YAZ_EXPORT void zebra_shutdown (ZebraService zs); +YAZ_EXPORT int zebra_shutdown (ZebraService zs); -YAZ_EXPORT void zebra_admin_import_begin (ZebraHandle zh, const char *database, +YAZ_EXPORT int zebra_admin_import_begin (ZebraHandle zh, const char *database, const char *record_type); -YAZ_EXPORT void zebra_admin_import_segment (ZebraHandle zh, +YAZ_EXPORT int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment); -void zebra_admin_import_end (ZebraHandle zh); +YAZ_EXPORT int zebra_admin_import_end (ZebraHandle zh); int zebra_admin_exchange_record (ZebraHandle zh, const char *database, @@ -207,19 +215,19 @@ int zebra_clean (ZebraHandle zh); int zebra_init (ZebraHandle zh); int zebra_compact (ZebraHandle zh); -void zebra_repository_update (ZebraHandle zh); -void zebra_repository_delete (ZebraHandle zh); -void zebra_repository_show (ZebraHandle zh); -int zebra_record_insert (ZebraHandle zh, const char *buf, int len); +int zebra_repository_update (ZebraHandle zh); +int zebra_repository_delete (ZebraHandle zh); +int zebra_repository_show (ZebraHandle zh); +int zebra_record_insert (ZebraHandle zh, const char *buf, int len, int *sysno); -YAZ_EXPORT void zebra_set_group (ZebraHandle zh, struct recordGroup *rg); +YAZ_EXPORT int zebra_set_group (ZebraHandle zh, struct recordGroup *rg); YAZ_EXPORT int zebra_resultSetTerms (ZebraHandle zh, const char *setname, int no, int *count, int *type, char *out, size_t *len); -YAZ_EXPORT void zebra_sort (ZebraHandle zh, ODR stream, +YAZ_EXPORT int zebra_sort (ZebraHandle zh, ODR stream, int num_input_setnames, const char **input_setnames, const char *output_setname, @@ -235,17 +243,17 @@ YAZ_EXPORT int zebra_select_database (ZebraHandle zh, const char *basename); YAZ_EXPORT -void zebra_shadow_enable (ZebraHandle zh, int value); +int zebra_shadow_enable (ZebraHandle zh, int value); YAZ_EXPORT -void zebra_register_statistics (ZebraHandle zh, int dumpdict); +int zebra_register_statistics (ZebraHandle zh, int dumpdict); YAZ_EXPORT int zebra_record_encoding (ZebraHandle zh, const char *encoding); /* Resources */ YAZ_EXPORT -void zebra_set_resource(ZebraHandle zh, const char *name, const char *value); +int zebra_set_resource(ZebraHandle zh, const char *name, const char *value); YAZ_EXPORT const char *zebra_get_resource(ZebraHandle zh, const char *name, const char *defaultvalue); diff --git a/index/index.h b/index/index.h index 985c39b..fb865b5 100644 --- a/index/index.h +++ b/index/index.h @@ -1,4 +1,4 @@ -/* $Id: index.h,v 1.100 2003-06-18 11:46:33 adam Exp $ +/* $Id: index.h,v 1.101 2003-06-20 14:21:23 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -462,7 +462,7 @@ int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname, const struct recordGroup *rGroup, int deleteFlag); int zebra_begin_read (ZebraHandle zh); -void zebra_end_read (ZebraHandle zh); +int zebra_end_read (ZebraHandle zh); int zebra_file_stat (const char *file_name, struct stat *buf, int follow_links); diff --git a/index/invstat.c b/index/invstat.c index 3709ed0..bb537a1 100644 --- a/index/invstat.c +++ b/index/invstat.c @@ -1,4 +1,4 @@ -/* $Id: invstat.c,v 1.34 2002-08-05 14:08:08 adam Exp $ +/* $Id: invstat.c,v 1.35 2003-06-20 14:21:23 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -236,7 +236,7 @@ static int inv_stat_handle (char *name, const char *info, int pos, return 0; } -void zebra_register_statistics (ZebraHandle zh, int dumpdict) +int zebra_register_statistics (ZebraHandle zh, int dumpdict) { int blocks; int size; @@ -249,7 +249,7 @@ void zebra_register_statistics (ZebraHandle zh, int dumpdict) char term_dict[2*IT_MAX_WORD+2]; if (zebra_begin_read (zh)) - return; + return 1; stat_info.zh = zh; stat_info.dumpwords=dumpdict; @@ -393,5 +393,6 @@ void zebra_register_statistics (ZebraHandle zh, int dumpdict) prev, stat_info.isam_occurrences[i]); xmalloc_trav("unfreed"); /*! while hunting memory leaks */ zebra_end_read (zh); + return 0; } diff --git a/index/zebraapi.c b/index/zebraapi.c index 410a4ea..0a4d939 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.105 2003-06-18 11:46:33 adam Exp $ +/* $Id: zebraapi.c,v 1.106 2003-06-20 14:21:23 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003 Index Data Aps @@ -365,7 +365,7 @@ struct zebra_register *zebra_register_open (ZebraService zs, const char *name, return reg; } -void zebra_admin_shutdown (ZebraHandle zh) +int zebra_admin_shutdown (ZebraHandle zh) { ASSERTZH; yaz_log(LOG_API,"zebra_admin_shutdown"); @@ -374,9 +374,10 @@ void zebra_admin_shutdown (ZebraHandle zh) zebra_mutex_cond_lock (&zh->service->session_lock); zh->service->stop_flag = 1; zebra_mutex_cond_unlock (&zh->service->session_lock); + return 0; } -void zebra_admin_start (ZebraHandle zh) +int zebra_admin_start (ZebraHandle zh) { ZebraService zs; ASSERTZH; @@ -385,6 +386,7 @@ void zebra_admin_start (ZebraHandle zh) zs = zh->service; zebra_mutex_cond_lock (&zs->session_lock); zebra_mutex_cond_unlock (&zs->session_lock); + return 0; } static void zebra_register_close (ZebraService zs, struct zebra_register *reg) @@ -427,7 +429,7 @@ static void zebra_register_close (ZebraService zs, struct zebra_register *reg) xfree (reg); } -void zebra_stop(ZebraService zs) +int zebra_stop(ZebraService zs) { if (!zs) return ; @@ -447,9 +449,10 @@ void zebra_stop(ZebraService zs) xfree (zs->configName); xfree (zs->path_root); xfree (zs); + return 0; } -void zebra_close (ZebraHandle zh) +int zebra_close (ZebraHandle zh) { ZebraService zs; struct zebra_session **sp; @@ -501,6 +504,7 @@ void zebra_close (ZebraHandle zh) xfree (zh->reg_name); zh->service=0; /* more likely to trigger an assert */ xfree (zh); + return 0; } struct map_baseinfo { @@ -762,7 +766,7 @@ int zebra_select_databases (ZebraHandle zh, int num_bases, return 0; } -void zebra_search_RPN (ZebraHandle zh, ODR o, +int zebra_search_RPN (ZebraHandle zh, ODR o, Z_RPNQuery *query, const char *setname, int *hits) { ASSERTZH; @@ -772,7 +776,7 @@ void zebra_search_RPN (ZebraHandle zh, ODR o, *hits = 0; if (zebra_begin_read (zh)) - return; + return 1; zebra_livcode_transform(zh, query); @@ -782,9 +786,10 @@ void zebra_search_RPN (ZebraHandle zh, ODR o, zebra_end_read (zh); *hits = zh->hits; + return 0; } -void zebra_records_retrieve (ZebraHandle zh, ODR stream, +int zebra_records_retrieve (ZebraHandle zh, ODR stream, const char *setname, Z_RecordComposition *comp, oid_value input_format, int num_recs, ZebraRetrievalRecord *recs) @@ -799,13 +804,13 @@ void zebra_records_retrieve (ZebraHandle zh, ODR stream, { zh->errCode = 30; zh->errString = odr_strdup (stream, setname); - return; + return 1; } zh->errCode = 0; if (zebra_begin_read (zh)) - return; + return 1; pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array)); for (i = 0; inum_basenames, zh->basenames, position, num_entries, entries, is_partial, 0, 0); zebra_end_read (zh); + return 0; } -void zebra_sort (ZebraHandle zh, ODR stream, +int 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) @@ -884,10 +891,11 @@ void zebra_sort (ZebraHandle zh, ODR stream, yaz_log(LOG_API,"zebra_sort"); zh->errCode=0; if (zebra_begin_read (zh)) - return; + return 1; resultSetSort (zh, stream->mem, num_input_setnames, input_setnames, output_setname, sort_sequence, sort_status); zebra_end_read(zh); + return 0; } int zebra_deleleResultSet(ZebraHandle zh, int function, @@ -946,6 +954,15 @@ char *zebra_errAdd (ZebraHandle zh) return a; } +void zebra_clearError(ZebraHandle zh) +{ + if (zh) + { + zh->errCode=0; + zh->errString=""; + } +} + int zebra_auth (ZebraHandle zh, const char *user, const char *pass) { ZebraService zs; @@ -963,7 +980,7 @@ int zebra_auth (ZebraHandle zh, const char *user, const char *pass) return 1; } -void zebra_admin_import_begin (ZebraHandle zh, const char *database, +int zebra_admin_import_begin (ZebraHandle zh, const char *database, const char *record_type) { ASSERTZH; @@ -971,21 +988,23 @@ void zebra_admin_import_begin (ZebraHandle zh, const char *database, database, record_type); zh->errCode=0; if (zebra_select_database(zh, database)) - return; + return 1; zebra_begin_trans (zh, 1); xfree (zh->admin_databaseName); zh->admin_databaseName = xstrdup(database); + return 0; } -void zebra_admin_import_end (ZebraHandle zh) +int zebra_admin_import_end (ZebraHandle zh) { ASSERTZH; yaz_log(LOG_API,"zebra_admin_import_end"); zh->errCode=0; zebra_end_trans (zh); + return 0; } -void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment) +int zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment) { int sysno; int i; @@ -1022,6 +1041,7 @@ void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment) } } } + return 0; } int zebra_admin_exchange_record (ZebraHandle zh, @@ -1070,7 +1090,7 @@ int zebra_admin_exchange_record (ZebraHandle zh, return 0; } -void zebra_create_database (ZebraHandle zh, const char *database) +int zebra_create_database (ZebraHandle zh, const char *database) { ZebraService zs; ASSERTZH; @@ -1078,19 +1098,22 @@ void zebra_create_database (ZebraHandle zh, const char *database) zh->errCode=0; if (zebra_select_database (zh, database)) - return; + return -1; if (zebra_begin_trans (zh, 1)) - return; + return -1; zs = zh->service; /* announce database */ if (zebraExplain_newDatabase (zh->reg->zei, database, 0 /* explainDatabase */)) { + zebra_end_trans (zh); zh->errCode = 224; zh->errString = "database already exist"; + return -1; } zebra_end_trans (zh); + return 0; } int zebra_string_norm (ZebraHandle zh, unsigned reg_id, @@ -1116,7 +1139,7 @@ int zebra_string_norm (ZebraHandle zh, unsigned reg_id, } -void zebra_set_state (ZebraHandle zh, int val, int seqno) +int zebra_set_state (ZebraHandle zh, int val, int seqno) { char state_fname[256]; char *fname; @@ -1134,9 +1157,10 @@ void zebra_set_state (ZebraHandle zh, int val, int seqno) fprintf (f, "%c %d %ld\n", val, seqno, p); fclose (f); xfree (fname); + return 0; } -void zebra_get_state (ZebraHandle zh, char *val, int *seqno) +int zebra_get_state (ZebraHandle zh, char *val, int *seqno) { char state_fname[256]; char *fname; @@ -1157,6 +1181,7 @@ void zebra_get_state (ZebraHandle zh, char *val, int *seqno) fclose (f); } xfree (fname); + return 0; } int zebra_begin_read (ZebraHandle zh) @@ -1164,9 +1189,9 @@ int zebra_begin_read (ZebraHandle zh) return zebra_begin_trans(zh, 0); } -void zebra_end_read (ZebraHandle zh) +int zebra_end_read (ZebraHandle zh) { - zebra_end_trans(zh); + return zebra_end_trans(zh); } int zebra_begin_trans (ZebraHandle zh, int rw) @@ -1454,28 +1479,31 @@ int zebra_end_transaction (ZebraHandle zh, ZebraTransactionStatus *status) return 0; } -void zebra_repository_update (ZebraHandle zh) +int zebra_repository_update (ZebraHandle zh) { ASSERTZH; zh->errCode=0; logf (LOG_LOG|LOG_API, "updating %s", zh->rGroup.path); repositoryUpdate (zh); + return zh->errCode; } -void zebra_repository_delete (ZebraHandle zh) +int zebra_repository_delete (ZebraHandle zh) { ASSERTZH; zh->errCode=0; logf (LOG_LOG|LOG_API, "deleting %s", zh->rGroup.path); repositoryDelete (zh); + return zh->errCode; } -void zebra_repository_show (ZebraHandle zh) +int zebra_repository_show (ZebraHandle zh) { ASSERTZH; yaz_log(LOG_API,"zebra_repository_show"); zh->errCode=0; repositoryShow (zh); + return zh->errCode; } static int zebra_commit_ex (ZebraHandle zh, int clean_only) @@ -1594,50 +1622,57 @@ int zebra_compact (ZebraHandle zh) return 0; } -int zebra_record_insert (ZebraHandle zh, const char *buf, int len) +int zebra_record_insert (ZebraHandle zh, const char *buf, int len, int *sysno) { - int sysno = 0; + int sysn=0; ASSERTZH; yaz_log(LOG_API,"zebra_record_insert"); + if (sysno) + *sysno=0; zh->errCode=0; if (zebra_begin_trans (zh, 1)) - return 0; + return -1; extract_rec_in_mem (zh, "grs.sgml", buf, len, "Default", /* database */ 0 /* delete_flag */, 0 /* test_mode */, - &sysno /* sysno */, + &sysn /* sysno */, 1 /* store_keys */, 1 /* store_data */, 0 /* match criteria */); if (zebra_end_trans (zh)) - return 0; - return sysno; + return -1; + if (sysno) + *sysno=sysn; + return 0; } -void zebra_set_group (ZebraHandle zh, struct recordGroup *rg) +int zebra_set_group (ZebraHandle zh, struct recordGroup *rg) { ASSERTZH; yaz_log(LOG_API,"zebra_set_group"); zh->errCode=0; memcpy (&zh->rGroup, rg, sizeof(*rg)); + return 0; } -void zebra_result (ZebraHandle zh, int *code, char **addinfo) +int zebra_result (ZebraHandle zh, int *code, char **addinfo) { ASSERTZH; yaz_log(LOG_API,"zebra_result"); *code = zh->errCode; *addinfo = zh->errString; + return 0; } -void zebra_shadow_enable (ZebraHandle zh, int value) +int zebra_shadow_enable (ZebraHandle zh, int value) { ASSERTZH; yaz_log(LOG_API,"zebra_shadow_enable"); zh->errCode=0; zh->shadow_enable = value; + return 0; } int zebra_record_encoding (ZebraHandle zh, const char *encoding) @@ -1649,6 +1684,7 @@ int zebra_record_encoding (ZebraHandle zh, const char *encoding) /* * Fixme! + * Something about charset aliases. Oleg??? */ if (zh->iconv_to_utf8 != 0) @@ -1672,12 +1708,13 @@ int zebra_record_encoding (ZebraHandle zh, const char *encoding) return 0; } -void zebra_set_resource(ZebraHandle zh, const char *name, const char *value) +int zebra_set_resource(ZebraHandle zh, const char *name, const char *value) { ASSERTZH; yaz_log(LOG_API,"zebra_set_resource %s:%s",name,value); zh->errCode=0; res_put(zh->res, name, value); + return 0; } const char *zebra_get_resource(ZebraHandle zh, @@ -1692,6 +1729,7 @@ const char *zebra_get_resource(ZebraHandle zh, } /* moved from zebra_api_ext.c by pop */ +/* FIXME: Should this really be public??? -Heikki */ int zebra_trans_no (ZebraHandle zh) { ASSERTZH; @@ -1706,12 +1744,13 @@ int zebra_get_shadow_enable (ZebraHandle zh) { return (zh->shadow_enable); } -void zebra_set_shadow_enable (ZebraHandle zh, int value) { +int zebra_set_shadow_enable (ZebraHandle zh, int value) { yaz_log(LOG_API,"zebra_set_shadow_enable %d",value); zh->shadow_enable = value; + return 0; } -void init_recordGroup (struct recordGroup *rg) { +int init_recordGroup (struct recordGroup *rg) { assert(rg); yaz_log(LOG_API,"init_recordGroup"); rg->groupName = NULL; @@ -1726,6 +1765,7 @@ void init_recordGroup (struct recordGroup *rg) { rg->explainDatabase = 0; rg->fileVerboseLimit = 100000; rg->followLinks = -1; + return 0; } @@ -1895,12 +1935,14 @@ void api_records_retrieve (ZebraHandle zh, ODR stream, If sysno is provided, then it's used to identify the reocord. 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_insert_record (ZebraHandle zh, struct recordGroup *rGroup, const char *recordType, - int sysno, const char *match, const char *fname, + int *sysno, const char *match, const char *fname, const char *buf, int buf_size, int force_update) /* This one is ignored */ @@ -1915,19 +1957,19 @@ int zebra_insert_record (ZebraHandle zh, 0, /* delete_flag */ 0, /* test_mode */ recordType, - &sysno, + sysno, match, fname, force_update, 0); /* allow_update */ zebra_end_trans(zh); if (res < 0) return (res); - return sysno; + return res; } int zebra_update_record (ZebraHandle zh, struct recordGroup *rGroup, const char *recordType, - int sysno, const char *match, const char *fname, + int* sysno, const char *match, const char *fname, const char *buf, int buf_size, int force_update) @@ -1942,12 +1984,12 @@ int zebra_update_record (ZebraHandle zh, 0, /* delete_flag */ 0, /* test_mode */ recordType, - &sysno, + sysno, match, fname, force_update, 1); /* allow_update */ zebra_end_trans(zh); - return sysno; + return res; } @@ -1955,7 +1997,7 @@ int zebra_update_record (ZebraHandle zh, int zebra_delete_record (ZebraHandle zh, struct recordGroup *rGroup, const char *recordType, - int sysno, const char *match, const char *fname, + int *sysno, const char *match, const char *fname, const char *buf, int buf_size, int force_update) { @@ -1969,12 +2011,12 @@ int zebra_delete_record (ZebraHandle zh, 1, /* delete_flag */ 0, /* test_mode */ recordType, - &sysno, + sysno, match,fname, force_update, 1); /* allow_update */ zebra_end_trans(zh); - return sysno; + return res; } /* --------------------------------------------------------------------------- @@ -1982,11 +2024,13 @@ int zebra_delete_record (ZebraHandle zh, */ int zebra_search_PQF (ZebraHandle zh, const char *pqf_query, - const char *setname) + const char *setname, int *numhits) { int hits = 0; + int res=-1; Z_RPNQuery *query; ODR odr = odr_createmem(ODR_ENCODE); + assert(numhits); yaz_log(LOG_API,"zebra_search_PQF s=%s q=%s",setname, pqf_query); query = p_query_rpn (odr, PROTO_Z3950, pqf_query); @@ -1994,18 +2038,20 @@ int zebra_search_PQF (ZebraHandle zh, const char *pqf_query, if (!query) yaz_log (LOG_WARN, "bad query %s\n", pqf_query); else - zebra_search_RPN (zh, odr, query, setname, &hits); + res=zebra_search_RPN (zh, odr, query, setname, &hits); odr_destroy(odr); - return hits; + *numhits=hits; + + return res; } /* --------------------------------------------------------------------------- Sort - a simplified interface, with optional read locks. FIXME - This is a horrible name, will conflict with half the applications */ -int sort (ZebraHandle zh, +int zebra_sort_2 (ZebraHandle zh, ODR stream, const char *sort_spec, const char *output_setname, diff --git a/index/zebrash.c b/index/zebrash.c index 760e2d5..79575d5 100644 --- a/index/zebrash.c +++ b/index/zebrash.c @@ -1,5 +1,5 @@ /* zebrash.c - command-line interface to zebra API - * $Id: zebrash.c,v 1.9 2003-06-02 13:33:16 adam Exp $ + * $Id: zebrash.c,v 1.10 2003-06-20 14:21:23 heikki Exp $ * * Copyrigth 2003 Index Data Aps * @@ -84,7 +84,7 @@ static int defargint( char *arg, int def ) return v; } -int onecommand( char *line, char *outbuff); +int onecommand( char *line, char *outbuff, const char *prevout); /************************************** * Simple support commands @@ -155,16 +155,22 @@ static int cmd_zebra_close( char *args[], char *outbuff) static int cmd_quickstart( char *args[], char *outbuff) { char tmp[128]; - onecommand("yaz_log_file zebrash.log",outbuff); - onecommand("yaz_log_prefix ZebraSh", outbuff); + int rc=0; + if (!rc) + rc=onecommand("yaz_log_file zebrash.log",outbuff,""); + if (!rc) + rc=onecommand("yaz_log_prefix ZebraSh", outbuff,""); sprintf(tmp, "yaz_log_level 0x%x", LOG_DEFAULT_LEVEL | LOG_APP); - onecommand(tmp,outbuff); + if (!rc) + rc=onecommand(tmp,outbuff,""); logf(LOG_APP,"quickstart"); - onecommand("zebra_start",outbuff); - onecommand("zebra_open",outbuff); - onecommand("select_database Default",outbuff); - strcat(outbuff,"ok\n"); - return 0; + if (!rc) + rc=onecommand("zebra_start",outbuff,""); + if (!rc) + rc=onecommand("zebra_open",outbuff,""); + if (!rc) + rc=onecommand("select_database Default",outbuff,""); + return rc; } /************************************** @@ -270,11 +276,25 @@ static int cmd_init ( char *args[], char *outbuff) static int cmd_select_database ( char *args[], char *outbuff) { char *db=args[1]; - if (!db) - db="Default"; + if (!db) { + db="Default"; + strcat(outbuff,"Selecting database 'Default'\n"); + } return zebra_select_database(zh, db); } +static int cmd_create_database( char *args[], char *outbuff) +{ + char *db=args[1]; + int rc; + if (!db) + db="Default"; + strcat(outbuff,"Creating database "); + strcat(outbuff,db); + strcat(outbuff,"\n"); + + return zebra_create_database(zh, db); +} /************************************** * Command table, parser, and help */ @@ -352,7 +372,9 @@ struct cmdstruct cmds[] = { { "select_database", "basename", "Selects a database", cmd_select_database}, - + { "create_database", "basename", + "Creates a database", + cmd_create_database}, { "", "Misc:","", 0}, { "echo", "string", "ouputs the string", @@ -368,7 +390,10 @@ struct cmdstruct cmds[] = { {0,0,0,0} /* end marker */ }; -int onecommand( char *line, char *outbuff) +int onecommand( + char *line, /* input line */ + char *outbuff, /* output goes here */ + const char *prevout) /* prev output, for 'expect' */ { int i; char *args[MAX_NO_ARGS]; @@ -387,7 +412,23 @@ int onecommand( char *line, char *outbuff) } #endif if (0==n) - return -1; /* no command on line, too bad */ + return -90; /* no command on line, too bad */ + + if (0==strcmp(args[0],"expect")) + { + char *rest; + if (n>1) + rest= line + (args[1]-argbuf); /* rest of the line */ + else + return -1; /* need something to expect */ + printf("expecting '%s'\n",rest); /*!*/ + if (0==strstr(prevout,rest)) + { + printf( "Failed expectation, '%s' not found\n"); + exit(9); + } + return 0; + } for (i=0;cmds[i].cmd;i++) if (0==strcmp(cmds[i].cmd, args[0])) { @@ -401,7 +442,7 @@ int onecommand( char *line, char *outbuff) strcat(outbuff,args[0] ); strcat(outbuff,"'. Try help"); logf(LOG_APP,"Unknown command"); - return -2; + return -90; } static int cmd_help( char *args[], char *outbuff) @@ -469,12 +510,13 @@ static void Zerrors ( char *outbuff) ec=zebra_errCode (zh); if (ec) { - sprintf(tmp, "Zebra error %d: %s, (%s)", + sprintf(tmp, " Zebra error %d: %s, (%s)", ec, zebra_errString (zh), zebra_errAdd (zh) ); strcat(outbuff, tmp); strcat(outbuff, "\n"); logf(LOG_APP, tmp); + zebra_clearError(zh); } } @@ -486,10 +528,11 @@ void shell() { int rc=0; char tmp[MAX_ARG_LEN]; + char outbuff[MAX_OUT_BUFF]=""; + char prevout[MAX_OUT_BUFF]=""; /* previous output for 'expect' */ while (rc!=-99) { char buf[MAX_ARG_LEN]; - char outbuff[MAX_OUT_BUFF]; #if HAVE_READLINE_READLINE_H char* line_in; line_in=readline(PROMPT); @@ -511,16 +554,18 @@ void shell() if (!fgets (buf, MAX_ARG_LEN-1, stdin)) break; #endif + + strncpy(prevout, outbuff, MAX_OUT_BUFF); outbuff[0]='\0'; - rc=onecommand(buf, outbuff); + rc=onecommand(buf, outbuff, prevout); if (rc==0) { - strcat(outbuff, "OK\n"); + strcat(outbuff, " OK\n"); logf(LOG_APP, "OK"); } - else if (rc > 0) + else if (rc>-90) { - sprintf(tmp, "command returned %d\n",rc); + sprintf(tmp, " command returned %d\n",rc); strcat(outbuff,tmp); } Zerrors(outbuff); diff --git a/test/api/t2.c b/test/api/t2.c index fb585af..d61ab16 100644 --- a/test/api/t2.c +++ b/test/api/t2.c @@ -1,4 +1,4 @@ -/* $Id: t2.c,v 1.6 2003-05-20 13:52:41 adam Exp $ +/* $Id: t2.c,v 1.7 2003-06-20 14:21:24 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -42,9 +42,9 @@ int main(int argc, char **argv) zh = zebra_open (zs); zebra_select_database(zh, "Default"); zebra_begin_trans (zh, 1); - zebra_record_insert (zh, myrec, strlen(myrec)); + zebra_record_insert (zh, myrec, strlen(myrec),0); - hits = zebra_search_PQF (zh, "@attr 1=4 my", "set1"); + zebra_search_PQF (zh, "@attr 1=4 my", "set1", &hits); if (hits < 1) { yaz_log(LOG_FATAL, "At least one hit expected"); diff --git a/test/api/t3.c b/test/api/t3.c index 70d9729..c8808ab 100644 --- a/test/api/t3.c +++ b/test/api/t3.c @@ -1,4 +1,4 @@ -/* $Id: t3.c,v 1.3 2003-06-18 11:46:34 adam Exp $ +/* $Id: t3.c,v 1.4 2003-06-20 14:21:24 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -43,7 +43,7 @@ int main(int argc, char **argv) zebra_select_database(zh, "Default"); zebra_begin_trans (zh, 1); - zebra_record_insert (zh, myrec, strlen(myrec)); + zebra_record_insert (zh, myrec, strlen(myrec),0); zebra_end_trans (zh); for (i = 0; i<4; i++) diff --git a/test/api/t4.c b/test/api/t4.c index 9ab82e9..d9e03dd 100644 --- a/test/api/t4.c +++ b/test/api/t4.c @@ -1,4 +1,4 @@ -/* $Id: t4.c,v 1.1 2003-06-18 11:46:34 adam Exp $ +/* $Id: t4.c,v 1.2 2003-06-20 14:21:24 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -44,7 +44,7 @@ int main(int argc, char **argv) zebra_begin_trans (zh, 1); for (i = 0; i<1200; i++) - zebra_record_insert (zh, myrec, strlen(myrec)); + zebra_record_insert (zh, myrec, strlen(myrec),0); zebra_end_trans (zh); zebra_close(zh); zebra_stop(zs); -- 1.7.10.4