2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1999-02-02 14:51:11 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.13 1998/12/16 12:23:30 adam
11 * Added facility for database name mapping using resource mapdb.
13 * Revision 1.12 1998/11/16 10:18:10 adam
14 * Better error reporting for result sets.
16 * Revision 1.11 1998/10/16 08:14:34 adam
17 * Updated record control system.
19 * Revision 1.10 1998/09/22 10:03:42 adam
20 * Changed result sets to be persistent in the sense that they can
21 * be re-searched if needed.
22 * Fixed memory leak in rsm_or.
24 * Revision 1.9 1998/09/02 13:53:17 adam
25 * Extra parameter decode added to search routines to implement
28 * Revision 1.8 1998/08/24 17:29:23 adam
31 * Revision 1.7 1998/06/24 12:16:13 adam
32 * Support for relations on text operands. Open range support in
33 * DFA module (i.e. [-j], [g-]).
35 * Revision 1.6 1998/06/22 11:36:47 adam
36 * Added authentication check facility to zebra.
38 * Revision 1.5 1998/06/13 00:14:08 adam
41 * Revision 1.4 1998/06/12 12:22:12 adam
44 * Revision 1.3 1998/05/27 16:57:44 adam
45 * Zebra returns surrogate diagnostic for single records when
48 * Revision 1.2 1998/05/20 10:12:19 adam
49 * Implemented automatic EXPLAIN database maintenance.
50 * Modified Zebra to work with ASN.1 compiled version of YAZ.
52 * Revision 1.1 1998/03/05 08:45:13 adam
53 * New result set model and modular ranking system. Moved towards
54 * descent server API. System information stored as "SGML" records.
69 static int zebra_register_lock (ZebraHandle zh)
72 int state = zebra_server_lock_get_state(zh, &lastChange);
82 zebra_server_lock (zh, state);
86 if (zh->registerState == state)
88 if (zh->registerChange >= lastChange)
90 logf (LOG_LOG, "Register completely updated since last access");
92 else if (zh->registerState == -1)
93 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
96 logf (LOG_LOG, "Register has changed state from %d to %d",
97 zh->registerState, state);
98 zh->registerChange = lastChange;
101 zebraExplain_close (zh->zei, 0, 0);
102 dict_close (zh->dict);
103 sortIdx_close (zh->sortIdx);
107 isc_close (zh->isamc);
108 rec_close (&zh->records);
110 bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
111 zh->registerState = state;
112 zh->records = rec_open (zh->bfs, 0);
113 if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0)))
115 logf (LOG_WARN, "dict_open");
118 if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
120 logf (LOG_WARN, "sortIdx_open");
125 if (!res_get_match (zh->res, "isam", "i", NULL))
127 if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
128 0, key_isamc_m(zh->res))))
130 logf (LOG_WARN, "isc_open");
137 if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
138 sizeof (struct it_key), zh->res)))
140 logf (LOG_WARN, "is_open");
144 zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
149 static void zebra_register_unlock (ZebraHandle zh)
151 static int waitSec = -1;
155 logf (LOG_LOG, "user/system: %ld/%ld",
156 (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
157 (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
161 char *s = res_get (zh->res, "debugRequestWait");
172 if (zh->registerState != -1)
173 zebra_server_unlock (zh, zh->registerState);
176 ZebraHandle zebra_open (const char *configName)
178 ZebraHandle zh = xmalloc (sizeof(*zh));
180 if (!(zh->res = res_open (configName)))
182 logf (LOG_WARN, "Failed to read resources `%s'", configName);
185 zebra_server_lock_init (zh);
186 zh->dh = data1_create ();
187 zh->bfs = bfs_create (res_get (zh->res, "register"));
188 bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
189 data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
191 zh->registerState = -1; /* trigger open of registers! */
192 zh->registerChange = 0;
193 zh->recTypes = recTypes_init (zh->dh);
194 recTypes_default_handlers (zh->recTypes);
197 zh->zebra_maps = zebra_maps_open (zh->res);
198 zh->rank_classes = NULL;
202 zebraRankInstall (zh, rank1_class);
204 if (!res_get (zh->res, "passwd"))
205 zh->passwd_db = NULL;
208 zh->passwd_db = passwd_db_open ();
210 logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
212 passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
217 void zebra_close (ZebraHandle zh)
221 resultSetDestroy (zh);
222 zebraExplain_close (zh->zei, 0, 0);
223 dict_close (zh->dict);
224 sortIdx_close (zh->sortIdx);
228 isc_close (zh->isamc);
229 rec_close (&zh->records);
230 zebra_register_unlock (zh);
232 recTypes_destroy (zh->recTypes);
233 zebra_maps_close (zh->zebra_maps);
234 zebraRankDestroy (zh);
235 bfs_destroy (zh->bfs);
236 data1_destroy (zh->dh);
237 zebra_server_lock_destroy (zh);
240 passwd_db_close (zh->passwd_db);
245 struct map_baseinfo {
251 char **new_basenames;
255 void map_basenames_func (void *vp, const char *name, const char *value)
257 struct map_baseinfo *p = vp;
259 char fromdb[128], todb[8][128];
262 sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
263 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
264 todb[5], todb[6], todb[7]);
268 for (i = 0; i<p->num_bases; i++)
269 if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
272 for (i = 0; i < no; i++)
274 if (p->new_num_bases == p->new_num_max)
276 p->new_basenames[(p->new_num_bases)++] =
277 nmem_strdup (p->mem, todb[i]);
283 void map_basenames (ZebraHandle zh, ODR stream,
284 int *num_bases, char ***basenames)
286 struct map_baseinfo info;
287 struct map_baseinfo *p = &info;
291 info.num_bases = *num_bases;
292 info.basenames = *basenames;
293 info.new_num_max = 128;
294 info.new_num_bases = 0;
295 info.new_basenames = (char **)
296 odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
297 info.mem = stream->mem;
299 res_trav (zh->res, "mapdb", &info, map_basenames_func);
301 for (i = 0; i<p->num_bases; i++)
302 if (p->basenames[i] && p->new_num_bases < p->new_num_max)
304 p->new_basenames[(p->new_num_bases)++] =
305 nmem_strdup (p->mem, p->basenames[i]);
307 *num_bases = info.new_num_bases;
308 *basenames = info.new_basenames;
309 for (i = 0; i<*num_bases; i++)
310 logf (LOG_LOG, "base %s", (*basenames)[i]);
313 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
314 Z_RPNQuery *query, int num_bases, char **basenames,
317 zebra_register_lock (zh);
319 zh->errString = NULL;
322 map_basenames (zh, stream, &num_bases, &basenames);
323 resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
325 zebra_register_unlock (zh);
328 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
329 const char *setname, Z_RecordComposition *comp,
330 oid_value input_format, int num_recs,
331 ZebraRetrievalRecord *recs)
337 zh->errString = NULL;
338 pos_array = xmalloc (num_recs * sizeof(*pos_array));
339 for (i = 0; i<num_recs; i++)
340 pos_array[i] = recs[i].position;
342 zebra_register_lock (zh);
344 poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
347 logf (LOG_DEBUG, "zebraPosSetCreate error");
349 zh->errString = nmem_strdup (stream->mem, setname);
353 for (i = 0; i<num_recs; i++)
359 sprintf (num_str, "%d", pos_array[i]);
361 zh->errString = nmem_strdup (stream->mem, num_str);
367 zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
368 stream, input_format, comp,
369 &recs[i].format, &recs[i].buf,
372 recs[i].errString = NULL;
375 zebraPosSetDestroy (zh, poset, num_recs);
377 zebra_register_unlock (zh);
381 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
382 oid_value attributeset,
383 int num_bases, char **basenames,
384 int *position, int *num_entries, ZebraScanEntry **entries,
388 zh->errString = NULL;
389 zebra_register_lock (zh);
390 map_basenames (zh, stream, &num_bases, &basenames);
391 rpn_scan (zh, stream, zapt, attributeset,
392 num_bases, basenames, position,
393 num_entries, entries, is_partial);
394 zebra_register_unlock (zh);
397 void zebra_sort (ZebraHandle zh, ODR stream,
398 int num_input_setnames, const char **input_setnames,
399 const char *output_setname, Z_SortKeySpecList *sort_sequence,
403 zh->errString = NULL;
404 zebra_register_lock (zh);
405 resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
406 output_setname, sort_sequence, sort_status);
407 zebra_register_unlock (zh);
410 int zebra_errCode (ZebraHandle zh)
415 const char *zebra_errString (ZebraHandle zh)
417 return diagbib1_str (zh->errCode);
420 char *zebra_errAdd (ZebraHandle zh)
422 return zh->errString;
425 int zebra_hits (ZebraHandle zh)
430 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
432 if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
437 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
442 void zebra_setRecordType (ZebraHandle zh, const char *type)
447 void zebra_setGroup (ZebraHandle zh, const char *group)
452 void zebra_admin (ZebraHandle zh, const char *command)