2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
23 #define KEY_SIZE (1+sizeof(struct it_key))
24 #define INP_NAME_MAX 768
25 #define INP_BUF_START 60000
26 #define INP_BUF_ADD 400000
28 static int no_diffs = 0;
29 static int no_updates = 0;
30 static int no_deletions = 0;
31 static int no_insertions = 0;
32 static int no_iterations = 0;
36 off_t offset; /* file offset */
37 unsigned char *buf; /* buffer block */
38 size_t buf_size; /* number of read bytes in block */
39 size_t chunk; /* number of bytes allocated */
40 size_t buf_ptr; /* current position in buffer */
41 char *prev_name; /* last word read */
42 int sysno; /* last sysno */
43 int seqno; /* last seqno */
44 off_t length; /* length of file */
45 /* handler invoked in each read */
46 void (*readHandler)(struct key_file *keyp, void *rinfo);
50 void getFnameTmp (char *fname, int no)
54 pre = res_get_def (common_resource, "keyTmpDir", ".");
55 sprintf (fname, "%s/key%d.tmp", pre, no);
58 void key_file_chunk_read (struct key_file *f)
60 int nr = 0, r = 0, fd;
62 getFnameTmp (fname, f->no);
63 fd = open (fname, O_BINARY|O_RDONLY);
66 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
71 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
73 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
77 if (lseek (fd, f->offset, SEEK_SET) == -1)
79 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
82 while (f->chunk - nr > 0)
84 r = read (fd, f->buf + nr, f->chunk - nr);
91 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
97 (*f->readHandler)(f, f->readInfo);
101 struct key_file *key_file_init (int no, int chunk)
105 f = (struct key_file *) xmalloc (sizeof(*f));
112 f->readHandler = NULL;
113 f->buf = (unsigned char *) xmalloc (f->chunk);
114 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
115 *f->prev_name = '\0';
116 key_file_chunk_read (f);
120 int key_file_getc (struct key_file *f)
122 if (f->buf_ptr < f->buf_size)
123 return f->buf[(f->buf_ptr)++];
124 if (f->buf_size < f->chunk)
126 f->offset += f->buf_size;
127 key_file_chunk_read (f);
128 if (f->buf_ptr < f->buf_size)
129 return f->buf[(f->buf_ptr)++];
134 int key_file_decode (struct key_file *f)
138 c = key_file_getc (f);
145 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
148 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
149 d = (d << 8) + (key_file_getc (f) & 0xff);
152 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
153 d = (d << 8) + (key_file_getc (f) & 0xff);
154 d = (d << 8) + (key_file_getc (f) & 0xff);
160 int key_file_read (struct key_file *f, char *key)
165 c = key_file_getc (f);
168 strcpy (key, f->prev_name);
177 while ((key[i++] = key_file_getc (f)))
179 strcpy (f->prev_name, key);
182 d = key_file_decode (f);
185 itkey.sysno = d + f->sysno;
188 f->sysno = itkey.sysno;
191 d = key_file_decode (f);
192 itkey.seqno = d + f->seqno;
193 f->seqno = itkey.seqno;
194 memcpy (key + i, &itkey, sizeof(struct it_key));
195 return i + sizeof (struct it_key);
200 struct key_file **file;
205 int (*cmp)(const void *p1, const void *p2);
213 struct heap_info *key_heap_init (int nkeys,
214 int (*cmp)(const void *p1, const void *p2))
216 struct heap_info *hi;
219 hi = (struct heap_info *) xmalloc (sizeof(*hi));
220 hi->info.file = (struct key_file **)
221 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
222 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
224 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
226 for (i = 0; i<= nkeys; i++)
229 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
234 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
239 hi->ptr[i1] = hi->ptr[i2];
244 static void key_heap_delete (struct heap_info *hi)
246 int cur = 1, child = 2;
248 assert (hi->heapnum > 0);
250 key_heap_swap (hi, 1, hi->heapnum);
252 while (child <= hi->heapnum) {
253 if (child < hi->heapnum &&
254 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
255 &hi->info.buf[hi->ptr[child+1]]) > 0)
257 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
258 &hi->info.buf[hi->ptr[child]]) > 0)
260 key_heap_swap (hi, cur, child);
269 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
274 cur = ++(hi->heapnum);
275 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
276 hi->info.file[hi->ptr[cur]] = kf;
279 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
280 &hi->info.buf[hi->ptr[cur]]) > 0)
282 key_heap_swap (hi, cur, parent);
288 static int heap_read_one (struct heap_info *hi, char *name, char *key)
291 char rbuf[INP_NAME_MAX];
297 strcpy (name, hi->info.buf[n]);
298 kf = hi->info.file[n];
300 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
301 key_heap_delete (hi);
302 if ((r = key_file_read (kf, rbuf)))
303 key_heap_insert (hi, rbuf, r, kf);
308 struct heap_cread_info {
309 char prev_name[INP_NAME_MAX];
310 char cur_name[INP_NAME_MAX];
312 struct heap_info *hi;
317 int heap_cread_item (void *vp, char **dst, int *insertMode)
319 struct heap_cread_info *p = (struct heap_cread_info *) vp;
320 struct heap_info *hi = p->hi;
324 *insertMode = p->key[0];
325 memcpy (*dst, p->key+1, sizeof(struct it_key));
326 (*dst) += sizeof(struct it_key);
330 strcpy (p->prev_name, p->cur_name);
331 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
333 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
338 *insertMode = p->key[0];
339 memcpy (*dst, p->key+1, sizeof(struct it_key));
340 (*dst) += sizeof(struct it_key);
344 int heap_inpc (struct heap_info *hi)
346 struct heap_cread_info hci;
347 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
349 hci.key = (char *) xmalloc (KEY_SIZE);
352 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
354 isamc_i->clientData = &hci;
355 isamc_i->read_item = heap_cread_item;
359 char this_name[INP_NAME_MAX];
360 ISAMC_P isamc_p, isamc_p2;
363 strcpy (this_name, hci.cur_name);
364 assert (hci.cur_name[1]);
366 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
368 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
369 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
373 if (!dict_delete (hi->dict, this_name))
379 if (isamc_p2 != isamc_p)
380 dict_insert (hi->dict, this_name,
381 sizeof(ISAMC_P), &isamc_p2);
386 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
388 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
395 int heap_inps (struct heap_info *hi)
397 struct heap_cread_info hci;
398 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
400 hci.key = (char *) xmalloc (KEY_SIZE);
403 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
405 isams_i->clientData = &hci;
406 isams_i->read_item = heap_cread_item;
410 char this_name[INP_NAME_MAX];
414 strcpy (this_name, hci.cur_name);
415 assert (hci.cur_name[1]);
417 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
419 isams_p = isams_merge (hi->isams, isams_i);
421 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
430 int heap_inph (struct heap_info *hi)
432 struct heap_cread_info hci;
433 ISAMH_I isamh_i = (ISAMH_I) xmalloc (sizeof(*isamh_i));
435 hci.key = (char *) xmalloc (KEY_SIZE);
438 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
440 isamh_i->clientData = &hci;
441 isamh_i->read_item = heap_cread_item;
445 char this_name[INP_NAME_MAX];
446 ISAMH_P isamh_p, isamh_p2;
449 strcpy (this_name, hci.cur_name);
450 assert (hci.cur_name[1]);
452 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
454 memcpy (&isamh_p, dict_info+1, sizeof(ISAMH_P));
455 isamh_p2 = isamh_append (hi->isamh, isamh_p, isamh_i);
459 if (!dict_delete (hi->dict, this_name))
465 if (isamh_p2 != isamh_p)
466 dict_insert (hi->dict, this_name,
467 sizeof(ISAMH_P), &isamh_p2);
472 isamh_p = isamh_append (hi->isamh, 0, isamh_i);
474 dict_insert (hi->dict, this_name, sizeof(ISAMH_P), &isamh_p);
481 int heap_inp (struct heap_info *hi)
484 char next_name[INP_NAME_MAX];
485 char cur_name[INP_NAME_MAX];
486 int key_buf_size = INP_BUF_START;
492 next_key = (char *) xmalloc (KEY_SIZE);
493 key_buf = (char *) xmalloc (key_buf_size);
494 more = heap_read_one (hi, cur_name, key_buf);
495 while (more) /* EOF ? */
498 key_buf_ptr = KEY_SIZE;
501 if (!(more = heap_read_one (hi, next_name, next_key)))
503 if (*next_name && strcmp (next_name, cur_name))
505 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
506 key_buf_ptr += KEY_SIZE;
507 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
510 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
511 memcpy (new_key_buf, key_buf, key_buf_size);
512 key_buf_size += INP_BUF_ADD;
514 key_buf = new_key_buf;
518 nmemb = key_buf_ptr / KEY_SIZE;
519 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
520 if ((info = dict_lookup (hi->dict, cur_name)))
522 ISAM_P isam_p, isam_p2;
523 memcpy (&isam_p, info+1, sizeof(ISAM_P));
524 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
528 if (!dict_delete (hi->dict, cur_name))
534 if (isam_p2 != isam_p)
535 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
542 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
543 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
545 memcpy (key_buf, next_key, KEY_SIZE);
546 strcpy (cur_name, next_name);
551 struct progressInfo {
558 void progressFunc (struct key_file *keyp, void *info)
560 struct progressInfo *p = (struct progressInfo *) info;
561 time_t now, remaining;
563 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
567 if (now >= p->lastTime+10)
570 remaining = (time_t) ((now - p->startTime)*
571 ((double) p->totalBytes/p->totalOffset - 1.0));
572 if (remaining <= 130)
573 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
574 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
576 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
577 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
579 p->totalOffset += keyp->buf_size;
586 void key_input (BFiles bfs, int nkeys, int cache)
594 struct key_file **kf;
597 struct heap_info *hi;
598 struct progressInfo progressInfo;
606 getFnameTmp (fname, nkeys+1);
607 if (access (fname, R_OK) == -1)
614 dict = dict_open (bfs, FNAME_DICT, cache, 1, 0);
617 logf (LOG_FATAL, "dict_open fail");
620 if (res_get_match (common_resource, "isam", "s", NULL))
622 isams = isams_open (bfs, FNAME_ISAMS, 1,
623 key_isams_m (common_resource));
626 logf (LOG_FATAL, "isams_open fail");
630 else if (res_get_match (common_resource, "isam", "i", NULL))
632 isam = is_open (bfs, FNAME_ISAM, key_compare, 1,
633 sizeof(struct it_key), common_resource);
636 logf (LOG_FATAL, "is_open fail");
640 else if (res_get_match (common_resource, "isam", "h", NULL))
642 isamh = isamh_open (bfs, FNAME_ISAMH, 1,
643 key_isamh_m (common_resource));
646 logf (LOG_FATAL, "isamh_open fail");
652 isamc = isc_open (bfs, FNAME_ISAMC, 1,
653 key_isamc_m (common_resource));
656 logf (LOG_FATAL, "isc_open fail");
660 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
661 progressInfo.totalBytes = 0;
662 progressInfo.totalOffset = 0;
663 time (&progressInfo.startTime);
664 time (&progressInfo.lastTime);
665 for (i = 1; i<=nkeys; i++)
667 kf[i] = key_file_init (i, 32768);
668 kf[i]->readHandler = progressFunc;
669 kf[i]->readInfo = &progressInfo;
670 progressInfo.totalBytes += kf[i]->length;
671 progressInfo.totalOffset += kf[i]->buf_size;
673 hi = key_heap_init (nkeys, key_qsort_compare);
680 for (i = 1; i<=nkeys; i++)
681 if ((r = key_file_read (kf[i], rbuf)))
682 key_heap_insert (hi, rbuf, r, kf[i]);
702 for (i = 1; i<=nkeys; i++)
704 getFnameTmp (rbuf, i);
707 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
708 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
709 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
710 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
711 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
718 * Revision 1.35 1999-06-30 15:07:23 heikki
721 * Revision 1.34 1999/05/26 07:49:13 adam
724 * Revision 1.33 1999/05/15 14:36:38 adam
725 * Updated dictionary. Implemented "compression" of dictionary.
727 * Revision 1.32 1999/05/12 13:08:06 adam
728 * First version of ISAMS.
730 * Revision 1.31 1999/02/02 14:50:56 adam
731 * Updated WIN32 code specific sections. Changed header.
733 * Revision 1.30 1998/10/28 10:53:57 adam
734 * Added type cast to prevent warning.
736 * Revision 1.29 1998/06/11 15:41:39 adam
739 * Revision 1.28 1998/03/05 08:45:12 adam
740 * New result set model and modular ranking system. Moved towards
741 * descent server API. System information stored as "SGML" records.
743 * Revision 1.27 1998/02/17 10:32:52 adam
744 * Fixed bug: binary files weren't opened with flag b on NT.
746 * Revision 1.26 1998/01/29 13:39:13 adam
747 * Compress ISAM is default.
749 * Revision 1.25 1997/09/17 12:19:14 adam
750 * Zebra version corresponds to YAZ version 1.4.
751 * Changed Zebra server so that it doesn't depend on global common_resource.
753 * Revision 1.24 1997/09/09 13:38:07 adam
754 * Partial port to WIN95/NT.
756 * Revision 1.23 1997/09/04 13:57:39 adam
757 * Added O_BINARY for open calls.
759 * Revision 1.22 1997/02/12 20:39:45 adam
760 * Implemented options -f <n> that limits the log to the first <n>
762 * Changed some log messages also.
764 * Revision 1.21 1996/11/08 11:10:23 adam
765 * Buffers used during file match got bigger.
766 * Compressed ISAM support everywhere.
767 * Bug fixes regarding masking characters in queries.
768 * Redesigned Regexp-2 queries.
770 * Revision 1.20 1996/11/01 08:58:41 adam
771 * Interface to isamc system now includes update and delete.
773 * Revision 1.19 1996/10/29 14:09:46 adam
774 * Use of cisam system - enabled if setting isamc is 1.
776 * Revision 1.18 1996/06/04 10:18:59 adam
777 * Minor changes - removed include of ctype.h.
779 * Revision 1.17 1996/05/14 15:47:07 adam
780 * Cleanup of various buffer size entities.
782 * Revision 1.16 1996/04/09 10:05:20 adam
783 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
785 * Revision 1.15 1996/03/21 14:50:09 adam
786 * File update uses modify-time instead of change-time.
788 * Revision 1.14 1996/02/07 14:06:37 adam
789 * Better progress report during register merge.
790 * New command: clean - removes temporary shadow files.
792 * Revision 1.13 1996/02/05 12:30:00 adam
793 * Logging reduced a bit.
794 * The remaining running time is estimated during register merge.
796 * Revision 1.12 1995/12/06 17:49:19 adam
797 * Uses dict_delete now.
799 * Revision 1.11 1995/12/06 16:06:43 adam
800 * Better diagnostics. Work on 'real' dictionary deletion.
802 * Revision 1.10 1995/12/06 12:41:22 adam
803 * New command 'stat' for the index program.
804 * Filenames can be read from stdin by specifying '-'.
805 * Bug fix/enhancement of the transformation from terms to regular
806 * expressons in the search engine.
808 * Revision 1.9 1995/10/10 12:24:39 adam
809 * Temporary sort files are compressed.
811 * Revision 1.8 1995/10/04 16:57:19 adam
812 * Key input and merge sort in one pass.
814 * Revision 1.7 1995/10/02 15:18:52 adam
815 * New member in recRetrieveCtrl: diagnostic.
817 * Revision 1.6 1995/09/29 15:51:56 adam
818 * First work on multi-way read.
820 * Revision 1.5 1995/09/29 14:01:43 adam
823 * Revision 1.4 1995/09/28 14:22:57 adam
824 * Sort uses smaller temporary files.
826 * Revision 1.3 1995/09/06 16:11:17 adam
827 * Option: only one word key per file.
829 * Revision 1.2 1995/09/04 12:33:42 adam
830 * Various cleanup. YAZ util used instead.
832 * Revision 1.1 1995/09/04 09:10:37 adam
833 * More work on index add/del/update.
834 * Merge sort implemented.
835 * Initial work on z39 server.