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