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