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