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