cc464c299d7588a885194047e661804f899fadad
[idzebra-moved-to-github.git] / index / kinput.c
1 /* $Id: kinput.c,v 1.65 2005-04-13 13:03:47 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         ISAM_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(ISAM_P));
558             isamc_p2 = isamc_p;
559             isamc_merge (hi->reg->isamc, &isamc_p2, isamc_i);
560             if (!isamc_p2)
561             {
562                 hi->no_deletions++;
563                 if (!dict_delete (hi->reg->dict, this_name))
564                     abort();
565             }
566             else 
567             {
568                 hi->no_updates++;
569                 if (isamc_p2 != isamc_p)
570                     dict_insert (hi->reg->dict, this_name,
571                                  sizeof(ISAM_P), &isamc_p2);
572             }
573         } 
574         else
575         {
576             isamc_p = 0;
577             isamc_merge (hi->reg->isamc, &isamc_p, isamc_i);
578             hi->no_insertions++;
579             dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isamc_p);
580         }
581     }
582     xfree (isamc_i);
583     xfree (hci.key);
584     xfree (hci.key_1);
585     xfree (hci.key_2);
586     return 0;
587
588
589 #if 0
590 /* for debugging only */
591 static void print_dict_item (ZebraMaps zm, const char *s)
592 {
593     int reg_type = s[1];
594     char keybuf[IT_MAX_WORD+1];
595     char *to = keybuf;
596     const char *from = s + 2;
597
598     while (*from)
599     {
600         const char *res = zebra_maps_output (zm, reg_type, &from);
601         if (!res)
602             *to++ = *from++;
603         else
604             while (*res)
605                 *to++ = *res++;
606     }
607     *to = '\0';
608     yaz_log (YLOG_LOG, "%s", keybuf);
609 }
610 #endif
611
612 int heap_inpb (struct heap_info *hi)
613 {
614     struct heap_cread_info hci;
615     ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
616
617     hci.key = (char *) xmalloc (KEY_SIZE);
618     hci.key_1 = (char *) xmalloc (KEY_SIZE);
619     hci.key_2 = (char *) xmalloc (KEY_SIZE);
620     hci.ret = -1;
621     hci.first_in_list = 1;
622     hci.hi = hi;
623     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
624
625     isamc_i->clientData = &hci;
626     isamc_i->read_item = heap_cread_item2;
627
628     while (hci.more)
629     {
630         char this_name[INP_NAME_MAX];
631         ISAM_P isamc_p, isamc_p2;
632         char *dict_info;
633
634         strcpy (this_name, hci.cur_name);
635         assert (hci.cur_name[1]);
636         hi->no_diffs++;
637
638 #if 0
639         print_dict_item (hi->reg->zebra_maps, hci.cur_name);
640 #endif
641         if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
642         {
643             memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
644             isamc_p2 = isamc_p;
645             isamb_merge (hi->reg->isamb, &isamc_p2, isamc_i);
646             if (!isamc_p2)
647             {
648                 hi->no_deletions++;
649                 if (!dict_delete (hi->reg->dict, this_name))
650                     abort();
651             }
652             else 
653             {
654                 hi->no_updates++;
655                 if (isamc_p2 != isamc_p)
656                     dict_insert (hi->reg->dict, this_name,
657                                  sizeof(ISAM_P), &isamc_p2);
658             }
659         } 
660         else
661         {
662             isamc_p = 0;
663             isamb_merge (hi->reg->isamb, &isamc_p, isamc_i);
664             hi->no_insertions++;
665             dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isamc_p);
666         }
667     }
668     xfree (isamc_i);
669     xfree (hci.key);
670     xfree (hci.key_1);
671     xfree (hci.key_2);
672     return 0;
673
674
675 int heap_inps (struct heap_info *hi)
676 {
677     struct heap_cread_info hci;
678     ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
679
680     hci.key = (char *) xmalloc (KEY_SIZE);
681     hci.key_1 = (char *) xmalloc (KEY_SIZE);
682     hci.key_2 = (char *) xmalloc (KEY_SIZE);
683     hci.first_in_list = 1;
684     hci.ret = -1;
685     hci.hi = hi;
686     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
687
688     isams_i->clientData = &hci;
689     isams_i->read_item = heap_cread_item;
690
691     while (hci.more)
692     {
693         char this_name[INP_NAME_MAX];
694         ISAM_P isams_p;
695         char *dict_info;
696
697         strcpy (this_name, hci.cur_name);
698         assert (hci.cur_name[1]);
699         hi->no_diffs++;
700         if (!(dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
701         {
702             isams_p = isams_merge (hi->reg->isams, isams_i);
703             hi->no_insertions++;
704             dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
705         }
706         else
707         {
708             yaz_log (YLOG_FATAL, "isams doesn't support this kind of update");
709             break;
710         }
711     }
712     xfree (isams_i);
713     return 0;
714
715
716 struct progressInfo {
717     time_t   startTime;
718     time_t   lastTime;
719     off_t    totalBytes;
720     off_t    totalOffset;
721 };
722
723 void progressFunc (struct key_file *keyp, void *info)
724 {
725     struct progressInfo *p = (struct progressInfo *) info;
726     time_t now, remaining;
727
728     if (keyp->buf_size <= 0 || p->totalBytes <= 0)
729         return ;
730     time (&now);
731
732     if (now >= p->lastTime+10)
733     {
734         p->lastTime = now;
735         remaining = (time_t) ((now - p->startTime)*
736             ((double) p->totalBytes/p->totalOffset - 1.0));
737         if (remaining <= 130)
738             yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
739                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
740         else
741             yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
742                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
743     }
744     p->totalOffset += keyp->buf_size;
745 }
746
747 #ifndef R_OK
748 #define R_OK 4
749 #endif
750
751 void zebra_index_merge (ZebraHandle zh)
752 {
753     struct key_file **kf = 0;
754     char rbuf[1024];
755     int i, r;
756     struct heap_info *hi;
757     struct progressInfo progressInfo;
758     int nkeys = zh->reg->key_file_no;
759     int usefile; 
760     
761     yaz_log (YLOG_DEBUG, " index_merge called with nk=%d b=%p", 
762                     nkeys, zh->reg->key_buf);
763     if ( (nkeys==0) && (zh->reg->key_buf==0) )
764         return; /* nothing to merge - probably flush after end-trans */
765     
766     usefile = (nkeys!=0); 
767
768     if (usefile)
769     {
770         if (nkeys < 0)
771         {
772             char fname[1024];
773             nkeys = 0;
774             while (1)
775             {
776                 extract_get_fname_tmp  (zh, fname, nkeys+1);
777                 if (access (fname, R_OK) == -1)
778                         break;
779                 nkeys++;
780             }
781             if (!nkeys)
782                 return ;
783         }
784         kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
785         progressInfo.totalBytes = 0;
786         progressInfo.totalOffset = 0;
787         time (&progressInfo.startTime);
788         time (&progressInfo.lastTime);
789         for (i = 1; i<=nkeys; i++)
790         {
791             kf[i] = key_file_init (i, 8192, zh->res);
792             kf[i]->readHandler = progressFunc;
793             kf[i]->readInfo = &progressInfo;
794             progressInfo.totalBytes += kf[i]->length;
795             progressInfo.totalOffset += kf[i]->buf_size;
796         }
797         hi = key_heap_init (nkeys, key_qsort_compare);
798         hi->reg = zh->reg;
799         
800         for (i = 1; i<=nkeys; i++)
801             if ((r = key_file_read (kf[i], rbuf)))
802                 key_heap_insert (hi, rbuf, r, kf[i]);
803     }  /* use file */
804     else 
805     { /* do not use file, read straight from buffer */
806         hi = key_heap_init_buff (zh,key_qsort_compare);
807         hi->reg = zh->reg;
808     }
809     if (zh->reg->isams)
810         heap_inps (hi);
811     if (zh->reg->isamc)
812         heap_inpc (hi);
813     if (zh->reg->isamb)
814         heap_inpb (hi);
815         
816     if (usefile)
817     {
818         for (i = 1; i<=nkeys; i++)
819         {
820             extract_get_fname_tmp  (zh, rbuf, i);
821             unlink (rbuf);
822         }
823         for (i = 1; i<=nkeys; i++)
824             key_file_destroy (kf[i]);
825         xfree (kf);
826     }
827     if (hi->no_iterations)
828     { /* do not log if nothing happened */
829         yaz_log (YLOG_LOG, "Iterations . . .%7d", hi->no_iterations);
830         yaz_log (YLOG_LOG, "Distinct words .%7d", hi->no_diffs);
831         yaz_log (YLOG_LOG, "Updates. . . . .%7d", hi->no_updates);
832         yaz_log (YLOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
833         yaz_log (YLOG_LOG, "Insertions . . .%7d", hi->no_insertions);
834     }
835     zh->reg->key_file_no = 0;
836
837     key_heap_destroy (hi, nkeys);
838 }