2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1999-08-02 10:13:47 adam
8 * Fixed bug regarding zebra_hits.
10 * Revision 1.21 1999/07/14 10:59:26 adam
11 * Changed functions isc_getmethod, isams_getmethod.
12 * Improved fatal error handling (such as missing EXPLAIN schema).
14 * Revision 1.20 1999/07/06 12:28:04 adam
15 * Updated record index structure. Format includes version ID. Compression
16 * algorithm ID is stored for each record block.
18 * Revision 1.19 1999/05/26 07:49:13 adam
21 * Revision 1.18 1999/05/15 14:36:38 adam
22 * Updated dictionary. Implemented "compression" of dictionary.
24 * Revision 1.17 1999/05/12 13:08:06 adam
25 * First version of ISAMS.
27 * Revision 1.16 1999/02/19 10:38:30 adam
28 * Implemented chdir-setting.
30 * Revision 1.15 1999/02/17 12:18:12 adam
31 * Fixed zebra_close so that a NULL pointer is ignored.
33 * Revision 1.14 1999/02/02 14:51:11 adam
34 * Updated WIN32 code specific sections. Changed header.
36 * Revision 1.13 1998/12/16 12:23:30 adam
37 * Added facility for database name mapping using resource mapdb.
39 * Revision 1.12 1998/11/16 10:18:10 adam
40 * Better error reporting for result sets.
42 * Revision 1.11 1998/10/16 08:14:34 adam
43 * Updated record control system.
45 * Revision 1.10 1998/09/22 10:03:42 adam
46 * Changed result sets to be persistent in the sense that they can
47 * be re-searched if needed.
48 * Fixed memory leak in rsm_or.
50 * Revision 1.9 1998/09/02 13:53:17 adam
51 * Extra parameter decode added to search routines to implement
54 * Revision 1.8 1998/08/24 17:29:23 adam
57 * Revision 1.7 1998/06/24 12:16:13 adam
58 * Support for relations on text operands. Open range support in
59 * DFA module (i.e. [-j], [g-]).
61 * Revision 1.6 1998/06/22 11:36:47 adam
62 * Added authentication check facility to zebra.
64 * Revision 1.5 1998/06/13 00:14:08 adam
67 * Revision 1.4 1998/06/12 12:22:12 adam
70 * Revision 1.3 1998/05/27 16:57:44 adam
71 * Zebra returns surrogate diagnostic for single records when
74 * Revision 1.2 1998/05/20 10:12:19 adam
75 * Implemented automatic EXPLAIN database maintenance.
76 * Modified Zebra to work with ASN.1 compiled version of YAZ.
78 * Revision 1.1 1998/03/05 08:45:13 adam
79 * New result set model and modular ranking system. Moved towards
80 * descent server API. System information stored as "SGML" records.
96 static void zebra_chdir (ZebraHandle zh)
98 const char *dir = res_get (zh->res, "chdir");
101 logf (LOG_DEBUG, "chdir %s", dir);
109 static void zebra_register_unlock (ZebraHandle zh);
111 static int zebra_register_lock (ZebraHandle zh)
121 state = zebra_server_lock_get_state(zh, &lastChange);
131 zebra_server_lock (zh, state);
135 if (zh->registerState == state)
137 if (zh->registerChange >= lastChange)
139 logf (LOG_LOG, "Register completely updated since last access");
141 else if (zh->registerState == -1)
142 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
145 logf (LOG_LOG, "Register has changed state from %d to %d",
146 zh->registerState, state);
147 zh->registerChange = lastChange;
150 zebraExplain_close (zh->zei, 0, 0);
152 dict_close (zh->dict);
154 sortIdx_close (zh->sortIdx);
158 isc_close (zh->isamc);
160 isams_close (zh->isams);
161 rec_close (&zh->records);
163 bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
164 zh->registerState = state;
173 if (!(zh->records = rec_open (zh->bfs, 0, 0)))
175 logf (LOG_WARN, "rec_open");
180 if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
182 logf (LOG_WARN, "dict_open");
185 if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
187 logf (LOG_WARN, "sortIdx_open");
190 if (res_get_match (zh->res, "isam", "i", NULL))
192 if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
193 sizeof (struct it_key), zh->res)))
195 logf (LOG_WARN, "is_open");
199 else if (res_get_match (zh->res, "isam", "s", NULL))
201 struct ISAMS_M_s isams_m;
202 if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 0,
203 key_isams_m(zh->res, &isams_m))))
205 logf (LOG_WARN, "isams_open");
211 struct ISAMC_M_s isamc_m;
212 if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
213 0, key_isamc_m(zh->res, &isamc_m))))
215 logf (LOG_WARN, "isc_open");
219 zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
222 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
228 zebra_register_unlock (zh);
229 zh->registerState = -1;
235 static void zebra_register_unlock (ZebraHandle zh)
237 static int waitSec = -1;
241 logf (LOG_LOG, "user/system: %ld/%ld",
242 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
243 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
247 char *s = res_get (zh->res, "debugRequestWait");
258 if (zh->registerState != -1)
259 zebra_server_unlock (zh, zh->registerState);
262 ZebraHandle zebra_open (const char *configName)
266 zh = (ZebraHandle) xmalloc (sizeof(*zh));
267 if (!(zh->res = res_open (configName)))
269 logf (LOG_WARN, "Failed to read resources `%s'", configName);
273 zebra_server_lock_init (zh);
274 zh->dh = data1_create ();
275 zh->bfs = bfs_create (res_get (zh->res, "register"));
276 bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
277 data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
279 zh->registerState = -1; /* trigger open of registers! */
280 zh->registerChange = 0;
281 zh->recTypes = recTypes_init (zh->dh);
282 recTypes_default_handlers (zh->recTypes);
285 zh->zebra_maps = zebra_maps_open (zh->res);
286 zh->rank_classes = NULL;
290 zebraRankInstall (zh, rank1_class);
292 if (!res_get (zh->res, "passwd"))
293 zh->passwd_db = NULL;
296 zh->passwd_db = passwd_db_open ();
298 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
300 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
305 void zebra_close (ZebraHandle zh)
312 resultSetDestroy (zh);
313 zebraExplain_close (zh->zei, 0, 0);
314 dict_close (zh->dict);
315 sortIdx_close (zh->sortIdx);
319 isc_close (zh->isamc);
321 isams_close (zh->isams);
322 rec_close (&zh->records);
323 zebra_register_unlock (zh);
325 recTypes_destroy (zh->recTypes);
326 zebra_maps_close (zh->zebra_maps);
327 zebraRankDestroy (zh);
328 bfs_destroy (zh->bfs);
329 data1_destroy (zh->dh);
330 zebra_server_lock_destroy (zh);
333 passwd_db_close (zh->passwd_db);
339 struct map_baseinfo {
345 char **new_basenames;
349 void map_basenames_func (void *vp, const char *name, const char *value)
351 struct map_baseinfo *p = (struct map_baseinfo *) vp;
353 char fromdb[128], todb[8][128];
356 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
357 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
358 todb[5], todb[6], todb[7]);
362 for (i = 0; i<p->num_bases; i++)
363 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
366 for (i = 0; i < no; i++)
368 if (p->new_num_bases == p->new_num_max)
370 p->new_basenames[(p->new_num_bases)++] =
371 nmem_strdup (p->mem, todb[i]);
377 void map_basenames (ZebraHandle zh, ODR stream,
378 int *num_bases, char ***basenames)
380 struct map_baseinfo info;
381 struct map_baseinfo *p = &info;
385 info.num_bases = *num_bases;
386 info.basenames = *basenames;
387 info.new_num_max = 128;
388 info.new_num_bases = 0;
389 info.new_basenames = (char **)
390 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
391 info.mem = stream->mem;
393 res_trav (zh->res, "mapdb", &info, map_basenames_func);
395 for (i = 0; i<p->num_bases; i++)
396 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
398 p->new_basenames[(p->new_num_bases)++] =
399 nmem_strdup (p->mem, p->basenames[i]);
401 *num_bases = info.new_num_bases;
402 *basenames = info.new_basenames;
403 for (i = 0; i<*num_bases; i++)
404 logf (LOG_LOG, "base %s", (*basenames)[i]);
407 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
408 Z_RPNQuery *query, int num_bases, char **basenames,
412 if (zebra_register_lock (zh))
414 map_basenames (zh, stream, &num_bases, &basenames);
415 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
417 zebra_register_unlock (zh);
420 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
421 const char *setname, Z_RecordComposition *comp,
422 oid_value input_format, int num_recs,
423 ZebraRetrievalRecord *recs)
428 if (zebra_register_lock (zh))
430 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
431 for (i = 0; i<num_recs; i++)
432 pos_array[i] = recs[i].position;
433 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
436 logf (LOG_DEBUG, "zebraPosSetCreate error");
438 zh->errString = nmem_strdup (stream->mem, setname);
442 for (i = 0; i<num_recs; i++)
448 sprintf (num_str, "%d", pos_array[i]);
450 zh->errString = nmem_strdup (stream->mem, num_str);
456 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
457 stream, input_format, comp,
458 &recs[i].format, &recs[i].buf,
461 recs[i].errString = NULL;
464 zebraPosSetDestroy (zh, poset, num_recs);
466 zebra_register_unlock (zh);
470 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
471 oid_value attributeset,
472 int num_bases, char **basenames,
473 int *position, int *num_entries, ZebraScanEntry **entries,
476 if (zebra_register_lock (zh))
482 map_basenames (zh, stream, &num_bases, &basenames);
483 rpn_scan (zh, stream, zapt, attributeset,
484 num_bases, basenames, position,
485 num_entries, entries, is_partial);
486 zebra_register_unlock (zh);
489 void zebra_sort (ZebraHandle zh, ODR stream,
490 int num_input_setnames, const char **input_setnames,
491 const char *output_setname, Z_SortKeySpecList *sort_sequence,
494 if (zebra_register_lock (zh))
496 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
497 output_setname, sort_sequence, sort_status);
498 zebra_register_unlock (zh);
501 int zebra_errCode (ZebraHandle zh)
506 const char *zebra_errString (ZebraHandle zh)
508 return diagbib1_str (zh->errCode);
511 char *zebra_errAdd (ZebraHandle zh)
513 return zh->errString;
516 int zebra_hits (ZebraHandle zh)
521 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
523 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
528 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
533 void zebra_setRecordType (ZebraHandle zh, const char *type)
538 void zebra_setGroup (ZebraHandle zh, const char *group)
543 void zebra_admin (ZebraHandle zh, const char *command)