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