X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=index%2Fzebraapi.c;h=ec242905ed2cd0a610c491e5cac762cac30620b2;hb=9b4f970d2816490fa4cc850fabc51f494ee5612b;hp=7062e1730cda4a56909b0d4adb355ae4083b056e;hpb=c41c84a497ae744aa825a90f144c85b54f1cd4bb;p=idzebra-moved-to-github.git diff --git a/index/zebraapi.c b/index/zebraapi.c index 7062e17..ec24290 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -3,7 +3,54 @@ * All rights reserved. * * $Log: zebraapi.c,v $ - * Revision 1.29 2000-03-20 19:08:36 adam + * Revision 1.43 2000-12-05 12:22:53 adam + * Termlist source implemented (so that we can index values of XML/SGML + * attributes). + * + * Revision 1.42 2000/12/05 10:01:44 adam + * Fixed bug regarding user-defined attribute sets. + * + * Revision 1.41 2000/12/01 17:59:08 adam + * Fixed bug regarding online updates on WIN32. + * When zebra.cfg is not available the server will not abort. + * + * Revision 1.40 2000/11/29 15:21:31 adam + * Fixed problem with passwd db. + * + * Revision 1.39 2000/11/29 14:24:01 adam + * Script configure uses yaz pthreads options. Added locking for + * zebra_register_{lock,unlock}. + * + * Revision 1.38 2000/11/08 13:46:58 adam + * Fixed scan: server could break if bad attribute/database was selected. + * Work on remote update. + * + * Revision 1.37 2000/10/17 12:37:09 adam + * Fixed notification of live-updates. Fixed minor problem with mf_init + * where it didn't handle shadow area file names correctly. + * + * Revision 1.36 2000/09/06 08:59:36 adam + * Using read-only (for now) for server. + * + * Revision 1.35 2000/07/07 12:49:20 adam + * Optimized resultSetInsert{Rank,Sort}. + * + * Revision 1.34 2000/06/09 13:56:38 ian + * Added some logging on Authentication and searches. + * + * Revision 1.33 2000/05/18 12:01:36 adam + * System call times(2) used again. More 64-bit fixes. + * + * Revision 1.32 2000/04/19 14:35:59 adam + * WIN32 update (this version is known not to work on Windows). + * + * Revision 1.31 2000/04/05 10:07:02 adam + * Minor zebra compile fix. + * + * Revision 1.30 2000/04/05 09:49:35 adam + * On Unix, zebra/z'mbol uses automake. + * + * Revision 1.29 2000/03/20 19:08:36 adam * Added remote record import using Z39.50 extended services and Segment * Requests. * @@ -141,18 +188,80 @@ static void extract_index (ZebraHandle zh); static void zebra_register_unlock (ZebraHandle zh); -static int zebra_register_lock (ZebraHandle zh) +static int zebra_register_activate (ZebraService zh, int rw); +static int zebra_register_deactivate (ZebraService zh); + +static int zebra_register_lock (ZebraHandle zh, int rw) { + time_t lastChange; + int state; + zh->errCode = 0; + zh->errString = 0; if (!zh->service->active) { zh->errCode = 1019; return 1; } + +#if HAVE_SYS_TIMES_H + times (&zh->tms1); +#endif + + zebra_mutex_cond_lock (&zh->service->session_lock); + + state = zebra_server_lock_get_state(zh->service, &lastChange); + + zebra_server_lock (zh->service, state); + + switch (state) + { + case 'c': + state = 1; + break; + default: + state = 0; + } + if (rw) + logf (LOG_LOG, "Register in read/write mode"); + else if (zh->service->registerState == state) + { + logf (LOG_DEBUG, "registerChange = %ld lastChange = %ld", + (long) zh->service->registerChange, (long)lastChange); + if (zh->service->registerChange >= lastChange) + { + return 0; + } + logf (LOG_LOG, "Register completely updated since last access"); + } + else if (zh->service->registerState == -1) + logf (LOG_LOG, "Reading register using state %d pid=%ld", state, + (long) getpid()); + else + logf (LOG_LOG, "Register has changed state from %d to %d", + zh->service->registerState, state); + zh->service->registerChange = lastChange; + + zebra_register_deactivate (zh->service); + + zh->service->registerState = state; + + zebra_register_activate (zh->service, rw); return 0; } + static void zebra_register_unlock (ZebraHandle zh) { + if (zh->service->registerState != -1) + zebra_server_unlock (zh->service, zh->service->registerState); + zebra_mutex_cond_unlock (&zh->service->session_lock); +#if HAVE_SYS_TIMES_H + times (&zh->tms2); + logf (LOG_LOG, "user/system: %ld/%ld", + (long) (zh->tms2.tms_utime - zh->tms1.tms_utime), + (long) (zh->tms2.tms_stime - zh->tms1.tms_stime)); + +#endif } ZebraHandle zebra_open (ZebraService zs) @@ -161,12 +270,10 @@ ZebraHandle zebra_open (ZebraService zs) assert (zs); if (zs->stop_flag) - { - zh->errCode = 1019; return 0; - } zh = (ZebraHandle) xmalloc (sizeof(*zh)); + yaz_log (LOG_LOG, "zebra_open zs=%p returns %p", zs, zh); zh->service = zs; zh->sets = 0; @@ -175,6 +282,7 @@ ZebraHandle zebra_open (ZebraService zs) zh->errString = 0; zh->key_buf = 0; + zh->admin_databaseName = 0; zebra_mutex_cond_lock (&zs->session_lock); @@ -182,11 +290,10 @@ ZebraHandle zebra_open (ZebraService zs) zs->sessions = zh; zebra_mutex_cond_unlock (&zs->session_lock); + return zh; } -static int zebra_register_activate (ZebraService zh); -static int zebra_register_deactivate (ZebraService zh); ZebraService zebra_start (const char *configName) { @@ -197,23 +304,44 @@ ZebraService zebra_start (const char *configName) zh->configName = xstrdup(configName); zh->sessions = 0; zh->stop_flag = 0; - zh->active = 0; + zh->active = 1; + + zh->registerState = -1; + zh->registerChange = 0; + + if (!(zh->res = res_open (zh->configName))) + { + logf (LOG_WARN, "Failed to read resources `%s'", zh->configName); +// return zh; + } + zebra_chdir (zh); + zebra_server_lock_init (zh); zebra_mutex_cond_init (&zh->session_lock); - zebra_register_activate (zh); + if (!res_get (zh->res, "passwd")) + zh->passwd_db = NULL; + else + { + zh->passwd_db = passwd_db_open (); + if (!zh->passwd_db) + logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed"); + else + passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd")); + } + return zh; } -static int zebra_register_activate (ZebraService zh) +static int zebra_register_activate (ZebraService zh, int rw) { - if (zh->active) - return 0; - yaz_log (LOG_LOG, "zebra_register_activate"); - if (!(zh->res = res_open (zh->configName))) + if (zh->active > 1) { - logf (LOG_WARN, "Failed to read resources `%s'", zh->configName); - return -1; + yaz_log (LOG_LOG, "zebra_register_activate (ignored since active=%d)", + zh->active); + return 0; } - zebra_chdir (zh); + yaz_log (LOG_LOG, "zebra_register_activate shadow=%s", + zh->registerState ? "yes" : "no"); + zh->dh = data1_create (); if (!zh->dh) return -1; @@ -224,9 +352,8 @@ static int zebra_register_activate (ZebraService zh) return -1; } bf_lockDir (zh->bfs, res_get (zh->res, "lockDir")); + bf_cache (zh->bfs, zh->registerState ? res_get (zh->res, "shadow") : NULL); data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath")); - zh->registerState = -1; /* trigger open of registers! */ - zh->registerChange = 0; zh->recTypes = recTypes_init (zh->dh); recTypes_default_handlers (zh->recTypes); @@ -238,35 +365,26 @@ static int zebra_register_activate (ZebraService zh) zh->dict = 0; zh->sortIdx = 0; zh->isams = 0; +#if ZMBOL zh->isam = 0; zh->isamc = 0; zh->isamd = 0; +#endif zh->zei = 0; zebraRankInstall (zh, rank1_class); - if (!res_get (zh->res, "passwd")) - zh->passwd_db = NULL; - else - { - zh->passwd_db = passwd_db_open (); - if (!zh->passwd_db) - logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed"); - else - passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd")); - } - - if (!(zh->records = rec_open (zh->bfs, 1, 0))) + if (!(zh->records = rec_open (zh->bfs, rw, 0))) { logf (LOG_WARN, "rec_open"); return -1; } - if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 1, 0))) + if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 80, rw, 0))) { logf (LOG_WARN, "dict_open"); return -1; } - if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0))) + if (!(zh->sortIdx = sortIdx_open (zh->bfs, rw))) { logf (LOG_WARN, "sortIdx_open"); return -1; @@ -274,7 +392,7 @@ static int zebra_register_activate (ZebraService zh) if (res_get_match (zh->res, "isam", "s", ISAM_DEFAULT)) { struct ISAMS_M_s isams_m; - if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 1, + if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, rw, key_isams_m(zh->res, &isams_m)))) { logf (LOG_WARN, "isams_open"); @@ -284,7 +402,7 @@ static int zebra_register_activate (ZebraService zh) #if ZMBOL else if (res_get_match (zh->res, "isam", "i", ISAM_DEFAULT)) { - if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 1, + if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, rw, sizeof (struct it_key), zh->res))) { logf (LOG_WARN, "is_open"); @@ -295,7 +413,7 @@ static int zebra_register_activate (ZebraService zh) { struct ISAMC_M_s isamc_m; if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC, - 1, key_isamc_m(zh->res, &isamc_m)))) + rw, key_isamc_m(zh->res, &isamc_m)))) { logf (LOG_WARN, "isc_open"); return -1; @@ -306,7 +424,7 @@ static int zebra_register_activate (ZebraService zh) struct ISAMD_M_s isamd_m; if (!(zh->isamd = isamd_open (zh->bfs, FNAME_ISAMD, - 1, key_isamd_m(zh->res, &isamd_m)))) + rw, key_isamd_m(zh->res, &isamd_m)))) { logf (LOG_WARN, "isamd_open"); return -1; @@ -314,27 +432,25 @@ static int zebra_register_activate (ZebraService zh) } #endif zh->zei = zebraExplain_open (zh->records, zh->dh, - zh->res, 1, 0 /* rGroup */, + zh->res, rw, 0 /* rGroup */, explain_extract); if (!zh->zei) { logf (LOG_WARN, "Cannot obtain EXPLAIN information"); return -1; } - zh->active = 1; + zh->active = 2; yaz_log (LOG_LOG, "zebra_register_activate ok"); return 0; } void zebra_admin_shutdown (ZebraHandle zh) { - zebraExplain_flush (zh->service->zei, 1, zh); - extract_index (zh); - zebra_mutex_cond_lock (&zh->service->session_lock); zh->service->stop_flag = 1; if (!zh->service->sessions) zebra_register_deactivate(zh->service); + zh->service->active = 0; zebra_mutex_cond_unlock (&zh->service->session_lock); } @@ -344,66 +460,76 @@ void zebra_admin_start (ZebraHandle zh) zh->errCode = 0; zebra_mutex_cond_lock (&zs->session_lock); if (!zs->stop_flag) - zebra_register_activate(zs); + zh->service->active = 1; zebra_mutex_cond_unlock (&zs->session_lock); } -static int zebra_register_deactivate (ZebraService zh) +static int zebra_register_deactivate (ZebraService zs) { - zh->stop_flag = 0; - if (!zh->active) + zs->stop_flag = 0; + if (zs->active <= 1) + { + yaz_log(LOG_LOG, "zebra_register_deactivate (ignored since active=%d)", + zs->active); return 0; + } yaz_log(LOG_LOG, "zebra_register_deactivate"); - zebra_chdir (zh); - if (zh->records) + zebra_chdir (zs); + if (zs->records) { - zebraExplain_close (zh->zei, 1); - dict_close (zh->dict); - sortIdx_close (zh->sortIdx); - if (zh->isams) - isams_close (zh->isams); + zebraExplain_close (zs->zei, 0); + dict_close (zs->dict); + sortIdx_close (zs->sortIdx); + if (zs->isams) + isams_close (zs->isams); #if ZMBOL - if (zh->isam) - is_close (zh->isam); - if (zh->isamc) - isc_close (zh->isamc); - if (zh->isamd) - isamd_close (zh->isamd); + if (zs->isam) + is_close (zs->isam); + if (zs->isamc) + isc_close (zs->isamc); + if (zs->isamd) + isamd_close (zs->isamd); #endif - rec_close (&zh->records); + rec_close (&zs->records); } - recTypes_destroy (zh->recTypes); - zebra_maps_close (zh->zebra_maps); - zebraRankDestroy (zh); - bfs_destroy (zh->bfs); - data1_destroy (zh->dh); - - if (zh->passwd_db) - passwd_db_close (zh->passwd_db); - res_close (zh->res); - zh->active = 0; + recTypes_destroy (zs->recTypes); + zebra_maps_close (zs->zebra_maps); + zebraRankDestroy (zs); + bfs_destroy (zs->bfs); + data1_destroy (zs->dh); + + if (zs->passwd_db) + passwd_db_close (zs->passwd_db); + zs->active = 1; return 0; } -void zebra_stop(ZebraService zh) +void zebra_stop(ZebraService zs) { - if (!zh) + if (!zs) return ; yaz_log (LOG_LOG, "zebra_stop"); - assert (!zh->sessions); + zebra_mutex_cond_lock (&zs->session_lock); + while (zs->sessions) + zebra_close (zs->sessions); - zebra_mutex_cond_destroy (&zh->session_lock); + zebra_mutex_cond_unlock (&zs->session_lock); - zebra_register_deactivate(zh); - xfree (zh->configName); - xfree (zh); + zebra_mutex_cond_destroy (&zs->session_lock); + + zebra_register_deactivate(zs); + res_close (zs->res); + xfree (zs->configName); + xfree (zs); } void zebra_close (ZebraHandle zh) { ZebraService zs = zh->service; struct zebra_session **sp; + + yaz_log (LOG_LOG, "zebra_close zh=%p", zh); if (!zh) return ; resultSetDestroy (zh, -1, 0, 0); @@ -413,6 +539,7 @@ void zebra_close (ZebraHandle zh) xfree (zh->key_buf); zh->key_buf = 0; } + xfree (zh->admin_databaseName); zebra_mutex_cond_lock (&zs->session_lock); sp = &zs->sessions; while (1) @@ -504,12 +631,14 @@ void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode, const char *setname) { zh->hits = 0; - if (zebra_register_lock (zh)) + if (zebra_register_lock (zh, 0)) return; map_basenames (zh, stream, &num_bases, &basenames); resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname); zebra_register_unlock (zh); + + logf(LOG_APP,"SEARCH:%d:",zh->hits); } void zebra_records_retrieve (ZebraHandle zh, ODR stream, @@ -520,9 +649,8 @@ void zebra_records_retrieve (ZebraHandle zh, ODR stream, ZebraPosSet poset; int i, *pos_array; - if (zebra_register_lock (zh)) + if (zebra_register_lock (zh, 0)) return; - zh->errCode = 0; pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array)); for (i = 0; imem, num_input_setnames, input_setnames, output_setname, sort_sequence, sort_status); @@ -599,7 +727,7 @@ int zebra_deleleResultSet(ZebraHandle zh, int function, int *statuses) { int i, status; - if (zebra_register_lock (zh)) + if (zebra_register_lock (zh, 0)) return Z_DeleteStatus_systemProblemAtTarget; switch (function) { @@ -641,63 +769,72 @@ int zebra_hits (ZebraHandle zh) int zebra_auth (ZebraService zh, const char *user, const char *pass) { if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass)) + { + logf(LOG_APP,"AUTHOK:%s", user?user:"ANONYMOUS"); return 0; + } + + logf(LOG_APP,"AUTHFAIL:%s", user?user:"ANONYMOUS"); return 1; } void zebra_admin_import_begin (ZebraHandle zh, const char *database) { - if (zebra_register_lock (zh)) + if (zebra_register_lock (zh, 1)) return; - zebra_register_unlock(zh); + xfree (zh->admin_databaseName); + zh->admin_databaseName = xstrdup(database); +} + +void zebra_admin_import_end (ZebraHandle zh) +{ + zebraExplain_flush (zh->service->zei, 1, zh); + extract_index (zh); + zebra_register_unlock (zh); } void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment) { - if (zebra_register_lock (zh)) + int sysno; + int i; + if (zh->service->active < 2) return; - if (segment->num_segmentRecords == 0) + for (i = 0; inum_segmentRecords; i++) { - zebraExplain_flush (zh->service->zei, 1, zh); - extract_index (zh); - } - else - { - int sysno; - int i; - for (i = 0; inum_segmentRecords; i++) + Z_NamePlusRecord *npr = segment->segmentRecords[i]; + const char *databaseName = npr->databaseName; + + if (!databaseName) + databaseName = zh->admin_databaseName; + printf ("--------------%d--------------------\n", i); + if (npr->which == Z_NamePlusRecord_intermediateFragment) { - Z_NamePlusRecord *npr = segment->segmentRecords[i]; - printf ("--------------%d--------------------\n", i); - if (npr->which == Z_NamePlusRecord_intermediateFragment) + Z_FragmentSyntax *fragment = npr->u.intermediateFragment; + if (fragment->which == Z_FragmentSyntax_notExternallyTagged) { - Z_FragmentSyntax *fragment = npr->u.intermediateFragment; - if (fragment->which == Z_FragmentSyntax_notExternallyTagged) - { - Odr_oct *oct = fragment->u.notExternallyTagged; - printf ("%.*s", (oct->len > 100 ? 100 : oct->len) , - oct->buf); - - sysno = 0; - extract_rec_in_mem (zh, "grs.sgml", - oct->buf, oct->len, - "Default", 0 /* delete_flag */, - 0 /* test_mode */, - &sysno /* sysno */, - 1 /* store_keys */, - 1 /* store_data */, - 0 /* match criteria */); - } + Odr_oct *oct = fragment->u.notExternallyTagged; + printf ("%.*s", (oct->len > 100 ? 100 : oct->len) , + oct->buf); + + sysno = 0; + extract_rec_in_mem (zh, "grs.sgml", + oct->buf, oct->len, + databaseName, + 0 /* delete_flag */, + 0 /* test_mode */, + &sysno /* sysno */, + 1 /* store_keys */, + 1 /* store_data */, + 0 /* match criteria */); } } } - zebra_register_unlock(zh); } void zebra_admin_create (ZebraHandle zh, const char *database) { ZebraService zs = zh->service; - if (zebra_register_lock(zh)) + if (zebra_register_lock(zh, 1)) { zh->errCode = 1019; return; @@ -708,6 +845,8 @@ void zebra_admin_create (ZebraHandle zh, const char *database) zh->errCode = 224; zh->errString = "Database already exist"; } + zebraExplain_flush (zh->service->zei, 1, zh); + extract_index (zh); zebra_register_unlock(zh); } @@ -750,6 +889,7 @@ static void extract_add_index_string (RecWord *p, const char *string, int diff = 0; int *pseqno = &p->seqnos[p->reg_type]; ZebraHandle zh = p->extractCtrl->handle; + ZebraExplainInfo zei = zh->service->zei; struct recKeys *keys = &zh->keys; if (keys->buf_used+1024 > keys->buf_max) @@ -785,6 +925,21 @@ static void extract_add_index_string (RecWord *p, const char *string, *dst++ = lead; +#if SU_SCHEME + if ((lead & 3) < 3) + { + int ch = zebraExplain_lookupSU (zei, attrSet, attrUse); + if (ch < 0) + { + ch = zebraExplain_addSU (zei, attrSet, attrUse); + yaz_log (LOG_LOG, "addSU set=%d use=%d SU=%d", + attrSet, attrUse, ch); + } + assert (ch > 0); + memcpy (dst, &ch, sizeof(ch)); + dst += sizeof(ch); + } +#else if (!(lead & 1)) { memcpy (dst, &attrSet, sizeof(attrSet)); @@ -795,6 +950,7 @@ static void extract_add_index_string (RecWord *p, const char *string, memcpy (dst, &attrUse, sizeof(attrUse)); dst += sizeof(attrUse); } +#endif *dst++ = p->reg_type; memcpy (dst, string, length); dst += length; @@ -1130,10 +1286,14 @@ static void extract_flushWriteKeys (ZebraHandle zh) static void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno, int cmd, struct recKeys *reckeys) { +#if SU_SCHEME +#else unsigned char attrSet = (unsigned char) -1; unsigned short attrUse = (unsigned short) -1; +#endif int seqno = 0; int off = 0; + int ch = 0; ZebraExplainInfo zei = zh->service->zei; if (!zh->key_buf) @@ -1150,10 +1310,17 @@ static void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno, { const char *src = reckeys->buf + off; struct it_key key; - int lead, ch; + int lead; lead = *src++; +#if SU_SCHEME + if ((lead & 3) < 3) + { + memcpy (&ch, src, sizeof(ch)); + src += sizeof(ch); + } +#else if (!(lead & 1)) { memcpy (&attrSet, src, sizeof(attrSet)); @@ -1164,15 +1331,18 @@ static void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno, memcpy (&attrUse, src, sizeof(attrUse)); src += sizeof(attrUse); } +#endif if (zh->key_buf_used + 1024 > (zh->ptr_top-zh->ptr_i)*sizeof(char*)) extract_flushWriteKeys (zh); ++(zh->ptr_i); (zh->key_buf)[zh->ptr_top - zh->ptr_i] = (char*)zh->key_buf + zh->key_buf_used; - +#if SU_SCHEME +#else ch = zebraExplain_lookupSU (zei, attrSet, attrUse); if (ch < 0) ch = zebraExplain_addSU (zei, attrSet, attrUse); +#endif assert (ch > 0); zh->key_buf_used += key_SU_code (ch,((char*)zh->key_buf) + zh->key_buf_used); @@ -1313,7 +1483,6 @@ static int extract_rec_in_mem (ZebraHandle zh, const char *recordType, zh->keys.prevSeqNo = 0; zh->sortKeys = 0; - extractCtrl.subType = subType; extractCtrl.init = extract_init; extractCtrl.tokenAdd = extract_token_add;