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