2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
9 * - Allocates a lot of memory for the merge process, but never releases it.
10 * Doesn't matter, as the program terminates soon after.
27 #define KEY_SIZE (1+sizeof(struct it_key))
28 #define INP_NAME_MAX 768
29 #define INP_BUF_START 60000
30 #define INP_BUF_ADD 400000
32 static int no_diffs = 0;
33 static int no_updates = 0;
34 static int no_deletions = 0;
35 static int no_insertions = 0;
36 static int no_iterations = 0;
40 off_t offset; /* file offset */
41 unsigned char *buf; /* buffer block */
42 size_t buf_size; /* number of read bytes in block */
43 size_t chunk; /* number of bytes allocated */
44 size_t buf_ptr; /* current position in buffer */
45 char *prev_name; /* last word read */
46 int sysno; /* last sysno */
47 int seqno; /* last seqno */
48 off_t length; /* length of file */
49 /* handler invoked in each read */
50 void (*readHandler)(struct key_file *keyp, void *rinfo);
54 void getFnameTmp (char *fname, int no)
58 pre = res_get_def (common_resource, "keyTmpDir", ".");
59 sprintf (fname, "%s/key%d.tmp", pre, no);
62 void key_file_chunk_read (struct key_file *f)
64 int nr = 0, r = 0, fd;
66 getFnameTmp (fname, f->no);
67 fd = open (fname, O_BINARY|O_RDONLY);
70 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
75 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
77 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
81 if (lseek (fd, f->offset, SEEK_SET) == -1)
83 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
86 while (f->chunk - nr > 0)
88 r = read (fd, f->buf + nr, f->chunk - nr);
95 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
101 (*f->readHandler)(f, f->readInfo);
105 struct key_file *key_file_init (int no, int chunk)
109 f = (struct key_file *) xmalloc (sizeof(*f));
116 f->readHandler = NULL;
117 f->buf = (unsigned char *) xmalloc (f->chunk);
118 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
119 *f->prev_name = '\0';
120 key_file_chunk_read (f);
124 int key_file_getc (struct key_file *f)
126 if (f->buf_ptr < f->buf_size)
127 return f->buf[(f->buf_ptr)++];
128 if (f->buf_size < f->chunk)
130 f->offset += f->buf_size;
131 key_file_chunk_read (f);
132 if (f->buf_ptr < f->buf_size)
133 return f->buf[(f->buf_ptr)++];
138 int key_file_decode (struct key_file *f)
142 c = key_file_getc (f);
149 d = ((c&63) << 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);
156 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
157 d = (d << 8) + (key_file_getc (f) & 0xff);
158 d = (d << 8) + (key_file_getc (f) & 0xff);
164 int key_file_read (struct key_file *f, char *key)
169 c = key_file_getc (f);
172 strcpy (key, f->prev_name);
181 while ((key[i++] = key_file_getc (f)))
183 strcpy (f->prev_name, key);
186 d = key_file_decode (f);
189 itkey.sysno = d + f->sysno;
192 f->sysno = itkey.sysno;
195 d = key_file_decode (f);
196 itkey.seqno = d + f->seqno;
197 f->seqno = itkey.seqno;
198 memcpy (key + i, &itkey, sizeof(struct it_key));
199 return i + sizeof (struct it_key);
204 struct key_file **file;
209 int (*cmp)(const void *p1, const void *p2);
218 struct heap_info *key_heap_init (int nkeys,
219 int (*cmp)(const void *p1, const void *p2))
221 struct heap_info *hi;
224 hi = (struct heap_info *) xmalloc (sizeof(*hi));
225 hi->info.file = (struct key_file **)
226 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
227 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
229 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
231 for (i = 0; i<= nkeys; i++)
234 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
239 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
244 hi->ptr[i1] = hi->ptr[i2];
249 static void key_heap_delete (struct heap_info *hi)
251 int cur = 1, child = 2;
253 assert (hi->heapnum > 0);
255 key_heap_swap (hi, 1, hi->heapnum);
257 while (child <= hi->heapnum) {
258 if (child < hi->heapnum &&
259 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
260 &hi->info.buf[hi->ptr[child+1]]) > 0)
262 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
263 &hi->info.buf[hi->ptr[child]]) > 0)
265 key_heap_swap (hi, cur, child);
274 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
279 cur = ++(hi->heapnum);
280 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
281 hi->info.file[hi->ptr[cur]] = kf;
284 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
285 &hi->info.buf[hi->ptr[cur]]) > 0)
287 key_heap_swap (hi, cur, parent);
293 static int heap_read_one (struct heap_info *hi, char *name, char *key)
296 char rbuf[INP_NAME_MAX];
302 strcpy (name, hi->info.buf[n]);
303 kf = hi->info.file[n];
305 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
306 key_heap_delete (hi);
307 if ((r = key_file_read (kf, rbuf)))
308 key_heap_insert (hi, rbuf, r, kf);
313 struct heap_cread_info {
314 char prev_name[INP_NAME_MAX];
315 char cur_name[INP_NAME_MAX];
317 struct heap_info *hi;
322 int heap_cread_item (void *vp, char **dst, int *insertMode)
324 struct heap_cread_info *p = (struct heap_cread_info *) vp;
325 struct heap_info *hi = p->hi;
329 *insertMode = p->key[0];
330 memcpy (*dst, p->key+1, sizeof(struct it_key));
331 (*dst) += sizeof(struct it_key);
335 strcpy (p->prev_name, p->cur_name);
336 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
338 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
343 *insertMode = p->key[0];
344 memcpy (*dst, p->key+1, sizeof(struct it_key));
345 (*dst) += sizeof(struct it_key);
349 int heap_inpc (struct heap_info *hi)
351 struct heap_cread_info hci;
352 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
354 hci.key = (char *) xmalloc (KEY_SIZE);
357 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
359 isamc_i->clientData = &hci;
360 isamc_i->read_item = heap_cread_item;
364 char this_name[INP_NAME_MAX];
365 ISAMC_P isamc_p, isamc_p2;
368 strcpy (this_name, hci.cur_name);
369 assert (hci.cur_name[1]);
371 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
373 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
374 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
378 if (!dict_delete (hi->dict, this_name))
384 if (isamc_p2 != isamc_p)
385 dict_insert (hi->dict, this_name,
386 sizeof(ISAMC_P), &isamc_p2);
391 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
393 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
400 int heap_inps (struct heap_info *hi)
402 struct heap_cread_info hci;
403 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
405 hci.key = (char *) xmalloc (KEY_SIZE);
408 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
410 isams_i->clientData = &hci;
411 isams_i->read_item = heap_cread_item;
415 char this_name[INP_NAME_MAX];
419 strcpy (this_name, hci.cur_name);
420 assert (hci.cur_name[1]);
422 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
424 isams_p = isams_merge (hi->isams, isams_i);
426 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
435 int heap_inph (struct heap_info *hi)
437 struct heap_cread_info hci;
438 ISAMH_I isamh_i = (ISAMH_I) xmalloc (sizeof(*isamh_i));
440 hci.key = (char *) xmalloc (KEY_SIZE);
443 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
445 isamh_i->clientData = &hci;
446 isamh_i->read_item = heap_cread_item;
450 char this_name[INP_NAME_MAX];
451 ISAMH_P isamh_p, isamh_p2;
454 strcpy (this_name, hci.cur_name);
455 assert (hci.cur_name[1]);
457 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
459 memcpy (&isamh_p, dict_info+1, sizeof(ISAMH_P));
460 isamh_p2 = isamh_append (hi->isamh, isamh_p, isamh_i);
464 if (!dict_delete (hi->dict, this_name))
470 if (isamh_p2 != isamh_p)
471 dict_insert (hi->dict, this_name,
472 sizeof(ISAMH_P), &isamh_p2);
477 isamh_p = isamh_append (hi->isamh, 0, isamh_i);
479 dict_insert (hi->dict, this_name, sizeof(ISAMH_P), &isamh_p);
486 int heap_inpd (struct heap_info *hi)
488 struct heap_cread_info hci;
489 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
491 hci.key = (char *) xmalloc (KEY_SIZE);
494 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
496 isamd_i->clientData = &hci;
497 isamd_i->read_item = heap_cread_item;
501 char this_name[INP_NAME_MAX];
502 ISAMD_P isamd_p, isamd_p2;
505 strcpy (this_name, hci.cur_name);
506 assert (hci.cur_name[1]);
508 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
510 memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
511 isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
515 if (!dict_delete (hi->dict, this_name))
521 if (isamd_p2 != isamd_p)
522 dict_insert (hi->dict, this_name,
523 sizeof(ISAMD_P), &isamd_p2);
528 isamd_p = isamd_append (hi->isamd, 0, isamd_i);
530 dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
540 int heap_inp (struct heap_info *hi)
543 char next_name[INP_NAME_MAX];
544 char cur_name[INP_NAME_MAX];
545 int key_buf_size = INP_BUF_START;
551 next_key = (char *) xmalloc (KEY_SIZE);
552 key_buf = (char *) xmalloc (key_buf_size);
553 more = heap_read_one (hi, cur_name, key_buf);
554 while (more) /* EOF ? */
557 key_buf_ptr = KEY_SIZE;
560 if (!(more = heap_read_one (hi, next_name, next_key)))
562 if (*next_name && strcmp (next_name, cur_name))
564 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
565 key_buf_ptr += KEY_SIZE;
566 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
569 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
570 memcpy (new_key_buf, key_buf, key_buf_size);
571 key_buf_size += INP_BUF_ADD;
573 key_buf = new_key_buf;
577 nmemb = key_buf_ptr / KEY_SIZE;
578 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
579 if ((info = dict_lookup (hi->dict, cur_name)))
581 ISAM_P isam_p, isam_p2;
582 memcpy (&isam_p, info+1, sizeof(ISAM_P));
583 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
587 if (!dict_delete (hi->dict, cur_name))
593 if (isam_p2 != isam_p)
594 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
601 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
602 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
604 memcpy (key_buf, next_key, KEY_SIZE);
605 strcpy (cur_name, next_name);
610 struct progressInfo {
617 void progressFunc (struct key_file *keyp, void *info)
619 struct progressInfo *p = (struct progressInfo *) info;
620 time_t now, remaining;
622 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
626 if (now >= p->lastTime+10)
629 remaining = (time_t) ((now - p->startTime)*
630 ((double) p->totalBytes/p->totalOffset - 1.0));
631 if (remaining <= 130)
632 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
633 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
635 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
636 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
638 p->totalOffset += keyp->buf_size;
645 void key_input (BFiles bfs, int nkeys, int cache)
654 struct key_file **kf;
657 struct heap_info *hi;
658 struct progressInfo progressInfo;
666 getFnameTmp (fname, nkeys+1);
667 if (access (fname, R_OK) == -1)
674 dict = dict_open (bfs, FNAME_DICT, cache, 1, 0);
677 logf (LOG_FATAL, "dict_open fail");
680 if (res_get_match (common_resource, "isam", "s", NULL))
682 struct ISAMS_M_s isams_m;
683 isams = isams_open (bfs, FNAME_ISAMS, 1,
684 key_isams_m (common_resource, &isams_m));
687 logf (LOG_FATAL, "isams_open fail");
691 else if (res_get_match (common_resource, "isam", "i", NULL))
693 isam = is_open (bfs, FNAME_ISAM, key_compare, 1,
694 sizeof(struct it_key), common_resource);
697 logf (LOG_FATAL, "is_open fail");
701 else if (res_get_match (common_resource, "isam", "h", NULL))
703 isamh = isamh_open (bfs, FNAME_ISAMH, 1,
704 key_isamh_m (common_resource));
707 logf (LOG_FATAL, "isamh_open fail");
711 else if (res_get_match (common_resource, "isam", "d", NULL))
713 struct ISAMD_M_s isamd_m;
714 isamd = isamd_open (bfs, FNAME_ISAMD, 1,
715 key_isamd_m (common_resource,&isamd_m));
718 logf (LOG_FATAL, "isamd_open fail");
724 struct ISAMC_M_s isamc_m;
725 isamc = isc_open (bfs, FNAME_ISAMC, 1,
726 key_isamc_m (common_resource, &isamc_m));
729 logf (LOG_FATAL, "isc_open fail");
733 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
734 progressInfo.totalBytes = 0;
735 progressInfo.totalOffset = 0;
736 time (&progressInfo.startTime);
737 time (&progressInfo.lastTime);
738 for (i = 1; i<=nkeys; i++)
740 kf[i] = key_file_init (i, 32768);
741 kf[i]->readHandler = progressFunc;
742 kf[i]->readInfo = &progressInfo;
743 progressInfo.totalBytes += kf[i]->length;
744 progressInfo.totalOffset += kf[i]->buf_size;
746 hi = key_heap_init (nkeys, key_qsort_compare);
754 for (i = 1; i<=nkeys; i++)
755 if ((r = key_file_read (kf[i], rbuf)))
756 key_heap_insert (hi, rbuf, r, kf[i]);
780 for (i = 1; i<=nkeys; i++)
782 getFnameTmp (rbuf, i);
785 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
786 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
787 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
788 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
789 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
791 /* xmalloc_trav("unfreed"); while hunting leaks */
798 * Revision 1.40 1999-09-08 12:12:39 adam
799 * Removed log message.
801 * Revision 1.39 1999/08/18 10:39:20 heikki
802 * Added a comment on memory leaks
804 * Revision 1.38 1999/08/18 08:38:04 heikki
805 * Memory leak hunting
807 * Revision 1.37 1999/07/14 13:21:34 heikki
808 * Added isam-d files. Compiles (almost) clean. Doesn't work at all
810 * Revision 1.36 1999/07/14 10:59:26 adam
811 * Changed functions isc_getmethod, isams_getmethod.
812 * Improved fatal error handling (such as missing EXPLAIN schema).
814 * Revision 1.35 1999/06/30 15:07:23 heikki
817 * Revision 1.34 1999/05/26 07:49:13 adam
820 * Revision 1.33 1999/05/15 14:36:38 adam
821 * Updated dictionary. Implemented "compression" of dictionary.
823 * Revision 1.32 1999/05/12 13:08:06 adam
824 * First version of ISAMS.
826 * Revision 1.31 1999/02/02 14:50:56 adam
827 * Updated WIN32 code specific sections. Changed header.
829 * Revision 1.30 1998/10/28 10:53:57 adam
830 * Added type cast to prevent warning.
832 * Revision 1.29 1998/06/11 15:41:39 adam
835 * Revision 1.28 1998/03/05 08:45:12 adam
836 * New result set model and modular ranking system. Moved towards
837 * descent server API. System information stored as "SGML" records.
839 * Revision 1.27 1998/02/17 10:32:52 adam
840 * Fixed bug: binary files weren't opened with flag b on NT.
842 * Revision 1.26 1998/01/29 13:39:13 adam
843 * Compress ISAM is default.
845 * Revision 1.25 1997/09/17 12:19:14 adam
846 * Zebra version corresponds to YAZ version 1.4.
847 * Changed Zebra server so that it doesn't depend on global common_resource.
849 * Revision 1.24 1997/09/09 13:38:07 adam
850 * Partial port to WIN95/NT.
852 * Revision 1.23 1997/09/04 13:57:39 adam
853 * Added O_BINARY for open calls.
855 * Revision 1.22 1997/02/12 20:39:45 adam
856 * Implemented options -f <n> that limits the log to the first <n>
858 * Changed some log messages also.
860 * Revision 1.21 1996/11/08 11:10:23 adam
861 * Buffers used during file match got bigger.
862 * Compressed ISAM support everywhere.
863 * Bug fixes regarding masking characters in queries.
864 * Redesigned Regexp-2 queries.
866 * Revision 1.20 1996/11/01 08:58:41 adam
867 * Interface to isamc system now includes update and delete.
869 * Revision 1.19 1996/10/29 14:09:46 adam
870 * Use of cisam system - enabled if setting isamc is 1.
872 * Revision 1.18 1996/06/04 10:18:59 adam
873 * Minor changes - removed include of ctype.h.
875 * Revision 1.17 1996/05/14 15:47:07 adam
876 * Cleanup of various buffer size entities.
878 * Revision 1.16 1996/04/09 10:05:20 adam
879 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
881 * Revision 1.15 1996/03/21 14:50:09 adam
882 * File update uses modify-time instead of change-time.
884 * Revision 1.14 1996/02/07 14:06:37 adam
885 * Better progress report during register merge.
886 * New command: clean - removes temporary shadow files.
888 * Revision 1.13 1996/02/05 12:30:00 adam
889 * Logging reduced a bit.
890 * The remaining running time is estimated during register merge.
892 * Revision 1.12 1995/12/06 17:49:19 adam
893 * Uses dict_delete now.
895 * Revision 1.11 1995/12/06 16:06:43 adam
896 * Better diagnostics. Work on 'real' dictionary deletion.
898 * Revision 1.10 1995/12/06 12:41:22 adam
899 * New command 'stat' for the index program.
900 * Filenames can be read from stdin by specifying '-'.
901 * Bug fix/enhancement of the transformation from terms to regular
902 * expressons in the search engine.
904 * Revision 1.9 1995/10/10 12:24:39 adam
905 * Temporary sort files are compressed.
907 * Revision 1.8 1995/10/04 16:57:19 adam
908 * Key input and merge sort in one pass.
910 * Revision 1.7 1995/10/02 15:18:52 adam
911 * New member in recRetrieveCtrl: diagnostic.
913 * Revision 1.6 1995/09/29 15:51:56 adam
914 * First work on multi-way read.
916 * Revision 1.5 1995/09/29 14:01:43 adam
919 * Revision 1.4 1995/09/28 14:22:57 adam
920 * Sort uses smaller temporary files.
922 * Revision 1.3 1995/09/06 16:11:17 adam
923 * Option: only one word key per file.
925 * Revision 1.2 1995/09/04 12:33:42 adam
926 * Various cleanup. YAZ util used instead.
928 * Revision 1.1 1995/09/04 09:10:37 adam
929 * More work on index add/del/update.
930 * Merge sort implemented.
931 * Initial work on z39 server.