Compress ISAM is default.
[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.54  1998-01-29 13:39:13  adam
8  * Compress ISAM is default.
9  *
10  * Revision 1.53  1998/01/12 15:04:09  adam
11  * The test option (-s) only uses read-lock (and not write lock).
12  *
13  * Revision 1.52  1997/11/18 10:05:08  adam
14  * Changed character map facility so that admin can specify character
15  * mapping files for each register type, w, p, etc.
16  *
17  * Revision 1.51  1997/10/27 14:33:06  adam
18  * Moved towards generic character mapping depending on "structure"
19  * field in abstract syntax file. Fixed a few memory leaks. Fixed
20  * bug with negative integers when doing searches with relational
21  * operators.
22  *
23  * Revision 1.50  1997/09/29 09:08:36  adam
24  * Revised locking system to be thread safe for the server.
25  *
26  * Revision 1.49  1997/09/25 14:57:23  adam
27  * Windows NT port.
28  *
29  * Revision 1.48  1997/09/17 12:19:19  adam
30  * Zebra version corresponds to YAZ version 1.4.
31  * Changed Zebra server so that it doesn't depend on global common_resource.
32  *
33  * Revision 1.47  1997/09/04 13:58:36  adam
34  * New retrieve/extract method tellf (added).
35  * Added O_BINARY for open calls.
36  *
37  * Revision 1.46  1997/07/28 08:30:47  adam
38  * Server returns diagnostic 14 when record doesn't exist.
39  *
40  * Revision 1.45  1996/12/23 15:30:45  adam
41  * Work on truncation.
42  * Bug fix: result sets weren't deleted after server shut down.
43  *
44  * Revision 1.44  1996/12/11 12:08:01  adam
45  * Added better compression.
46  *
47  * Revision 1.43  1996/11/15 15:03:58  adam
48  * Logging of execution speed by using the times(2) call.
49  *
50  * Revision 1.42  1996/11/08  11:10:36  adam
51  * Buffers used during file match got bigger.
52  * Compressed ISAM support everywhere.
53  * Bug fixes regarding masking characters in queries.
54  * Redesigned Regexp-2 queries.
55  *
56  * Revision 1.41  1996/10/29 14:09:56  adam
57  * Use of cisam system - enabled if setting isamc is 1.
58  *
59  * Revision 1.40  1996/06/04 10:19:02  adam
60  * Minor changes - removed include of ctype.h.
61  *
62  * Revision 1.39  1996/05/31  09:07:05  quinn
63  * Work on character-set handling
64  *
65  * Revision 1.38  1996/05/14  11:34:01  adam
66  * Scan support in multiple registers/databases.
67  *
68  * Revision 1.37  1996/05/14  06:16:48  adam
69  * Compact use/set bytes used in search service.
70  *
71  * Revision 1.36  1996/05/01 13:46:37  adam
72  * First work on multiple records in one file.
73  * New option, -offset, to the "unread" command in the filter module.
74  *
75  * Revision 1.35  1996/03/26  16:01:14  adam
76  * New setting lockPath: directory of various lock files.
77  *
78  * Revision 1.34  1996/03/20  09:36:46  adam
79  * Function dict_lookup_grep got extra parameter, init_pos, which marks
80  * from which position in pattern approximate pattern matching should occur.
81  * Approximate pattern matching is used in relevance=re-2.
82  *
83  * Revision 1.33  1996/01/17  14:57:56  adam
84  * Prototype changed for reader functions in extract/retrieve. File
85  *  is identified by 'void *' instead of 'int.
86  *
87  * Revision 1.32  1995/12/11  09:12:58  adam
88  * The rec_get function returns NULL if record doesn't exist - will
89  * happen in the server if the result set records have been deleted since
90  * the creation of the set (i.e. the search).
91  * The server saves a result temporarily if it is 'volatile', i.e. the
92  * set is register dependent.
93  *
94  * Revision 1.31  1995/12/08  16:22:56  adam
95  * Work on update while servers are running. Three lock files introduced.
96  * The servers reload their registers when necessary, but they don't
97  * reestablish result sets yet.
98  *
99  * Revision 1.30  1995/12/07  17:38:48  adam
100  * Work locking mechanisms for concurrent updates/commit.
101  *
102  * Revision 1.29  1995/12/04  14:22:32  adam
103  * Extra arg to recType_byName.
104  * Started work on new regular expression parsed input to
105  * structured records.
106  *
107  * Revision 1.28  1995/11/28  09:09:48  adam
108  * Zebra config renamed.
109  * Use setting 'recordId' to identify record now.
110  * Bug fix in recindex.c: rec_release_blocks was invokeded even
111  * though the blocks were already released.
112  * File traversal properly deletes records when needed.
113  *
114  * Revision 1.27  1995/11/27  13:58:54  adam
115  * New option -t. storeStore data implemented in server.
116  *
117  * Revision 1.26  1995/11/25  10:24:07  adam
118  * More record fields - they are enumerated now.
119  * New options: flagStoreData flagStoreKey.
120  *
121  * Revision 1.25  1995/11/21  15:29:13  adam
122  * Config file 'base' read by default by both indexer and server.
123  *
124  * Revision 1.24  1995/11/20  16:59:47  adam
125  * New update method: the 'old' keys are saved for each records.
126  *
127  * Revision 1.23  1995/11/16  17:00:56  adam
128  * Better logging of rpn query.
129  *
130  * Revision 1.22  1995/11/16  15:34:55  adam
131  * Uses new record management system in both indexer and server.
132  *
133  * Revision 1.21  1995/11/01  16:25:52  quinn
134  * *** empty log message ***
135  *
136  * Revision 1.20  1995/10/27  14:00:12  adam
137  * Implemented detection of database availability.
138  *
139  * Revision 1.19  1995/10/17  18:02:11  adam
140  * New feature: databases. Implemented as prefix to words in dictionary.
141  *
142  * Revision 1.18  1995/10/16  14:03:09  quinn
143  * Changes to support element set names and espec1
144  *
145  * Revision 1.17  1995/10/16  09:32:40  adam
146  * More work on relational op.
147  *
148  * Revision 1.16  1995/10/13  12:26:44  adam
149  * Optimization of truncation.
150  *
151  * Revision 1.15  1995/10/12  12:40:55  adam
152  * Bug fixes in rpn_prox.
153  *
154  * Revision 1.14  1995/10/09  16:18:37  adam
155  * Function dict_lookup_grep got extra client data parameter.
156  *
157  * Revision 1.13  1995/10/06  14:38:00  adam
158  * New result set method: r_score.
159  * Local no (sysno) and score is transferred to retrieveCtrl.
160  *
161  * Revision 1.12  1995/10/06  13:52:06  adam
162  * Bug fixes. Handler may abort further scanning.
163  *
164  * Revision 1.11  1995/10/06  10:43:57  adam
165  * Scan added. 'occurrences' in scan entries not set yet.
166  *
167  * Revision 1.10  1995/10/02  16:43:32  quinn
168  * Set default resulting record type in fetch.
169  *
170  * Revision 1.9  1995/10/02  15:18:52  adam
171  * New member in recRetrieveCtrl: diagnostic.
172  *
173  * Revision 1.8  1995/09/28  09:19:47  adam
174  * xfree/xmalloc used everywhere.
175  * Extract/retrieve method seems to work for text records.
176  *
177  * Revision 1.7  1995/09/27  16:17:32  adam
178  * More work on retrieve.
179  *
180  * Revision 1.6  1995/09/08  08:53:22  adam
181  * Record buffer maintained in server_info.
182  *
183  * Revision 1.5  1995/09/06  16:11:18  adam
184  * Option: only one word key per file.
185  *
186  * Revision 1.4  1995/09/06  10:33:04  adam
187  * More work on present. Some log messages removed.
188  *
189  * Revision 1.3  1995/09/05  15:28:40  adam
190  * More work on search engine.
191  *
192  * Revision 1.2  1995/09/04  12:33:43  adam
193  * Various cleanup. YAZ util used instead.
194  *
195  * Revision 1.1  1995/09/04  09:10:41  adam
196  * More work on index add/del/update.
197  * Merge sort implemented.
198  * Initial work on z39 server.
199  *
200  */
201 #include <stdio.h>
202 #include <assert.h>
203 #ifdef WINDOWS
204 #include <io.h>
205 #include <process.h>
206 #else
207 #include <unistd.h>
208 #endif
209 #include <fcntl.h>
210
211 #include <data1.h>
212 #include <recctrl.h>
213 #include <dmalloc.h>
214
215 #include "zserver.h"
216
217 static int register_lock (ZServerInfo *zi)
218 {
219     time_t lastChange;
220     int state = zebra_server_lock_get_state(zi, &lastChange);
221
222     switch (state)
223     {
224     case 'c':
225         state = 1;
226         break;
227     default:
228         state = 0;
229     }
230     zebra_server_lock (zi, state);
231 #if USE_TIMES
232     times (&zi->tms1);
233 #endif
234     if (zi->registerState == state)
235     {
236         if (zi->registerChange >= lastChange)
237             return 0;
238         logf (LOG_LOG, "Register completely updated since last access");
239     }
240     else if (zi->registerState == -1)
241         logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
242               (long) getpid());
243     else
244         logf (LOG_LOG, "Register has changed state from %d to %d",
245               zi->registerState, state);
246     zi->registerChange = lastChange;
247     if (zi->records)
248     {
249         zebTargetInfo_close (zi->zti, 0);
250         dict_close (zi->dict);
251         if (zi->isam)
252             is_close (zi->isam);
253         if (zi->isamc)
254             isc_close (zi->isamc);
255         rec_close (&zi->records);
256     }
257     bf_cache (zi->bfs, state ? res_get (zi->res, "shadow") : NULL);
258     zi->registerState = state;
259     zi->records = rec_open (zi->bfs, 0);
260     if (!(zi->dict = dict_open (zi->bfs, FNAME_DICT, 40, 0)))
261         return -1;
262     zi->isam = NULL;
263     zi->isamc = NULL;
264     if (!res_get_match (zi->res, "isam", "i", NULL))
265     {
266         if (!(zi->isamc = isc_open (zi->bfs, FNAME_ISAMC,
267                                     0, key_isamc_m(zi->res))))
268             return -1;
269
270     }
271     else
272     {
273         if (!(zi->isam = is_open (zi->bfs, FNAME_ISAM, key_compare, 0,
274                                   sizeof (struct it_key), zi->res)))
275             return -1;
276     }
277     zi->zti = zebTargetInfo_open (zi->records, 0);
278
279     return 0;
280 }
281
282 static void register_unlock (ZServerInfo *zi)
283 {
284     static int waitSec = -1;
285
286 #if USE_TIMES
287     times (&zi->tms2);
288     logf (LOG_LOG, "user/system: %ld/%ld",
289                         (long) (zi->tms2.tms_utime - zi->tms1.tms_utime),
290                         (long) (zi->tms2.tms_stime - zi->tms1.tms_stime));
291 #endif
292     if (waitSec == -1)
293     {
294         char *s = res_get (zi->res, "debugRequestWait");
295         if (s)
296             waitSec = atoi (s);
297         else
298             waitSec = 0;
299     }
300 #ifdef WINDOWS
301 #else
302     if (waitSec > 0)
303         sleep (waitSec);
304 #endif
305     if (zi->registerState != -1)
306         zebra_server_unlock (zi, zi->registerState);
307 }
308
309 bend_initresult *bend_init (bend_initrequest *q)
310 {
311     bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
312     ZServerInfo *zi = xmalloc (sizeof(*zi));
313     struct statserv_options_block *sob;
314
315     r->errcode = 0;
316     r->errstring = 0;
317     r->handle = zi;
318
319     logf (LOG_DEBUG, "bend_init");
320
321     sob = statserv_getcontrol ();
322     logf (LOG_LOG, "Reading resources from %s", sob->configname);
323     if (!(zi->res = res_open (sob->configname)))
324     {
325         logf (LOG_FATAL, "Failed to read resources `%s'", sob->configname);
326         r->errcode = 1;
327         return r;
328     }
329     zebra_server_lock_init (zi);
330     zi->dh = data1_create ();
331     zi->bfs = bfs_create (res_get (zi->res, "register"));
332     bf_lockDir (zi->bfs, res_get (zi->res, "lockDir"));
333     data1_set_tabpath (zi->dh, res_get(zi->res, "profilePath"));
334     zi->sets = NULL;
335     zi->registerState = -1;  /* trigger open of registers! */
336     zi->registerChange = 0;
337
338     zi->records = NULL;
339     zi->odr = odr_createmem (ODR_ENCODE);
340     zi->registered_sets = NULL;
341     zi->zebra_maps = zebra_maps_open (res_get(zi->res, "profilePath"),
342                                       zi->res);
343     return r;
344 }
345
346 bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
347 {
348     ZServerInfo *zi = handle;
349     bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
350
351     r->errcode = 0;
352     r->errstring = 0;
353     r->hits = 0;
354
355     register_lock (zi);
356     odr_reset (zi->odr);
357     zi->errCode = 0;
358     zi->errString = NULL;
359
360     logf (LOG_LOG, "ResultSet '%s'", q->setname);
361     switch (q->query->which)
362     {
363     case Z_Query_type_1: case Z_Query_type_101:
364         r->errcode = rpn_search (zi, q->query->u.type_1,
365                                 q->num_bases, q->basenames, q->setname,
366                                 &r->hits);
367         r->errstring = zi->errString;
368         break;
369     default:
370         r->errcode = 107;
371     }
372     register_unlock (zi);
373     return r;
374 }
375
376 struct fetch_control {
377     int record_offset;
378     int record_int_pos;
379     char *record_int_buf;
380     int record_int_len;
381     int fd;
382 };
383
384 static int record_ext_read (void *fh, char *buf, size_t count)
385 {
386     struct fetch_control *fc = fh;
387     return read (fc->fd, buf, count);
388 }
389
390 static off_t record_ext_seek (void *fh, off_t offset)
391 {
392     struct fetch_control *fc = fh;
393     return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
394 }
395
396 static off_t record_ext_tell (void *fh)
397 {
398     struct fetch_control *fc = fh;
399     return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
400 }
401
402 static off_t record_int_seek (void *fh, off_t offset)
403 {
404     struct fetch_control *fc = fh;
405     return (off_t) (fc->record_int_pos = offset);
406 }
407
408 static off_t record_int_tell (void *fh)
409 {
410     struct fetch_control *fc = fh;
411     return (off_t) fc->record_int_pos;
412 }
413
414 static int record_int_read (void *fh, char *buf, size_t count)
415 {
416     struct fetch_control *fc = fh;
417     int l = fc->record_int_len - fc->record_int_pos;
418     if (l <= 0)
419         return 0;
420     l = (l < count) ? l : count;
421     memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
422     fc->record_int_pos += l;
423     return l;
424 }
425
426 static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
427                           oid_value input_format, Z_RecordComposition *comp,
428                           oid_value *output_format, char **rec_bufp,
429                           int *rec_lenp, char **basenamep)
430 {
431     Record rec;
432     char *fname, *file_type, *basename;
433     RecType rt;
434     struct recRetrieveCtrl retrieveCtrl;
435     char subType[128];
436     struct fetch_control fc;
437
438     rec = rec_get (zi->records, sysno);
439     if (!rec)
440     {
441         logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
442         return 14;
443     }
444     file_type = rec->info[recInfo_fileType];
445     fname = rec->info[recInfo_filename];
446     basename = rec->info[recInfo_databaseName];
447     *basenamep = odr_malloc (stream, strlen(basename)+1);
448     strcpy (*basenamep, basename);
449
450     if (!(rt = recType_byName (file_type, subType)))
451     {
452         logf (LOG_WARN, "Retrieve: Cannot handle type %s",  file_type);
453         return 14;
454     }
455     logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
456     retrieveCtrl.fh = &fc;
457     fc.fd = -1;
458     if (rec->size[recInfo_storeData] > 0)
459     {
460         retrieveCtrl.readf = record_int_read;
461         retrieveCtrl.seekf = record_int_seek;
462         retrieveCtrl.tellf = record_int_tell;
463         fc.record_int_len = rec->size[recInfo_storeData];
464         fc.record_int_buf = rec->info[recInfo_storeData];
465         fc.record_int_pos = 0;
466         logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
467     }
468     else 
469     {
470         if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
471         {
472             logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
473                   fname);
474             rec_rm (&rec);
475             return 14;
476         }
477         memcpy (&fc.record_offset, rec->info[recInfo_offset],
478                 sizeof(fc.record_offset));
479
480         retrieveCtrl.readf = record_ext_read;
481         retrieveCtrl.seekf = record_ext_seek;
482         retrieveCtrl.tellf = record_ext_tell;
483
484         record_ext_seek (retrieveCtrl.fh, 0);
485     }
486     retrieveCtrl.subType = subType;
487     retrieveCtrl.localno = sysno;
488     retrieveCtrl.score = score;
489     retrieveCtrl.odr = stream;
490     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
491     retrieveCtrl.comp = comp;
492     retrieveCtrl.diagnostic = 0;
493     retrieveCtrl.dh = zi->dh;
494     (*rt->retrieve)(&retrieveCtrl);
495     *output_format = retrieveCtrl.output_format;
496     *rec_bufp = retrieveCtrl.rec_buf;
497     *rec_lenp = retrieveCtrl.rec_len;
498     if (fc.fd != -1)
499         close (fc.fd);
500     rec_rm (&rec);
501
502     return retrieveCtrl.diagnostic;
503 }
504
505 bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
506 {
507     ZServerInfo *zi = handle;
508     bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
509     int positions[2];
510     ZServerSetSysno *records;
511
512     register_lock (zi);
513
514     r->errstring = 0;
515     r->last_in_set = 0;
516     r->basename = "base";
517
518     odr_reset (zi->odr);
519     zi->errCode = 0;
520
521     positions[0] = q->number;
522     records = resultSetSysnoGet (zi, q->setname, 1, positions);
523     if (!records)
524     {
525         logf (LOG_DEBUG, "resultSetRecordGet, error");
526         r->errcode = 13;
527         register_unlock (zi);
528         return r;
529     }
530     if (!records[0].sysno)
531     {
532         r->errcode = 13;
533         logf (LOG_DEBUG, "Out of range. pos=%d", q->number);
534         register_unlock (zi);
535         return r;
536     }
537     r->errcode = record_fetch (zi, records[0].sysno,
538                               records[0].score, q->stream, q->format,
539                               q->comp, &r->format, &r->record, &r->len,
540                               &r->basename);
541     resultSetSysnoDel (zi, records, 1);
542     register_unlock (zi);
543     return r;
544 }
545
546 bend_deleteresult *bend_delete (void *handle, bend_deleterequest *q, int *num)
547 {
548     ZServerInfo *zi = handle;
549     register_lock (zi);
550     register_unlock (zi);
551     return 0;
552 }
553
554 bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
555 {
556     ZServerInfo *zi = handle;
557     bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
558     int status;
559
560     register_lock (zi);
561     odr_reset (zi->odr);
562     zi->errCode = 0;
563     zi->errString = 0;
564
565     r->term_position = q->term_position;
566     r->num_entries = q->num_entries;
567     r->errcode = rpn_scan (zi, q->term,
568                           q->attributeset,
569                           q->num_bases, q->basenames,
570                           &r->term_position,
571                           &r->num_entries, &r->entries, &status);
572     r->errstring = zi->errString;
573     r->status = status;
574     register_unlock (zi);
575     return r;
576 }
577
578 void bend_close (void *handle)
579 {
580     ZServerInfo *zi = handle;
581
582     if (zi->records)
583     {
584         resultSetDestroy (zi);
585         zebTargetInfo_close (zi->zti, 0);
586         dict_close (zi->dict);
587         if (zi->isam)
588             is_close (zi->isam);
589         if (zi->isamc)
590             isc_close (zi->isamc);
591         rec_close (&zi->records);
592         register_unlock (zi);
593     }
594     odr_destroy (zi->odr);
595     zebra_maps_close (zi->zebra_maps);
596     bfs_destroy (zi->bfs);
597     data1_destroy (zi->dh);
598     zebra_server_lock_destroy (zi);
599
600     res_close (zi->res);
601     xfree (zi);
602 }
603
604 #ifndef WINDOWS
605 static void pre_init (struct statserv_options_block *sob)
606 {
607     char *pidfile = "zebrasrv.pid";
608     int fd = creat (pidfile, 0666);
609     
610     if (fd == -1)
611         logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
612     else
613     {
614         char pidstr[30];
615         
616         sprintf (pidstr, "%ld", (long) getpid ());
617         write (fd, pidstr, strlen(pidstr));
618         close (fd);
619     }
620 }
621 #endif
622
623 int main (int argc, char **argv)
624 {
625     struct statserv_options_block *sob;
626
627     sob = statserv_getcontrol ();
628     strcpy (sob->configname, FNAME_CONFIG);
629 #ifndef WINDOWS
630     sob->pre_init = pre_init;
631 #endif
632     statserv_setcontrol (sob);
633
634     return statserv_main (argc, argv);
635 }