Handle zh==NULL in print_dict_item
[idzebra-moved-to-github.git] / index / kinput.c
1 /* $Id: kinput.c,v 1.69 2005-11-10 11:25:13 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 #endif
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <assert.h>
34
35 #include "index.h"
36
37 #define KEY_SIZE (1+sizeof(struct it_key))
38 #define INP_NAME_MAX 768
39 #define INP_BUF_START 60000
40 #define INP_BUF_ADD  400000
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_LOW 0
398 #define PR_KEY_TOP 0
399
400 #if 1
401 static void pkey(const char *b, int mode)
402 {
403     key_logdump_txt(YLOG_LOG, b, mode ? "i" : "d");
404 }
405 #endif
406
407 #if 1
408 /* for debugging only */
409 static void print_dict_item(ZebraHandle zh, const char *s)
410 {
411     char dst[IT_MAX_WORD+1];
412     int ord;
413     int len = key_SU_decode(&ord, (const unsigned char *) s);
414     int index_type;
415     const char *db = 0;
416
417     if (!zh)
418         yaz_log(YLOG_LOG, "ord=%d", ord);
419     else
420     {
421         zebraExplain_lookup_ord (zh->reg->zei,
422                              ord, &index_type, &db, 0, 0);
423
424         zebra_term_untrans(zh, index_type, dst, s + len);
425
426         yaz_log(YLOG_LOG, "ord=%d term=%s", ord, dst);
427     }
428 }
429 #endif
430
431 struct heap_cread_info {
432     char prev_name[INP_NAME_MAX];
433     char cur_name[INP_NAME_MAX];
434     char *key;
435     char *key_1, *key_2;
436     int mode_1, mode_2;
437     int sz_1, sz_2;
438     struct heap_info *hi;
439     int first_in_list;
440     int more;
441     int ret;
442     int look_level;
443 };
444
445 static int heap_cread_item (void *vp, char **dst, int *insertMode);
446
447 int heap_cread_item2(void *vp, char **dst, int *insertMode)
448 {
449     struct heap_cread_info *p = (struct heap_cread_info *) vp;
450     int level = 0;
451
452     if (p->look_level)
453     {
454         if (p->look_level > 0)
455         {
456             *insertMode = 1;
457             p->look_level--;
458         }
459         else
460         {
461             *insertMode = 0;
462             p->look_level++;
463         }
464         memcpy (*dst, p->key_1, p->sz_1);
465 #if 1
466         yaz_log(YLOG_LOG, "DUP level=%d", p->look_level);
467         pkey(*dst, *insertMode);
468 #endif
469         (*dst) += p->sz_1;
470         return 1;
471     }
472     if (p->ret == 0)    /* lookahead was 0?. Return that in read next round */
473     {
474         p->ret = -1;
475         return 0;
476     }
477     else if (p->ret == -1) /* Must read new item ? */
478     {
479         char *dst_1 = p->key_1;
480         p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
481         p->sz_1 = dst_1 - p->key_1;
482     }
483     else
484     {        /* lookahead in 2 . Now in 1. */
485         p->sz_1 = p->sz_2;
486         p->mode_1 = p->mode_2;
487         memcpy (p->key_1, p->key_2, p->sz_2);
488     }
489     if (p->mode_1)
490         level = 1;     /* insert */
491     else
492         level = -1;    /* delete */
493     while(1)
494     {
495         char *dst_2 = p->key_2;
496         p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
497         if (!p->ret)
498         {
499             if (level)
500                 break;
501             p->ret = -1;
502             return 0;
503         }
504         p->sz_2 = dst_2 - p->key_2;
505
506         if (key_compare(p->key_1, p->key_2) == 0)
507         {
508             if (p->mode_2) /* adjust level according to deletes/inserts */
509                 level++;
510             else
511                 level--;
512         }
513         else
514         {
515             if (level)
516                 break;
517             /* all the same. new round .. */
518             p->sz_1 = p->sz_2;
519             p->mode_1 = p->mode_2;
520             memcpy (p->key_1, p->key_2, p->sz_1);
521             if (p->mode_1)
522                 level = 1;     /* insert */
523             else
524                 level = -1;    /* delete */
525         }
526     }
527     /* outcome is insert (1) or delete (0) depending on final level */
528     if (level > 0)
529     {
530         *insertMode = 1;
531         level--;
532     }
533     else
534     {
535         *insertMode = 0;
536         level++;
537     }
538     p->look_level = level;
539     memcpy (*dst, p->key_1, p->sz_1);
540 #if 0
541     pkey(*dst, *insertMode);
542 #endif
543     (*dst) += p->sz_1;
544     return 1;
545 }
546       
547 int heap_cread_item (void *vp, char **dst, int *insertMode)
548 {
549     struct heap_cread_info *p = (struct heap_cread_info *) vp;
550     struct heap_info *hi = p->hi;
551
552     if (p->first_in_list)
553     {
554         *insertMode = p->key[0];
555         memcpy (*dst, p->key+1, sizeof(struct it_key));
556 #if PR_KEY_LOW
557         pkey(*dst, *insertMode);
558 #endif
559         (*dst) += sizeof(struct it_key);
560         p->first_in_list = 0;
561         return 1;
562     }
563     strcpy (p->prev_name, p->cur_name);
564     if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
565         return 0;
566     if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
567     {
568         p->first_in_list = 1;
569         return 0;
570     }
571     *insertMode = p->key[0];
572     memcpy (*dst, p->key+1, sizeof(struct it_key));
573 #if PR_KEY_LOW
574     pkey(*dst, *insertMode);
575 #endif
576     (*dst) += sizeof(struct it_key);
577     return 1;
578 }
579
580 int heap_inpc (struct heap_cread_info *hci, struct heap_info *hi)
581 {
582     ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
583
584     isamc_i->clientData = hci;
585     isamc_i->read_item = heap_cread_item2;
586
587     while (hci->more)
588     {
589         char this_name[INP_NAME_MAX];
590         ISAM_P isamc_p, isamc_p2;
591         char *dict_info;
592
593         strcpy (this_name, hci->cur_name);
594         assert (hci->cur_name[1]);
595         hi->no_diffs++;
596         if ((dict_info = dict_lookup (hi->reg->dict, hci->cur_name)))
597         {
598             memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
599             isamc_p2 = isamc_p;
600             isamc_merge (hi->reg->isamc, &isamc_p2, isamc_i);
601             if (!isamc_p2)
602             {
603                 hi->no_deletions++;
604                 if (!dict_delete (hi->reg->dict, this_name))
605                     abort();
606             }
607             else 
608             {
609                 hi->no_updates++;
610                 if (isamc_p2 != isamc_p)
611                     dict_insert (hi->reg->dict, this_name,
612                                  sizeof(ISAM_P), &isamc_p2);
613             }
614         } 
615         else
616         {
617             isamc_p = 0;
618             isamc_merge (hi->reg->isamc, &isamc_p, isamc_i);
619             hi->no_insertions++;
620             if (isamc_p)
621                 dict_insert (hi->reg->dict, this_name,
622                              sizeof(ISAM_P), &isamc_p);
623         }
624     }
625     xfree (isamc_i);
626     return 0;
627
628
629 int heap_inp0(struct heap_cread_info *hci, struct heap_info *hi)
630 {
631     while (hci->more)
632     {
633         char this_name[INP_NAME_MAX];
634         char mybuf[1024];
635         char *dst = mybuf;
636         int mode;
637
638         strcpy (this_name, hci->cur_name);
639         assert (hci->cur_name[1]);
640         hi->no_diffs++;
641
642         while (heap_cread_item2(hci, &dst, &mode))
643             ;
644     }
645     return 0;
646
647
648
649 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
650 {
651     ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
652
653     isamc_i->clientData = hci;
654     isamc_i->read_item = heap_cread_item2;
655
656     while (hci->more)
657     {
658         char this_name[INP_NAME_MAX];
659         ISAM_P isamc_p, isamc_p2;
660         char *dict_info;
661
662         strcpy (this_name, hci->cur_name);
663         assert (hci->cur_name[1]);
664         hi->no_diffs++;
665
666 #if 0
667         print_dict_item(hi->zh, hci->cur_name);
668 #endif
669         if ((dict_info = dict_lookup (hi->reg->dict, hci->cur_name)))
670         {
671             memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
672             isamc_p2 = isamc_p;
673             isamb_merge (hi->reg->isamb, &isamc_p2, isamc_i);
674             if (!isamc_p2)
675             {
676                 hi->no_deletions++;
677                 if (!dict_delete (hi->reg->dict, this_name))
678                     abort();
679             }
680             else 
681             {
682                 hi->no_updates++;
683                 if (isamc_p2 != isamc_p)
684                     dict_insert (hi->reg->dict, this_name,
685                                  sizeof(ISAM_P), &isamc_p2);
686             }
687         } 
688         else
689         {
690             isamc_p = 0;
691             isamb_merge (hi->reg->isamb, &isamc_p, isamc_i);
692             hi->no_insertions++;
693             if (isamc_p)
694                 dict_insert (hi->reg->dict, this_name,
695                              sizeof(ISAM_P), &isamc_p);
696         }
697     }
698     xfree(isamc_i);
699     return 0;
700
701
702 int heap_inps (struct heap_cread_info *hci, struct heap_info *hi)
703 {
704     ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
705
706     isams_i->clientData = hci;
707     isams_i->read_item = heap_cread_item;
708
709     while (hci->more)
710     {
711         char this_name[INP_NAME_MAX];
712         ISAM_P isams_p;
713         char *dict_info;
714
715         strcpy (this_name, hci->cur_name);
716         assert (hci->cur_name[1]);
717         hi->no_diffs++;
718         if (!(dict_info = dict_lookup (hi->reg->dict, hci->cur_name)))
719         {
720             isams_p = isams_merge (hi->reg->isams, isams_i);
721             hi->no_insertions++;
722             dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
723         }
724         else
725         {
726             yaz_log (YLOG_FATAL, "isams doesn't support this kind of update");
727             break;
728         }
729     }
730     xfree (isams_i);
731     return 0;
732
733
734 struct progressInfo {
735     time_t   startTime;
736     time_t   lastTime;
737     off_t    totalBytes;
738     off_t    totalOffset;
739 };
740
741 void progressFunc (struct key_file *keyp, void *info)
742 {
743     struct progressInfo *p = (struct progressInfo *) info;
744     time_t now, remaining;
745
746     if (keyp->buf_size <= 0 || p->totalBytes <= 0)
747         return ;
748     time (&now);
749
750     if (now >= p->lastTime+10)
751     {
752         p->lastTime = now;
753         remaining = (time_t) ((now - p->startTime)*
754             ((double) p->totalBytes/p->totalOffset - 1.0));
755         if (remaining <= 130)
756             yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
757                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
758         else
759             yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
760                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
761     }
762     p->totalOffset += keyp->buf_size;
763 }
764
765 #ifndef R_OK
766 #define R_OK 4
767 #endif
768
769 void zebra_index_merge (ZebraHandle zh)
770 {
771     struct key_file **kf = 0;
772     char rbuf[1024];
773     int i, r;
774     struct heap_info *hi;
775     struct progressInfo progressInfo;
776     int nkeys = zh->reg->key_file_no;
777     int usefile; 
778     yaz_log (YLOG_DEBUG, " index_merge called with nk=%d b=%p", 
779                     nkeys, zh->reg->key_buf);
780     if ( (nkeys==0) && (zh->reg->key_buf==0) )
781         return; /* nothing to merge - probably flush after end-trans */
782     
783     usefile = (nkeys!=0); 
784
785     if (usefile)
786     {
787         if (nkeys < 0)
788         {
789             char fname[1024];
790             nkeys = 0;
791             while (1)
792             {
793                 extract_get_fname_tmp  (zh, fname, nkeys+1);
794                 if (access (fname, R_OK) == -1)
795                         break;
796                 nkeys++;
797             }
798             if (!nkeys)
799                 return ;
800         }
801         kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
802         progressInfo.totalBytes = 0;
803         progressInfo.totalOffset = 0;
804         time (&progressInfo.startTime);
805         time (&progressInfo.lastTime);
806         for (i = 1; i<=nkeys; i++)
807         {
808             kf[i] = key_file_init (i, 8192, zh->res);
809             kf[i]->readHandler = progressFunc;
810             kf[i]->readInfo = &progressInfo;
811             progressInfo.totalBytes += kf[i]->length;
812             progressInfo.totalOffset += kf[i]->buf_size;
813         }
814         hi = key_heap_init (nkeys, key_qsort_compare);
815         hi->reg = zh->reg;
816         
817         for (i = 1; i<=nkeys; i++)
818             if ((r = key_file_read (kf[i], rbuf)))
819                 key_heap_insert (hi, rbuf, r, kf[i]);
820     }  /* use file */
821     else 
822     { /* do not use file, read straight from buffer */
823         hi = key_heap_init_buff (zh, key_qsort_compare);
824         hi->reg = zh->reg;
825     }
826
827     if (1)
828     {
829         struct heap_cread_info hci;
830     
831         hci.key = (char *) xmalloc (KEY_SIZE);
832         hci.key_1 = (char *) xmalloc (KEY_SIZE);
833         hci.key_2 = (char *) xmalloc (KEY_SIZE);
834         hci.ret = -1;
835         hci.first_in_list = 1;
836         hci.hi = hi;
837         hci.look_level = 0;
838         hci.more = heap_read_one (hi, hci.cur_name, hci.key);    
839         
840         if (zh->reg->isams)
841             heap_inps(&hci, hi);
842         if (zh->reg->isamc)
843             heap_inpc(&hci, hi);
844         if (zh->reg->isamb)
845             heap_inpb(&hci, hi);
846         
847         xfree (hci.key);
848         xfree (hci.key_1);
849         xfree (hci.key_2);
850     }
851         
852     if (usefile)
853     {
854         for (i = 1; i<=nkeys; i++)
855         {
856             extract_get_fname_tmp  (zh, rbuf, i);
857             unlink (rbuf);
858         }
859         for (i = 1; i<=nkeys; i++)
860             key_file_destroy (kf[i]);
861         xfree (kf);
862     }
863     if (hi->no_iterations)
864     { /* do not log if nothing happened */
865         yaz_log (YLOG_LOG, "Iterations . . .%7d", hi->no_iterations);
866         yaz_log (YLOG_LOG, "Distinct words .%7d", hi->no_diffs);
867         yaz_log (YLOG_LOG, "Updates. . . . .%7d", hi->no_updates);
868         yaz_log (YLOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
869         yaz_log (YLOG_LOG, "Insertions . . .%7d", hi->no_insertions);
870     }
871     zh->reg->key_file_no = 0;
872
873     key_heap_destroy (hi, nkeys);
874 }