Added truncation 5=106.
[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.24  1999-10-14 14:33:50  adam
8  * Added truncation 5=106.
9  *
10  * Revision 1.23  1999/09/07 11:36:32  adam
11  * Minor changes.
12  *
13  * Revision 1.22  1999/08/02 10:13:47  adam
14  * Fixed bug regarding zebra_hits.
15  *
16  * Revision 1.21  1999/07/14 10:59:26  adam
17  * Changed functions isc_getmethod, isams_getmethod.
18  * Improved fatal error handling (such as missing EXPLAIN schema).
19  *
20  * Revision 1.20  1999/07/06 12:28:04  adam
21  * Updated record index structure. Format includes version ID. Compression
22  * algorithm ID is stored for each record block.
23  *
24  * Revision 1.19  1999/05/26 07:49:13  adam
25  * C++ compilation.
26  *
27  * Revision 1.18  1999/05/15 14:36:38  adam
28  * Updated dictionary. Implemented "compression" of dictionary.
29  *
30  * Revision 1.17  1999/05/12 13:08:06  adam
31  * First version of ISAMS.
32  *
33  * Revision 1.16  1999/02/19 10:38:30  adam
34  * Implemented chdir-setting.
35  *
36  * Revision 1.15  1999/02/17 12:18:12  adam
37  * Fixed zebra_close so that a NULL pointer is ignored.
38  *
39  * Revision 1.14  1999/02/02 14:51:11  adam
40  * Updated WIN32 code specific sections. Changed header.
41  *
42  * Revision 1.13  1998/12/16 12:23:30  adam
43  * Added facility for database name mapping using resource mapdb.
44  *
45  * Revision 1.12  1998/11/16 10:18:10  adam
46  * Better error reporting for result sets.
47  *
48  * Revision 1.11  1998/10/16 08:14:34  adam
49  * Updated record control system.
50  *
51  * Revision 1.10  1998/09/22 10:03:42  adam
52  * Changed result sets to be persistent in the sense that they can
53  * be re-searched if needed.
54  * Fixed memory leak in rsm_or.
55  *
56  * Revision 1.9  1998/09/02 13:53:17  adam
57  * Extra parameter decode added to search routines to implement
58  * persistent queries.
59  *
60  * Revision 1.8  1998/08/24 17:29:23  adam
61  * Minor changes.
62  *
63  * Revision 1.7  1998/06/24 12:16:13  adam
64  * Support for relations on text operands. Open range support in
65  * DFA module (i.e. [-j], [g-]).
66  *
67  * Revision 1.6  1998/06/22 11:36:47  adam
68  * Added authentication check facility to zebra.
69  *
70  * Revision 1.5  1998/06/13 00:14:08  adam
71  * Minor changes.
72  *
73  * Revision 1.4  1998/06/12 12:22:12  adam
74  * Work on Zebra API.
75  *
76  * Revision 1.3  1998/05/27 16:57:44  adam
77  * Zebra returns surrogate diagnostic for single records when
78  * appropriate.
79  *
80  * Revision 1.2  1998/05/20 10:12:19  adam
81  * Implemented automatic EXPLAIN database maintenance.
82  * Modified Zebra to work with ASN.1 compiled version of YAZ.
83  *
84  * Revision 1.1  1998/03/05 08:45:13  adam
85  * New result set model and modular ranking system. Moved towards
86  * descent server API. System information stored as "SGML" records.
87  *
88  */
89
90 #include <stdio.h>
91 #ifdef WIN32
92 #include <io.h>
93 #include <process.h>
94 #include <direct.h>
95 #else
96 #include <unistd.h>
97 #endif
98
99 #include <diagbib1.h>
100 #include "zserver.h"
101
102 static void zebra_chdir (ZebraHandle zh)
103 {
104     const char *dir = res_get (zh->res, "chdir");
105     if (!dir)
106         return;
107     logf (LOG_DEBUG, "chdir %s", dir);
108 #ifdef WIN32
109     _chdir(dir);
110 #else
111     chdir (dir);
112 #endif
113 }
114
115 static void zebra_register_unlock (ZebraHandle zh);
116
117 static int zebra_register_lock (ZebraHandle zh)
118 {
119     time_t lastChange;
120     int state;
121
122     zh->errCode = 0;
123     zh->errString = 0;
124
125     zebra_chdir (zh);
126
127     state = zebra_server_lock_get_state(zh, &lastChange);
128
129     switch (state)
130     {
131     case 'c':
132         state = 1;
133         break;
134     default:
135         state = 0;
136     }
137     zebra_server_lock (zh, state);
138 #if HAVE_SYS_TIMES_H
139     times (&zh->tms1);
140 #endif
141     if (zh->registerState == state)
142     {
143         if (zh->registerChange >= lastChange)
144             return 0;
145         logf (LOG_LOG, "Register completely updated since last access");
146     }
147     else if (zh->registerState == -1)
148         logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
149               (long) getpid());
150     else
151         logf (LOG_LOG, "Register has changed state from %d to %d",
152               zh->registerState, state);
153     zh->registerChange = lastChange;
154     if (zh->records)
155     {
156         zebraExplain_close (zh->zei, 0, 0);
157         if (zh->dict)
158             dict_close (zh->dict);
159         if (zh->sortIdx)
160             sortIdx_close (zh->sortIdx);
161         if (zh->isam)
162             is_close (zh->isam);
163         if (zh->isamc)
164             isc_close (zh->isamc);
165         if (zh->isams)
166             isams_close (zh->isams);
167         rec_close (&zh->records);
168     }
169     bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
170     zh->registerState = state;
171
172     zh->isam = NULL;
173     zh->isamc = NULL;
174     zh->isams = NULL;
175     zh->dict = NULL;
176     zh->sortIdx = NULL;
177     zh->zei = NULL;
178
179     if (!(zh->records = rec_open (zh->bfs, 0, 0)))
180     {
181         logf (LOG_WARN, "rec_open");
182         zh->errCode = 2;
183     }
184     else
185     {
186         if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0, 0)))
187         {
188             logf (LOG_WARN, "dict_open");
189             zh->errCode = 2;
190         }
191         if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
192         {
193             logf (LOG_WARN, "sortIdx_open");
194             zh->errCode = 2;
195         }
196         if (res_get_match (zh->res, "isam", "i", NULL))
197         {
198             if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
199                                       sizeof (struct it_key), zh->res)))
200             {
201                 logf (LOG_WARN, "is_open");
202                 zh->errCode = 2;
203             }
204         }
205         else if (res_get_match (zh->res, "isam", "s", NULL))
206         {
207             struct ISAMS_M_s isams_m;
208             if (!(zh->isams = isams_open (zh->bfs, FNAME_ISAMS, 0,
209                                           key_isams_m(zh->res, &isams_m))))
210             {
211                 logf (LOG_WARN, "isams_open");
212                 zh->errCode = 2;
213             }
214         }
215         else
216         {
217             struct ISAMC_M_s isamc_m;
218             if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
219                                         0, key_isamc_m(zh->res, &isamc_m))))
220             {
221                 logf (LOG_WARN, "isc_open");
222                 zh->errCode = 2;
223             }
224         }
225         zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
226         if (!zh->zei)
227         {
228             logf (LOG_WARN, "Cannot obtain EXPLAIN information");
229             zh->errCode = 2;
230         }
231     }
232     if (zh->errCode)
233     {
234         zebra_register_unlock (zh);
235         zh->registerState = -1;
236         return -1;
237     }
238     return 0;
239 }
240
241 static void zebra_register_unlock (ZebraHandle zh)
242 {
243     static int waitSec = -1;
244
245 #if HAVE_SYS_TIMES_H
246     times (&zh->tms2);
247     logf (LOG_LOG, "user/system: %ld/%ld",
248                         (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
249                         (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
250 #endif
251     if (waitSec == -1)
252     {
253         char *s = res_get (zh->res, "debugRequestWait");
254         if (s)
255             waitSec = atoi (s);
256         else
257             waitSec = 0;
258     }
259 #ifdef WIN32
260 #else
261     if (waitSec > 0)
262         sleep (waitSec);
263 #endif
264     if (zh->registerState != -1)
265         zebra_server_unlock (zh, zh->registerState);
266 }
267
268 ZebraHandle zebra_open (const char *configName)
269 {
270     ZebraHandle zh;
271
272     zh = (ZebraHandle) xmalloc (sizeof(*zh));
273     if (!(zh->res = res_open (configName)))
274     {
275         logf (LOG_WARN, "Failed to read resources `%s'", configName);
276         return NULL;
277     }
278     zebra_chdir (zh);
279     zebra_server_lock_init (zh);
280     zh->dh = data1_create ();
281     if (!zh->dh)
282     {
283         zebra_server_lock_destroy (zh);
284         xfree (zh);
285         return 0;
286     }
287     zh->bfs = bfs_create (res_get (zh->res, "register"));
288     if (!zh->bfs)
289     {
290         zebra_server_lock_destroy (zh);
291         data1_destroy(zh->dh);
292         xfree (zh);
293         return 0;
294     }
295     bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
296     data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
297     zh->sets = NULL;
298     zh->registerState = -1;  /* trigger open of registers! */
299     zh->registerChange = 0;
300     zh->recTypes = recTypes_init (zh->dh);
301     recTypes_default_handlers (zh->recTypes);
302
303     zh->records = NULL;
304     zh->zebra_maps = zebra_maps_open (zh->res);
305     zh->rank_classes = NULL;
306     zh->errCode = 0;
307     zh->errString = 0;
308     
309     zebraRankInstall (zh, rank1_class);
310
311     if (!res_get (zh->res, "passwd"))
312         zh->passwd_db = NULL;
313     else
314     {
315         zh->passwd_db = passwd_db_open ();
316         if (!zh->passwd_db)
317             logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
318         else
319             passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
320     }
321     return zh;
322 }
323
324 void zebra_close (ZebraHandle zh)
325 {
326     if (!zh)
327         return;
328     zebra_chdir (zh);
329     if (zh->records)
330     {
331         resultSetDestroy (zh);
332         zebraExplain_close (zh->zei, 0, 0);
333         dict_close (zh->dict);
334         sortIdx_close (zh->sortIdx);
335         if (zh->isam)
336             is_close (zh->isam);
337         if (zh->isamc)
338             isc_close (zh->isamc);
339         if (zh->isams)
340             isams_close (zh->isams);
341         rec_close (&zh->records);
342         zebra_register_unlock (zh);
343     }
344     recTypes_destroy (zh->recTypes);
345     zebra_maps_close (zh->zebra_maps);
346     zebraRankDestroy (zh);
347     bfs_destroy (zh->bfs);
348     data1_destroy (zh->dh);
349     zebra_server_lock_destroy (zh);
350
351     if (zh->passwd_db)
352         passwd_db_close (zh->passwd_db);
353     res_close (zh->res);
354     xfree (zh);
355 }
356
357 struct map_baseinfo {
358     ZebraHandle zh;
359     NMEM mem;
360     int num_bases;
361     char **basenames;
362     int new_num_bases;
363     char **new_basenames;
364     int new_num_max;
365 };
366         
367 void map_basenames_func (void *vp, const char *name, const char *value)
368 {
369     struct map_baseinfo *p = (struct map_baseinfo *) vp;
370     int i, no;
371     char fromdb[128], todb[8][128];
372     
373     no =
374         sscanf (value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
375                 fromdb, todb[0], todb[1], todb[2], todb[3], todb[4],
376                 todb[5], todb[6], todb[7]);
377     if (no < 2)
378         return ;
379     no--;
380     for (i = 0; i<p->num_bases; i++)
381         if (p->basenames[i] && !strcmp (p->basenames[i], fromdb))
382         {
383             p->basenames[i] = 0;
384             for (i = 0; i < no; i++)
385             {
386                 if (p->new_num_bases == p->new_num_max)
387                     return;
388                 p->new_basenames[(p->new_num_bases)++] = 
389                     nmem_strdup (p->mem, todb[i]);
390             }
391             return;
392         }
393 }
394
395 void map_basenames (ZebraHandle zh, ODR stream,
396                     int *num_bases, char ***basenames)
397 {
398     struct map_baseinfo info;
399     struct map_baseinfo *p = &info;
400     int i;
401
402     info.zh = zh;
403     info.num_bases = *num_bases;
404     info.basenames = *basenames;
405     info.new_num_max = 128;
406     info.new_num_bases = 0;
407     info.new_basenames = (char **)
408         odr_malloc (stream, sizeof(*info.new_basenames) * info.new_num_max);
409     info.mem = stream->mem;
410
411     res_trav (zh->res, "mapdb", &info, map_basenames_func);
412     
413     for (i = 0; i<p->num_bases; i++)
414         if (p->basenames[i] && p->new_num_bases < p->new_num_max)
415         {
416             p->new_basenames[(p->new_num_bases)++] = 
417                 nmem_strdup (p->mem, p->basenames[i]);
418         }
419     *num_bases = info.new_num_bases;
420     *basenames = info.new_basenames;
421     for (i = 0; i<*num_bases; i++)
422         logf (LOG_LOG, "base %s", (*basenames)[i]);
423 }
424
425 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
426                        Z_RPNQuery *query, int num_bases, char **basenames, 
427                        const char *setname)
428 {
429     zh->hits = 0;
430     if (zebra_register_lock (zh))
431         return;
432     map_basenames (zh, stream, &num_bases, &basenames);
433     resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
434
435     zebra_register_unlock (zh);
436 }
437
438 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
439                              const char *setname, Z_RecordComposition *comp,
440                              oid_value input_format, int num_recs,
441                              ZebraRetrievalRecord *recs)
442 {
443     ZebraPosSet poset;
444     int i, *pos_array;
445
446     if (zebra_register_lock (zh))
447         return;
448     pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
449     for (i = 0; i<num_recs; i++)
450         pos_array[i] = recs[i].position;
451     poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
452     if (!poset)
453     {
454         logf (LOG_DEBUG, "zebraPosSetCreate error");
455         zh->errCode = 30;
456         zh->errString = nmem_strdup (stream->mem, setname);
457     }
458     else
459     {
460         for (i = 0; i<num_recs; i++)
461         {
462             if (!poset[i].sysno)
463             {
464                 char num_str[20];
465
466                 sprintf (num_str, "%d", pos_array[i]);  
467                 zh->errCode = 13;
468                 zh->errString = nmem_strdup (stream->mem, num_str);
469                 break;
470             }
471             else
472             {
473                 recs[i].errCode =
474                     zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
475                                         stream, input_format, comp,
476                                         &recs[i].format, &recs[i].buf,
477                                         &recs[i].len,
478                                         &recs[i].base);
479                 recs[i].errString = NULL;
480             }
481         }
482         zebraPosSetDestroy (zh, poset, num_recs);
483     }
484     zebra_register_unlock (zh);
485     xfree (pos_array);
486 }
487
488 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
489                  oid_value attributeset,
490                  int num_bases, char **basenames,
491                  int *position, int *num_entries, ZebraScanEntry **entries,
492                  int *is_partial)
493 {
494     if (zebra_register_lock (zh))
495     {
496         *entries = 0;
497         *num_entries = 0;
498         return;
499     }
500     map_basenames (zh, stream, &num_bases, &basenames);
501     rpn_scan (zh, stream, zapt, attributeset,
502               num_bases, basenames, position,
503               num_entries, entries, is_partial);
504     zebra_register_unlock (zh);
505 }
506
507 void zebra_sort (ZebraHandle zh, ODR stream,
508                  int num_input_setnames, const char **input_setnames,
509                  const char *output_setname, Z_SortKeySpecList *sort_sequence,
510                  int *sort_status)
511 {
512     if (zebra_register_lock (zh))
513         return;
514     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
515                    output_setname, sort_sequence, sort_status);
516     zebra_register_unlock (zh);
517 }
518
519 int zebra_errCode (ZebraHandle zh)
520 {
521     return zh->errCode;
522 }
523
524 const char *zebra_errString (ZebraHandle zh)
525 {
526     return diagbib1_str (zh->errCode);
527 }
528
529 char *zebra_errAdd (ZebraHandle zh)
530 {
531     return zh->errString;
532 }
533
534 int zebra_hits (ZebraHandle zh)
535 {
536     return zh->hits;
537 }
538
539 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
540 {
541     if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
542         return 0;
543     return 1;
544 }
545
546 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
547 {
548
549 }
550
551 void zebra_setRecordType (ZebraHandle zh, const char *type)
552 {
553
554 }
555
556 void zebra_setGroup (ZebraHandle zh, const char *group)
557 {
558
559 }
560
561 void zebra_admin (ZebraHandle zh, const char *command)
562 {
563
564 }