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