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