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