9b7b8b2721c5f6fade873af65624a5907278eb6a
[idzebra-moved-to-github.git] / index / kinput.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
5  *
6  * $Id: kinput.c,v 1.51 2002-07-12 18:12:22 heikki Exp $
7  *
8  * Bugs
9  *  - Allocates a lot of memory for the merge process, but never releases it.
10  *    Doesn't matter, as the program terminates soon after.  
11  
12  */
13  
14 #include <fcntl.h>
15 #ifdef WIN32
16 #include <io.h>
17 #else
18 #include <unistd.h>
19 #endif
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <assert.h>
24
25 #include "index.h"
26
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
31
32
33 struct key_file {
34     int   no;            /* file no */
35     off_t offset;        /* file offset */
36     unsigned char *buf;  /* buffer block */
37     size_t buf_size;     /* number of read bytes in block */
38     size_t chunk;        /* number of bytes allocated */
39     size_t buf_ptr;      /* current position in buffer */
40     char *prev_name;     /* last word read */
41     int   sysno;         /* last sysno */
42     int   seqno;         /* last seqno */
43     off_t length;        /* length of file */
44                          /* handler invoked in each read */
45     void (*readHandler)(struct key_file *keyp, void *rinfo);
46     void *readInfo;
47     Res res;
48 };
49
50 void getFnameTmp (Res res, char *fname, int no)
51 {
52     const char *pre;
53     
54     pre = res_get_def (res, "keyTmpDir", ".");
55     sprintf (fname, "%s/key%d.tmp", pre, no);
56 }
57
58 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
59 {
60     const char *pre;
61     
62     pre = res_get_def (zh->res, "keyTmpDir", ".");
63     sprintf (fname, "%s/key%d.tmp", pre, no);
64 }
65
66 void key_file_chunk_read (struct key_file *f)
67 {
68     int nr = 0, r = 0, fd;
69     char fname[1024];
70     getFnameTmp (f->res, fname, f->no);
71     fd = open (fname, O_BINARY|O_RDONLY);
72
73     f->buf_ptr = 0;
74     f->buf_size = 0;
75     if (fd == -1)
76     {
77         logf (LOG_WARN|LOG_ERRNO, "cannot open %s", fname);
78         return ;
79     }
80     if (!f->length)
81     {
82         if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
83         {
84             logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
85             close (fd);
86             return ;
87         }
88     }
89     if (lseek (fd, f->offset, SEEK_SET) == -1)
90     {
91         logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
92         close(fd);
93         return ;
94     }
95     while (f->chunk - nr > 0)
96     {
97         r = read (fd, f->buf + nr, f->chunk - nr);
98         if (r <= 0)
99             break;
100         nr += r;
101     }
102     if (r == -1)
103     {
104         logf (LOG_WARN|LOG_ERRNO, "read of %s", fname);
105         close (fd);
106         return;
107     }
108     f->buf_size = nr;
109     if (f->readHandler)
110         (*f->readHandler)(f, f->readInfo);
111     close (fd);
112 }
113
114 void key_file_destroy (struct key_file *f)
115 {
116     xfree (f->buf);
117     xfree (f->prev_name);
118     xfree (f);
119 }
120
121 struct key_file *key_file_init (int no, int chunk, Res res)
122 {
123     struct key_file *f;
124
125     f = (struct key_file *) xmalloc (sizeof(*f));
126     f->res = res;
127     f->sysno = 0;
128     f->seqno = 0;
129     f->no = no;
130     f->chunk = chunk;
131     f->offset = 0;
132     f->length = 0;
133     f->readHandler = NULL;
134     f->buf = (unsigned char *) xmalloc (f->chunk);
135     f->prev_name = (char *) xmalloc (INP_NAME_MAX);
136     *f->prev_name = '\0';
137     key_file_chunk_read (f);
138     return f;
139 }
140
141 int key_file_getc (struct key_file *f)
142 {
143     if (f->buf_ptr < f->buf_size)
144         return f->buf[(f->buf_ptr)++];
145     if (f->buf_size < f->chunk)
146         return EOF;
147     f->offset += f->buf_size;
148     key_file_chunk_read (f);
149     if (f->buf_ptr < f->buf_size)
150         return f->buf[(f->buf_ptr)++];
151     else
152         return EOF;
153 }
154
155 int key_file_decode (struct key_file *f)
156 {
157     int c, d;
158
159     c = key_file_getc (f);
160     switch (c & 192) 
161     {
162     case 0:
163         d = c;
164         break;
165     case 64:
166         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
167         break;
168     case 128:
169         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
170         d = (d << 8) + (key_file_getc (f) & 0xff);
171         break;
172     case 192:
173         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
174         d = (d << 8) + (key_file_getc (f) & 0xff);
175         d = (d << 8) + (key_file_getc (f) & 0xff);
176         break;
177     }
178     return d;
179 }
180
181 int key_file_read (struct key_file *f, char *key)
182 {
183     int i, d, c;
184     struct it_key itkey;
185
186     c = key_file_getc (f);
187     if (c == 0)
188     {
189         strcpy (key, f->prev_name);
190         i = 1+strlen (key);
191     }
192     else if (c == EOF)
193         return 0;
194     else
195     {
196         i = 0;
197         key[i++] = c;
198         while ((key[i++] = key_file_getc (f)))
199             ;
200         strcpy (f->prev_name, key);
201         f->sysno = 0;
202     }
203     d = key_file_decode (f);
204     key[i++] = d & 1;
205     d = d >> 1;
206     itkey.sysno = d + f->sysno;
207     if (d) 
208     {
209         f->sysno = itkey.sysno;
210         f->seqno = 0;
211     }
212     d = key_file_decode (f);
213     itkey.seqno = d + f->seqno;
214     f->seqno = itkey.seqno;
215     memcpy (key + i, &itkey, sizeof(struct it_key));
216     return i + sizeof (struct it_key);
217 }
218
219 struct heap_info {
220     struct {
221         struct key_file **file;
222         char   **buf;
223     } info;
224     int    heapnum;
225     int    *ptr;
226     int    (*cmp)(const void *p1, const void *p2);
227     struct zebra_register *reg;
228
229     int no_diffs;
230     int no_updates;
231     int no_deletions;
232     int no_insertions;
233     int no_iterations;
234 };
235
236 struct heap_info *key_heap_init (int nkeys,
237                                  int (*cmp)(const void *p1, const void *p2))
238 {
239     struct heap_info *hi;
240     int i;
241
242     hi = (struct heap_info *) xmalloc (sizeof(*hi));
243     hi->info.file = (struct key_file **)
244         xmalloc (sizeof(*hi->info.file) * (1+nkeys));
245     hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
246     hi->heapnum = 0;
247     hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
248     hi->cmp = cmp;
249     for (i = 0; i<= nkeys; i++)
250     {
251         hi->ptr[i] = i;
252         hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
253     }
254     hi->no_diffs = 0;
255     hi->no_diffs = 0;
256     hi->no_updates = 0;
257     hi->no_deletions = 0;
258     hi->no_insertions = 0;
259     hi->no_iterations = 0;
260     return hi;
261 }
262
263 void key_heap_destroy (struct heap_info *hi, int nkeys)
264 {
265     int i;
266     yaz_log (LOG_LOG, "key_heap_destroy");
267     for (i = 0; i<=nkeys; i++)
268         xfree (hi->info.buf[i]);
269     
270     xfree (hi->info.buf);
271     xfree (hi->ptr);
272     xfree (hi->info.file);
273     xfree (hi);
274 }
275
276 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
277 {
278     int swap;
279
280     swap = hi->ptr[i1];
281     hi->ptr[i1] = hi->ptr[i2];
282     hi->ptr[i2] = swap;
283 }
284
285
286 static void key_heap_delete (struct heap_info *hi)
287 {
288     int cur = 1, child = 2;
289
290     assert (hi->heapnum > 0);
291
292     key_heap_swap (hi, 1, hi->heapnum);
293     hi->heapnum--;
294     while (child <= hi->heapnum) {
295         if (child < hi->heapnum &&
296             (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
297                        &hi->info.buf[hi->ptr[child+1]]) > 0)
298             child++;
299         if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
300                        &hi->info.buf[hi->ptr[child]]) > 0)
301         {            
302             key_heap_swap (hi, cur, child);
303             cur = child;
304             child = 2*cur;
305         }
306         else
307             break;
308     }
309 }
310
311 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
312                              struct key_file *kf)
313 {
314     int cur, parent;
315
316     cur = ++(hi->heapnum);
317     memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
318     hi->info.file[hi->ptr[cur]] = kf;
319
320     parent = cur/2;
321     while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
322                                 &hi->info.buf[hi->ptr[cur]]) > 0)
323     {
324         key_heap_swap (hi, cur, parent);
325         cur = parent;
326         parent = cur/2;
327     }
328 }
329
330 static int heap_read_one (struct heap_info *hi, char *name, char *key)
331 {
332     int n, r;
333     char rbuf[INP_NAME_MAX];
334     struct key_file *kf;
335
336     if (!hi->heapnum)
337         return 0;
338     n = hi->ptr[1];
339     strcpy (name, hi->info.buf[n]);
340     kf = hi->info.file[n];
341     r = strlen(name);
342     memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
343     key_heap_delete (hi);
344     if ((r = key_file_read (kf, rbuf)))
345         key_heap_insert (hi, rbuf, r, kf);
346     hi->no_iterations++;
347     return 1;
348 }
349
350 struct heap_cread_info {
351     char prev_name[INP_NAME_MAX];
352     char cur_name[INP_NAME_MAX];
353     char *key;
354     struct heap_info *hi;
355     int mode;
356     int more;
357 };
358       
359 int heap_cread_item (void *vp, char **dst, int *insertMode)
360 {
361     struct heap_cread_info *p = (struct heap_cread_info *) vp;
362     struct heap_info *hi = p->hi;
363
364     if (p->mode == 1)
365     {
366         *insertMode = p->key[0];
367         memcpy (*dst, p->key+1, sizeof(struct it_key));
368         (*dst) += sizeof(struct it_key);
369         p->mode = 2;
370         return 1;
371     }
372     strcpy (p->prev_name, p->cur_name);
373     if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
374         return 0;
375     if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
376     {
377         p->mode = 1;
378         return 0;
379     }
380     *insertMode = p->key[0];
381     memcpy (*dst, p->key+1, sizeof(struct it_key));
382     (*dst) += sizeof(struct it_key);
383     return 1;
384 }
385
386 int heap_inpc (struct heap_info *hi)
387 {
388     struct heap_cread_info hci;
389     ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
390
391     hci.key = (char *) xmalloc (KEY_SIZE);
392     hci.mode = 1;
393     hci.hi = hi;
394     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
395
396     isamc_i->clientData = &hci;
397     isamc_i->read_item = heap_cread_item;
398
399     while (hci.more)
400     {
401         char this_name[INP_NAME_MAX];
402         ISAMC_P isamc_p, isamc_p2;
403         char *dict_info;
404
405         strcpy (this_name, hci.cur_name);
406         assert (hci.cur_name[1]);
407         hi->no_diffs++;
408         if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
409         {
410             memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
411             isamc_p2 = isc_merge (hi->reg->isamc, isamc_p, isamc_i);
412             if (!isamc_p2)
413             {
414                 hi->no_deletions++;
415                 if (!dict_delete (hi->reg->dict, this_name))
416                     abort();
417             }
418             else 
419             {
420                 hi->no_updates++;
421                 if (isamc_p2 != isamc_p)
422                     dict_insert (hi->reg->dict, this_name,
423                                  sizeof(ISAMC_P), &isamc_p2);
424             }
425         } 
426         else
427         {
428             isamc_p = isc_merge (hi->reg->isamc, 0, isamc_i);
429             hi->no_insertions++;
430             dict_insert (hi->reg->dict, this_name, sizeof(ISAMC_P), &isamc_p);
431         }
432     }
433     xfree (isamc_i);
434     xfree (hci.key);
435     return 0;
436
437
438 /* for debugging only */
439 static void print_dict_item (ZebraMaps zm, const char *s)
440 {
441     int reg_type = s[1];
442     char keybuf[IT_MAX_WORD+1];
443     char *to = keybuf;
444     const char *from = s + 2;
445
446     while (*from)
447     {
448         const char *res = zebra_maps_output (zm, reg_type, &from);
449         if (!res)
450             *to++ = *from++;
451         else
452             while (*res)
453                 *to++ = *res++;
454     }
455     *to = '\0';
456     yaz_log (LOG_LOG, "%s", keybuf);
457 }
458
459 int heap_inpb (struct heap_info *hi)
460 {
461     struct heap_cread_info hci;
462     ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
463
464     hci.key = (char *) xmalloc (KEY_SIZE);
465     hci.mode = 1;
466     hci.hi = hi;
467     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
468
469     isamc_i->clientData = &hci;
470     isamc_i->read_item = heap_cread_item;
471
472     while (hci.more)
473     {
474         char this_name[INP_NAME_MAX];
475         ISAMC_P isamc_p, isamc_p2;
476         char *dict_info;
477
478         strcpy (this_name, hci.cur_name);
479         assert (hci.cur_name[1]);
480         hi->no_diffs++;
481
482 #if 0
483         print_dict_item (hi->reg->zebra_maps, hci.cur_name);
484 #endif
485         if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
486         {
487             memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
488             isamc_p2 = isamb_merge (hi->reg->isamb, isamc_p, isamc_i);
489             if (!isamc_p2)
490             {
491                 hi->no_deletions++;
492                 if (!dict_delete (hi->reg->dict, this_name))
493                     abort();
494             }
495             else 
496             {
497                 hi->no_updates++;
498                 if (isamc_p2 != isamc_p)
499                     dict_insert (hi->reg->dict, this_name,
500                                  sizeof(ISAMC_P), &isamc_p2);
501             }
502         } 
503         else
504         {
505             isamc_p = isamb_merge (hi->reg->isamb, 0, isamc_i);
506             hi->no_insertions++;
507             dict_insert (hi->reg->dict, this_name, sizeof(ISAMC_P), &isamc_p);
508         }
509     }
510     xfree (isamc_i);
511     xfree (hci.key);
512     return 0;
513
514
515 int heap_inpd (struct heap_info *hi)
516 {
517     struct heap_cread_info hci;
518     ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
519
520     hci.key = (char *) xmalloc (KEY_SIZE);
521     hci.mode = 1;
522     hci.hi = hi;
523     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
524
525     isamd_i->clientData = &hci;
526     isamd_i->read_item = heap_cread_item;
527
528     while (hci.more)
529     {
530         char this_name[INP_NAME_MAX];
531         char *dict_info;
532         char dictentry[ISAMD_MAX_DICT_LEN+1];
533         char dictlen;
534
535         strcpy (this_name, hci.cur_name);
536         
537         /* print_dict_item (hi->reg->zebra_maps, hci.cur_name); */
538         /*!*/ /* FIXME: depend on isamd-debug */
539
540         assert (hci.cur_name[1]);
541         hi->no_diffs++;
542         if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
543         {
544             dictlen=dict_info[0];
545             memcpy (dictentry, dict_info+1, dictlen );
546 #ifdef SKIPTHIS
547             logf(LOG_LOG,"dictentry before. len=%d: %d %d %d %d %d %d %d %d %d",
548                dictlen,dictentry[0], dictentry[1], dictentry[2],
549                        dictentry[3], dictentry[4], dictentry[5],
550                        dictentry[6], dictentry[7], dictentry[8]); /*!*/
551 #endif
552             dictlen= isamd_append(hi->reg->isamd, dictentry, dictlen, isamd_i);
553              /* logf dictentry after */
554             if (dictlen)
555             {
556                 hi->no_updates++;
557                 if ( (dictlen!=dict_info[0]) ||
558                      (0!=memcmp(dictentry, dict_info+1, dictlen)) )
559                 {
560                     dict_insert(hi->reg->dict, this_name,
561                                 dictlen,dictentry);
562                 }
563             }
564             else
565             {
566                 hi->no_deletions++;
567                 if (!dict_delete (hi->reg->dict, this_name)) 
568                 {
569                     logf (LOG_FATAL, "dict_delete failed");
570                     abort();
571                 }
572             }
573         } 
574         else
575         {
576             dictlen=0;
577             memset (dictentry, '\0', ISAMD_MAX_DICT_LEN);
578             dictlen= isamd_append(hi->reg->isamd, dictentry, dictlen, isamd_i);
579              /* logf dictentry first */
580             hi->no_insertions++;
581             if (dictlen)
582                 dict_insert(hi->reg->dict, this_name,
583                                 dictlen,dictentry);
584         }
585     }
586     xfree (isamd_i);
587     return 0;
588
589
590 int heap_inp (struct heap_info *hi)
591 {
592     char *info;
593     char next_name[INP_NAME_MAX];
594     char cur_name[INP_NAME_MAX];
595     int key_buf_size = INP_BUF_START;
596     int key_buf_ptr;
597     char *next_key;
598     char *key_buf;
599     int more;
600     
601     next_key = (char *) xmalloc (KEY_SIZE);
602     key_buf = (char *) xmalloc (key_buf_size);
603     more = heap_read_one (hi, cur_name, key_buf);
604     while (more)                   /* EOF ? */
605     {
606         int nmemb;
607         key_buf_ptr = KEY_SIZE;
608         while (1)
609         {
610             if (!(more = heap_read_one (hi, next_name, next_key)))
611                 break;
612             if (*next_name && strcmp (next_name, cur_name))
613                 break;
614             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
615             key_buf_ptr += KEY_SIZE;
616             if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
617             {
618                 char *new_key_buf;
619                 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
620                 memcpy (new_key_buf, key_buf, key_buf_size);
621                 key_buf_size += INP_BUF_ADD;
622                 xfree (key_buf);
623                 key_buf = new_key_buf;
624             }
625         }
626         hi->no_diffs++;
627         nmemb = key_buf_ptr / KEY_SIZE;
628         assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
629         if ((info = dict_lookup (hi->reg->dict, cur_name)))
630         {
631             ISAM_P isam_p, isam_p2;
632             memcpy (&isam_p, info+1, sizeof(ISAM_P));
633             isam_p2 = is_merge (hi->reg->isam, isam_p, nmemb, key_buf);
634             if (!isam_p2)
635             {
636                 hi->no_deletions++;
637                 if (!dict_delete (hi->reg->dict, cur_name))
638                     abort ();
639             }
640             else 
641             {
642                 hi->no_updates++;
643                 if (isam_p2 != isam_p)
644                     dict_insert (hi->reg->dict, cur_name,
645                                  sizeof(ISAM_P), &isam_p2);
646             }
647         }
648         else
649         {
650             ISAM_P isam_p;
651             hi->no_insertions++;
652             isam_p = is_merge (hi->reg->isam, 0, nmemb, key_buf);
653             dict_insert (hi->reg->dict, cur_name, sizeof(ISAM_P), &isam_p);
654         }
655         memcpy (key_buf, next_key, KEY_SIZE);
656         strcpy (cur_name, next_name);
657     }
658     return 0;
659 }
660
661 int heap_inps (struct heap_info *hi)
662 {
663     struct heap_cread_info hci;
664     ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
665
666     hci.key = (char *) xmalloc (KEY_SIZE);
667     hci.mode = 1;
668     hci.hi = hi;
669     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
670
671     isams_i->clientData = &hci;
672     isams_i->read_item = heap_cread_item;
673
674     while (hci.more)
675     {
676         char this_name[INP_NAME_MAX];
677         ISAMS_P isams_p;
678         char *dict_info;
679
680         strcpy (this_name, hci.cur_name);
681         assert (hci.cur_name[1]);
682         hi->no_diffs++;
683         if (!(dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
684         {
685             isams_p = isams_merge (hi->reg->isams, isams_i);
686             hi->no_insertions++;
687             dict_insert (hi->reg->dict, this_name, sizeof(ISAMS_P), &isams_p);
688         }
689         else
690         {
691             logf (LOG_FATAL, "isams doesn't support this kind of update");
692             break;
693         }
694     }
695     xfree (isams_i);
696     return 0;
697
698
699 struct progressInfo {
700     time_t   startTime;
701     time_t   lastTime;
702     off_t    totalBytes;
703     off_t    totalOffset;
704 };
705
706 void progressFunc (struct key_file *keyp, void *info)
707 {
708     struct progressInfo *p = (struct progressInfo *) info;
709     time_t now, remaining;
710
711     if (keyp->buf_size <= 0 || p->totalBytes <= 0)
712         return ;
713     time (&now);
714
715     if (now >= p->lastTime+10)
716     {
717         p->lastTime = now;
718         remaining = (time_t) ((now - p->startTime)*
719             ((double) p->totalBytes/p->totalOffset - 1.0));
720         if (remaining <= 130)
721             logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
722                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
723         else
724             logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
725                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
726     }
727     p->totalOffset += keyp->buf_size;
728 }
729
730 #ifndef R_OK
731 #define R_OK 4
732 #endif
733
734 void zebra_index_merge (ZebraHandle zh)
735 {
736     struct key_file **kf;
737     char rbuf[1024];
738     int i, r;
739     struct heap_info *hi;
740     struct progressInfo progressInfo;
741     int nkeys = zh->reg->key_file_no;
742     
743     if (nkeys < 0)
744     {
745         char fname[1024];
746         nkeys = 0;
747         while (1)
748         {
749             extract_get_fname_tmp  (zh, fname, nkeys+1);
750             if (access (fname, R_OK) == -1)
751                 break;
752             nkeys++;
753         }
754         if (!nkeys)
755             return ;
756     }
757     kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
758     progressInfo.totalBytes = 0;
759     progressInfo.totalOffset = 0;
760     time (&progressInfo.startTime);
761     time (&progressInfo.lastTime);
762     for (i = 1; i<=nkeys; i++)
763     {
764         kf[i] = key_file_init (i, 8192, zh->res);
765         kf[i]->readHandler = progressFunc;
766         kf[i]->readInfo = &progressInfo;
767         progressInfo.totalBytes += kf[i]->length;
768         progressInfo.totalOffset += kf[i]->buf_size;
769     }
770     hi = key_heap_init (nkeys, key_qsort_compare);
771     hi->reg = zh->reg;
772     
773     for (i = 1; i<=nkeys; i++)
774         if ((r = key_file_read (kf[i], rbuf)))
775             key_heap_insert (hi, rbuf, r, kf[i]);
776     if (zh->reg->isams)
777         heap_inps (hi);
778     if (zh->reg->isamc)
779         heap_inpc (hi);
780     if (zh->reg->isam)
781         heap_inp (hi);
782     if (zh->reg->isamd)
783         heap_inpd (hi);
784     if (zh->reg->isamb)
785         heap_inpb (hi);
786         
787     for (i = 1; i<=nkeys; i++)
788     {
789         extract_get_fname_tmp  (zh, rbuf, i);
790         unlink (rbuf);
791     }
792     logf (LOG_LOG, "Iterations . . .%7d", hi->no_iterations);
793     logf (LOG_LOG, "Distinct words .%7d", hi->no_diffs);
794     logf (LOG_LOG, "Updates. . . . .%7d", hi->no_updates);
795     logf (LOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
796     logf (LOG_LOG, "Insertions . . .%7d", hi->no_insertions);
797     zh->reg->key_file_no = 0;
798
799     key_heap_destroy (hi, nkeys);
800     for (i = 1; i<=nkeys; i++)
801         key_file_destroy (kf[i]);
802     xfree (kf);
803 }
804
805