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