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