2 * Copyright (C) 1995-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.55 1998-02-10 12:03:06 adam
10 * Revision 1.54 1998/01/29 13:39:13 adam
11 * Compress ISAM is default.
13 * Revision 1.53 1998/01/12 15:04:09 adam
14 * The test option (-s) only uses read-lock (and not write lock).
16 * Revision 1.52 1997/11/18 10:05:08 adam
17 * Changed character map facility so that admin can specify character
18 * mapping files for each register type, w, p, etc.
20 * Revision 1.51 1997/10/27 14:33:06 adam
21 * Moved towards generic character mapping depending on "structure"
22 * field in abstract syntax file. Fixed a few memory leaks. Fixed
23 * bug with negative integers when doing searches with relational
26 * Revision 1.50 1997/09/29 09:08:36 adam
27 * Revised locking system to be thread safe for the server.
29 * Revision 1.49 1997/09/25 14:57:23 adam
32 * Revision 1.48 1997/09/17 12:19:19 adam
33 * Zebra version corresponds to YAZ version 1.4.
34 * Changed Zebra server so that it doesn't depend on global common_resource.
36 * Revision 1.47 1997/09/04 13:58:36 adam
37 * New retrieve/extract method tellf (added).
38 * Added O_BINARY for open calls.
40 * Revision 1.46 1997/07/28 08:30:47 adam
41 * Server returns diagnostic 14 when record doesn't exist.
43 * Revision 1.45 1996/12/23 15:30:45 adam
45 * Bug fix: result sets weren't deleted after server shut down.
47 * Revision 1.44 1996/12/11 12:08:01 adam
48 * Added better compression.
50 * Revision 1.43 1996/11/15 15:03:58 adam
51 * Logging of execution speed by using the times(2) call.
53 * Revision 1.42 1996/11/08 11:10:36 adam
54 * Buffers used during file match got bigger.
55 * Compressed ISAM support everywhere.
56 * Bug fixes regarding masking characters in queries.
57 * Redesigned Regexp-2 queries.
59 * Revision 1.41 1996/10/29 14:09:56 adam
60 * Use of cisam system - enabled if setting isamc is 1.
62 * Revision 1.40 1996/06/04 10:19:02 adam
63 * Minor changes - removed include of ctype.h.
65 * Revision 1.39 1996/05/31 09:07:05 quinn
66 * Work on character-set handling
68 * Revision 1.38 1996/05/14 11:34:01 adam
69 * Scan support in multiple registers/databases.
71 * Revision 1.37 1996/05/14 06:16:48 adam
72 * Compact use/set bytes used in search service.
74 * Revision 1.36 1996/05/01 13:46:37 adam
75 * First work on multiple records in one file.
76 * New option, -offset, to the "unread" command in the filter module.
78 * Revision 1.35 1996/03/26 16:01:14 adam
79 * New setting lockPath: directory of various lock files.
81 * Revision 1.34 1996/03/20 09:36:46 adam
82 * Function dict_lookup_grep got extra parameter, init_pos, which marks
83 * from which position in pattern approximate pattern matching should occur.
84 * Approximate pattern matching is used in relevance=re-2.
86 * Revision 1.33 1996/01/17 14:57:56 adam
87 * Prototype changed for reader functions in extract/retrieve. File
88 * is identified by 'void *' instead of 'int.
90 * Revision 1.32 1995/12/11 09:12:58 adam
91 * The rec_get function returns NULL if record doesn't exist - will
92 * happen in the server if the result set records have been deleted since
93 * the creation of the set (i.e. the search).
94 * The server saves a result temporarily if it is 'volatile', i.e. the
95 * set is register dependent.
97 * Revision 1.31 1995/12/08 16:22:56 adam
98 * Work on update while servers are running. Three lock files introduced.
99 * The servers reload their registers when necessary, but they don't
100 * reestablish result sets yet.
102 * Revision 1.30 1995/12/07 17:38:48 adam
103 * Work locking mechanisms for concurrent updates/commit.
105 * Revision 1.29 1995/12/04 14:22:32 adam
106 * Extra arg to recType_byName.
107 * Started work on new regular expression parsed input to
108 * structured records.
110 * Revision 1.28 1995/11/28 09:09:48 adam
111 * Zebra config renamed.
112 * Use setting 'recordId' to identify record now.
113 * Bug fix in recindex.c: rec_release_blocks was invokeded even
114 * though the blocks were already released.
115 * File traversal properly deletes records when needed.
117 * Revision 1.27 1995/11/27 13:58:54 adam
118 * New option -t. storeStore data implemented in server.
120 * Revision 1.26 1995/11/25 10:24:07 adam
121 * More record fields - they are enumerated now.
122 * New options: flagStoreData flagStoreKey.
124 * Revision 1.25 1995/11/21 15:29:13 adam
125 * Config file 'base' read by default by both indexer and server.
127 * Revision 1.24 1995/11/20 16:59:47 adam
128 * New update method: the 'old' keys are saved for each records.
130 * Revision 1.23 1995/11/16 17:00:56 adam
131 * Better logging of rpn query.
133 * Revision 1.22 1995/11/16 15:34:55 adam
134 * Uses new record management system in both indexer and server.
136 * Revision 1.21 1995/11/01 16:25:52 quinn
137 * *** empty log message ***
139 * Revision 1.20 1995/10/27 14:00:12 adam
140 * Implemented detection of database availability.
142 * Revision 1.19 1995/10/17 18:02:11 adam
143 * New feature: databases. Implemented as prefix to words in dictionary.
145 * Revision 1.18 1995/10/16 14:03:09 quinn
146 * Changes to support element set names and espec1
148 * Revision 1.17 1995/10/16 09:32:40 adam
149 * More work on relational op.
151 * Revision 1.16 1995/10/13 12:26:44 adam
152 * Optimization of truncation.
154 * Revision 1.15 1995/10/12 12:40:55 adam
155 * Bug fixes in rpn_prox.
157 * Revision 1.14 1995/10/09 16:18:37 adam
158 * Function dict_lookup_grep got extra client data parameter.
160 * Revision 1.13 1995/10/06 14:38:00 adam
161 * New result set method: r_score.
162 * Local no (sysno) and score is transferred to retrieveCtrl.
164 * Revision 1.12 1995/10/06 13:52:06 adam
165 * Bug fixes. Handler may abort further scanning.
167 * Revision 1.11 1995/10/06 10:43:57 adam
168 * Scan added. 'occurrences' in scan entries not set yet.
170 * Revision 1.10 1995/10/02 16:43:32 quinn
171 * Set default resulting record type in fetch.
173 * Revision 1.9 1995/10/02 15:18:52 adam
174 * New member in recRetrieveCtrl: diagnostic.
176 * Revision 1.8 1995/09/28 09:19:47 adam
177 * xfree/xmalloc used everywhere.
178 * Extract/retrieve method seems to work for text records.
180 * Revision 1.7 1995/09/27 16:17:32 adam
181 * More work on retrieve.
183 * Revision 1.6 1995/09/08 08:53:22 adam
184 * Record buffer maintained in server_info.
186 * Revision 1.5 1995/09/06 16:11:18 adam
187 * Option: only one word key per file.
189 * Revision 1.4 1995/09/06 10:33:04 adam
190 * More work on present. Some log messages removed.
192 * Revision 1.3 1995/09/05 15:28:40 adam
193 * More work on search engine.
195 * Revision 1.2 1995/09/04 12:33:43 adam
196 * Various cleanup. YAZ util used instead.
198 * Revision 1.1 1995/09/04 09:10:41 adam
199 * More work on index add/del/update.
200 * Merge sort implemented.
201 * Initial work on z39 server.
220 static int register_lock (ZServerInfo *zi)
223 int state = zebra_server_lock_get_state(zi, &lastChange);
233 zebra_server_lock (zi, state);
237 if (zi->registerState == state)
239 if (zi->registerChange >= lastChange)
241 logf (LOG_LOG, "Register completely updated since last access");
243 else if (zi->registerState == -1)
244 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
247 logf (LOG_LOG, "Register has changed state from %d to %d",
248 zi->registerState, state);
249 zi->registerChange = lastChange;
252 zebTargetInfo_close (zi->zti, 0);
253 dict_close (zi->dict);
254 sortIdx_close (zi->sortIdx);
258 isc_close (zi->isamc);
259 rec_close (&zi->records);
261 bf_cache (zi->bfs, state ? res_get (zi->res, "shadow") : NULL);
262 zi->registerState = state;
263 zi->records = rec_open (zi->bfs, 0);
264 if (!(zi->dict = dict_open (zi->bfs, FNAME_DICT, 40, 0)))
266 if (!(zi->sortIdx = sortIdx_open (zi->bfs, 0)))
270 if (!res_get_match (zi->res, "isam", "i", NULL))
272 if (!(zi->isamc = isc_open (zi->bfs, FNAME_ISAMC,
273 0, key_isamc_m(zi->res))))
279 if (!(zi->isam = is_open (zi->bfs, FNAME_ISAM, key_compare, 0,
280 sizeof (struct it_key), zi->res)))
283 zi->zti = zebTargetInfo_open (zi->records, 0);
288 static void register_unlock (ZServerInfo *zi)
290 static int waitSec = -1;
294 logf (LOG_LOG, "user/system: %ld/%ld",
295 (long) (zi->tms2.tms_utime - zi->tms1.tms_utime),
296 (long) (zi->tms2.tms_stime - zi->tms1.tms_stime));
300 char *s = res_get (zi->res, "debugRequestWait");
311 if (zi->registerState != -1)
312 zebra_server_unlock (zi, zi->registerState);
315 static int bend_sort (void *handle, bend_sort_rr *rr);
317 bend_initresult *bend_init (bend_initrequest *q)
319 bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
320 ZServerInfo *zi = xmalloc (sizeof(*zi));
321 struct statserv_options_block *sob;
326 q->bend_sort = bend_sort;
328 logf (LOG_DEBUG, "bend_init");
330 sob = statserv_getcontrol ();
331 logf (LOG_LOG, "Reading resources from %s", sob->configname);
332 if (!(zi->res = res_open (sob->configname)))
334 logf (LOG_FATAL, "Failed to read resources `%s'", sob->configname);
338 zebra_server_lock_init (zi);
339 zi->dh = data1_create ();
340 zi->bfs = bfs_create (res_get (zi->res, "register"));
341 bf_lockDir (zi->bfs, res_get (zi->res, "lockDir"));
342 data1_set_tabpath (zi->dh, res_get(zi->res, "profilePath"));
344 zi->registerState = -1; /* trigger open of registers! */
345 zi->registerChange = 0;
348 zi->registered_sets = NULL;
349 zi->zebra_maps = zebra_maps_open (res_get(zi->res, "profilePath"),
354 bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
356 ZServerInfo *zi = handle;
357 bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
365 zi->errString = NULL;
367 logf (LOG_LOG, "ResultSet '%s'", q->setname);
368 switch (q->query->which)
370 case Z_Query_type_1: case Z_Query_type_101:
371 r->errcode = rpn_search (zi, q->stream, q->query->u.type_1,
372 q->num_bases, q->basenames, q->setname,
374 r->errstring = zi->errString;
379 register_unlock (zi);
383 struct fetch_control {
386 char *record_int_buf;
391 static int record_ext_read (void *fh, char *buf, size_t count)
393 struct fetch_control *fc = fh;
394 return read (fc->fd, buf, count);
397 static off_t record_ext_seek (void *fh, off_t offset)
399 struct fetch_control *fc = fh;
400 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
403 static off_t record_ext_tell (void *fh)
405 struct fetch_control *fc = fh;
406 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
409 static off_t record_int_seek (void *fh, off_t offset)
411 struct fetch_control *fc = fh;
412 return (off_t) (fc->record_int_pos = offset);
415 static off_t record_int_tell (void *fh)
417 struct fetch_control *fc = fh;
418 return (off_t) fc->record_int_pos;
421 static int record_int_read (void *fh, char *buf, size_t count)
423 struct fetch_control *fc = fh;
424 int l = fc->record_int_len - fc->record_int_pos;
427 l = (l < count) ? l : count;
428 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
429 fc->record_int_pos += l;
433 static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
434 oid_value input_format, Z_RecordComposition *comp,
435 oid_value *output_format, char **rec_bufp,
436 int *rec_lenp, char **basenamep)
439 char *fname, *file_type, *basename;
441 struct recRetrieveCtrl retrieveCtrl;
443 struct fetch_control fc;
445 rec = rec_get (zi->records, sysno);
448 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
451 file_type = rec->info[recInfo_fileType];
452 fname = rec->info[recInfo_filename];
453 basename = rec->info[recInfo_databaseName];
454 *basenamep = odr_malloc (stream, strlen(basename)+1);
455 strcpy (*basenamep, basename);
457 if (!(rt = recType_byName (file_type, subType)))
459 logf (LOG_WARN, "Retrieve: Cannot handle type %s", file_type);
462 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
463 retrieveCtrl.fh = &fc;
465 if (rec->size[recInfo_storeData] > 0)
467 retrieveCtrl.readf = record_int_read;
468 retrieveCtrl.seekf = record_int_seek;
469 retrieveCtrl.tellf = record_int_tell;
470 fc.record_int_len = rec->size[recInfo_storeData];
471 fc.record_int_buf = rec->info[recInfo_storeData];
472 fc.record_int_pos = 0;
473 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
477 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
479 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
484 memcpy (&fc.record_offset, rec->info[recInfo_offset],
485 sizeof(fc.record_offset));
487 retrieveCtrl.readf = record_ext_read;
488 retrieveCtrl.seekf = record_ext_seek;
489 retrieveCtrl.tellf = record_ext_tell;
491 record_ext_seek (retrieveCtrl.fh, 0);
493 retrieveCtrl.subType = subType;
494 retrieveCtrl.localno = sysno;
495 retrieveCtrl.score = score;
496 retrieveCtrl.odr = stream;
497 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
498 retrieveCtrl.comp = comp;
499 retrieveCtrl.diagnostic = 0;
500 retrieveCtrl.dh = zi->dh;
501 (*rt->retrieve)(&retrieveCtrl);
502 *output_format = retrieveCtrl.output_format;
503 *rec_bufp = retrieveCtrl.rec_buf;
504 *rec_lenp = retrieveCtrl.rec_len;
509 return retrieveCtrl.diagnostic;
512 bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
514 ZServerInfo *zi = handle;
515 bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
517 ZServerSetSysno *records;
523 r->basename = "base";
527 positions[0] = q->number;
528 records = resultSetSysnoGet (zi, q->setname, 1, positions);
531 logf (LOG_DEBUG, "resultSetRecordGet, error");
533 register_unlock (zi);
536 if (!records[0].sysno)
539 logf (LOG_DEBUG, "Out of range. pos=%d", q->number);
540 register_unlock (zi);
543 r->errcode = record_fetch (zi, records[0].sysno,
544 records[0].score, q->stream, q->format,
545 q->comp, &r->format, &r->record, &r->len,
547 resultSetSysnoDel (zi, records, 1);
548 register_unlock (zi);
552 bend_deleteresult *bend_delete (void *handle, bend_deleterequest *q, int *num)
554 ZServerInfo *zi = handle;
556 register_unlock (zi);
560 bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
562 ZServerInfo *zi = handle;
563 bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
570 r->term_position = q->term_position;
571 r->num_entries = q->num_entries;
572 r->errcode = rpn_scan (zi, q->stream, q->term,
574 q->num_bases, q->basenames,
576 &r->num_entries, &r->entries, &status);
577 r->errstring = zi->errString;
579 register_unlock (zi);
583 void bend_close (void *handle)
585 ZServerInfo *zi = handle;
589 resultSetDestroy (zi);
590 zebTargetInfo_close (zi->zti, 0);
591 dict_close (zi->dict);
592 sortIdx_close (zi->sortIdx);
596 isc_close (zi->isamc);
597 rec_close (&zi->records);
598 register_unlock (zi);
600 zebra_maps_close (zi->zebra_maps);
601 bfs_destroy (zi->bfs);
602 data1_destroy (zi->dh);
603 zebra_server_lock_destroy (zi);
610 static void pre_init (struct statserv_options_block *sob)
612 char *pidfile = "zebrasrv.pid";
613 int fd = creat (pidfile, 0666);
616 logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
621 sprintf (pidstr, "%ld", (long) getpid ());
622 write (fd, pidstr, strlen(pidstr));
628 int bend_sort (void *handle, bend_sort_rr *rr)
630 ZServerInfo *zi = handle;
635 resultSetSort (zi, rr);
637 register_unlock (zi);
642 int main (int argc, char **argv)
644 struct statserv_options_block *sob;
646 sob = statserv_getcontrol ();
647 strcpy (sob->configname, FNAME_CONFIG);
649 sob->pre_init = pre_init;
651 statserv_setcontrol (sob);
653 return statserv_main (argc, argv);