2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1999-10-14 14:33:50 adam
8 * Added truncation 5=106.
10 * Revision 1.23 1999/09/07 11:36:32 adam
13 * Revision 1.22 1999/08/02 10:13:47 adam
14 * Fixed bug regarding zebra_hits.
16 * Revision 1.21 1999/07/14 10:59:26 adam
17 * Changed functions isc_getmethod, isams_getmethod.
18 * Improved fatal error handling (such as missing EXPLAIN schema).
20 * Revision 1.20 1999/07/06 12:28:04 adam
21 * Updated record index structure. Format includes version ID. Compression
22 * algorithm ID is stored for each record block.
24 * Revision 1.19 1999/05/26 07:49:13 adam
27 * Revision 1.18 1999/05/15 14:36:38 adam
28 * Updated dictionary. Implemented "compression" of dictionary.
30 * Revision 1.17 1999/05/12 13:08:06 adam
31 * First version of ISAMS.
33 * Revision 1.16 1999/02/19 10:38:30 adam
34 * Implemented chdir-setting.
36 * Revision 1.15 1999/02/17 12:18:12 adam
37 * Fixed zebra_close so that a NULL pointer is ignored.
39 * Revision 1.14 1999/02/02 14:51:11 adam
40 * Updated WIN32 code specific sections. Changed header.
42 * Revision 1.13 1998/12/16 12:23:30 adam
43 * Added facility for database name mapping using resource mapdb.
45 * Revision 1.12 1998/11/16 10:18:10 adam
46 * Better error reporting for result sets.
48 * Revision 1.11 1998/10/16 08:14:34 adam
49 * Updated record control system.
51 * Revision 1.10 1998/09/22 10:03:42 adam
52 * Changed result sets to be persistent in the sense that they can
53 * be re-searched if needed.
54 * Fixed memory leak in rsm_or.
56 * Revision 1.9 1998/09/02 13:53:17 adam
57 * Extra parameter decode added to search routines to implement
60 * Revision 1.8 1998/08/24 17:29:23 adam
63 * Revision 1.7 1998/06/24 12:16:13 adam
64 * Support for relations on text operands. Open range support in
65 * DFA module (i.e. [-j], [g-]).
67 * Revision 1.6 1998/06/22 11:36:47 adam
68 * Added authentication check facility to zebra.
70 * Revision 1.5 1998/06/13 00:14:08 adam
73 * Revision 1.4 1998/06/12 12:22:12 adam
76 * Revision 1.3 1998/05/27 16:57:44 adam
77 * Zebra returns surrogate diagnostic for single records when
80 * Revision 1.2 1998/05/20 10:12:19 adam
81 * Implemented automatic EXPLAIN database maintenance.
82 * Modified Zebra to work with ASN.1 compiled version of YAZ.
84 * Revision 1.1 1998/03/05 08:45:13 adam
85 * New result set model and modular ranking system. Moved towards
86 * descent server API. System information stored as "SGML" records.
102 static void zebra_chdir (ZebraHandle zh)
104 const char *dir = res_get (zh->res, "chdir");
107 logf (LOG_DEBUG, "chdir %s", dir);
115 static void zebra_register_unlock (ZebraHandle zh);
117 static int zebra_register_lock (ZebraHandle zh)
127 state = zebra_server_lock_get_state(zh, &lastChange);
137 zebra_server_lock (zh, state);
141 if (zh->registerState == state)
143 if (zh->registerChange >= lastChange)
145 logf (LOG_LOG, "Register completely updated since last access");
147 else if (zh->registerState == -1)
148 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
151 logf (LOG_LOG, "Register has changed state from %d to %d",
152 zh->registerState, state);
153 zh->registerChange = lastChange;
156 zebraExplain_close (zh->zei, 0, 0);
158 dict_close (zh->dict);
160 sortIdx_close (zh->sortIdx);
164 isc_close (zh->isamc);
166 isams_close (zh->isams);
167 rec_close (&zh->records);
169 bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
170 zh->registerState = state;
179 if (!(zh->records = rec_open (zh->bfs, 0, 0)))
181 logf (LOG_WARN, "rec_open");
186 if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
188 logf (LOG_WARN, "dict_open");
191 if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
193 logf (LOG_WARN, "sortIdx_open");
196 if (res_get_match (zh->res, "isam", "i", NULL))
198 if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
199 sizeof (struct it_key), zh->res)))
201 logf (LOG_WARN, "is_open");
205 else if (res_get_match (zh->res, "isam", "s", NULL))
207 struct ISAMS_M_s isams_m;
208 if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 0,
209 key_isams_m(zh->res, &isams_m))))
211 logf (LOG_WARN, "isams_open");
217 struct ISAMC_M_s isamc_m;
218 if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
219 0, key_isamc_m(zh->res, &isamc_m))))
221 logf (LOG_WARN, "isc_open");
225 zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
228 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
234 zebra_register_unlock (zh);
235 zh->registerState = -1;
241 static void zebra_register_unlock (ZebraHandle zh)
243 static int waitSec = -1;
247 logf (LOG_LOG, "user/system: %ld/%ld",
248 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
249 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
253 char *s = res_get (zh->res, "debugRequestWait");
264 if (zh->registerState != -1)
265 zebra_server_unlock (zh, zh->registerState);
268 ZebraHandle zebra_open (const char *configName)
272 zh = (ZebraHandle) xmalloc (sizeof(*zh));
273 if (!(zh->res = res_open (configName)))
275 logf (LOG_WARN, "Failed to read resources `%s'", configName);
279 zebra_server_lock_init (zh);
280 zh->dh = data1_create ();
283 zebra_server_lock_destroy (zh);
287 zh->bfs = bfs_create (res_get (zh->res, "register"));
290 zebra_server_lock_destroy (zh);
291 data1_destroy(zh->dh);
295 bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
296 data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
298 zh->registerState = -1; /* trigger open of registers! */
299 zh->registerChange = 0;
300 zh->recTypes = recTypes_init (zh->dh);
301 recTypes_default_handlers (zh->recTypes);
304 zh->zebra_maps = zebra_maps_open (zh->res);
305 zh->rank_classes = NULL;
309 zebraRankInstall (zh, rank1_class);
311 if (!res_get (zh->res, "passwd"))
312 zh->passwd_db = NULL;
315 zh->passwd_db = passwd_db_open ();
317 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
319 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
324 void zebra_close (ZebraHandle zh)
331 resultSetDestroy (zh);
332 zebraExplain_close (zh->zei, 0, 0);
333 dict_close (zh->dict);
334 sortIdx_close (zh->sortIdx);
338 isc_close (zh->isamc);
340 isams_close (zh->isams);
341 rec_close (&zh->records);
342 zebra_register_unlock (zh);
344 recTypes_destroy (zh->recTypes);
345 zebra_maps_close (zh->zebra_maps);
346 zebraRankDestroy (zh);
347 bfs_destroy (zh->bfs);
348 data1_destroy (zh->dh);
349 zebra_server_lock_destroy (zh);
352 passwd_db_close (zh->passwd_db);
357 struct map_baseinfo {
363 char **new_basenames;
367 void map_basenames_func (void *vp, const char *name, const char *value)
369 struct map_baseinfo *p = (struct map_baseinfo *) vp;
371 char fromdb[128], todb[8][128];
374 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
375 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
376 todb[5], todb[6], todb[7]);
380 for (i = 0; i<p->num_bases; i++)
381 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
384 for (i = 0; i < no; i++)
386 if (p->new_num_bases == p->new_num_max)
388 p->new_basenames[(p->new_num_bases)++] =
389 nmem_strdup (p->mem, todb[i]);
395 void map_basenames (ZebraHandle zh, ODR stream,
396 int *num_bases, char ***basenames)
398 struct map_baseinfo info;
399 struct map_baseinfo *p = &info;
403 info.num_bases = *num_bases;
404 info.basenames = *basenames;
405 info.new_num_max = 128;
406 info.new_num_bases = 0;
407 info.new_basenames = (char **)
408 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
409 info.mem = stream->mem;
411 res_trav (zh->res, "mapdb", &info, map_basenames_func);
413 for (i = 0; i<p->num_bases; i++)
414 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
416 p->new_basenames[(p->new_num_bases)++] =
417 nmem_strdup (p->mem, p->basenames[i]);
419 *num_bases = info.new_num_bases;
420 *basenames = info.new_basenames;
421 for (i = 0; i<*num_bases; i++)
422 logf (LOG_LOG, "base %s", (*basenames)[i]);
425 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
426 Z_RPNQuery *query, int num_bases, char **basenames,
430 if (zebra_register_lock (zh))
432 map_basenames (zh, stream, &num_bases, &basenames);
433 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
435 zebra_register_unlock (zh);
438 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
439 const char *setname, Z_RecordComposition *comp,
440 oid_value input_format, int num_recs,
441 ZebraRetrievalRecord *recs)
446 if (zebra_register_lock (zh))
448 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
449 for (i = 0; i<num_recs; i++)
450 pos_array[i] = recs[i].position;
451 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
454 logf (LOG_DEBUG, "zebraPosSetCreate error");
456 zh->errString = nmem_strdup (stream->mem, setname);
460 for (i = 0; i<num_recs; i++)
466 sprintf (num_str, "%d", pos_array[i]);
468 zh->errString = nmem_strdup (stream->mem, num_str);
474 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
475 stream, input_format, comp,
476 &recs[i].format, &recs[i].buf,
479 recs[i].errString = NULL;
482 zebraPosSetDestroy (zh, poset, num_recs);
484 zebra_register_unlock (zh);
488 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
489 oid_value attributeset,
490 int num_bases, char **basenames,
491 int *position, int *num_entries, ZebraScanEntry **entries,
494 if (zebra_register_lock (zh))
500 map_basenames (zh, stream, &num_bases, &basenames);
501 rpn_scan (zh, stream, zapt, attributeset,
502 num_bases, basenames, position,
503 num_entries, entries, is_partial);
504 zebra_register_unlock (zh);
507 void zebra_sort (ZebraHandle zh, ODR stream,
508 int num_input_setnames, const char **input_setnames,
509 const char *output_setname, Z_SortKeySpecList *sort_sequence,
512 if (zebra_register_lock (zh))
514 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
515 output_setname, sort_sequence, sort_status);
516 zebra_register_unlock (zh);
519 int zebra_errCode (ZebraHandle zh)
524 const char *zebra_errString (ZebraHandle zh)
526 return diagbib1_str (zh->errCode);
529 char *zebra_errAdd (ZebraHandle zh)
531 return zh->errString;
534 int zebra_hits (ZebraHandle zh)
539 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
541 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
546 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
551 void zebra_setRecordType (ZebraHandle zh, const char *type)
556 void zebra_setGroup (ZebraHandle zh, const char *group)
561 void zebra_admin (ZebraHandle zh, const char *command)