Changed functions isc_getmethod, isams_getmethod.
[idzebra-moved-to-github.git] / index / zserver.c
1 /*
2  * Copyright (C) 1995-1999, Index Data 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zserver.c,v $
7  * Revision 1.71  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.70  1999/06/10 12:14:56  adam
12  * Fixed to use bend_start instead of pre_init.
13  *
14  * Revision 1.69  1999/06/10 09:20:03  adam
15  * Minor change to pre_init handler.
16  *
17  * Revision 1.68  1999/05/26 07:49:13  adam
18  * C++ compilation.
19  *
20  * Revision 1.67  1999/02/02 14:51:14  adam
21  * Updated WIN32 code specific sections. Changed header.
22  *
23  * Revision 1.66  1998/10/28 10:54:41  adam
24  * SDRKit integration.
25  *
26  * Revision 1.65  1998/10/18 07:54:54  adam
27  * Additional info added for diagnostics 114 (Unsupported use attribute) and
28  * 121 (Unsupported attribute set).
29  *
30  * Revision 1.64  1998/09/22 10:48:21  adam
31  * Minor changes in search API.
32  *
33  * Revision 1.63  1998/09/02 13:53:21  adam
34  * Extra parameter decode added to search routines to implement
35  * persistent queries.
36  *
37  * Revision 1.62  1998/08/06 14:35:28  adam
38  * Routine bend_deleterequest removed.
39  *
40  * Revision 1.61  1998/06/24 12:16:15  adam
41  * Support for relations on text operands. Open range support in
42  * DFA module (i.e. [-j], [g-]).
43  *
44  * Revision 1.60  1998/06/22 11:36:49  adam
45  * Added authentication check facility to zebra.
46  *
47  * Revision 1.59  1998/06/12 12:22:13  adam
48  * Work on Zebra API.
49  *
50  * Revision 1.58  1998/05/27 16:57:46  adam
51  * Zebra returns surrogate diagnostic for single records when
52  * appropriate.
53  *
54  * Revision 1.57  1998/04/03 14:45:18  adam
55  * Fixed setting of last_in_set in bend_fetch.
56  *
57  * Revision 1.56  1998/03/05 08:45:13  adam
58  * New result set model and modular ranking system. Moved towards
59  * descent server API. System information stored as "SGML" records.
60  *
61  * Revision 1.55  1998/02/10 12:03:06  adam
62  * Implemented Sort.
63  *
64  * Revision 1.54  1998/01/29 13:39:13  adam
65  * Compress ISAM is default.
66  *
67  * Revision 1.53  1998/01/12 15:04:09  adam
68  * The test option (-s) only uses read-lock (and not write lock).
69  *
70  * Revision 1.52  1997/11/18 10:05:08  adam
71  * Changed character map facility so that admin can specify character
72  * mapping files for each register type, w, p, etc.
73  *
74  * Revision 1.51  1997/10/27 14:33:06  adam
75  * Moved towards generic character mapping depending on "structure"
76  * field in abstract syntax file. Fixed a few memory leaks. Fixed
77  * bug with negative integers when doing searches with relational
78  * operators.
79  *
80  * Revision 1.50  1997/09/29 09:08:36  adam
81  * Revised locking system to be thread safe for the server.
82  *
83  * Revision 1.49  1997/09/25 14:57:23  adam
84  * Windows NT port.
85  *
86  * Revision 1.48  1997/09/17 12:19:19  adam
87  * Zebra version corresponds to YAZ version 1.4.
88  * Changed Zebra server so that it doesn't depend on global common_resource.
89  *
90  * Revision 1.47  1997/09/04 13:58:36  adam
91  * New retrieve/extract method tellf (added).
92  * Added O_BINARY for open calls.
93  *
94  * Revision 1.46  1997/07/28 08:30:47  adam
95  * Server returns diagnostic 14 when record doesn't exist.
96  *
97  * Revision 1.45  1996/12/23 15:30:45  adam
98  * Work on truncation.
99  * Bug fix: result sets weren't deleted after server shut down.
100  *
101  * Revision 1.44  1996/12/11 12:08:01  adam
102  * Added better compression.
103  *
104  * Revision 1.43  1996/11/15 15:03:58  adam
105  * Logging of execution speed by using the times(2) call.
106  *
107  * Revision 1.42  1996/11/08  11:10:36  adam
108  * Buffers used during file match got bigger.
109  * Compressed ISAM support everywhere.
110  * Bug fixes regarding masking characters in queries.
111  * Redesigned Regexp-2 queries.
112  *
113  * Revision 1.41  1996/10/29 14:09:56  adam
114  * Use of cisam system - enabled if setting isamc is 1.
115  *
116  * Revision 1.40  1996/06/04 10:19:02  adam
117  * Minor changes - removed include of ctype.h.
118  *
119  * Revision 1.39  1996/05/31  09:07:05  quinn
120  * Work on character-set handling
121  *
122  * Revision 1.38  1996/05/14  11:34:01  adam
123  * Scan support in multiple registers/databases.
124  *
125  * Revision 1.37  1996/05/14  06:16:48  adam
126  * Compact use/set bytes used in search service.
127  *
128  * Revision 1.36  1996/05/01 13:46:37  adam
129  * First work on multiple records in one file.
130  * New option, -offset, to the "unread" command in the filter module.
131  *
132  * Revision 1.35  1996/03/26  16:01:14  adam
133  * New setting lockPath: directory of various lock files.
134  *
135  * Revision 1.34  1996/03/20  09:36:46  adam
136  * Function dict_lookup_grep got extra parameter, init_pos, which marks
137  * from which position in pattern approximate pattern matching should occur.
138  * Approximate pattern matching is used in relevance=re-2.
139  *
140  * Revision 1.33  1996/01/17  14:57:56  adam
141  * Prototype changed for reader functions in extract/retrieve. File
142  *  is identified by 'void *' instead of 'int.
143  *
144  * Revision 1.32  1995/12/11  09:12:58  adam
145  * The rec_get function returns NULL if record doesn't exist - will
146  * happen in the server if the result set records have been deleted since
147  * the creation of the set (i.e. the search).
148  * The server saves a result temporarily if it is 'volatile', i.e. the
149  * set is register dependent.
150  *
151  * Revision 1.31  1995/12/08  16:22:56  adam
152  * Work on update while servers are running. Three lock files introduced.
153  * The servers reload their registers when necessary, but they don't
154  * reestablish result sets yet.
155  *
156  * Revision 1.30  1995/12/07  17:38:48  adam
157  * Work locking mechanisms for concurrent updates/commit.
158  *
159  * Revision 1.29  1995/12/04  14:22:32  adam
160  * Extra arg to recType_byName.
161  * Started work on new regular expression parsed input to
162  * structured records.
163  *
164  * Revision 1.28  1995/11/28  09:09:48  adam
165  * Zebra config renamed.
166  * Use setting 'recordId' to identify record now.
167  * Bug fix in recindex.c: rec_release_blocks was invokeded even
168  * though the blocks were already released.
169  * File traversal properly deletes records when needed.
170  *
171  * Revision 1.27  1995/11/27  13:58:54  adam
172  * New option -t. storeStore data implemented in server.
173  *
174  * Revision 1.26  1995/11/25  10:24:07  adam
175  * More record fields - they are enumerated now.
176  * New options: flagStoreData flagStoreKey.
177  *
178  * Revision 1.25  1995/11/21  15:29:13  adam
179  * Config file 'base' read by default by both indexer and server.
180  *
181  * Revision 1.24  1995/11/20  16:59:47  adam
182  * New update method: the 'old' keys are saved for each records.
183  *
184  * Revision 1.23  1995/11/16  17:00:56  adam
185  * Better logging of rpn query.
186  *
187  * Revision 1.22  1995/11/16  15:34:55  adam
188  * Uses new record management system in both indexer and server.
189  *
190  * Revision 1.21  1995/11/01  16:25:52  quinn
191  * *** empty log message ***
192  *
193  * Revision 1.20  1995/10/27  14:00:12  adam
194  * Implemented detection of database availability.
195  *
196  * Revision 1.19  1995/10/17  18:02:11  adam
197  * New feature: databases. Implemented as prefix to words in dictionary.
198  *
199  * Revision 1.18  1995/10/16  14:03:09  quinn
200  * Changes to support element set names and espec1
201  *
202  * Revision 1.17  1995/10/16  09:32:40  adam
203  * More work on relational op.
204  *
205  * Revision 1.16  1995/10/13  12:26:44  adam
206  * Optimization of truncation.
207  *
208  * Revision 1.15  1995/10/12  12:40:55  adam
209  * Bug fixes in rpn_prox.
210  *
211  * Revision 1.14  1995/10/09  16:18:37  adam
212  * Function dict_lookup_grep got extra client data parameter.
213  *
214  * Revision 1.13  1995/10/06  14:38:00  adam
215  * New result set method: r_score.
216  * Local no (sysno) and score is transferred to retrieveCtrl.
217  *
218  * Revision 1.12  1995/10/06  13:52:06  adam
219  * Bug fixes. Handler may abort further scanning.
220  *
221  * Revision 1.11  1995/10/06  10:43:57  adam
222  * Scan added. 'occurrences' in scan entries not set yet.
223  *
224  * Revision 1.10  1995/10/02  16:43:32  quinn
225  * Set default resulting record type in fetch.
226  *
227  * Revision 1.9  1995/10/02  15:18:52  adam
228  * New member in recRetrieveCtrl: diagnostic.
229  *
230  * Revision 1.8  1995/09/28  09:19:47  adam
231  * xfree/xmalloc used everywhere.
232  * Extract/retrieve method seems to work for text records.
233  *
234  * Revision 1.7  1995/09/27  16:17:32  adam
235  * More work on retrieve.
236  *
237  * Revision 1.6  1995/09/08  08:53:22  adam
238  * Record buffer maintained in server_info.
239  *
240  * Revision 1.5  1995/09/06  16:11:18  adam
241  * Option: only one word key per file.
242  *
243  * Revision 1.4  1995/09/06  10:33:04  adam
244  * More work on present. Some log messages removed.
245  *
246  * Revision 1.3  1995/09/05  15:28:40  adam
247  * More work on search engine.
248  *
249  * Revision 1.2  1995/09/04  12:33:43  adam
250  * Various cleanup. YAZ util used instead.
251  *
252  * Revision 1.1  1995/09/04  09:10:41  adam
253  * More work on index add/del/update.
254  * Merge sort implemented.
255  * Initial work on z39 server.
256  *
257  */
258
259 #include <stdio.h>
260 #include <assert.h>
261 #include <fcntl.h>
262 #ifdef WIN32
263 #include <io.h>
264 #include <process.h>
265 #else
266 #include <unistd.h>
267 #endif
268
269 #include <data1.h>
270
271 #include "zserver.h"
272
273 #ifndef ZEBRASDR
274 #define ZEBRASDR 0
275 #endif
276 #if ZEBRASDR
277 #include "zebrasdr.h"
278 #endif
279
280 static int bend_sort (void *handle, bend_sort_rr *rr);
281
282 bend_initresult *bend_init (bend_initrequest *q)
283 {
284     bend_initresult *r = (bend_initresult *)
285         odr_malloc (q->stream, sizeof(*r));
286     ZebraHandle zh;
287     struct statserv_options_block *sob;
288     char *user = NULL;
289     char *passwd = NULL;
290
291     r->errcode = 0;
292     r->errstring = 0;
293     q->bend_sort = bend_sort;
294
295     logf (LOG_DEBUG, "bend_init");
296
297     sob = statserv_getcontrol ();
298     if (!(zh = zebra_open (sob->configname)))
299     {
300         logf (LOG_FATAL, "Failed to open Zebra `%s'", sob->configname);
301         r->errcode = 1;
302         return r;
303     }
304     if (q->auth)
305     {
306         if (q->auth->which == Z_IdAuthentication_open)
307         {
308             char *openpass = xstrdup (q->auth->u.open);
309             char *cp = strchr (openpass, '/');
310             if (cp)
311             {
312                 *cp = '\0';
313                 user = nmem_strdup (odr_getmem (q->stream), openpass);
314                 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
315             }
316             xfree (openpass);
317         }
318     }
319     if (zebra_auth (zh, user, passwd))
320     {
321         r->errcode = 222;
322         r->errstring = user;
323         zebra_close (zh);
324         return r;
325     }
326     r->handle = zh;
327     return r;
328 }
329
330 bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
331 {
332     ZebraHandle zh = (ZebraHandle) handle;
333     bend_searchresult *r = (bend_searchresult *)
334         odr_malloc (q->stream, sizeof(*r));
335
336     r->hits = 0;
337     r->errcode = 0;
338     r->errstring = NULL;
339     
340     logf (LOG_LOG, "ResultSet '%s'", q->setname);
341     switch (q->query->which)
342     {
343     case Z_Query_type_1: case Z_Query_type_101:
344         zebra_search_rpn (zh, q->decode, q->stream, q->query->u.type_1,
345                           q->num_bases, q->basenames, q->setname);
346         r->errcode = zh->errCode;
347         r->errstring = zh->errString;
348         r->hits = zh->hits;
349         break;
350     case Z_Query_type_2:
351         r->errcode = 107;
352         r->errstring = "type-2";
353         break;
354     default:
355         r->errcode = 107;
356     }
357     return r;
358 }
359
360 bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
361 {
362     ZebraHandle zh = (ZebraHandle) handle;
363     bend_fetchresult *r = (bend_fetchresult *)
364         odr_malloc (q->stream, sizeof(*r));
365     ZebraRetrievalRecord retrievalRecord;
366
367     retrievalRecord.position = q->number;
368     
369     r->last_in_set = 0;
370     zebra_records_retrieve (zh, q->stream, q->setname, q->comp,
371                             q->format, 1, &retrievalRecord);
372     if (zh->errCode)                  /* non Surrogate Diagnostic */
373     {
374         r->errcode = zh->errCode;
375         r->errstring = zh->errString;
376     }
377     else if (retrievalRecord.errCode) /* Surrogate Diagnostic */
378     {
379         q->surrogate_flag = 1;
380         r->errcode = retrievalRecord.errCode;
381         r->errstring = retrievalRecord.errString;
382         r->basename = retrievalRecord.base;
383     }
384     else                              /* Database Record */
385     {
386         r->errcode = 0;
387         r->basename = retrievalRecord.base;
388         r->record = retrievalRecord.buf;
389         r->len = retrievalRecord.len;
390         r->format = retrievalRecord.format;
391     }
392     return r;
393 }
394
395 bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
396 {
397     ZebraScanEntry *entries;
398     ZebraHandle zh = (ZebraHandle) handle;
399     bend_scanresult *r = (bend_scanresult *)
400         odr_malloc (q->stream, sizeof(*r));
401     int is_partial, i;
402     
403     r->term_position = q->term_position;
404     r->num_entries = q->num_entries;
405
406     r->entries = (struct scan_entry *)
407         odr_malloc (q->stream, sizeof(*r->entries) * q->num_entries);
408     zebra_scan (zh, q->stream, q->term,
409                 q->attributeset,
410                 q->num_bases, q->basenames,
411                 &r->term_position,
412                 &r->num_entries, &entries, &is_partial);
413     if (is_partial)
414         r->status = BEND_SCAN_PARTIAL;
415     else
416         r->status = BEND_SCAN_SUCCESS;
417     for (i = 0; i < r->num_entries; i++)
418     {
419         r->entries[i].term = entries[i].term;
420         r->entries[i].occurrences = entries[i].occurrences;
421     }
422     r->errcode = zh->errCode;
423     r->errstring = zh->errString;
424     return r;
425 }
426
427 void bend_close (void *handle)
428 {
429     zebra_close ((ZebraHandle) handle);
430 }
431
432 int bend_sort (void *handle, bend_sort_rr *rr)
433 {
434     ZebraHandle zh = (ZebraHandle) handle;
435
436     zebra_sort (zh, rr->stream,
437                 rr->num_input_setnames, (const char **) rr->input_setnames,
438                 rr->output_setname, rr->sort_sequence, &rr->sort_status);
439     rr->errcode = zh->errCode;
440     rr->errstring = zh->errString;
441     return 0;
442 }
443
444 #ifndef WIN32
445 static void bend_start (struct statserv_options_block *sob)
446 {
447     if (!sob->inetd) 
448     {
449         char *pidfile = "zebrasrv.pid";
450         int fd = creat (pidfile, 0666);
451         if (fd == -1)
452             logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
453         else
454         {
455             char pidstr[30];
456         
457             sprintf (pidstr, "%ld", (long) getpid ());
458             write (fd, pidstr, strlen(pidstr));
459             close (fd);
460         }
461     }
462 }
463 #endif
464
465 int main (int argc, char **argv)
466 {
467     struct statserv_options_block *sob;
468
469     sob = statserv_getcontrol ();
470     strcpy (sob->configname, FNAME_CONFIG);
471 #ifndef WIN32
472     sob->bend_start = bend_start;
473 #endif
474     statserv_setcontrol (sob);
475
476 #if ZEBRASDR
477     zebraSdr_std ();
478 #endif
479     return statserv_main (argc, argv);
480 }