2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 1999-05-12 13:08:06 adam
8 * First version of ISAMS.
10 * Revision 1.31 1999/02/02 14:50:56 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.30 1998/10/28 10:53:57 adam
14 * Added type cast to prevent warning.
16 * Revision 1.29 1998/06/11 15:41:39 adam
19 * Revision 1.28 1998/03/05 08:45:12 adam
20 * New result set model and modular ranking system. Moved towards
21 * descent server API. System information stored as "SGML" records.
23 * Revision 1.27 1998/02/17 10:32:52 adam
24 * Fixed bug: binary files weren't opened with flag b on NT.
26 * Revision 1.26 1998/01/29 13:39:13 adam
27 * Compress ISAM is default.
29 * Revision 1.25 1997/09/17 12:19:14 adam
30 * Zebra version corresponds to YAZ version 1.4.
31 * Changed Zebra server so that it doesn't depend on global common_resource.
33 * Revision 1.24 1997/09/09 13:38:07 adam
34 * Partial port to WIN95/NT.
36 * Revision 1.23 1997/09/04 13:57:39 adam
37 * Added O_BINARY for open calls.
39 * Revision 1.22 1997/02/12 20:39:45 adam
40 * Implemented options -f <n> that limits the log to the first <n>
42 * Changed some log messages also.
44 * Revision 1.21 1996/11/08 11:10:23 adam
45 * Buffers used during file match got bigger.
46 * Compressed ISAM support everywhere.
47 * Bug fixes regarding masking characters in queries.
48 * Redesigned Regexp-2 queries.
50 * Revision 1.20 1996/11/01 08:58:41 adam
51 * Interface to isamc system now includes update and delete.
53 * Revision 1.19 1996/10/29 14:09:46 adam
54 * Use of cisam system - enabled if setting isamc is 1.
56 * Revision 1.18 1996/06/04 10:18:59 adam
57 * Minor changes - removed include of ctype.h.
59 * Revision 1.17 1996/05/14 15:47:07 adam
60 * Cleanup of various buffer size entities.
62 * Revision 1.16 1996/04/09 10:05:20 adam
63 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
65 * Revision 1.15 1996/03/21 14:50:09 adam
66 * File update uses modify-time instead of change-time.
68 * Revision 1.14 1996/02/07 14:06:37 adam
69 * Better progress report during register merge.
70 * New command: clean - removes temporary shadow files.
72 * Revision 1.13 1996/02/05 12:30:00 adam
73 * Logging reduced a bit.
74 * The remaining running time is estimated during register merge.
76 * Revision 1.12 1995/12/06 17:49:19 adam
77 * Uses dict_delete now.
79 * Revision 1.11 1995/12/06 16:06:43 adam
80 * Better diagnostics. Work on 'real' dictionary deletion.
82 * Revision 1.10 1995/12/06 12:41:22 adam
83 * New command 'stat' for the index program.
84 * Filenames can be read from stdin by specifying '-'.
85 * Bug fix/enhancement of the transformation from terms to regular
86 * expressons in the search engine.
88 * Revision 1.9 1995/10/10 12:24:39 adam
89 * Temporary sort files are compressed.
91 * Revision 1.8 1995/10/04 16:57:19 adam
92 * Key input and merge sort in one pass.
94 * Revision 1.7 1995/10/02 15:18:52 adam
95 * New member in recRetrieveCtrl: diagnostic.
97 * Revision 1.6 1995/09/29 15:51:56 adam
98 * First work on multi-way read.
100 * Revision 1.5 1995/09/29 14:01:43 adam
103 * Revision 1.4 1995/09/28 14:22:57 adam
104 * Sort uses smaller temporary files.
106 * Revision 1.3 1995/09/06 16:11:17 adam
107 * Option: only one word key per file.
109 * Revision 1.2 1995/09/04 12:33:42 adam
110 * Various cleanup. YAZ util used instead.
112 * Revision 1.1 1995/09/04 09:10:37 adam
113 * More work on index add/del/update.
114 * Merge sort implemented.
115 * Initial work on z39 server.
132 #define KEY_SIZE (1+sizeof(struct it_key))
133 #define INP_NAME_MAX 768
134 #define INP_BUF_START 60000
135 #define INP_BUF_ADD 400000
137 static int no_diffs = 0;
138 static int no_updates = 0;
139 static int no_deletions = 0;
140 static int no_insertions = 0;
141 static int no_iterations = 0;
144 int no; /* file no */
145 off_t offset; /* file offset */
146 unsigned char *buf; /* buffer block */
147 size_t buf_size; /* number of read bytes in block */
148 size_t chunk; /* number of bytes allocated */
149 size_t buf_ptr; /* current position in buffer */
150 char *prev_name; /* last word read */
151 int sysno; /* last sysno */
152 int seqno; /* last seqno */
153 off_t length; /* length of file */
154 /* handler invoked in each read */
155 void (*readHandler)(struct key_file *keyp, void *rinfo);
159 void getFnameTmp (char *fname, int no)
163 pre = res_get_def (common_resource, "keyTmpDir", ".");
164 sprintf (fname, "%s/key%d.tmp", pre, no);
167 void key_file_chunk_read (struct key_file *f)
169 int nr = 0, r = 0, fd;
171 getFnameTmp (fname, f->no);
172 fd = open (fname, O_BINARY|O_RDONLY);
175 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
180 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
182 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
186 if (lseek (fd, f->offset, SEEK_SET) == -1)
188 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
191 while (f->chunk - nr > 0)
193 r = read (fd, f->buf + nr, f->chunk - nr);
200 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
206 (*f->readHandler)(f, f->readInfo);
210 struct key_file *key_file_init (int no, int chunk)
214 f = xmalloc (sizeof(*f));
221 f->readHandler = NULL;
222 f->buf = xmalloc (f->chunk);
223 f->prev_name = xmalloc (INP_NAME_MAX);
224 *f->prev_name = '\0';
225 key_file_chunk_read (f);
229 int key_file_getc (struct key_file *f)
231 if (f->buf_ptr < f->buf_size)
232 return f->buf[(f->buf_ptr)++];
233 if (f->buf_size < f->chunk)
235 f->offset += f->buf_size;
236 key_file_chunk_read (f);
237 if (f->buf_ptr < f->buf_size)
238 return f->buf[(f->buf_ptr)++];
243 int key_file_decode (struct key_file *f)
247 c = key_file_getc (f);
254 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
257 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
258 d = (d << 8) + (key_file_getc (f) & 0xff);
261 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
262 d = (d << 8) + (key_file_getc (f) & 0xff);
263 d = (d << 8) + (key_file_getc (f) & 0xff);
269 int key_file_read (struct key_file *f, char *key)
274 c = key_file_getc (f);
277 strcpy (key, f->prev_name);
286 while ((key[i++] = key_file_getc (f)))
288 strcpy (f->prev_name, key);
291 d = key_file_decode (f);
294 itkey.sysno = d + f->sysno;
297 f->sysno = itkey.sysno;
300 d = key_file_decode (f);
301 itkey.seqno = d + f->seqno;
302 f->seqno = itkey.seqno;
303 memcpy (key + i, &itkey, sizeof(struct it_key));
304 return i + sizeof (struct it_key);
309 struct key_file **file;
314 int (*cmp)(const void *p1, const void *p2);
321 struct heap_info *key_heap_init (int nkeys,
322 int (*cmp)(const void *p1, const void *p2))
324 struct heap_info *hi;
327 hi = xmalloc (sizeof(*hi));
328 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
329 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
331 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
333 for (i = 0; i<= nkeys; i++)
336 hi->info.buf[i] = xmalloc (INP_NAME_MAX);
341 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
346 hi->ptr[i1] = hi->ptr[i2];
351 static void key_heap_delete (struct heap_info *hi)
353 int cur = 1, child = 2;
355 assert (hi->heapnum > 0);
357 key_heap_swap (hi, 1, hi->heapnum);
359 while (child <= hi->heapnum) {
360 if (child < hi->heapnum &&
361 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
362 &hi->info.buf[hi->ptr[child+1]]) > 0)
364 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
365 &hi->info.buf[hi->ptr[child]]) > 0)
367 key_heap_swap (hi, cur, child);
376 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
381 cur = ++(hi->heapnum);
382 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
383 hi->info.file[hi->ptr[cur]] = kf;
386 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
387 &hi->info.buf[hi->ptr[cur]]) > 0)
389 key_heap_swap (hi, cur, parent);
395 static int heap_read_one (struct heap_info *hi, char *name, char *key)
398 char rbuf[INP_NAME_MAX];
404 strcpy (name, hi->info.buf[n]);
405 kf = hi->info.file[n];
407 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
408 key_heap_delete (hi);
409 if ((r = key_file_read (kf, rbuf)))
410 key_heap_insert (hi, rbuf, r, kf);
415 struct heap_cread_info {
416 char prev_name[INP_NAME_MAX];
417 char cur_name[INP_NAME_MAX];
419 struct heap_info *hi;
424 int heap_cread_item (void *vp, char **dst, int *insertMode)
426 struct heap_cread_info *p = vp;
427 struct heap_info *hi = p->hi;
431 *insertMode = p->key[0];
432 memcpy (*dst, p->key+1, sizeof(struct it_key));
433 (*dst) += sizeof(struct it_key);
437 strcpy (p->prev_name, p->cur_name);
438 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
440 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
445 *insertMode = p->key[0];
446 memcpy (*dst, p->key+1, sizeof(struct it_key));
447 (*dst) += sizeof(struct it_key);
451 int heap_inpc (struct heap_info *hi)
453 struct heap_cread_info hci;
454 ISAMC_I isamc_i = xmalloc (sizeof(*isamc_i));
456 hci.key = xmalloc (KEY_SIZE);
459 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
461 isamc_i->clientData = &hci;
462 isamc_i->read_item = heap_cread_item;
466 char this_name[INP_NAME_MAX];
467 ISAMC_P isamc_p, isamc_p2;
470 strcpy (this_name, hci.cur_name);
471 assert (hci.cur_name[1]);
473 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
475 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
476 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
480 if (!dict_delete (hi->dict, this_name))
486 if (isamc_p2 != isamc_p)
487 dict_insert (hi->dict, this_name,
488 sizeof(ISAMC_P), &isamc_p2);
493 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
495 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
502 int heap_inps (struct heap_info *hi)
504 struct heap_cread_info hci;
505 ISAMS_I isams_i = xmalloc (sizeof(*isams_i));
507 hci.key = xmalloc (KEY_SIZE);
510 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
512 isams_i->clientData = &hci;
513 isams_i->read_item = heap_cread_item;
517 char this_name[INP_NAME_MAX];
521 strcpy (this_name, hci.cur_name);
522 assert (hci.cur_name[1]);
524 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
526 isams_p = isams_merge (hi->isams, isams_i);
528 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
538 int heap_inp (struct heap_info *hi)
541 char next_name[INP_NAME_MAX];
542 char cur_name[INP_NAME_MAX];
543 int key_buf_size = INP_BUF_START;
549 next_key = xmalloc (KEY_SIZE);
550 key_buf = xmalloc (key_buf_size);
551 more = heap_read_one (hi, cur_name, key_buf);
552 while (more) /* EOF ? */
555 key_buf_ptr = KEY_SIZE;
558 if (!(more = heap_read_one (hi, next_name, next_key)))
560 if (*next_name && strcmp (next_name, cur_name))
562 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
563 key_buf_ptr += KEY_SIZE;
564 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
567 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
568 memcpy (new_key_buf, key_buf, key_buf_size);
569 key_buf_size += INP_BUF_ADD;
571 key_buf = new_key_buf;
575 nmemb = key_buf_ptr / KEY_SIZE;
576 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
577 if ((info = dict_lookup (hi->dict, cur_name)))
579 ISAM_P isam_p, isam_p2;
580 memcpy (&isam_p, info+1, sizeof(ISAM_P));
581 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
585 if (!dict_delete (hi->dict, cur_name))
591 if (isam_p2 != isam_p)
592 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
599 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
600 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
602 memcpy (key_buf, next_key, KEY_SIZE);
603 strcpy (cur_name, next_name);
608 struct progressInfo {
615 void progressFunc (struct key_file *keyp, void *info)
617 struct progressInfo *p = info;
618 time_t now, remaining;
620 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
624 if (now >= p->lastTime+10)
627 remaining = (time_t) ((now - p->startTime)*
628 ((double) p->totalBytes/p->totalOffset - 1.0));
629 if (remaining <= 130)
630 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
631 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
633 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
634 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
636 p->totalOffset += keyp->buf_size;
643 void key_input (BFiles bfs, int nkeys, int cache)
650 struct key_file **kf;
653 struct heap_info *hi;
654 struct progressInfo progressInfo;
662 getFnameTmp (fname, nkeys+1);
663 if (access (fname, R_OK) == -1)
670 dict = dict_open (bfs, FNAME_DICT, cache, 1);
673 logf (LOG_FATAL, "dict_open fail");
676 if (res_get_match (common_resource, "isam", "s", NULL))
678 isams = isams_open (bfs, FNAME_ISAMS, 1,
679 key_isams_m (common_resource));
682 logf (LOG_FATAL, "isams_open fail");
686 else if (res_get_match (common_resource, "isam", "i", NULL))
688 isam = is_open (bfs, FNAME_ISAM, key_compare, 1,
689 sizeof(struct it_key), common_resource);
692 logf (LOG_FATAL, "is_open fail");
698 isamc = isc_open (bfs, FNAME_ISAMC, 1,
699 key_isamc_m (common_resource));
702 logf (LOG_FATAL, "isc_open fail");
706 kf = xmalloc ((1+nkeys) * sizeof(*kf));
707 progressInfo.totalBytes = 0;
708 progressInfo.totalOffset = 0;
709 time (&progressInfo.startTime);
710 time (&progressInfo.lastTime);
711 for (i = 1; i<=nkeys; i++)
713 kf[i] = key_file_init (i, 32768);
714 kf[i]->readHandler = progressFunc;
715 kf[i]->readInfo = &progressInfo;
716 progressInfo.totalBytes += kf[i]->length;
717 progressInfo.totalOffset += kf[i]->buf_size;
719 hi = key_heap_init (nkeys, key_qsort_compare);
725 for (i = 1; i<=nkeys; i++)
726 if ((r = key_file_read (kf[i], rbuf)))
727 key_heap_insert (hi, rbuf, r, kf[i]);
742 for (i = 1; i<=nkeys; i++)
744 getFnameTmp (rbuf, i);
747 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
748 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
749 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
750 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
751 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);