2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.33 1999-05-15 14:36:38 adam
8 * Updated dictionary. Implemented "compression" of dictionary.
10 * Revision 1.32 1999/05/12 13:08:06 adam
11 * First version of ISAMS.
13 * Revision 1.31 1999/02/02 14:50:56 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.30 1998/10/28 10:53:57 adam
17 * Added type cast to prevent warning.
19 * Revision 1.29 1998/06/11 15:41:39 adam
22 * Revision 1.28 1998/03/05 08:45:12 adam
23 * New result set model and modular ranking system. Moved towards
24 * descent server API. System information stored as "SGML" records.
26 * Revision 1.27 1998/02/17 10:32:52 adam
27 * Fixed bug: binary files weren't opened with flag b on NT.
29 * Revision 1.26 1998/01/29 13:39:13 adam
30 * Compress ISAM is default.
32 * Revision 1.25 1997/09/17 12:19:14 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.24 1997/09/09 13:38:07 adam
37 * Partial port to WIN95/NT.
39 * Revision 1.23 1997/09/04 13:57:39 adam
40 * Added O_BINARY for open calls.
42 * Revision 1.22 1997/02/12 20:39:45 adam
43 * Implemented options -f <n> that limits the log to the first <n>
45 * Changed some log messages also.
47 * Revision 1.21 1996/11/08 11:10:23 adam
48 * Buffers used during file match got bigger.
49 * Compressed ISAM support everywhere.
50 * Bug fixes regarding masking characters in queries.
51 * Redesigned Regexp-2 queries.
53 * Revision 1.20 1996/11/01 08:58:41 adam
54 * Interface to isamc system now includes update and delete.
56 * Revision 1.19 1996/10/29 14:09:46 adam
57 * Use of cisam system - enabled if setting isamc is 1.
59 * Revision 1.18 1996/06/04 10:18:59 adam
60 * Minor changes - removed include of ctype.h.
62 * Revision 1.17 1996/05/14 15:47:07 adam
63 * Cleanup of various buffer size entities.
65 * Revision 1.16 1996/04/09 10:05:20 adam
66 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
68 * Revision 1.15 1996/03/21 14:50:09 adam
69 * File update uses modify-time instead of change-time.
71 * Revision 1.14 1996/02/07 14:06:37 adam
72 * Better progress report during register merge.
73 * New command: clean - removes temporary shadow files.
75 * Revision 1.13 1996/02/05 12:30:00 adam
76 * Logging reduced a bit.
77 * The remaining running time is estimated during register merge.
79 * Revision 1.12 1995/12/06 17:49:19 adam
80 * Uses dict_delete now.
82 * Revision 1.11 1995/12/06 16:06:43 adam
83 * Better diagnostics. Work on 'real' dictionary deletion.
85 * Revision 1.10 1995/12/06 12:41:22 adam
86 * New command 'stat' for the index program.
87 * Filenames can be read from stdin by specifying '-'.
88 * Bug fix/enhancement of the transformation from terms to regular
89 * expressons in the search engine.
91 * Revision 1.9 1995/10/10 12:24:39 adam
92 * Temporary sort files are compressed.
94 * Revision 1.8 1995/10/04 16:57:19 adam
95 * Key input and merge sort in one pass.
97 * Revision 1.7 1995/10/02 15:18:52 adam
98 * New member in recRetrieveCtrl: diagnostic.
100 * Revision 1.6 1995/09/29 15:51:56 adam
101 * First work on multi-way read.
103 * Revision 1.5 1995/09/29 14:01:43 adam
106 * Revision 1.4 1995/09/28 14:22:57 adam
107 * Sort uses smaller temporary files.
109 * Revision 1.3 1995/09/06 16:11:17 adam
110 * Option: only one word key per file.
112 * Revision 1.2 1995/09/04 12:33:42 adam
113 * Various cleanup. YAZ util used instead.
115 * Revision 1.1 1995/09/04 09:10:37 adam
116 * More work on index add/del/update.
117 * Merge sort implemented.
118 * Initial work on z39 server.
135 #define KEY_SIZE (1+sizeof(struct it_key))
136 #define INP_NAME_MAX 768
137 #define INP_BUF_START 60000
138 #define INP_BUF_ADD 400000
140 static int no_diffs = 0;
141 static int no_updates = 0;
142 static int no_deletions = 0;
143 static int no_insertions = 0;
144 static int no_iterations = 0;
147 int no; /* file no */
148 off_t offset; /* file offset */
149 unsigned char *buf; /* buffer block */
150 size_t buf_size; /* number of read bytes in block */
151 size_t chunk; /* number of bytes allocated */
152 size_t buf_ptr; /* current position in buffer */
153 char *prev_name; /* last word read */
154 int sysno; /* last sysno */
155 int seqno; /* last seqno */
156 off_t length; /* length of file */
157 /* handler invoked in each read */
158 void (*readHandler)(struct key_file *keyp, void *rinfo);
162 void getFnameTmp (char *fname, int no)
166 pre = res_get_def (common_resource, "keyTmpDir", ".");
167 sprintf (fname, "%s/key%d.tmp", pre, no);
170 void key_file_chunk_read (struct key_file *f)
172 int nr = 0, r = 0, fd;
174 getFnameTmp (fname, f->no);
175 fd = open (fname, O_BINARY|O_RDONLY);
178 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
183 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
185 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
189 if (lseek (fd, f->offset, SEEK_SET) == -1)
191 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
194 while (f->chunk - nr > 0)
196 r = read (fd, f->buf + nr, f->chunk - nr);
203 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
209 (*f->readHandler)(f, f->readInfo);
213 struct key_file *key_file_init (int no, int chunk)
217 f = xmalloc (sizeof(*f));
224 f->readHandler = NULL;
225 f->buf = xmalloc (f->chunk);
226 f->prev_name = xmalloc (INP_NAME_MAX);
227 *f->prev_name = '\0';
228 key_file_chunk_read (f);
232 int key_file_getc (struct key_file *f)
234 if (f->buf_ptr < f->buf_size)
235 return f->buf[(f->buf_ptr)++];
236 if (f->buf_size < f->chunk)
238 f->offset += f->buf_size;
239 key_file_chunk_read (f);
240 if (f->buf_ptr < f->buf_size)
241 return f->buf[(f->buf_ptr)++];
246 int key_file_decode (struct key_file *f)
250 c = key_file_getc (f);
257 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
260 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
261 d = (d << 8) + (key_file_getc (f) & 0xff);
264 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
265 d = (d << 8) + (key_file_getc (f) & 0xff);
266 d = (d << 8) + (key_file_getc (f) & 0xff);
272 int key_file_read (struct key_file *f, char *key)
277 c = key_file_getc (f);
280 strcpy (key, f->prev_name);
289 while ((key[i++] = key_file_getc (f)))
291 strcpy (f->prev_name, key);
294 d = key_file_decode (f);
297 itkey.sysno = d + f->sysno;
300 f->sysno = itkey.sysno;
303 d = key_file_decode (f);
304 itkey.seqno = d + f->seqno;
305 f->seqno = itkey.seqno;
306 memcpy (key + i, &itkey, sizeof(struct it_key));
307 return i + sizeof (struct it_key);
312 struct key_file **file;
317 int (*cmp)(const void *p1, const void *p2);
324 struct heap_info *key_heap_init (int nkeys,
325 int (*cmp)(const void *p1, const void *p2))
327 struct heap_info *hi;
330 hi = xmalloc (sizeof(*hi));
331 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
332 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
334 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
336 for (i = 0; i<= nkeys; i++)
339 hi->info.buf[i] = xmalloc (INP_NAME_MAX);
344 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
349 hi->ptr[i1] = hi->ptr[i2];
354 static void key_heap_delete (struct heap_info *hi)
356 int cur = 1, child = 2;
358 assert (hi->heapnum > 0);
360 key_heap_swap (hi, 1, hi->heapnum);
362 while (child <= hi->heapnum) {
363 if (child < hi->heapnum &&
364 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
365 &hi->info.buf[hi->ptr[child+1]]) > 0)
367 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
368 &hi->info.buf[hi->ptr[child]]) > 0)
370 key_heap_swap (hi, cur, child);
379 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
384 cur = ++(hi->heapnum);
385 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
386 hi->info.file[hi->ptr[cur]] = kf;
389 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
390 &hi->info.buf[hi->ptr[cur]]) > 0)
392 key_heap_swap (hi, cur, parent);
398 static int heap_read_one (struct heap_info *hi, char *name, char *key)
401 char rbuf[INP_NAME_MAX];
407 strcpy (name, hi->info.buf[n]);
408 kf = hi->info.file[n];
410 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
411 key_heap_delete (hi);
412 if ((r = key_file_read (kf, rbuf)))
413 key_heap_insert (hi, rbuf, r, kf);
418 struct heap_cread_info {
419 char prev_name[INP_NAME_MAX];
420 char cur_name[INP_NAME_MAX];
422 struct heap_info *hi;
427 int heap_cread_item (void *vp, char **dst, int *insertMode)
429 struct heap_cread_info *p = vp;
430 struct heap_info *hi = p->hi;
434 *insertMode = p->key[0];
435 memcpy (*dst, p->key+1, sizeof(struct it_key));
436 (*dst) += sizeof(struct it_key);
440 strcpy (p->prev_name, p->cur_name);
441 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
443 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
448 *insertMode = p->key[0];
449 memcpy (*dst, p->key+1, sizeof(struct it_key));
450 (*dst) += sizeof(struct it_key);
454 int heap_inpc (struct heap_info *hi)
456 struct heap_cread_info hci;
457 ISAMC_I isamc_i = xmalloc (sizeof(*isamc_i));
459 hci.key = xmalloc (KEY_SIZE);
462 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
464 isamc_i->clientData = &hci;
465 isamc_i->read_item = heap_cread_item;
469 char this_name[INP_NAME_MAX];
470 ISAMC_P isamc_p, isamc_p2;
473 strcpy (this_name, hci.cur_name);
474 assert (hci.cur_name[1]);
476 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
478 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
479 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
483 if (!dict_delete (hi->dict, this_name))
489 if (isamc_p2 != isamc_p)
490 dict_insert (hi->dict, this_name,
491 sizeof(ISAMC_P), &isamc_p2);
496 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
498 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
505 int heap_inps (struct heap_info *hi)
507 struct heap_cread_info hci;
508 ISAMS_I isams_i = xmalloc (sizeof(*isams_i));
510 hci.key = xmalloc (KEY_SIZE);
513 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
515 isams_i->clientData = &hci;
516 isams_i->read_item = heap_cread_item;
520 char this_name[INP_NAME_MAX];
524 strcpy (this_name, hci.cur_name);
525 assert (hci.cur_name[1]);
527 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
529 isams_p = isams_merge (hi->isams, isams_i);
531 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
541 int heap_inp (struct heap_info *hi)
544 char next_name[INP_NAME_MAX];
545 char cur_name[INP_NAME_MAX];
546 int key_buf_size = INP_BUF_START;
552 next_key = xmalloc (KEY_SIZE);
553 key_buf = xmalloc (key_buf_size);
554 more = heap_read_one (hi, cur_name, key_buf);
555 while (more) /* EOF ? */
558 key_buf_ptr = KEY_SIZE;
561 if (!(more = heap_read_one (hi, next_name, next_key)))
563 if (*next_name && strcmp (next_name, cur_name))
565 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
566 key_buf_ptr += KEY_SIZE;
567 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
570 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
571 memcpy (new_key_buf, key_buf, key_buf_size);
572 key_buf_size += INP_BUF_ADD;
574 key_buf = new_key_buf;
578 nmemb = key_buf_ptr / KEY_SIZE;
579 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
580 if ((info = dict_lookup (hi->dict, cur_name)))
582 ISAM_P isam_p, isam_p2;
583 memcpy (&isam_p, info+1, sizeof(ISAM_P));
584 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
588 if (!dict_delete (hi->dict, cur_name))
594 if (isam_p2 != isam_p)
595 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
602 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
603 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
605 memcpy (key_buf, next_key, KEY_SIZE);
606 strcpy (cur_name, next_name);
611 struct progressInfo {
618 void progressFunc (struct key_file *keyp, void *info)
620 struct progressInfo *p = info;
621 time_t now, remaining;
623 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
627 if (now >= p->lastTime+10)
630 remaining = (time_t) ((now - p->startTime)*
631 ((double) p->totalBytes/p->totalOffset - 1.0));
632 if (remaining <= 130)
633 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
634 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
636 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
637 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
639 p->totalOffset += keyp->buf_size;
646 void key_input (BFiles bfs, int nkeys, int cache)
653 struct key_file **kf;
656 struct heap_info *hi;
657 struct progressInfo progressInfo;
665 getFnameTmp (fname, nkeys+1);
666 if (access (fname, R_OK) == -1)
673 dict = dict_open (bfs, FNAME_DICT, cache, 1, 0);
676 logf (LOG_FATAL, "dict_open fail");
679 if (res_get_match (common_resource, "isam", "s", NULL))
681 isams = isams_open (bfs, FNAME_ISAMS, 1,
682 key_isams_m (common_resource));
685 logf (LOG_FATAL, "isams_open fail");
689 else if (res_get_match (common_resource, "isam", "i", NULL))
691 isam = is_open (bfs, FNAME_ISAM, key_compare, 1,
692 sizeof(struct it_key), common_resource);
695 logf (LOG_FATAL, "is_open fail");
701 isamc = isc_open (bfs, FNAME_ISAMC, 1,
702 key_isamc_m (common_resource));
705 logf (LOG_FATAL, "isc_open fail");
709 kf = xmalloc ((1+nkeys) * sizeof(*kf));
710 progressInfo.totalBytes = 0;
711 progressInfo.totalOffset = 0;
712 time (&progressInfo.startTime);
713 time (&progressInfo.lastTime);
714 for (i = 1; i<=nkeys; i++)
716 kf[i] = key_file_init (i, 32768);
717 kf[i]->readHandler = progressFunc;
718 kf[i]->readInfo = &progressInfo;
719 progressInfo.totalBytes += kf[i]->length;
720 progressInfo.totalOffset += kf[i]->buf_size;
722 hi = key_heap_init (nkeys, key_qsort_compare);
728 for (i = 1; i<=nkeys; i++)
729 if ((r = key_file_read (kf[i], rbuf)))
730 key_heap_insert (hi, rbuf, r, kf[i]);
745 for (i = 1; i<=nkeys; i++)
747 getFnameTmp (rbuf, i);
750 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
751 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
752 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
753 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
754 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);