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