Remove obsolete references
[idzebra-moved-to-github.git] / index / kinput.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19  
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
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
40 struct key_file {
41     int   no;            /* file no */
42     off_t offset;        /* file offset */
43     unsigned char *buf;  /* buffer block */
44     size_t buf_size;     /* number of read bytes in block */
45     size_t chunk;        /* number of bytes allocated */
46     size_t buf_ptr;      /* current position in buffer */
47     char *prev_name;     /* last word read */
48     void *decode_handle;
49     off_t length;        /* length of file */
50                          /* handler invoked in each read */
51     void (*readHandler)(struct key_file *keyp, void *rinfo);
52     void *readInfo;
53     Res res;
54 };
55
56
57 #define PR_KEY_LOW 0
58 #define PR_KEY_TOP 0
59
60 #if PR_KEY_LOW || PR_KEY_TOP
61
62 static void pkey(const char *b, int mode)
63 {
64     key_logdump_txt(YLOG_LOG, b, mode ? "i" : "d");
65 }
66 #endif
67
68
69 void getFnameTmp(Res res, char *fname, int no)
70 {
71     const char *pre;
72     
73     pre = res_get_def(res, "keyTmpDir", ".");
74     sprintf(fname, "%s/key%d.tmp", pre, no);
75 }
76
77 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no)
78 {
79     const char *pre;
80     
81     pre = res_get_def(zh->res, "keyTmpDir", ".");
82     sprintf(fname, "%s/key%d.tmp", pre, no);
83 }
84
85 void key_file_chunk_read(struct key_file *f)
86 {
87     int nr = 0, r = 0, fd;
88     char fname[1024];
89     getFnameTmp(f->res, fname, f->no);
90     fd = open(fname, O_BINARY|O_RDONLY);
91
92     f->buf_ptr = 0;
93     f->buf_size = 0;
94     if (fd == -1)
95     {
96         yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot open %s", fname);
97         return ;
98     }
99     if (!f->length)
100     {
101         if ((f->length = lseek(fd, 0L, SEEK_END)) == (off_t) -1)
102         {
103             yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
104             close(fd);
105             return ;
106         }
107     }
108     if (lseek(fd, f->offset, SEEK_SET) == -1)
109     {
110         yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
111         close(fd);
112         return ;
113     }
114     while (f->chunk - nr > 0)
115     {
116         r = read(fd, f->buf + nr, f->chunk - nr);
117         if (r <= 0)
118             break;
119         nr += r;
120     }
121     if (r == -1)
122     {
123         yaz_log(YLOG_WARN|YLOG_ERRNO, "read of %s", fname);
124         close(fd);
125         return;
126     }
127     f->buf_size = nr;
128     if (f->readHandler)
129         (*f->readHandler)(f, f->readInfo);
130     close(fd);
131 }
132
133 void key_file_destroy(struct key_file *f)
134 {
135     iscz1_stop(f->decode_handle);
136     xfree(f->buf);
137     xfree(f->prev_name);
138     xfree(f);
139 }
140
141 struct key_file *key_file_init(int no, int chunk, Res res)
142 {
143     struct key_file *f;
144
145     f = (struct key_file *) xmalloc(sizeof(*f));
146     f->res = res;
147     f->decode_handle = iscz1_start();
148     f->no = no;
149     f->chunk = chunk;
150     f->offset = 0;
151     f->length = 0;
152     f->readHandler = NULL;
153     f->buf = (unsigned char *) xmalloc(f->chunk);
154     f->prev_name = (char *) xmalloc(INP_NAME_MAX);
155     *f->prev_name = '\0';
156     key_file_chunk_read(f);
157     return f;
158 }
159
160 int key_file_getc(struct key_file *f)
161 {
162     if (f->buf_ptr < f->buf_size)
163         return f->buf[(f->buf_ptr)++];
164     if (f->buf_size < f->chunk)
165         return EOF;
166     f->offset += f->buf_size;
167     key_file_chunk_read(f);
168     if (f->buf_ptr < f->buf_size)
169         return f->buf[(f->buf_ptr)++];
170     else
171         return EOF;
172 }
173
174 int key_file_read(struct key_file *f, char *key)
175 {
176     int i, c;
177     char srcbuf[128];
178     const char *src = srcbuf;
179     char *dst;
180     int j;
181
182     c = key_file_getc(f);
183     if (c == 0)
184     {
185         strcpy(key, f->prev_name);
186         i = 1+strlen(key);
187     }
188     else if (c == EOF)
189         return 0;
190     else
191     {
192         i = 0;
193         key[i++] = c;
194         while ((c = key_file_getc(f)))
195         {
196             if (i < INP_NAME_MAX-2)
197                 key[i++] = c;
198         }
199         key[i++] = '\0';
200         strcpy(f->prev_name, key);
201         iscz1_reset(f->decode_handle);
202     }
203     c = key_file_getc(f); /* length +  insert/delete combined */
204     key[i++] = c & 128;
205     c = c & 127;
206     for (j = 0; j < c; j++)
207         srcbuf[j] = key_file_getc(f);
208     dst = key + i;
209     iscz1_decode(f->decode_handle, &dst, &src);
210
211 #if 0
212     /* debugging */
213     if (1)
214     {
215         struct it_key k;
216         memcpy(&k, key+i, sizeof(k));
217         if (!k.mem[1])
218             yaz_log(YLOG_LOG, "00 KEY");
219     }
220 #endif
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;
234     zint no_diffs;
235     zint no_updates;
236     zint no_deletions;
237     zint no_insertions;
238     zint no_iterations;
239 };
240
241 static struct heap_info *key_heap_malloc(void)
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->no_diffs = 0;
250     hi->no_diffs = 0;
251     hi->no_updates = 0;
252     hi->no_deletions = 0;
253     hi->no_insertions = 0;
254     hi->no_iterations = 0;
255     return hi;
256 }
257
258 struct heap_info *key_heap_init_file(ZebraHandle zh,
259                                      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->zh = zh;
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 void key_heap_destroy(struct heap_info *hi, int nkeys)
281 {
282     int i;
283     for (i = 0; i<=nkeys; i++)
284         xfree(hi->info.buf[i]);
285     xfree(hi->info.buf);
286     xfree(hi->ptr);
287     xfree(hi->info.file);
288     xfree(hi);
289 }
290
291 static void key_heap_swap(struct heap_info *hi, int i1, int i2)
292 {
293     int swap;
294
295     swap = hi->ptr[i1];
296     hi->ptr[i1] = hi->ptr[i2];
297     hi->ptr[i2] = swap;
298 }
299
300
301 static void key_heap_delete(struct heap_info *hi)
302 {
303     int cur = 1, child = 2;
304
305     assert(hi->heapnum > 0);
306
307     key_heap_swap(hi, 1, hi->heapnum);
308     hi->heapnum--;
309     while (child <= hi->heapnum) {
310         if (child < hi->heapnum &&
311             (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
312                        &hi->info.buf[hi->ptr[child+1]]) > 0)
313             child++;
314         if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
315                        &hi->info.buf[hi->ptr[child]]) > 0)
316         {            
317             key_heap_swap(hi, cur, child);
318             cur = child;
319             child = 2*cur;
320         }
321         else
322             break;
323     }
324 }
325
326 static void key_heap_insert(struct heap_info *hi, const char *buf, int nbytes,
327                              struct key_file *kf)
328 {
329     int cur, parent;
330
331     cur = ++(hi->heapnum);
332     memcpy(hi->info.buf[hi->ptr[cur]], buf, nbytes);
333     hi->info.file[hi->ptr[cur]] = kf;
334
335     parent = cur/2;
336     while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
337                                 &hi->info.buf[hi->ptr[cur]]) > 0)
338     {
339         key_heap_swap(hi, cur, parent);
340         cur = parent;
341         parent = cur/2;
342     }
343 }
344
345 static int heap_read_one(struct heap_info *hi, char *name, char *key)
346 {
347     int n, r;
348     char rbuf[INP_NAME_MAX];
349     struct key_file *kf;
350
351     if (!hi->heapnum)
352         return 0;
353     n = hi->ptr[1];
354     strcpy(name, hi->info.buf[n]);
355     kf = hi->info.file[n];
356     r = strlen(name);
357     memcpy(key, hi->info.buf[n] + r+1, KEY_SIZE);
358     key_heap_delete(hi);
359     if ((r = key_file_read(kf, rbuf)))
360         key_heap_insert(hi, rbuf, r, kf);
361     hi->no_iterations++;
362     return 1;
363 }
364
365
366 /* for debugging only */
367 void zebra_log_dict_entry(ZebraHandle zh, const char *s)
368 {
369     char dst[INP_NAME_MAX+1];
370     int ord;
371     int len = key_SU_decode(&ord, (const unsigned char *) s);
372     const char *index_type;
373
374     if (!zh)
375         yaz_log(YLOG_LOG, "ord=%d", ord);
376     else
377     {
378         const char *string_index;
379         const char *db = 0;
380         zebraExplain_lookup_ord(zh->reg->zei,
381                                 ord, &index_type, &db, &string_index);
382
383         zebra_term_untrans(zh, index_type, dst, s + len);
384
385         yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s term=%s",
386                 ord, index_type, string_index, dst);
387     }
388 }
389
390 struct heap_cread_info {
391     char prev_name[INP_NAME_MAX];
392     char cur_name[INP_NAME_MAX];
393     char *key;
394     char *key_1, *key_2;
395     int mode_1, mode_2;
396     int sz_1, sz_2;
397     struct heap_info *hi;
398     int first_in_list;
399     int more;
400     int ret;
401     int look_level;
402 };
403
404 static int heap_cread_item(void *vp, char **dst, int *insertMode);
405
406 int heap_cread_item2(void *vp, char **dst, int *insertMode)
407 {
408     struct heap_cread_info *p = (struct heap_cread_info *) vp;
409     int level = 0;
410
411     if (p->look_level)
412     {
413         if (p->look_level > 0)
414         {
415             *insertMode = 1;
416             p->look_level--;
417         }
418         else
419         {
420             *insertMode = 0;
421             p->look_level++;
422         }
423         memcpy(*dst, p->key_1, p->sz_1);
424 #if PR_KEY_TOP
425         yaz_log(YLOG_LOG, "DUP level=%d", p->look_level);
426         pkey(*dst, *insertMode);
427 #endif
428         (*dst) += p->sz_1;
429         return 1;
430     }
431     if (p->ret == 0)    /* lookahead was 0?. Return that in read next round */
432     {
433         p->ret = -1;
434         return 0;
435     }
436     else if (p->ret == -1) /* Must read new item ? */
437     {
438         char *dst_1 = p->key_1;
439         p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
440         p->sz_1 = dst_1 - p->key_1;
441     }
442     else
443     {        /* lookahead in 2 . Now in 1. */
444         p->sz_1 = p->sz_2;
445         p->mode_1 = p->mode_2;
446         memcpy(p->key_1, p->key_2, p->sz_2);
447     }
448     if (p->mode_1)
449         level = 1;     /* insert */
450     else
451         level = -1;    /* delete */
452     while(1)
453     {
454         char *dst_2 = p->key_2;
455         p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
456         if (!p->ret)
457         {
458             if (level)
459                 break;
460             p->ret = -1;
461             return 0;
462         }
463         p->sz_2 = dst_2 - p->key_2;
464
465         if (key_compare(p->key_1, p->key_2) == 0)
466         {
467             if (p->mode_2) /* adjust level according to deletes/inserts */
468                 level++;
469             else
470                 level--;
471         }
472         else
473         {
474             if (level)
475                 break;
476             /* all the same. new round .. */
477             p->sz_1 = p->sz_2;
478             p->mode_1 = p->mode_2;
479             memcpy(p->key_1, p->key_2, p->sz_1);
480             if (p->mode_1)
481                 level = 1;     /* insert */
482             else
483                 level = -1;    /* delete */
484         }
485     }
486     /* outcome is insert (1) or delete (0) depending on final level */
487     if (level > 0)
488     {
489         *insertMode = 1;
490         level--;
491     }
492     else
493     {
494         *insertMode = 0;
495         level++;
496     }
497     p->look_level = level;
498     memcpy(*dst, p->key_1, p->sz_1);
499 #if PR_KEY_TOP
500     yaz_log(YLOG_LOG, "TOP");
501     pkey(*dst, *insertMode);
502 #endif
503     (*dst) += p->sz_1;
504     return 1;
505 }
506       
507 int heap_cread_item(void *vp, char **dst, int *insertMode)
508 {
509     struct heap_cread_info *p = (struct heap_cread_info *) vp;
510     struct heap_info *hi = p->hi;
511
512     if (p->first_in_list)
513     {
514         *insertMode = p->key[0];
515         memcpy(*dst, p->key+1, sizeof(struct it_key));
516 #if PR_KEY_LOW
517         zebra_log_dict_entry(hi->zh, p->cur_name);
518         pkey(*dst, *insertMode);
519 #endif
520         (*dst) += sizeof(struct it_key);
521         p->first_in_list = 0;
522         return 1;
523     }
524     strcpy(p->prev_name, p->cur_name);
525     if (!(p->more = heap_read_one(hi, p->cur_name, p->key)))
526         return 0;
527     if (*p->cur_name && strcmp(p->cur_name, p->prev_name))
528     {
529         p->first_in_list = 1;
530         return 0;
531     }
532     *insertMode = p->key[0];
533     memcpy(*dst, p->key+1, sizeof(struct it_key));
534 #if PR_KEY_LOW
535     zebra_log_dict_entry(hi->zh, p->cur_name);
536     pkey(*dst, *insertMode);
537 #endif
538     (*dst) += sizeof(struct it_key);
539     return 1;
540 }
541
542 int heap_inpc(struct heap_cread_info *hci, struct heap_info *hi)
543 {
544     ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
545
546     isamc_i->clientData = hci;
547     isamc_i->read_item = heap_cread_item2;
548
549     while (hci->more)
550     {
551         char this_name[INP_NAME_MAX];
552         ISAM_P isamc_p, isamc_p2;
553         char *dict_info;
554
555         strcpy(this_name, hci->cur_name);
556         assert(hci->cur_name[0]);
557         hi->no_diffs++;
558         if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
559         {
560             memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
561             isamc_p2 = isamc_p;
562             isamc_merge(hi->reg->isamc, &isamc_p2, isamc_i);
563             if (!isamc_p2)
564             {
565                 hi->no_deletions++;
566                 if (!dict_delete(hi->reg->dict, this_name))
567                     abort();
568             }
569             else 
570             {
571                 hi->no_updates++;
572                 if (isamc_p2 != isamc_p)
573                     dict_insert(hi->reg->dict, this_name,
574                                  sizeof(ISAM_P), &isamc_p2);
575             }
576         } 
577         else
578         {
579             isamc_p = 0;
580             isamc_merge(hi->reg->isamc, &isamc_p, isamc_i);
581             hi->no_insertions++;
582             if (isamc_p)
583                 dict_insert(hi->reg->dict, this_name,
584                              sizeof(ISAM_P), &isamc_p);
585         }
586     }
587     xfree(isamc_i);
588     return 0;
589
590
591 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
592 {
593     ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
594
595     isamc_i->clientData = hci;
596     isamc_i->read_item = heap_cread_item2;
597
598     while (hci->more)
599     {
600         char this_name[INP_NAME_MAX];
601         ISAM_P isamc_p, isamc_p2;
602         char *dict_info;
603
604         strcpy(this_name, hci->cur_name);
605         assert(hci->cur_name[0]);
606         hi->no_diffs++;
607
608 #if 0
609         assert(hi->zh);
610         zebra_log_dict_entry(hi->zh, hci->cur_name);
611 #endif
612         if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
613         {
614             memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
615             isamc_p2 = isamc_p;
616             isamb_merge(hi->reg->isamb, &isamc_p2, isamc_i);
617             if (!isamc_p2)
618             {
619                 hi->no_deletions++;
620                 if (!dict_delete(hi->reg->dict, this_name))
621                     abort();
622             }
623             else 
624             {
625                 hi->no_updates++;
626                 if (isamc_p2 != isamc_p)
627                     dict_insert(hi->reg->dict, this_name,
628                                  sizeof(ISAM_P), &isamc_p2);
629             }
630         } 
631         else
632         {
633             isamc_p = 0;
634             isamb_merge(hi->reg->isamb, &isamc_p, isamc_i);
635             hi->no_insertions++;
636             if (isamc_p)
637                 dict_insert(hi->reg->dict, this_name,
638                              sizeof(ISAM_P), &isamc_p);
639         }
640     }
641     xfree(isamc_i);
642     return 0;
643
644
645 int heap_inps(struct heap_cread_info *hci, struct heap_info *hi)
646 {
647     ISAMS_I isams_i = (ISAMS_I) xmalloc(sizeof(*isams_i));
648
649     isams_i->clientData = hci;
650     isams_i->read_item = heap_cread_item;
651
652     while (hci->more)
653     {
654         char this_name[INP_NAME_MAX];
655         ISAM_P isams_p;
656         char *dict_info;
657
658         strcpy(this_name, hci->cur_name);
659         assert(hci->cur_name[0]);
660         hi->no_diffs++;
661         if (!(dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
662         {
663             isams_p = isams_merge(hi->reg->isams, isams_i);
664             hi->no_insertions++;
665             dict_insert(hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
666         }
667         else
668         {
669             yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
670             break;
671         }
672     }
673     xfree(isams_i);
674     return 0;
675
676
677 struct progressInfo {
678     time_t   startTime;
679     time_t   lastTime;
680     off_t    totalBytes;
681     off_t    totalOffset;
682 };
683
684 void progressFunc(struct key_file *keyp, void *info)
685 {
686     struct progressInfo *p = (struct progressInfo *) info;
687     time_t now, remaining;
688
689     if (keyp->buf_size <= 0 || p->totalBytes <= 0)
690         return ;
691     time (&now);
692
693     if (now >= p->lastTime+10)
694     {
695         p->lastTime = now;
696         remaining = (time_t) ((now - p->startTime)*
697             ((double) p->totalBytes/p->totalOffset - 1.0));
698         if (remaining <= 130)
699             yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
700                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
701         else
702             yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
703                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
704     }
705     p->totalOffset += keyp->buf_size;
706 }
707
708 #ifndef R_OK
709 #define R_OK 4
710 #endif
711
712 void zebra_index_merge(ZebraHandle zh)
713 {
714     struct key_file **kf = 0;
715     char rbuf[1024];
716     int i, r;
717     struct heap_info *hi;
718     struct progressInfo progressInfo;
719     int nkeys = key_block_get_no_files(zh->reg->key_block);
720
721     if (nkeys == 0)
722         return;
723     
724     if (nkeys < 0)
725     {
726         char fname[1024];
727         nkeys = 0;
728         while (1)
729         {
730             extract_get_fname_tmp (zh, fname, nkeys+1);
731             if (access(fname, R_OK) == -1)
732                 break;
733             nkeys++;
734         }
735         if (!nkeys)
736             return ;
737     }
738     kf = (struct key_file **) xmalloc((1+nkeys) * sizeof(*kf));
739     progressInfo.totalBytes = 0;
740     progressInfo.totalOffset = 0;
741     time(&progressInfo.startTime);
742     time(&progressInfo.lastTime);
743     for (i = 1; i<=nkeys; i++)
744     {
745         kf[i] = key_file_init(i, 8192, zh->res);
746         kf[i]->readHandler = progressFunc;
747         kf[i]->readInfo = &progressInfo;
748         progressInfo.totalBytes += kf[i]->length;
749         progressInfo.totalOffset += kf[i]->buf_size;
750     }
751     hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
752     hi->reg = zh->reg;
753     
754     for (i = 1; i<=nkeys; i++)
755         if ((r = key_file_read(kf[i], rbuf)))
756             key_heap_insert(hi, rbuf, r, kf[i]);
757
758     if (1)
759     {
760         struct heap_cread_info hci;
761         
762         hci.key = (char *) xmalloc(KEY_SIZE);
763         hci.key_1 = (char *) xmalloc(KEY_SIZE);
764         hci.key_2 = (char *) xmalloc(KEY_SIZE);
765         hci.ret = -1;
766         hci.first_in_list = 1;
767         hci.hi = hi;
768         hci.look_level = 0;
769         hci.more = heap_read_one(hi, hci.cur_name, hci.key);    
770         
771         if (zh->reg->isams)
772             heap_inps(&hci, hi);
773         if (zh->reg->isamc)
774             heap_inpc(&hci, hi);
775         if (zh->reg->isamb)
776             heap_inpb(&hci, hi);
777         
778         xfree(hci.key);
779         xfree(hci.key_1);
780         xfree(hci.key_2);
781     }
782         
783     for (i = 1; i<=nkeys; i++)
784     {
785         extract_get_fname_tmp (zh, rbuf, i);
786         unlink(rbuf);
787     }
788     for (i = 1; i<=nkeys; i++)
789         key_file_destroy(kf[i]);
790     xfree(kf);
791     if (hi->no_iterations)
792     { /* do not log if nothing happened */
793         yaz_log(YLOG_LOG, "Iterations: isam/dict " 
794                 ZINT_FORMAT "/" ZINT_FORMAT,
795                 hi->no_iterations, hi->no_diffs);
796         yaz_log(YLOG_LOG, "Dict: inserts/updates/deletions: "
797                 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
798                 hi->no_insertions, hi->no_updates, hi->no_deletions);
799     }
800     key_block_destroy(&zh->reg->key_block);
801     key_heap_destroy(hi, nkeys);
802 }
803 /*
804  * Local variables:
805  * c-basic-offset: 4
806  * c-file-style: "Stroustrup"
807  * indent-tabs-mode: nil
808  * End:
809  * vim: shiftwidth=4 tabstop=8 expandtab
810  */
811