Changed functions isc_getmethod, isams_getmethod.
[idzebra-moved-to-github.git] / index / zebraapi.c
1 /*
2  * Copyright (C) 1995-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebraapi.c,v $
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).
10  *
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.
14  *
15  * Revision 1.19  1999/05/26 07:49:13  adam
16  * C++ compilation.
17  *
18  * Revision 1.18  1999/05/15 14:36:38  adam
19  * Updated dictionary. Implemented "compression" of dictionary.
20  *
21  * Revision 1.17  1999/05/12 13:08:06  adam
22  * First version of ISAMS.
23  *
24  * Revision 1.16  1999/02/19 10:38:30  adam
25  * Implemented chdir-setting.
26  *
27  * Revision 1.15  1999/02/17 12:18:12  adam
28  * Fixed zebra_close so that a NULL pointer is ignored.
29  *
30  * Revision 1.14  1999/02/02 14:51:11  adam
31  * Updated WIN32 code specific sections. Changed header.
32  *
33  * Revision 1.13  1998/12/16 12:23:30  adam
34  * Added facility for database name mapping using resource mapdb.
35  *
36  * Revision 1.12  1998/11/16 10:18:10  adam
37  * Better error reporting for result sets.
38  *
39  * Revision 1.11  1998/10/16 08:14:34  adam
40  * Updated record control system.
41  *
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.
46  *
47  * Revision 1.9  1998/09/02 13:53:17  adam
48  * Extra parameter decode added to search routines to implement
49  * persistent queries.
50  *
51  * Revision 1.8  1998/08/24 17:29:23  adam
52  * Minor changes.
53  *
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-]).
57  *
58  * Revision 1.6  1998/06/22 11:36:47  adam
59  * Added authentication check facility to zebra.
60  *
61  * Revision 1.5  1998/06/13 00:14:08  adam
62  * Minor changes.
63  *
64  * Revision 1.4  1998/06/12 12:22:12  adam
65  * Work on Zebra API.
66  *
67  * Revision 1.3  1998/05/27 16:57:44  adam
68  * Zebra returns surrogate diagnostic for single records when
69  * appropriate.
70  *
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.
74  *
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.
78  *
79  */
80
81 #include <stdio.h>
82 #ifdef WIN32
83 #include <io.h>
84 #include <process.h>
85 #include <direct.h>
86 #else
87 #include <unistd.h>
88 #endif
89
90 #include <diagbib1.h>
91 #include "zserver.h"
92
93 static void zebra_chdir (ZebraHandle zh)
94 {
95     const char *dir = res_get (zh->res, "chdir");
96     if (!dir)
97         return;
98     logf (LOG_DEBUG, "chdir %s", dir);
99 #ifdef WIN32
100     _chdir(dir);
101 #else
102     chdir (dir);
103 #endif
104 }
105
106 static void zebra_register_unlock (ZebraHandle zh);
107
108 static int zebra_register_lock (ZebraHandle zh)
109 {
110     time_t lastChange;
111     int state;
112
113     zh->errCode = 0;
114     zh->errString = 0;
115     zh->hits = 0;
116
117     zebra_chdir (zh);
118
119     state = zebra_server_lock_get_state(zh, &lastChange);
120
121     switch (state)
122     {
123     case 'c':
124         state = 1;
125         break;
126     default:
127         state = 0;
128     }
129     zebra_server_lock (zh, state);
130 #if HAVE_SYS_TIMES_H
131     times (&zh->tms1);
132 #endif
133     if (zh->registerState == state)
134     {
135         if (zh->registerChange >= lastChange)
136             return 0;
137         logf (LOG_LOG, "Register completely updated since last access");
138     }
139     else if (zh->registerState == -1)
140         logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
141               (long) getpid());
142     else
143         logf (LOG_LOG, "Register has changed state from %d to %d",
144               zh->registerState, state);
145     zh->registerChange = lastChange;
146     if (zh->records)
147     {
148         zebraExplain_close (zh->zei, 0, 0);
149         if (zh->dict)
150             dict_close (zh->dict);
151         if (zh->sortIdx)
152             sortIdx_close (zh->sortIdx);
153         if (zh->isam)
154             is_close (zh->isam);
155         if (zh->isamc)
156             isc_close (zh->isamc);
157         if (zh->isams)
158             isams_close (zh->isams);
159         rec_close (&zh->records);
160     }
161     bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
162     zh->registerState = state;
163
164     zh->isam = NULL;
165     zh->isamc = NULL;
166     zh->isams = NULL;
167     zh->dict = NULL;
168     zh->sortIdx = NULL;
169     zh->zei = NULL;
170
171     if (!(zh->records = rec_open (zh->bfs, 0, 0)))
172     {
173         logf (LOG_WARN, "rec_open");
174         zh->errCode = 2;
175     }
176     else
177     {
178         if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
179         {
180             logf (LOG_WARN, "dict_open");
181             zh->errCode = 2;
182         }
183         if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
184         {
185             logf (LOG_WARN, "sortIdx_open");
186             zh->errCode = 2;
187         }
188         if (res_get_match (zh->res, "isam", "i", NULL))
189         {
190             if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
191                                       sizeof (struct it_key), zh->res)))
192             {
193                 logf (LOG_WARN, "is_open");
194                 zh->errCode = 2;
195             }
196         }
197         else if (res_get_match (zh->res, "isam", "s", NULL))
198         {
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))))
202             {
203                 logf (LOG_WARN, "isams_open");
204                 zh->errCode = 2;
205             }
206         }
207         else
208         {
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))))
212             {
213                 logf (LOG_WARN, "isc_open");
214                 zh->errCode = 2;
215             }
216         }
217         zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
218         if (!zh->zei)
219         {
220             logf (LOG_WARN, "Cannot obtain EXPLAIN information");
221             zh->errCode = 2;
222         }
223     }
224     if (zh->errCode)
225     {
226         zebra_register_unlock (zh);
227         zh->registerState = -1;
228         return -1;
229     }
230     return 0;
231 }
232
233 static void zebra_register_unlock (ZebraHandle zh)
234 {
235     static int waitSec = -1;
236
237 #if HAVE_SYS_TIMES_H
238     times (&zh->tms2);
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));
242 #endif
243     if (waitSec == -1)
244     {
245         char *s = res_get (zh->res, "debugRequestWait");
246         if (s)
247             waitSec = atoi (s);
248         else
249             waitSec = 0;
250     }
251 #ifdef WIN32
252 #else
253     if (waitSec > 0)
254         sleep (waitSec);
255 #endif
256     if (zh->registerState != -1)
257         zebra_server_unlock (zh, zh->registerState);
258 }
259
260 ZebraHandle zebra_open (const char *configName)
261 {
262     ZebraHandle zh;
263
264     zh = (ZebraHandle) xmalloc (sizeof(*zh));
265     if (!(zh->res = res_open (configName)))
266     {
267         logf (LOG_WARN, "Failed to read resources `%s'", configName);
268         return NULL;
269     }
270     zebra_chdir (zh);
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"));
276     zh->sets = NULL;
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);
281
282     zh->records = NULL;
283     zh->zebra_maps = zebra_maps_open (zh->res);
284     zh->rank_classes = NULL;
285     zh->errCode = 0;
286     zh->errString = 0;
287     
288     zebraRankInstall (zh, rank1_class);
289
290     if (!res_get (zh->res, "passwd"))
291         zh->passwd_db = NULL;
292     else
293     {
294         zh->passwd_db = passwd_db_open ();
295         if (!zh->passwd_db)
296             logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
297         else
298             passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
299     }
300     return zh;
301 }
302
303 void zebra_close (ZebraHandle zh)
304 {
305     if (!zh)
306         return;
307     zebra_chdir (zh);
308     if (zh->records)
309     {
310         resultSetDestroy (zh);
311         zebraExplain_close (zh->zei, 0, 0);
312         dict_close (zh->dict);
313         sortIdx_close (zh->sortIdx);
314         if (zh->isam)
315             is_close (zh->isam);
316         if (zh->isamc)
317             isc_close (zh->isamc);
318         if (zh->isams)
319             isams_close (zh->isams);
320         rec_close (&zh->records);
321         zebra_register_unlock (zh);
322     }
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);
329
330     if (zh->passwd_db)
331         passwd_db_close (zh->passwd_db);
332     res_close (zh->res);
333     xfree (zh);
334     xmalloc_trav("x");
335 }
336
337 struct map_baseinfo {
338     ZebraHandle zh;
339     NMEM mem;
340     int num_bases;
341     char **basenames;
342     int new_num_bases;
343     char **new_basenames;
344     int new_num_max;
345 };
346         
347 void map_basenames_func (void *vp, const char *name, const char *value)
348 {
349     struct map_baseinfo *p = (struct map_baseinfo *) vp;
350     int i, no;
351     char fromdb[128], todb[8][128];
352     
353     no =
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]);
357     if (no < 2)
358         return ;
359     no--;
360     for (i = 0; i<p->num_bases; i++)
361         if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
362         {
363             p->basenames[i] = 0;
364             for (i = 0; i < no; i++)
365             {
366                 if (p->new_num_bases == p->new_num_max)
367                     return;
368                 p->new_basenames[(p->new_num_bases)++] = 
369                     nmem_strdup (p->mem, todb[i]);
370             }
371             return;
372         }
373 }
374
375 void map_basenames (ZebraHandle zh, ODR stream,
376                     int *num_bases, char ***basenames)
377 {
378     struct map_baseinfo info;
379     struct map_baseinfo *p = &info;
380     int i;
381
382     info.zh = zh;
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;
390
391     res_trav (zh->res, "mapdb", &info, map_basenames_func);
392     
393     for (i = 0; i<p->num_bases; i++)
394         if (p->basenames[i] && p->new_num_bases < p->new_num_max)
395         {
396             p->new_basenames[(p->new_num_bases)++] = 
397                 nmem_strdup (p->mem, p->basenames[i]);
398         }
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]);
403 }
404
405 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
406                        Z_RPNQuery *query, int num_bases, char **basenames, 
407                        const char *setname)
408 {
409     if (zebra_register_lock (zh))
410         return;
411     map_basenames (zh, stream, &num_bases, &basenames);
412     resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
413
414     zebra_register_unlock (zh);
415 }
416
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)
421 {
422     ZebraPosSet poset;
423     int i, *pos_array;
424
425     if (zebra_register_lock (zh))
426         return;
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);
431     if (!poset)
432     {
433         logf (LOG_DEBUG, "zebraPosSetCreate error");
434         zh->errCode = 30;
435         zh->errString = nmem_strdup (stream->mem, setname);
436     }
437     else
438     {
439         for (i = 0; i<num_recs; i++)
440         {
441             if (!poset[i].sysno)
442             {
443                 char num_str[20];
444
445                 sprintf (num_str, "%d", pos_array[i]);  
446                 zh->errCode = 13;
447                 zh->errString = nmem_strdup (stream->mem, num_str);
448                 break;
449             }
450             else
451             {
452                 recs[i].errCode =
453                     zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
454                                         stream, input_format, comp,
455                                         &recs[i].format, &recs[i].buf,
456                                         &recs[i].len,
457                                         &recs[i].base);
458                 recs[i].errString = NULL;
459             }
460         }
461         zebraPosSetDestroy (zh, poset, num_recs);
462     }
463     zebra_register_unlock (zh);
464     xfree (pos_array);
465 }
466
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,
471                  int *is_partial)
472 {
473     if (zebra_register_lock (zh))
474     {
475         *entries = 0;
476         *num_entries = 0;
477         return;
478     }
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);
484 }
485
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,
489                  int *sort_status)
490 {
491     if (zebra_register_lock (zh))
492         return;
493     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
494                    output_setname, sort_sequence, sort_status);
495     zebra_register_unlock (zh);
496 }
497
498 int zebra_errCode (ZebraHandle zh)
499 {
500     return zh->errCode;
501 }
502
503 const char *zebra_errString (ZebraHandle zh)
504 {
505     return diagbib1_str (zh->errCode);
506 }
507
508 char *zebra_errAdd (ZebraHandle zh)
509 {
510     return zh->errString;
511 }
512
513 int zebra_hits (ZebraHandle zh)
514 {
515     return zh->hits;
516 }
517
518 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
519 {
520     if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
521         return 0;
522     return 1;
523 }
524
525 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
526 {
527
528 }
529
530 void zebra_setRecordType (ZebraHandle zh, const char *type)
531 {
532
533 }
534
535 void zebra_setGroup (ZebraHandle zh, const char *group)
536 {
537
538 }
539
540 void zebra_admin (ZebraHandle zh, const char *command)
541 {
542
543 }