2 * Copyright (C) 1995-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.50 1997-09-29 09:08:36 adam
8 * Revised locking system to be thread safe for the server.
10 * Revision 1.49 1997/09/25 14:57:23 adam
13 * Revision 1.48 1997/09/17 12:19:19 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.47 1997/09/04 13:58:36 adam
18 * New retrieve/extract method tellf (added).
19 * Added O_BINARY for open calls.
21 * Revision 1.46 1997/07/28 08:30:47 adam
22 * Server returns diagnostic 14 when record doesn't exist.
24 * Revision 1.45 1996/12/23 15:30:45 adam
26 * Bug fix: result sets weren't deleted after server shut down.
28 * Revision 1.44 1996/12/11 12:08:01 adam
29 * Added better compression.
31 * Revision 1.43 1996/11/15 15:03:58 adam
32 * Logging of execution speed by using the times(2) call.
34 * Revision 1.42 1996/11/08 11:10:36 adam
35 * Buffers used during file match got bigger.
36 * Compressed ISAM support everywhere.
37 * Bug fixes regarding masking characters in queries.
38 * Redesigned Regexp-2 queries.
40 * Revision 1.41 1996/10/29 14:09:56 adam
41 * Use of cisam system - enabled if setting isamc is 1.
43 * Revision 1.40 1996/06/04 10:19:02 adam
44 * Minor changes - removed include of ctype.h.
46 * Revision 1.39 1996/05/31 09:07:05 quinn
47 * Work on character-set handling
49 * Revision 1.38 1996/05/14 11:34:01 adam
50 * Scan support in multiple registers/databases.
52 * Revision 1.37 1996/05/14 06:16:48 adam
53 * Compact use/set bytes used in search service.
55 * Revision 1.36 1996/05/01 13:46:37 adam
56 * First work on multiple records in one file.
57 * New option, -offset, to the "unread" command in the filter module.
59 * Revision 1.35 1996/03/26 16:01:14 adam
60 * New setting lockPath: directory of various lock files.
62 * Revision 1.34 1996/03/20 09:36:46 adam
63 * Function dict_lookup_grep got extra parameter, init_pos, which marks
64 * from which position in pattern approximate pattern matching should occur.
65 * Approximate pattern matching is used in relevance=re-2.
67 * Revision 1.33 1996/01/17 14:57:56 adam
68 * Prototype changed for reader functions in extract/retrieve. File
69 * is identified by 'void *' instead of 'int.
71 * Revision 1.32 1995/12/11 09:12:58 adam
72 * The rec_get function returns NULL if record doesn't exist - will
73 * happen in the server if the result set records have been deleted since
74 * the creation of the set (i.e. the search).
75 * The server saves a result temporarily if it is 'volatile', i.e. the
76 * set is register dependent.
78 * Revision 1.31 1995/12/08 16:22:56 adam
79 * Work on update while servers are running. Three lock files introduced.
80 * The servers reload their registers when necessary, but they don't
81 * reestablish result sets yet.
83 * Revision 1.30 1995/12/07 17:38:48 adam
84 * Work locking mechanisms for concurrent updates/commit.
86 * Revision 1.29 1995/12/04 14:22:32 adam
87 * Extra arg to recType_byName.
88 * Started work on new regular expression parsed input to
91 * Revision 1.28 1995/11/28 09:09:48 adam
92 * Zebra config renamed.
93 * Use setting 'recordId' to identify record now.
94 * Bug fix in recindex.c: rec_release_blocks was invokeded even
95 * though the blocks were already released.
96 * File traversal properly deletes records when needed.
98 * Revision 1.27 1995/11/27 13:58:54 adam
99 * New option -t. storeStore data implemented in server.
101 * Revision 1.26 1995/11/25 10:24:07 adam
102 * More record fields - they are enumerated now.
103 * New options: flagStoreData flagStoreKey.
105 * Revision 1.25 1995/11/21 15:29:13 adam
106 * Config file 'base' read by default by both indexer and server.
108 * Revision 1.24 1995/11/20 16:59:47 adam
109 * New update method: the 'old' keys are saved for each records.
111 * Revision 1.23 1995/11/16 17:00:56 adam
112 * Better logging of rpn query.
114 * Revision 1.22 1995/11/16 15:34:55 adam
115 * Uses new record management system in both indexer and server.
117 * Revision 1.21 1995/11/01 16:25:52 quinn
118 * *** empty log message ***
120 * Revision 1.20 1995/10/27 14:00:12 adam
121 * Implemented detection of database availability.
123 * Revision 1.19 1995/10/17 18:02:11 adam
124 * New feature: databases. Implemented as prefix to words in dictionary.
126 * Revision 1.18 1995/10/16 14:03:09 quinn
127 * Changes to support element set names and espec1
129 * Revision 1.17 1995/10/16 09:32:40 adam
130 * More work on relational op.
132 * Revision 1.16 1995/10/13 12:26:44 adam
133 * Optimization of truncation.
135 * Revision 1.15 1995/10/12 12:40:55 adam
136 * Bug fixes in rpn_prox.
138 * Revision 1.14 1995/10/09 16:18:37 adam
139 * Function dict_lookup_grep got extra client data parameter.
141 * Revision 1.13 1995/10/06 14:38:00 adam
142 * New result set method: r_score.
143 * Local no (sysno) and score is transferred to retrieveCtrl.
145 * Revision 1.12 1995/10/06 13:52:06 adam
146 * Bug fixes. Handler may abort further scanning.
148 * Revision 1.11 1995/10/06 10:43:57 adam
149 * Scan added. 'occurrences' in scan entries not set yet.
151 * Revision 1.10 1995/10/02 16:43:32 quinn
152 * Set default resulting record type in fetch.
154 * Revision 1.9 1995/10/02 15:18:52 adam
155 * New member in recRetrieveCtrl: diagnostic.
157 * Revision 1.8 1995/09/28 09:19:47 adam
158 * xfree/xmalloc used everywhere.
159 * Extract/retrieve method seems to work for text records.
161 * Revision 1.7 1995/09/27 16:17:32 adam
162 * More work on retrieve.
164 * Revision 1.6 1995/09/08 08:53:22 adam
165 * Record buffer maintained in server_info.
167 * Revision 1.5 1995/09/06 16:11:18 adam
168 * Option: only one word key per file.
170 * Revision 1.4 1995/09/06 10:33:04 adam
171 * More work on present. Some log messages removed.
173 * Revision 1.3 1995/09/05 15:28:40 adam
174 * More work on search engine.
176 * Revision 1.2 1995/09/04 12:33:43 adam
177 * Various cleanup. YAZ util used instead.
179 * Revision 1.1 1995/09/04 09:10:41 adam
180 * More work on index add/del/update.
181 * Merge sort implemented.
182 * Initial work on z39 server.
201 static int register_lock (ZServerInfo *zi)
204 int state = zebra_server_lock_get_state(zi, &lastChange);
214 zebra_server_lock (zi, state);
218 if (zi->registerState == state)
220 if (zi->registerChange >= lastChange)
222 logf (LOG_LOG, "Register completely updated since last access");
224 else if (zi->registerState == -1)
225 logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
228 logf (LOG_LOG, "Register has changed state from %d to %d",
229 zi->registerState, state);
230 zi->registerChange = lastChange;
233 zebTargetInfo_close (zi->zti, 0);
234 dict_close (zi->dict);
238 isc_close (zi->isamc);
239 rec_close (&zi->records);
241 bf_cache (zi->bfs, state ? res_get (zi->res, "shadow") : NULL);
242 zi->registerState = state;
243 zi->records = rec_open (zi->bfs, 0);
244 if (!(zi->dict = dict_open (zi->bfs, FNAME_DICT, 40, 0)))
248 if (res_get_match (zi->res, "isam", "c", NULL))
250 if (!(zi->isamc = isc_open (zi->bfs, FNAME_ISAMC,
251 0, key_isamc_m(zi->res))))
257 if (!(zi->isam = is_open (zi->bfs, FNAME_ISAM, key_compare, 0,
258 sizeof (struct it_key), zi->res)))
261 zi->zti = zebTargetInfo_open (zi->records, 0);
262 init_charmap (zi->res);
266 static void register_unlock (ZServerInfo *zi)
268 static int waitSec = -1;
272 logf (LOG_LOG, "user/system: %ld/%ld",
273 (long) (zi->tms2.tms_utime - zi->tms1.tms_utime),
274 (long) (zi->tms2.tms_stime - zi->tms1.tms_stime));
278 char *s = res_get (zi->res, "debugRequestWait");
289 if (zi->registerState != -1)
290 zebra_server_unlock (zi, zi->registerState);
293 bend_initresult *bend_init (bend_initrequest *q)
295 bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
296 ZServerInfo *zi = xmalloc (sizeof(*zi));
297 struct statserv_options_block *sob;
303 logf (LOG_DEBUG, "bend_init");
305 sob = statserv_getcontrol ();
306 logf (LOG_LOG, "Reading resources from %s", sob->configname);
307 if (!(zi->res = res_open (sob->configname)))
309 logf (LOG_FATAL, "Cannot open resource `%s'", sob->configname);
312 zebra_server_lock_init (zi);
313 zi->dh = data1_create ();
314 zi->bfs = bfs_create (res_get (zi->res, "register"));
315 bf_lockDir (zi->bfs, res_get (zi->res, "lockDir"));
316 data1_set_tabpath (zi->dh, res_get(zi->res, "profilePath"));
318 zi->registerState = -1; /* trigger open of registers! */
319 zi->registerChange = 0;
322 zi->odr = odr_createmem (ODR_ENCODE);
323 zi->registered_sets = NULL;
327 bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
329 ZServerInfo *zi = handle;
330 bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
339 zi->errString = NULL;
341 logf (LOG_LOG, "ResultSet '%s'", q->setname);
342 switch (q->query->which)
344 case Z_Query_type_1: case Z_Query_type_101:
345 r->errcode = rpn_search (zi, q->query->u.type_1,
346 q->num_bases, q->basenames, q->setname,
348 r->errstring = zi->errString;
353 register_unlock (zi);
357 struct fetch_control {
360 char *record_int_buf;
365 static int record_ext_read (void *fh, char *buf, size_t count)
367 struct fetch_control *fc = fh;
368 return read (fc->fd, buf, count);
371 static off_t record_ext_seek (void *fh, off_t offset)
373 struct fetch_control *fc = fh;
374 return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
377 static off_t record_ext_tell (void *fh)
379 struct fetch_control *fc = fh;
380 return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
383 static off_t record_int_seek (void *fh, off_t offset)
385 struct fetch_control *fc = fh;
386 return (off_t) (fc->record_int_pos = offset);
389 static off_t record_int_tell (void *fh)
391 struct fetch_control *fc = fh;
392 return (off_t) fc->record_int_pos;
395 static int record_int_read (void *fh, char *buf, size_t count)
397 struct fetch_control *fc = fh;
398 int l = fc->record_int_len - fc->record_int_pos;
401 l = (l < count) ? l : count;
402 memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
403 fc->record_int_pos += l;
407 static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
408 oid_value input_format, Z_RecordComposition *comp,
409 oid_value *output_format, char **rec_bufp,
410 int *rec_lenp, char **basenamep)
413 char *fname, *file_type, *basename;
415 struct recRetrieveCtrl retrieveCtrl;
417 struct fetch_control fc;
419 rec = rec_get (zi->records, sysno);
422 logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
425 file_type = rec->info[recInfo_fileType];
426 fname = rec->info[recInfo_filename];
427 basename = rec->info[recInfo_databaseName];
428 *basenamep = odr_malloc (stream, strlen(basename)+1);
429 strcpy (*basenamep, basename);
431 if (!(rt = recType_byName (file_type, subType)))
433 logf (LOG_FATAL|LOG_ERRNO, "Retrieve: Cannot handle type %s",
437 logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
438 retrieveCtrl.fh = &fc;
440 if (rec->size[recInfo_storeData] > 0)
442 retrieveCtrl.readf = record_int_read;
443 retrieveCtrl.seekf = record_int_seek;
444 retrieveCtrl.tellf = record_int_tell;
445 fc.record_int_len = rec->size[recInfo_storeData];
446 fc.record_int_buf = rec->info[recInfo_storeData];
447 fc.record_int_pos = 0;
448 logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
452 if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
454 logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
459 memcpy (&fc.record_offset, rec->info[recInfo_offset],
460 sizeof(fc.record_offset));
462 retrieveCtrl.readf = record_ext_read;
463 retrieveCtrl.seekf = record_ext_seek;
464 retrieveCtrl.tellf = record_ext_tell;
466 record_ext_seek (retrieveCtrl.fh, 0);
468 retrieveCtrl.subType = subType;
469 retrieveCtrl.localno = sysno;
470 retrieveCtrl.score = score;
471 retrieveCtrl.odr = stream;
472 retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
473 retrieveCtrl.comp = comp;
474 retrieveCtrl.diagnostic = 0;
475 retrieveCtrl.dh = zi->dh;
476 (*rt->retrieve)(&retrieveCtrl);
477 *output_format = retrieveCtrl.output_format;
478 *rec_bufp = retrieveCtrl.rec_buf;
479 *rec_lenp = retrieveCtrl.rec_len;
484 return retrieveCtrl.diagnostic;
487 bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
489 ZServerInfo *zi = handle;
490 bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
492 ZServerSetSysno *records;
498 r->basename = "base";
503 positions[0] = q->number;
504 records = resultSetSysnoGet (zi, q->setname, 1, positions);
507 logf (LOG_DEBUG, "resultSetRecordGet, error");
509 register_unlock (zi);
512 if (!records[0].sysno)
515 logf (LOG_DEBUG, "Out of range. pos=%d", q->number);
516 register_unlock (zi);
519 r->errcode = record_fetch (zi, records[0].sysno,
520 records[0].score, q->stream, q->format,
521 q->comp, &r->format, &r->record, &r->len,
523 resultSetSysnoDel (zi, records, 1);
524 register_unlock (zi);
528 bend_deleteresult *bend_delete (void *handle, bend_deleterequest *q, int *num)
530 ZServerInfo *zi = handle;
532 register_unlock (zi);
536 bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
538 ZServerInfo *zi = handle;
539 bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
547 r->term_position = q->term_position;
548 r->num_entries = q->num_entries;
549 r->errcode = rpn_scan (zi, q->term,
551 q->num_bases, q->basenames,
553 &r->num_entries, &r->entries, &status);
554 r->errstring = zi->errString;
556 register_unlock (zi);
560 void bend_close (void *handle)
562 ZServerInfo *zi = handle;
565 resultSetDestroy (zi);
566 dict_close (zi->dict);
570 isc_close (zi->isamc);
571 rec_close (&zi->records);
572 register_unlock (zi);
574 bfs_destroy (zi->bfs);
575 data1_destroy (zi->dh);
576 zebra_server_lock_destroy (zi);
580 int main (int argc, char **argv)
582 struct statserv_options_block *sob;
584 sob = statserv_getcontrol ();
585 strcpy (sob->configname, FNAME_CONFIG);
586 statserv_setcontrol (sob);
588 return statserv_main (argc, argv);