Updated record control system.
[idzebra-moved-to-github.git] / index / zebraapi.c
1 /*
2  * Copyright (C) 1995-1998, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebraapi.c,v $
7  * Revision 1.11  1998-10-16 08:14:34  adam
8  * Updated record control system.
9  *
10  * Revision 1.10  1998/09/22 10:03:42  adam
11  * Changed result sets to be persistent in the sense that they can
12  * be re-searched if needed.
13  * Fixed memory leak in rsm_or.
14  *
15  * Revision 1.9  1998/09/02 13:53:17  adam
16  * Extra parameter decode added to search routines to implement
17  * persistent queries.
18  *
19  * Revision 1.8  1998/08/24 17:29:23  adam
20  * Minor changes.
21  *
22  * Revision 1.7  1998/06/24 12:16:13  adam
23  * Support for relations on text operands. Open range support in
24  * DFA module (i.e. [-j], [g-]).
25  *
26  * Revision 1.6  1998/06/22 11:36:47  adam
27  * Added authentication check facility to zebra.
28  *
29  * Revision 1.5  1998/06/13 00:14:08  adam
30  * Minor changes.
31  *
32  * Revision 1.4  1998/06/12 12:22:12  adam
33  * Work on Zebra API.
34  *
35  * Revision 1.3  1998/05/27 16:57:44  adam
36  * Zebra returns surrogate diagnostic for single records when
37  * appropriate.
38  *
39  * Revision 1.2  1998/05/20 10:12:19  adam
40  * Implemented automatic EXPLAIN database maintenance.
41  * Modified Zebra to work with ASN.1 compiled version of YAZ.
42  *
43  * Revision 1.1  1998/03/05 08:45:13  adam
44  * New result set model and modular ranking system. Moved towards
45  * descent server API. System information stored as "SGML" records.
46  *
47  */
48
49 #include <stdio.h>
50 #ifdef WINDOWS
51 #include <io.h>
52 #include <process.h>
53 #else
54 #include <unistd.h>
55 #endif
56
57 #include <diagbib1.h>
58 #include "zserver.h"
59
60 static int zebra_register_lock (ZebraHandle zh)
61 {
62     time_t lastChange;
63     int state = zebra_server_lock_get_state(zh, &lastChange);
64
65     switch (state)
66     {
67     case 'c':
68         state = 1;
69         break;
70     default:
71         state = 0;
72     }
73     zebra_server_lock (zh, state);
74 #if USE_TIMES
75     times (&zh->tms1);
76 #endif
77     if (zh->registerState == state)
78     {
79         if (zh->registerChange >= lastChange)
80             return 0;
81         logf (LOG_LOG, "Register completely updated since last access");
82     }
83     else if (zh->registerState == -1)
84         logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
85               (long) getpid());
86     else
87         logf (LOG_LOG, "Register has changed state from %d to %d",
88               zh->registerState, state);
89     zh->registerChange = lastChange;
90     if (zh->records)
91     {
92         zebraExplain_close (zh->zei, 0, 0);
93         dict_close (zh->dict);
94         sortIdx_close (zh->sortIdx);
95         if (zh->isam)
96             is_close (zh->isam);
97         if (zh->isamc)
98             isc_close (zh->isamc);
99         rec_close (&zh->records);
100     }
101     bf_cache (zh->bfs, state ? res_get (zh->res, "shadow") : NULL);
102     zh->registerState = state;
103     zh->records = rec_open (zh->bfs, 0);
104     if (!(zh->dict = dict_open (zh->bfs, FNAME_DICT, 40, 0)))
105     {
106         logf (LOG_WARN, "dict_open");
107         return -1;
108     }
109     if (!(zh->sortIdx = sortIdx_open (zh->bfs, 0)))
110     {
111         logf (LOG_WARN, "sortIdx_open");
112         return -1;
113     }
114     zh->isam = NULL;
115     zh->isamc = NULL;
116     if (!res_get_match (zh->res, "isam", "i", NULL))
117     {
118         if (!(zh->isamc = isc_open (zh->bfs, FNAME_ISAMC,
119                                     0, key_isamc_m(zh->res))))
120         {
121             logf (LOG_WARN, "isc_open");
122             return -1;
123         }
124
125     }
126     else
127     {
128         if (!(zh->isam = is_open (zh->bfs, FNAME_ISAM, key_compare, 0,
129                                   sizeof (struct it_key), zh->res)))
130         {
131             logf (LOG_WARN, "is_open");
132             return -1;
133         }
134     }
135     zh->zei = zebraExplain_open (zh->records, zh->dh, zh->res, 0, 0, 0);
136
137     return 0;
138 }
139
140 static void zebra_register_unlock (ZebraHandle zh)
141 {
142     static int waitSec = -1;
143
144 #if USE_TIMES
145     times (&zh->tms2);
146     logf (LOG_LOG, "user/system: %ld/%ld",
147                         (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
148                         (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
149 #endif
150     if (waitSec == -1)
151     {
152         char *s = res_get (zh->res, "debugRequestWait");
153         if (s)
154             waitSec = atoi (s);
155         else
156             waitSec = 0;
157     }
158 #ifdef WINDOWS
159 #else
160     if (waitSec > 0)
161         sleep (waitSec);
162 #endif
163     if (zh->registerState != -1)
164         zebra_server_unlock (zh, zh->registerState);
165 }
166
167 ZebraHandle zebra_open (const char *configName)
168 {
169     ZebraHandle zh = xmalloc (sizeof(*zh));
170
171     if (!(zh->res = res_open (configName)))
172     {
173         logf (LOG_WARN, "Failed to read resources `%s'", configName);
174         return NULL;
175     }
176     zebra_server_lock_init (zh);
177     zh->dh = data1_create ();
178     zh->bfs = bfs_create (res_get (zh->res, "register"));
179     bf_lockDir (zh->bfs, res_get (zh->res, "lockDir"));
180     data1_set_tabpath (zh->dh, res_get(zh->res, "profilePath"));
181     zh->sets = NULL;
182     zh->registerState = -1;  /* trigger open of registers! */
183     zh->registerChange = 0;
184     zh->recTypes = recTypes_init (zh->dh);
185     recTypes_default_handlers (zh->recTypes);
186
187     zh->records = NULL;
188     zh->zebra_maps = zebra_maps_open (zh->res);
189     zh->rank_classes = NULL;
190     zh->errCode = 0;
191     zh->errString = 0;
192     
193     zebraRankInstall (zh, rank1_class);
194
195     if (!res_get (zh->res, "passwd"))
196         zh->passwd_db = NULL;
197     else
198     {
199         zh->passwd_db = passwd_db_open ();
200         if (!zh->passwd_db)
201             logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
202         else
203             passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
204     }
205     return zh;
206 }
207
208 void zebra_close (ZebraHandle zh)
209 {
210     if (zh->records)
211     {
212         resultSetDestroy (zh);
213         zebraExplain_close (zh->zei, 0, 0);
214         dict_close (zh->dict);
215         sortIdx_close (zh->sortIdx);
216         if (zh->isam)
217             is_close (zh->isam);
218         if (zh->isamc)
219             isc_close (zh->isamc);
220         rec_close (&zh->records);
221         zebra_register_unlock (zh);
222     }
223     recTypes_destroy (zh->recTypes);
224     zebra_maps_close (zh->zebra_maps);
225     zebraRankDestroy (zh);
226     bfs_destroy (zh->bfs);
227     data1_destroy (zh->dh);
228     zebra_server_lock_destroy (zh);
229
230     if (zh->passwd_db)
231         passwd_db_close (zh->passwd_db);
232     res_close (zh->res);
233     xfree (zh);
234 }
235
236 void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
237                        Z_RPNQuery *query, int num_bases, char **basenames, 
238                        const char *setname)
239 {
240     zebra_register_lock (zh);
241     zh->errCode = 0;
242     zh->errString = NULL;
243     zh->hits = 0;
244
245     resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
246
247     zebra_register_unlock (zh);
248 }
249
250 void zebra_records_retrieve (ZebraHandle zh, ODR stream,
251                              const char *setname, Z_RecordComposition *comp,
252                              oid_value input_format, int num_recs,
253                              ZebraRetrievalRecord *recs)
254 {
255     ZebraPosSet poset;
256     int i, *pos_array;
257
258     zh->errCode = 0;
259     zh->errString = NULL;
260     pos_array = xmalloc (num_recs * sizeof(*pos_array));
261     for (i = 0; i<num_recs; i++)
262         pos_array[i] = recs[i].position;
263
264     zebra_register_lock (zh);
265
266     poset = zebraPosSetCreate (zh, setname, num_recs, pos_array);
267     if (!poset)
268     {
269         logf (LOG_DEBUG, "zebraPosSetCreate error");
270         zh->errCode = 13;
271     }
272     else
273     {
274         for (i = 0; i<num_recs; i++)
275         {
276             if (!poset[i].sysno)
277             {
278                 zh->errCode = 13;
279                 logf (LOG_DEBUG, "Out of range. pos=%d", pos_array[i]);
280             }
281             else
282             {
283                 recs[i].errCode =
284                     zebra_record_fetch (zh, poset[i].sysno, poset[i].score,
285                                         stream, input_format, comp,
286                                         &recs[i].format, &recs[i].buf,
287                                         &recs[i].len,
288                                         &recs[i].base);
289                 recs[i].errString = NULL;
290             }
291         }
292         zebraPosSetDestroy (zh, poset, num_recs);
293     }
294     zebra_register_unlock (zh);
295     xfree (pos_array);
296 }
297
298 void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
299                  oid_value attributeset,
300                  int num_bases, char **basenames,
301                  int *position, int *num_entries, ZebraScanEntry **entries,
302                  int *is_partial)
303 {
304     zh->errCode = 0;
305     zh->errString = NULL;
306     zebra_register_lock (zh);
307     rpn_scan (zh, stream, zapt, attributeset,
308               num_bases, basenames, position,
309               num_entries, entries, is_partial);
310     zebra_register_unlock (zh);
311 }
312
313 void zebra_sort (ZebraHandle zh, ODR stream,
314                  int num_input_setnames, const char **input_setnames,
315                  const char *output_setname, Z_SortKeySpecList *sort_sequence,
316                  int *sort_status)
317 {
318     zh->errCode = 0;
319     zh->errString = NULL;
320     zebra_register_lock (zh);
321     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
322                    output_setname, sort_sequence, sort_status);
323     zebra_register_unlock (zh);
324 }
325
326 int zebra_errCode (ZebraHandle zh)
327 {
328     return zh->errCode;
329 }
330
331 const char *zebra_errString (ZebraHandle zh)
332 {
333     return diagbib1_str (zh->errCode);
334 }
335
336 char *zebra_errAdd (ZebraHandle zh)
337 {
338     return zh->errString;
339 }
340
341 int zebra_hits (ZebraHandle zh)
342 {
343     return zh->hits;
344 }
345
346 int zebra_auth (ZebraHandle zh, const char *user, const char *pass)
347 {
348     if (!zh->passwd_db || !passwd_db_auth (zh->passwd_db, user, pass))
349         return 0;
350     return 1;
351 }
352
353 void zebra_setDB (ZebraHandle zh, int num_bases, char **basenames)
354 {
355
356 }
357
358 void zebra_setRecordType (ZebraHandle zh, const char *type)
359 {
360
361 }
362
363 void zebra_setGroup (ZebraHandle zh, const char *group)
364 {
365
366 }
367
368 void zebra_admin (ZebraHandle zh, const char *command)
369 {
370
371 }