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