2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1999-07-14 10:59:26 adam
8 * Changed functions isc_getmethod, isams_getmethod.
9 * Improved fatal error handling (such as missing EXPLAIN schema).
11 * Revision 1.20 1999/07/06 12:28:04 adam
12 * Updated record index structure. Format includes version ID. Compression
13 * algorithm ID is stored for each record block.
15 * Revision 1.19 1999/05/26 07:49:13 adam
18 * Revision 1.18 1999/05/15 14:36:38 adam
19 * Updated dictionary. Implemented "compression" of dictionary.
21 * Revision 1.17 1999/05/12 13:08:06 adam
22 * First version of ISAMS.
24 * Revision 1.16 1999/02/19 10:38:30 adam
25 * Implemented chdir-setting.
27 * Revision 1.15 1999/02/17 12:18:12 adam
28 * Fixed zebra_close so that a NULL pointer is ignored.
30 * Revision 1.14 1999/02/02 14:51:11 adam
31 * Updated WIN32 code specific sections. Changed header.
33 * Revision 1.13 1998/12/16 12:23:30 adam
34 * Added facility for database name mapping using resource mapdb.
36 * Revision 1.12 1998/11/16 10:18:10 adam
37 * Better error reporting for result sets.
39 * Revision 1.11 1998/10/16 08:14:34 adam
40 * Updated record control system.
42 * Revision 1.10 1998/09/22 10:03:42 adam
43 * Changed result sets to be persistent in the sense that they can
44 * be re-searched if needed.
45 * Fixed memory leak in rsm_or.
47 * Revision 1.9 1998/09/02 13:53:17 adam
48 * Extra parameter decode added to search routines to implement
51 * Revision 1.8 1998/08/24 17:29:23 adam
54 * Revision 1.7 1998/06/24 12:16:13 adam
55 * Support for relations on text operands. Open range support in
56 * DFA module (i.e. [-j], [g-]).
58 * Revision 1.6 1998/06/22 11:36:47 adam
59 * Added authentication check facility to zebra.
61 * Revision 1.5 1998/06/13 00:14:08 adam
64 * Revision 1.4 1998/06/12 12:22:12 adam
67 * Revision 1.3 1998/05/27 16:57:44 adam
68 * Zebra returns surrogate diagnostic for single records when
71 * Revision 1.2 1998/05/20 10:12:19 adam
72 * Implemented automatic EXPLAIN database maintenance.
73 * Modified Zebra to work with ASN.1 compiled version of YAZ.
75 * Revision 1.1 1998/03/05 08:45:13 adam
76 * New result set model and modular ranking system. Moved towards
77 * descent server API. System information stored as "SGML" records.
93 static void zebra_chdir (ZebraHandle zh)
95 const char *dir = res_get (zh->res, "chdir");
98 logf (LOG_DEBUG, "chdir %s", dir);
106 static void zebra_register_unlock (ZebraHandle zh);
108 static int zebra_register_lock (ZebraHandle zh)
119 state = zebra_server_lock_get_state(zh, &lastChange);
129 zebra_server_lock (zh, state);
133 if (zh->registerState == state)
135 if (zh->registerChange >= lastChange)
137 logf (LOG_LOG, "Register completely updated since last access");
139 else if (zh->registerState == -1)
140 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
143 logf (LOG_LOG, "Register has changed state from %d to %d",
144 zh->registerState, state);
145 zh->registerChange = lastChange;
148 zebraExplain_close (zh->zei, 0, 0);
150 dict_close (zh->dict);
152 sortIdx_close (zh->sortIdx);
156 isc_close (zh->isamc);
158 isams_close (zh->isams);
159 rec_close (&zh->records);
161 bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
162 zh->registerState = state;
171 if (!(zh->records = rec_open (zh->bfs, 0, 0)))
173 logf (LOG_WARN, "rec_open");
178 if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
180 logf (LOG_WARN, "dict_open");
183 if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
185 logf (LOG_WARN, "sortIdx_open");
188 if (res_get_match (zh->res, "isam", "i", NULL))
190 if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
191 sizeof (struct it_key), zh->res)))
193 logf (LOG_WARN, "is_open");
197 else if (res_get_match (zh->res, "isam", "s", NULL))
199 struct ISAMS_M_s isams_m;
200 if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 0,
201 key_isams_m(zh->res, &isams_m))))
203 logf (LOG_WARN, "isams_open");
209 struct ISAMC_M_s isamc_m;
210 if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
211 0, key_isamc_m(zh->res, &isamc_m))))
213 logf (LOG_WARN, "isc_open");
217 zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
220 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
226 zebra_register_unlock (zh);
227 zh->registerState = -1;
233 static void zebra_register_unlock (ZebraHandle zh)
235 static int waitSec = -1;
239 logf (LOG_LOG, "user/system: %ld/%ld",
240 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
241 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
245 char *s = res_get (zh->res, "debugRequestWait");
256 if (zh->registerState != -1)
257 zebra_server_unlock (zh, zh->registerState);
260 ZebraHandle zebra_open (const char *configName)
264 zh = (ZebraHandle) xmalloc (sizeof(*zh));
265 if (!(zh->res = res_open (configName)))
267 logf (LOG_WARN, "Failed to read resources `%s'", configName);
271 zebra_server_lock_init (zh);
272 zh->dh = data1_create ();
273 zh->bfs = bfs_create (res_get (zh->res, "register"));
274 bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
275 data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
277 zh->registerState = -1; /* trigger open of registers! */
278 zh->registerChange = 0;
279 zh->recTypes = recTypes_init (zh->dh);
280 recTypes_default_handlers (zh->recTypes);
283 zh->zebra_maps = zebra_maps_open (zh->res);
284 zh->rank_classes = NULL;
288 zebraRankInstall (zh, rank1_class);
290 if (!res_get (zh->res, "passwd"))
291 zh->passwd_db = NULL;
294 zh->passwd_db = passwd_db_open ();
296 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
298 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
303 void zebra_close (ZebraHandle zh)
310 resultSetDestroy (zh);
311 zebraExplain_close (zh->zei, 0, 0);
312 dict_close (zh->dict);
313 sortIdx_close (zh->sortIdx);
317 isc_close (zh->isamc);
319 isams_close (zh->isams);
320 rec_close (&zh->records);
321 zebra_register_unlock (zh);
323 recTypes_destroy (zh->recTypes);
324 zebra_maps_close (zh->zebra_maps);
325 zebraRankDestroy (zh);
326 bfs_destroy (zh->bfs);
327 data1_destroy (zh->dh);
328 zebra_server_lock_destroy (zh);
331 passwd_db_close (zh->passwd_db);
337 struct map_baseinfo {
343 char **new_basenames;
347 void map_basenames_func (void *vp, const char *name, const char *value)
349 struct map_baseinfo *p = (struct map_baseinfo *) vp;
351 char fromdb[128], todb[8][128];
354 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
355 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
356 todb[5], todb[6], todb[7]);
360 for (i = 0; i<p->num_bases; i++)
361 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
364 for (i = 0; i < no; i++)
366 if (p->new_num_bases == p->new_num_max)
368 p->new_basenames[(p->new_num_bases)++] =
369 nmem_strdup (p->mem, todb[i]);
375 void map_basenames (ZebraHandle zh, ODR stream,
376 int *num_bases, char ***basenames)
378 struct map_baseinfo info;
379 struct map_baseinfo *p = &info;
383 info.num_bases = *num_bases;
384 info.basenames = *basenames;
385 info.new_num_max = 128;
386 info.new_num_bases = 0;
387 info.new_basenames = (char **)
388 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
389 info.mem = stream->mem;
391 res_trav (zh->res, "mapdb", &info, map_basenames_func);
393 for (i = 0; i<p->num_bases; i++)
394 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
396 p->new_basenames[(p->new_num_bases)++] =
397 nmem_strdup (p->mem, p->basenames[i]);
399 *num_bases = info.new_num_bases;
400 *basenames = info.new_basenames;
401 for (i = 0; i<*num_bases; i++)
402 logf (LOG_LOG, "base %s", (*basenames)[i]);
405 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
406 Z_RPNQuery *query, int num_bases, char **basenames,
409 if (zebra_register_lock (zh))
411 map_basenames (zh, stream, &num_bases, &basenames);
412 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
414 zebra_register_unlock (zh);
417 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
418 const char *setname, Z_RecordComposition *comp,
419 oid_value input_format, int num_recs,
420 ZebraRetrievalRecord *recs)
425 if (zebra_register_lock (zh))
427 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
428 for (i = 0; i<num_recs; i++)
429 pos_array[i] = recs[i].position;
430 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
433 logf (LOG_DEBUG, "zebraPosSetCreate error");
435 zh->errString = nmem_strdup (stream->mem, setname);
439 for (i = 0; i<num_recs; i++)
445 sprintf (num_str, "%d", pos_array[i]);
447 zh->errString = nmem_strdup (stream->mem, num_str);
453 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
454 stream, input_format, comp,
455 &recs[i].format, &recs[i].buf,
458 recs[i].errString = NULL;
461 zebraPosSetDestroy (zh, poset, num_recs);
463 zebra_register_unlock (zh);
467 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
468 oid_value attributeset,
469 int num_bases, char **basenames,
470 int *position, int *num_entries, ZebraScanEntry **entries,
473 if (zebra_register_lock (zh))
479 map_basenames (zh, stream, &num_bases, &basenames);
480 rpn_scan (zh, stream, zapt, attributeset,
481 num_bases, basenames, position,
482 num_entries, entries, is_partial);
483 zebra_register_unlock (zh);
486 void zebra_sort (ZebraHandle zh, ODR stream,
487 int num_input_setnames, const char **input_setnames,
488 const char *output_setname, Z_SortKeySpecList *sort_sequence,
491 if (zebra_register_lock (zh))
493 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
494 output_setname, sort_sequence, sort_status);
495 zebra_register_unlock (zh);
498 int zebra_errCode (ZebraHandle zh)
503 const char *zebra_errString (ZebraHandle zh)
505 return diagbib1_str (zh->errCode);
508 char *zebra_errAdd (ZebraHandle zh)
510 return zh->errString;
513 int zebra_hits (ZebraHandle zh)
518 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
520 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
525 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
530 void zebra_setRecordType (ZebraHandle zh, const char *type)
535 void zebra_setGroup (ZebraHandle zh, const char *group)
540 void zebra_admin (ZebraHandle zh, const char *command)