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