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