2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.28 2000-03-15 15:00:30 adam
8 * First work on threaded version.
10 * Revision 1.27 2000/02/24 12:31:17 adam
11 * Added zebra_string_norm.
13 * Revision 1.26 1999/11/30 13:48:03 adam
14 * Improved installation. Updated for inclusion of YAZ header files.
16 * Revision 1.25 1999/11/04 15:00:45 adam
17 * Implemented delete result set(s).
19 * Revision 1.24 1999/10/14 14:33:50 adam
20 * Added truncation 5=106.
22 * Revision 1.23 1999/09/07 11:36:32 adam
25 * Revision 1.22 1999/08/02 10:13:47 adam
26 * Fixed bug regarding zebra_hits.
28 * Revision 1.21 1999/07/14 10:59:26 adam
29 * Changed functions isc_getmethod, isams_getmethod.
30 * Improved fatal error handling (such as missing EXPLAIN schema).
32 * Revision 1.20 1999/07/06 12:28:04 adam
33 * Updated record index structure. Format includes version ID. Compression
34 * algorithm ID is stored for each record block.
36 * Revision 1.19 1999/05/26 07:49:13 adam
39 * Revision 1.18 1999/05/15 14:36:38 adam
40 * Updated dictionary. Implemented "compression" of dictionary.
42 * Revision 1.17 1999/05/12 13:08:06 adam
43 * First version of ISAMS.
45 * Revision 1.16 1999/02/19 10:38:30 adam
46 * Implemented chdir-setting.
48 * Revision 1.15 1999/02/17 12:18:12 adam
49 * Fixed zebra_close so that a NULL pointer is ignored.
51 * Revision 1.14 1999/02/02 14:51:11 adam
52 * Updated WIN32 code specific sections. Changed header.
54 * Revision 1.13 1998/12/16 12:23:30 adam
55 * Added facility for database name mapping using resource mapdb.
57 * Revision 1.12 1998/11/16 10:18:10 adam
58 * Better error reporting for result sets.
60 * Revision 1.11 1998/10/16 08:14:34 adam
61 * Updated record control system.
63 * Revision 1.10 1998/09/22 10:03:42 adam
64 * Changed result sets to be persistent in the sense that they can
65 * be re-searched if needed.
66 * Fixed memory leak in rsm_or.
68 * Revision 1.9 1998/09/02 13:53:17 adam
69 * Extra parameter decode added to search routines to implement
72 * Revision 1.8 1998/08/24 17:29:23 adam
75 * Revision 1.7 1998/06/24 12:16:13 adam
76 * Support for relations on text operands. Open range support in
77 * DFA module (i.e. [-j], [g-]).
79 * Revision 1.6 1998/06/22 11:36:47 adam
80 * Added authentication check facility to zebra.
82 * Revision 1.5 1998/06/13 00:14:08 adam
85 * Revision 1.4 1998/06/12 12:22:12 adam
88 * Revision 1.3 1998/05/27 16:57:44 adam
89 * Zebra returns surrogate diagnostic for single records when
92 * Revision 1.2 1998/05/20 10:12:19 adam
93 * Implemented automatic EXPLAIN database maintenance.
94 * Modified Zebra to work with ASN.1 compiled version of YAZ.
96 * Revision 1.1 1998/03/05 08:45:13 adam
97 * New result set model and modular ranking system. Moved towards
98 * descent server API. System information stored as "SGML" records.
111 #include <yaz/diagbib1.h>
114 static void zebra_chdir (ZebraService zh)
116 const char *dir = res_get (zh->res, "chdir");
119 logf (LOG_DEBUG, "chdir %s", dir);
127 static void zebra_register_unlock (ZebraService zh);
129 static int zebra_register_lock (ZebraService zh)
139 state = zebra_server_lock_get_state(zh, &lastChange);
149 zebra_server_lock (zh, state);
153 if (zh->registerState == state)
155 if (zh->registerChange >= lastChange)
157 logf (LOG_LOG, "Register completely updated since last access");
159 else if (zh->registerState == -1)
160 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
163 logf (LOG_LOG, "Register has changed state from %d to %d",
164 zh->registerState, state);
165 zh->registerChange = lastChange;
168 zebraExplain_close (zh->zei, 0, 0);
170 dict_close (zh->dict);
172 sortIdx_close (zh->sortIdx);
174 isams_close (zh->isams);
179 isc_close (zh->isamc);
181 rec_close (&zh->records);
183 bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
184 zh->registerState = state;
195 if (!(zh->records = rec_open (zh->bfs, 0, 0)))
197 logf (LOG_WARN, "rec_open");
202 if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
204 logf (LOG_WARN, "dict_open");
207 if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
209 logf (LOG_WARN, "sortIdx_open");
212 if (res_get_match (zh->res, "isam", "s", ISAM_DEFAULT))
214 struct ISAMS_M_s isams_m;
215 if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 0,
216 key_isams_m(zh->res, &isams_m))))
218 logf (LOG_WARN, "isams_open");
223 else if (res_get_match (zh->res, "isam", "i", ISAM_DEFAULT))
225 if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
226 sizeof (struct it_key), zh->res)))
228 logf (LOG_WARN, "is_open");
232 else if (res_get_match (zh->res, "isam", "c", ISAM_DEFAULT))
234 struct ISAMC_M_s isamc_m;
235 if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
236 0, key_isamc_m(zh->res, &isamc_m))))
238 logf (LOG_WARN, "isc_open");
243 zh->zei = zebraExplain_open (zh->records, zh->dh,
247 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
253 zebra_register_unlock (zh);
254 zh->registerState = -1;
260 static void zebra_register_unlock (ZebraService zh)
262 static int waitSec = -1;
266 logf (LOG_LOG, "user/system: %ld/%ld",
267 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
268 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
272 char *s = res_get (zh->res, "debugRequestWait");
283 if (zh->registerState != -1)
284 zebra_server_unlock (zh, zh->registerState);
287 ZebraHandle zebra_open (ZebraService service)
291 zh = (ZebraHandle) xmalloc (sizeof(*zh));
293 zh->service = service;
299 ZebraService zebra_start (const char *configName)
301 ZebraService zh = xmalloc (sizeof(*zh));
302 zh->configName = xstrdup(configName);
304 yaz_log (LOG_LOG, "zebra_start %s", configName);
305 if (!(zh->res = res_open (configName)))
307 logf (LOG_WARN, "Failed to read resources `%s'", configName);
311 zebra_server_lock_init (zh);
312 zh->dh = data1_create ();
315 zebra_server_lock_destroy (zh);
319 zh->bfs = bfs_create (res_get (zh->res, "register"));
322 zebra_server_lock_destroy (zh);
323 data1_destroy(zh->dh);
327 bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
328 data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
329 zh->registerState = -1; /* trigger open of registers! */
330 zh->registerChange = 0;
331 zh->recTypes = recTypes_init (zh->dh);
332 recTypes_default_handlers (zh->recTypes);
335 zh->zebra_maps = zebra_maps_open (zh->res);
336 zh->rank_classes = NULL;
338 zebraRankInstall (zh, rank1_class);
340 if (!res_get (zh->res, "passwd"))
341 zh->passwd_db = NULL;
344 zh->passwd_db = passwd_db_open ();
346 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
348 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
353 void zebra_stop(ZebraService zh)
357 yaz_log (LOG_LOG, "zebra_stop");
359 /* wait for sessions to die ...... */
364 zebraExplain_close (zh->zei, 0, 0);
365 dict_close (zh->dict);
366 sortIdx_close (zh->sortIdx);
368 isams_close (zh->isams);
373 isc_close (zh->isamc);
375 rec_close (&zh->records);
376 zebra_register_unlock (zh);
378 recTypes_destroy (zh->recTypes);
379 zebra_maps_close (zh->zebra_maps);
380 zebraRankDestroy (zh);
381 bfs_destroy (zh->bfs);
382 data1_destroy (zh->dh);
383 zebra_server_lock_destroy (zh);
386 passwd_db_close (zh->passwd_db);
389 xfree (zh->configName);
393 void zebra_close (ZebraHandle zh)
397 resultSetDestroy (zh, -1, 0, 0);
402 struct map_baseinfo {
408 char **new_basenames;
412 void map_basenames_func (void *vp, const char *name, const char *value)
414 struct map_baseinfo *p = (struct map_baseinfo *) vp;
416 char fromdb[128], todb[8][128];
419 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
420 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
421 todb[5], todb[6], todb[7]);
425 for (i = 0; i<p->num_bases; i++)
426 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
429 for (i = 0; i < no; i++)
431 if (p->new_num_bases == p->new_num_max)
433 p->new_basenames[(p->new_num_bases)++] =
434 nmem_strdup (p->mem, todb[i]);
440 void map_basenames (ZebraHandle zh, ODR stream,
441 int *num_bases, char ***basenames)
443 struct map_baseinfo info;
444 struct map_baseinfo *p = &info;
448 info.num_bases = *num_bases;
449 info.basenames = *basenames;
450 info.new_num_max = 128;
451 info.new_num_bases = 0;
452 info.new_basenames = (char **)
453 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
454 info.mem = stream->mem;
456 res_trav (zh->service->res, "mapdb", &info, map_basenames_func);
458 for (i = 0; i<p->num_bases; i++)
459 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
461 p->new_basenames[(p->new_num_bases)++] =
462 nmem_strdup (p->mem, p->basenames[i]);
464 *num_bases = info.new_num_bases;
465 *basenames = info.new_basenames;
466 for (i = 0; i<*num_bases; i++)
467 logf (LOG_LOG, "base %s", (*basenames)[i]);
470 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
471 Z_RPNQuery *query, int num_bases, char **basenames,
475 if (zebra_register_lock (zh->service))
477 map_basenames (zh, stream, &num_bases, &basenames);
478 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
480 zebra_register_unlock (zh->service);
483 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
484 const char *setname, Z_RecordComposition *comp,
485 oid_value input_format, int num_recs,
486 ZebraRetrievalRecord *recs)
491 if (zebra_register_lock (zh->service))
493 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
494 for (i = 0; i<num_recs; i++)
495 pos_array[i] = recs[i].position;
496 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
499 logf (LOG_DEBUG, "zebraPosSetCreate error");
501 zh->errString = nmem_strdup (stream->mem, setname);
505 for (i = 0; i<num_recs; i++)
511 sprintf (num_str, "%d", pos_array[i]);
513 zh->errString = nmem_strdup (stream->mem, num_str);
519 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
520 stream, input_format, comp,
521 &recs[i].format, &recs[i].buf,
524 recs[i].errString = NULL;
527 zebraPosSetDestroy (zh, poset, num_recs);
529 zebra_register_unlock (zh->service);
533 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
534 oid_value attributeset,
535 int num_bases, char **basenames,
536 int *position, int *num_entries, ZebraScanEntry **entries,
539 if (zebra_register_lock (zh->service))
545 map_basenames (zh, stream, &num_bases, &basenames);
546 rpn_scan (zh, stream, zapt, attributeset,
547 num_bases, basenames, position,
548 num_entries, entries, is_partial);
549 zebra_register_unlock (zh->service);
552 void zebra_sort (ZebraHandle zh, ODR stream,
553 int num_input_setnames, const char **input_setnames,
554 const char *output_setname, Z_SortKeySpecList *sort_sequence,
557 if (zebra_register_lock (zh->service))
559 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
560 output_setname, sort_sequence, sort_status);
561 zebra_register_unlock (zh->service);
564 int zebra_deleleResultSet(ZebraHandle zh, int function,
565 int num_setnames, char **setnames,
569 if (zebra_register_lock (zh->service))
570 return Z_DeleteStatus_systemProblemAtTarget;
573 case Z_DeleteRequest_list:
574 resultSetDestroy (zh, num_setnames, setnames, statuses);
576 case Z_DeleteRequest_all:
577 resultSetDestroy (zh, -1, 0, statuses);
580 zebra_register_unlock (zh->service);
581 status = Z_DeleteStatus_success;
582 for (i = 0; i<num_setnames; i++)
583 if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
584 status = statuses[i];
588 int zebra_errCode (ZebraHandle zh)
593 const char *zebra_errString (ZebraHandle zh)
595 return diagbib1_str (zh->errCode);
598 char *zebra_errAdd (ZebraHandle zh)
600 return zh->errString;
603 int zebra_hits (ZebraHandle zh)
608 int zebra_auth (ZebraService zh, const char *user, const char *pass)
610 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
615 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
620 void zebra_setRecordType (ZebraHandle zh, const char *type)
625 void zebra_setGroup (ZebraHandle zh, const char *group)
630 void zebra_admin_create (ZebraHandle zh, const char *database)
635 int zebra_string_norm (ZebraHandle zh, unsigned reg_id,
636 const char *input_str, int input_len,
637 char *output_str, int output_len)
640 if (!zh->service->zebra_maps)
642 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, "",
643 input_str, input_len);
646 if (wrbuf_len(wrbuf) >= output_len)
648 if (wrbuf_len(wrbuf))
649 memcpy (output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
650 output_str[wrbuf_len(wrbuf)] = '\0';
651 return wrbuf_len(wrbuf);