2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1999-09-07 11:36:32 adam
10 * Revision 1.22 1999/08/02 10:13:47 adam
11 * Fixed bug regarding zebra_hits.
13 * Revision 1.21 1999/07/14 10:59:26 adam
14 * Changed functions isc_getmethod, isams_getmethod.
15 * Improved fatal error handling (such as missing EXPLAIN schema).
17 * Revision 1.20 1999/07/06 12:28:04 adam
18 * Updated record index structure. Format includes version ID. Compression
19 * algorithm ID is stored for each record block.
21 * Revision 1.19 1999/05/26 07:49:13 adam
24 * Revision 1.18 1999/05/15 14:36:38 adam
25 * Updated dictionary. Implemented "compression" of dictionary.
27 * Revision 1.17 1999/05/12 13:08:06 adam
28 * First version of ISAMS.
30 * Revision 1.16 1999/02/19 10:38:30 adam
31 * Implemented chdir-setting.
33 * Revision 1.15 1999/02/17 12:18:12 adam
34 * Fixed zebra_close so that a NULL pointer is ignored.
36 * Revision 1.14 1999/02/02 14:51:11 adam
37 * Updated WIN32 code specific sections. Changed header.
39 * Revision 1.13 1998/12/16 12:23:30 adam
40 * Added facility for database name mapping using resource mapdb.
42 * Revision 1.12 1998/11/16 10:18:10 adam
43 * Better error reporting for result sets.
45 * Revision 1.11 1998/10/16 08:14:34 adam
46 * Updated record control system.
48 * Revision 1.10 1998/09/22 10:03:42 adam
49 * Changed result sets to be persistent in the sense that they can
50 * be re-searched if needed.
51 * Fixed memory leak in rsm_or.
53 * Revision 1.9 1998/09/02 13:53:17 adam
54 * Extra parameter decode added to search routines to implement
57 * Revision 1.8 1998/08/24 17:29:23 adam
60 * Revision 1.7 1998/06/24 12:16:13 adam
61 * Support for relations on text operands. Open range support in
62 * DFA module (i.e. [-j], [g-]).
64 * Revision 1.6 1998/06/22 11:36:47 adam
65 * Added authentication check facility to zebra.
67 * Revision 1.5 1998/06/13 00:14:08 adam
70 * Revision 1.4 1998/06/12 12:22:12 adam
73 * Revision 1.3 1998/05/27 16:57:44 adam
74 * Zebra returns surrogate diagnostic for single records when
77 * Revision 1.2 1998/05/20 10:12:19 adam
78 * Implemented automatic EXPLAIN database maintenance.
79 * Modified Zebra to work with ASN.1 compiled version of YAZ.
81 * Revision 1.1 1998/03/05 08:45:13 adam
82 * New result set model and modular ranking system. Moved towards
83 * descent server API. System information stored as "SGML" records.
99 static void zebra_chdir (ZebraHandle zh)
101 const char *dir = res_get (zh->res, "chdir");
104 logf (LOG_DEBUG, "chdir %s", dir);
112 static void zebra_register_unlock (ZebraHandle zh);
114 static int zebra_register_lock (ZebraHandle zh)
124 state = zebra_server_lock_get_state(zh, &lastChange);
134 zebra_server_lock (zh, state);
138 if (zh->registerState == state)
140 if (zh->registerChange >= lastChange)
142 logf (LOG_LOG, "Register completely updated since last access");
144 else if (zh->registerState == -1)
145 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
148 logf (LOG_LOG, "Register has changed state from %d to %d",
149 zh->registerState, state);
150 zh->registerChange = lastChange;
153 zebraExplain_close (zh->zei, 0, 0);
155 dict_close (zh->dict);
157 sortIdx_close (zh->sortIdx);
161 isc_close (zh->isamc);
163 isams_close (zh->isams);
164 rec_close (&zh->records);
166 bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
167 zh->registerState = state;
176 if (!(zh->records = rec_open (zh->bfs, 0, 0)))
178 logf (LOG_WARN, "rec_open");
183 if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
185 logf (LOG_WARN, "dict_open");
188 if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
190 logf (LOG_WARN, "sortIdx_open");
193 if (res_get_match (zh->res, "isam", "i", NULL))
195 if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
196 sizeof (struct it_key), zh->res)))
198 logf (LOG_WARN, "is_open");
202 else if (res_get_match (zh->res, "isam", "s", NULL))
204 struct ISAMS_M_s isams_m;
205 if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 0,
206 key_isams_m(zh->res, &isams_m))))
208 logf (LOG_WARN, "isams_open");
214 struct ISAMC_M_s isamc_m;
215 if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
216 0, key_isamc_m(zh->res, &isamc_m))))
218 logf (LOG_WARN, "isc_open");
222 zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
225 logf (LOG_WARN, "Cannot obtain EXPLAIN information");
231 zebra_register_unlock (zh);
232 zh->registerState = -1;
238 static void zebra_register_unlock (ZebraHandle zh)
240 static int waitSec = -1;
244 logf (LOG_LOG, "user/system: %ld/%ld",
245 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
246 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
250 char *s = res_get (zh->res, "debugRequestWait");
261 if (zh->registerState != -1)
262 zebra_server_unlock (zh, zh->registerState);
265 ZebraHandle zebra_open (const char *configName)
269 zh = (ZebraHandle) xmalloc (sizeof(*zh));
270 if (!(zh->res = res_open (configName)))
272 logf (LOG_WARN, "Failed to read resources `%s'", configName);
276 zebra_server_lock_init (zh);
277 zh->dh = data1_create ();
278 zh->bfs = bfs_create (res_get (zh->res, "register"));
279 bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
280 data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
282 zh->registerState = -1; /* trigger open of registers! */
283 zh->registerChange = 0;
284 zh->recTypes = recTypes_init (zh->dh);
285 recTypes_default_handlers (zh->recTypes);
288 zh->zebra_maps = zebra_maps_open (zh->res);
289 zh->rank_classes = NULL;
293 zebraRankInstall (zh, rank1_class);
295 if (!res_get (zh->res, "passwd"))
296 zh->passwd_db = NULL;
299 zh->passwd_db = passwd_db_open ();
301 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
303 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
308 void zebra_close (ZebraHandle zh)
315 resultSetDestroy (zh);
316 zebraExplain_close (zh->zei, 0, 0);
317 dict_close (zh->dict);
318 sortIdx_close (zh->sortIdx);
322 isc_close (zh->isamc);
324 isams_close (zh->isams);
325 rec_close (&zh->records);
326 zebra_register_unlock (zh);
328 recTypes_destroy (zh->recTypes);
329 zebra_maps_close (zh->zebra_maps);
330 zebraRankDestroy (zh);
331 bfs_destroy (zh->bfs);
332 data1_destroy (zh->dh);
333 zebra_server_lock_destroy (zh);
336 passwd_db_close (zh->passwd_db);
341 struct map_baseinfo {
347 char **new_basenames;
351 void map_basenames_func (void *vp, const char *name, const char *value)
353 struct map_baseinfo *p = (struct map_baseinfo *) vp;
355 char fromdb[128], todb[8][128];
358 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
359 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
360 todb[5], todb[6], todb[7]);
364 for (i = 0; i<p->num_bases; i++)
365 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
368 for (i = 0; i < no; i++)
370 if (p->new_num_bases == p->new_num_max)
372 p->new_basenames[(p->new_num_bases)++] =
373 nmem_strdup (p->mem, todb[i]);
379 void map_basenames (ZebraHandle zh, ODR stream,
380 int *num_bases, char ***basenames)
382 struct map_baseinfo info;
383 struct map_baseinfo *p = &info;
387 info.num_bases = *num_bases;
388 info.basenames = *basenames;
389 info.new_num_max = 128;
390 info.new_num_bases = 0;
391 info.new_basenames = (char **)
392 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
393 info.mem = stream->mem;
395 res_trav (zh->res, "mapdb", &info, map_basenames_func);
397 for (i = 0; i<p->num_bases; i++)
398 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
400 p->new_basenames[(p->new_num_bases)++] =
401 nmem_strdup (p->mem, p->basenames[i]);
403 *num_bases = info.new_num_bases;
404 *basenames = info.new_basenames;
405 for (i = 0; i<*num_bases; i++)
406 logf (LOG_LOG, "base %s", (*basenames)[i]);
409 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
410 Z_RPNQuery *query, int num_bases, char **basenames,
414 if (zebra_register_lock (zh))
416 map_basenames (zh, stream, &num_bases, &basenames);
417 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
419 zebra_register_unlock (zh);
422 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
423 const char *setname, Z_RecordComposition *comp,
424 oid_value input_format, int num_recs,
425 ZebraRetrievalRecord *recs)
430 if (zebra_register_lock (zh))
432 pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
433 for (i = 0; i<num_recs; i++)
434 pos_array[i] = recs[i].position;
435 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
438 logf (LOG_DEBUG, "zebraPosSetCreate error");
440 zh->errString = nmem_strdup (stream->mem, setname);
444 for (i = 0; i<num_recs; i++)
450 sprintf (num_str, "%d", pos_array[i]);
452 zh->errString = nmem_strdup (stream->mem, num_str);
458 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
459 stream, input_format, comp,
460 &recs[i].format, &recs[i].buf,
463 recs[i].errString = NULL;
466 zebraPosSetDestroy (zh, poset, num_recs);
468 zebra_register_unlock (zh);
472 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
473 oid_value attributeset,
474 int num_bases, char **basenames,
475 int *position, int *num_entries, ZebraScanEntry **entries,
478 if (zebra_register_lock (zh))
484 map_basenames (zh, stream, &num_bases, &basenames);
485 rpn_scan (zh, stream, zapt, attributeset,
486 num_bases, basenames, position,
487 num_entries, entries, is_partial);
488 zebra_register_unlock (zh);
491 void zebra_sort (ZebraHandle zh, ODR stream,
492 int num_input_setnames, const char **input_setnames,
493 const char *output_setname, Z_SortKeySpecList *sort_sequence,
496 if (zebra_register_lock (zh))
498 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
499 output_setname, sort_sequence, sort_status);
500 zebra_register_unlock (zh);
503 int zebra_errCode (ZebraHandle zh)
508 const char *zebra_errString (ZebraHandle zh)
510 return diagbib1_str (zh->errCode);
513 char *zebra_errAdd (ZebraHandle zh)
515 return zh->errString;
518 int zebra_hits (ZebraHandle zh)
523 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
525 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
530 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
535 void zebra_setRecordType (ZebraHandle zh, const char *type)
540 void zebra_setGroup (ZebraHandle zh, const char *group)
545 void zebra_admin (ZebraHandle zh, const char *command)