Memory leak hunting
[idzebra-moved-to-github.git] / index / kinput.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
5  *
6  * (log at the end)
7  
8  */
9  
10 #include <fcntl.h>
11 #ifdef WIN32
12 #include <io.h>
13 #else
14 #include <unistd.h>
15 #endif
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <assert.h>
20
21 #include "index.h"
22
23 #define KEY_SIZE (1+sizeof(struct it_key))
24 #define INP_NAME_MAX 768
25 #define INP_BUF_START 60000
26 #define INP_BUF_ADD  400000
27
28 static int no_diffs   = 0;
29 static int no_updates = 0;
30 static int no_deletions = 0;
31 static int no_insertions = 0;
32 static int no_iterations = 0;
33
34 struct key_file {
35     int   no;            /* file no */
36     off_t offset;        /* file offset */
37     unsigned char *buf;  /* buffer block */
38     size_t buf_size;     /* number of read bytes in block */
39     size_t chunk;        /* number of bytes allocated */
40     size_t buf_ptr;      /* current position in buffer */
41     char *prev_name;     /* last word read */
42     int   sysno;         /* last sysno */
43     int   seqno;         /* last seqno */
44     off_t length;        /* length of file */
45                          /* handler invoked in each read */
46     void (*readHandler)(struct key_file *keyp, void *rinfo);
47     void *readInfo;
48 };
49
50 void getFnameTmp (char *fname, int no)
51 {
52     const char *pre;
53     
54     pre = res_get_def (common_resource, "keyTmpDir", ".");
55     sprintf (fname, "%s/key%d.tmp", pre, no);
56 }
57
58 void key_file_chunk_read (struct key_file *f)
59 {
60     int nr = 0, r = 0, fd;
61     char fname[1024];
62     getFnameTmp (fname, f->no);
63     fd = open (fname, O_BINARY|O_RDONLY);
64     if (fd == -1)
65     {
66         logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
67         exit (1);
68     }
69     if (!f->length)
70     {
71         if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
72         {
73             logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
74             exit (1);
75         }
76     }
77     if (lseek (fd, f->offset, SEEK_SET) == -1)
78     {
79         logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
80         exit (1);
81     }
82     while (f->chunk - nr > 0)
83     {
84         r = read (fd, f->buf + nr, f->chunk - nr);
85         if (r <= 0)
86             break;
87         nr += r;
88     }
89     if (r == -1)
90     {
91         logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
92         exit (1);
93     }
94     f->buf_size = nr;
95     f->buf_ptr = 0;
96     if (f->readHandler)
97         (*f->readHandler)(f, f->readInfo);
98     close (fd);
99 }
100
101 struct key_file *key_file_init (int no, int chunk)
102 {
103     struct key_file *f;
104
105     f = (struct key_file *) xmalloc (sizeof(*f));
106     f->sysno = 0;
107     f->seqno = 0;
108     f->no = no;
109     f->chunk = chunk;
110     f->offset = 0;
111     f->length = 0;
112     f->readHandler = NULL;
113     f->buf = (unsigned char *) xmalloc (f->chunk);
114     f->prev_name = (char *) xmalloc (INP_NAME_MAX);
115     *f->prev_name = '\0';
116     key_file_chunk_read (f);
117     return f;
118 }
119
120 int key_file_getc (struct key_file *f)
121 {
122     if (f->buf_ptr < f->buf_size)
123         return f->buf[(f->buf_ptr)++];
124     if (f->buf_size < f->chunk)
125         return EOF;
126     f->offset += f->buf_size;
127     key_file_chunk_read (f);
128     if (f->buf_ptr < f->buf_size)
129         return f->buf[(f->buf_ptr)++];
130     else
131         return EOF;
132 }
133
134 int key_file_decode (struct key_file *f)
135 {
136     int c, d;
137
138     c = key_file_getc (f);
139     switch (c & 192) 
140     {
141     case 0:
142         d = c;
143         break;
144     case 64:
145         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
146         break;
147     case 128:
148         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
149         d = (d << 8) + (key_file_getc (f) & 0xff);
150         break;
151     case 192:
152         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
153         d = (d << 8) + (key_file_getc (f) & 0xff);
154         d = (d << 8) + (key_file_getc (f) & 0xff);
155         break;
156     }
157     return d;
158 }
159
160 int key_file_read (struct key_file *f, char *key)
161 {
162     int i, d, c;
163     struct it_key itkey;
164
165     c = key_file_getc (f);
166     if (c == 0)
167     {
168         strcpy (key, f->prev_name);
169         i = 1+strlen (key);
170     }
171     else if (c == EOF)
172         return 0;
173     else
174     {
175         i = 0;
176         key[i++] = c;
177         while ((key[i++] = key_file_getc (f)))
178             ;
179         strcpy (f->prev_name, key);
180         f->sysno = 0;
181     }
182     d = key_file_decode (f);
183     key[i++] = d & 1;
184     d = d >> 1;
185     itkey.sysno = d + f->sysno;
186     if (d) 
187     {
188         f->sysno = itkey.sysno;
189         f->seqno = 0;
190     }
191     d = key_file_decode (f);
192     itkey.seqno = d + f->seqno;
193     f->seqno = itkey.seqno;
194     memcpy (key + i, &itkey, sizeof(struct it_key));
195     return i + sizeof (struct it_key);
196 }
197
198 struct heap_info {
199     struct {
200         struct key_file **file;
201         char   **buf;
202     } info;
203     int    heapnum;
204     int    *ptr;
205     int    (*cmp)(const void *p1, const void *p2);
206     Dict dict;
207     ISAM isam;
208     ISAMC isamc;
209     ISAMS isams;
210     ISAMH isamh;
211     ISAMD isamd;
212 };
213
214 struct heap_info *key_heap_init (int nkeys,
215                                  int (*cmp)(const void *p1, const void *p2))
216 {
217     struct heap_info *hi;
218     int i;
219
220     hi = (struct heap_info *) xmalloc (sizeof(*hi));
221     hi->info.file = (struct key_file **)
222         xmalloc (sizeof(*hi->info.file) * (1+nkeys));
223     hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
224     hi->heapnum = 0;
225     hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
226     hi->cmp = cmp;
227     for (i = 0; i<= nkeys; i++)
228     {
229         hi->ptr[i] = i;
230         hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
231     }
232     return hi;
233 }
234
235 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
236 {
237     int swap;
238
239     swap = hi->ptr[i1];
240     hi->ptr[i1] = hi->ptr[i2];
241     hi->ptr[i2] = swap;
242 }
243
244
245 static void key_heap_delete (struct heap_info *hi)
246 {
247     int cur = 1, child = 2;
248
249     assert (hi->heapnum > 0);
250
251     key_heap_swap (hi, 1, hi->heapnum);
252     hi->heapnum--;
253     while (child <= hi->heapnum) {
254         if (child < hi->heapnum &&
255             (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
256                        &hi->info.buf[hi->ptr[child+1]]) > 0)
257             child++;
258         if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
259                        &hi->info.buf[hi->ptr[child]]) > 0)
260         {            
261             key_heap_swap (hi, cur, child);
262             cur = child;
263             child = 2*cur;
264         }
265         else
266             break;
267     }
268 }
269
270 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
271                              struct key_file *kf)
272 {
273     int cur, parent;
274
275     cur = ++(hi->heapnum);
276     memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
277     hi->info.file[hi->ptr[cur]] = kf;
278
279     parent = cur/2;
280     while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
281                                 &hi->info.buf[hi->ptr[cur]]) > 0)
282     {
283         key_heap_swap (hi, cur, parent);
284         cur = parent;
285         parent = cur/2;
286     }
287 }
288
289 static int heap_read_one (struct heap_info *hi, char *name, char *key)
290 {
291     int n, r;
292     char rbuf[INP_NAME_MAX];
293     struct key_file *kf;
294
295     if (!hi->heapnum)
296         return 0;
297     n = hi->ptr[1];
298     strcpy (name, hi->info.buf[n]);
299     kf = hi->info.file[n];
300     r = strlen(name);
301     memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
302     key_heap_delete (hi);
303     if ((r = key_file_read (kf, rbuf)))
304         key_heap_insert (hi, rbuf, r, kf);
305     no_iterations++;
306     return 1;
307 }
308
309 struct heap_cread_info {
310     char prev_name[INP_NAME_MAX];
311     char cur_name[INP_NAME_MAX];
312     char *key;
313     struct heap_info *hi;
314     int mode;
315     int more;
316 };
317       
318 int heap_cread_item (void *vp, char **dst, int *insertMode)
319 {
320     struct heap_cread_info *p = (struct heap_cread_info *) vp;
321     struct heap_info *hi = p->hi;
322
323     if (p->mode == 1)
324     {
325         *insertMode = p->key[0];
326         memcpy (*dst, p->key+1, sizeof(struct it_key));
327         (*dst) += sizeof(struct it_key);
328         p->mode = 2;
329         return 1;
330     }
331     strcpy (p->prev_name, p->cur_name);
332     if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
333         return 0;
334     if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
335     {
336         p->mode = 1;
337         return 0;
338     }
339     *insertMode = p->key[0];
340     memcpy (*dst, p->key+1, sizeof(struct it_key));
341     (*dst) += sizeof(struct it_key);
342     return 1;
343 }
344
345 int heap_inpc (struct heap_info *hi)
346 {
347     struct heap_cread_info hci;
348     ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
349
350     hci.key = (char *) xmalloc (KEY_SIZE);
351     hci.mode = 1;
352     hci.hi = hi;
353     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
354
355     isamc_i->clientData = &hci;
356     isamc_i->read_item = heap_cread_item;
357
358     while (hci.more)
359     {
360         char this_name[INP_NAME_MAX];
361         ISAMC_P isamc_p, isamc_p2;
362         char *dict_info;
363
364         strcpy (this_name, hci.cur_name);
365         assert (hci.cur_name[1]);
366         no_diffs++;
367         if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
368         {
369             memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
370             isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
371             if (!isamc_p2)
372             {
373                 no_deletions++;
374                 if (!dict_delete (hi->dict, this_name))
375                     abort();
376             }
377             else 
378             {
379                 no_updates++;
380                 if (isamc_p2 != isamc_p)
381                     dict_insert (hi->dict, this_name,
382                                  sizeof(ISAMC_P), &isamc_p2);
383             }
384         } 
385         else
386         {
387             isamc_p = isc_merge (hi->isamc, 0, isamc_i);
388             no_insertions++;
389             dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
390         }
391     }
392     xfree (isamc_i);
393     return 0;
394
395
396 int heap_inps (struct heap_info *hi)
397 {
398     struct heap_cread_info hci;
399     ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
400
401     hci.key = (char *) xmalloc (KEY_SIZE);
402     hci.mode = 1;
403     hci.hi = hi;
404     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
405
406     isams_i->clientData = &hci;
407     isams_i->read_item = heap_cread_item;
408
409     while (hci.more)
410     {
411         char this_name[INP_NAME_MAX];
412         ISAMS_P isams_p;
413         char *dict_info;
414
415         strcpy (this_name, hci.cur_name);
416         assert (hci.cur_name[1]);
417         no_diffs++;
418         if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
419         {
420             isams_p = isams_merge (hi->isams, isams_i);
421             no_insertions++;
422             dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
423         }
424         else
425             abort();
426     }
427     xfree (isams_i);
428     return 0;
429
430
431 int heap_inph (struct heap_info *hi)
432 {
433     struct heap_cread_info hci;
434     ISAMH_I isamh_i = (ISAMH_I) xmalloc (sizeof(*isamh_i));
435
436     hci.key = (char *) xmalloc (KEY_SIZE);
437     hci.mode = 1;
438     hci.hi = hi;
439     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
440
441     isamh_i->clientData = &hci;
442     isamh_i->read_item = heap_cread_item;
443
444     while (hci.more)
445     {
446         char this_name[INP_NAME_MAX];
447         ISAMH_P isamh_p, isamh_p2;
448         char *dict_info;
449
450         strcpy (this_name, hci.cur_name);
451         assert (hci.cur_name[1]);
452         no_diffs++;
453         if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
454         {
455             memcpy (&isamh_p, dict_info+1, sizeof(ISAMH_P));
456             isamh_p2 = isamh_append (hi->isamh, isamh_p, isamh_i);
457             if (!isamh_p2)
458             {
459                 no_deletions++;
460                 if (!dict_delete (hi->dict, this_name))
461                     abort();
462             }
463             else 
464             {
465                 no_updates++;
466                 if (isamh_p2 != isamh_p)
467                     dict_insert (hi->dict, this_name,
468                                  sizeof(ISAMH_P), &isamh_p2);
469             }
470         } 
471         else
472         {
473             isamh_p = isamh_append (hi->isamh, 0, isamh_i);
474             no_insertions++;
475             dict_insert (hi->dict, this_name, sizeof(ISAMH_P), &isamh_p);
476         }
477     }
478     xfree (isamh_i);
479     return 0;
480
481
482 int heap_inpd (struct heap_info *hi)
483 {
484     struct heap_cread_info hci;
485     ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
486
487     hci.key = (char *) xmalloc (KEY_SIZE);
488     hci.mode = 1;
489     hci.hi = hi;
490     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
491
492     isamd_i->clientData = &hci;
493     isamd_i->read_item = heap_cread_item;
494
495     while (hci.more)
496     {
497         char this_name[INP_NAME_MAX];
498         ISAMD_P isamd_p, isamd_p2;
499         char *dict_info;
500
501         strcpy (this_name, hci.cur_name);
502         assert (hci.cur_name[1]);
503         no_diffs++;
504         if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
505         {
506             memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
507             isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
508             if (!isamd_p2)
509             {
510                 no_deletions++;
511                 if (!dict_delete (hi->dict, this_name))
512                     abort();
513             }
514             else 
515             {
516                 no_updates++;
517                 if (isamd_p2 != isamd_p)
518                     dict_insert (hi->dict, this_name,
519                                  sizeof(ISAMD_P), &isamd_p2);
520             }
521         } 
522         else
523         {
524             isamd_p = isamd_append (hi->isamd, 0, isamd_i);
525             no_insertions++;
526             dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
527         }
528     }
529     xfree (isamd_i);
530     return 0;
531
532
533
534
535
536 int heap_inp (struct heap_info *hi)
537 {
538     char *info;
539     char next_name[INP_NAME_MAX];
540     char cur_name[INP_NAME_MAX];
541     int key_buf_size = INP_BUF_START;
542     int key_buf_ptr;
543     char *next_key;
544     char *key_buf;
545     int more;
546     
547     next_key = (char *) xmalloc (KEY_SIZE);
548     key_buf = (char *) xmalloc (key_buf_size);
549     more = heap_read_one (hi, cur_name, key_buf);
550     while (more)                   /* EOF ? */
551     {
552         int nmemb;
553         key_buf_ptr = KEY_SIZE;
554         while (1)
555         {
556             if (!(more = heap_read_one (hi, next_name, next_key)))
557                 break;
558             if (*next_name && strcmp (next_name, cur_name))
559                 break;
560             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
561             key_buf_ptr += KEY_SIZE;
562             if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
563             {
564                 char *new_key_buf;
565                 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
566                 memcpy (new_key_buf, key_buf, key_buf_size);
567                 key_buf_size += INP_BUF_ADD;
568                 xfree (key_buf);
569                 key_buf = new_key_buf;
570             }
571         }
572         no_diffs++;
573         nmemb = key_buf_ptr / KEY_SIZE;
574         assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
575         if ((info = dict_lookup (hi->dict, cur_name)))
576         {
577             ISAM_P isam_p, isam_p2;
578             memcpy (&isam_p, info+1, sizeof(ISAM_P));
579             isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
580             if (!isam_p2)
581             {
582                 no_deletions++;
583                 if (!dict_delete (hi->dict, cur_name))
584                     abort ();
585             }
586             else 
587             {
588                 no_updates++;
589                 if (isam_p2 != isam_p)
590                     dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
591             }
592         }
593         else
594         {
595             ISAM_P isam_p;
596             no_insertions++;
597             isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
598             dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
599         }
600         memcpy (key_buf, next_key, KEY_SIZE);
601         strcpy (cur_name, next_name);
602     }
603     return 0;
604 }
605
606 struct progressInfo {
607     time_t   startTime;
608     time_t   lastTime;
609     off_t    totalBytes;
610     off_t    totalOffset;
611 };
612
613 void progressFunc (struct key_file *keyp, void *info)
614 {
615     struct progressInfo *p = (struct progressInfo *) info;
616     time_t now, remaining;
617
618     if (keyp->buf_size <= 0 || p->totalBytes <= 0)
619         return ;
620     time (&now);
621
622     if (now >= p->lastTime+10)
623     {
624         p->lastTime = now;
625         remaining = (time_t) ((now - p->startTime)*
626             ((double) p->totalBytes/p->totalOffset - 1.0));
627         if (remaining <= 130)
628             logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
629                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
630         else
631             logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
632                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
633     }
634     p->totalOffset += keyp->buf_size;
635 }
636
637 #ifndef R_OK
638 #define R_OK 4
639 #endif
640
641 void key_input (BFiles bfs, int nkeys, int cache)
642                 
643 {
644     Dict dict;
645     ISAM isam = NULL;
646     ISAMC isamc = NULL;
647     ISAMS isams = NULL;
648     ISAMH isamh = NULL;
649     ISAMD isamd = NULL;
650     struct key_file **kf;
651     char rbuf[1024];
652     int i, r;
653     struct heap_info *hi;
654     struct progressInfo progressInfo;
655
656     if (nkeys < 0)
657     {
658         char fname[1024];
659         nkeys = 0;
660         while (1)
661         {
662             getFnameTmp (fname, nkeys+1);
663             if (access (fname, R_OK) == -1)
664                 break;
665             nkeys++;
666         }
667         if (!nkeys)
668             return ;
669     }
670     dict = dict_open (bfs, FNAME_DICT, cache, 1, 0);
671     if (!dict)
672     {
673         logf (LOG_FATAL, "dict_open fail");
674         exit (1);
675     }
676     if (res_get_match (common_resource, "isam", "s", NULL))
677     {
678         struct ISAMS_M_s isams_m;
679         isams = isams_open (bfs, FNAME_ISAMS, 1,
680                             key_isams_m (common_resource, &isams_m));
681         if (!isams)
682         {
683             logf (LOG_FATAL, "isams_open fail");
684             exit (1);
685         }
686     }
687     else if (res_get_match (common_resource, "isam", "i", NULL))
688     {
689         isam = is_open (bfs, FNAME_ISAM, key_compare, 1,
690                         sizeof(struct it_key), common_resource);
691         if (!isam)
692         {
693             logf (LOG_FATAL, "is_open fail");
694             exit (1);
695         }
696     }
697     else if (res_get_match (common_resource, "isam", "h", NULL))
698     {
699         isamh = isamh_open (bfs, FNAME_ISAMH, 1,
700                           key_isamh_m (common_resource));
701         if (!isamh)
702         {
703             logf (LOG_FATAL, "isamh_open fail");
704             exit (1);
705         }
706     }
707     else if (res_get_match (common_resource, "isam", "d", NULL))
708     {
709         struct ISAMD_M_s isamd_m;
710         isamd = isamd_open (bfs, FNAME_ISAMD, 1,
711                           key_isamd_m (common_resource,&isamd_m));
712         if (!isamd)
713         {
714             logf (LOG_FATAL, "isamd_open fail");
715             exit (1);
716         }
717     }
718     else
719     {
720         struct ISAMC_M_s isamc_m;
721         isamc = isc_open (bfs, FNAME_ISAMC, 1,
722                           key_isamc_m (common_resource, &isamc_m));
723         if (!isamc)
724         {
725             logf (LOG_FATAL, "isc_open fail");
726             exit (1);
727         }
728     }
729     kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
730     progressInfo.totalBytes = 0;
731     progressInfo.totalOffset = 0;
732     time (&progressInfo.startTime);
733     time (&progressInfo.lastTime);
734     for (i = 1; i<=nkeys; i++)
735     {
736         kf[i] = key_file_init (i, 32768);
737         kf[i]->readHandler = progressFunc;
738         kf[i]->readInfo = &progressInfo;
739         progressInfo.totalBytes += kf[i]->length;
740         progressInfo.totalOffset += kf[i]->buf_size;
741     }
742     hi = key_heap_init (nkeys, key_qsort_compare);
743     hi->dict = dict;
744     hi->isam = isam;
745     hi->isamc = isamc;
746     hi->isams = isams;
747     hi->isamh = isamh;
748     hi->isamd = isamd;
749     
750     for (i = 1; i<=nkeys; i++)
751         if ((r = key_file_read (kf[i], rbuf)))
752             key_heap_insert (hi, rbuf, r, kf[i]);
753     if (isamc)
754         heap_inpc (hi);
755     else if (isams)
756         heap_inps (hi);
757     else if (isam)
758         heap_inp (hi);
759     else if (isamh)
760         heap_inph (hi);
761     else if (isamd)
762         heap_inpd (hi);
763         
764     dict_close (dict);
765     if (isam)
766         is_close (isam);
767     if (isamc)
768         isc_close (isamc);
769     if (isams)
770         isams_close (isams);
771     if (isamh)
772         isamh_close (isamh);
773     if (isamd)
774         isamd_close (isamd);
775    
776     for (i = 1; i<=nkeys; i++)
777     {
778         getFnameTmp (rbuf, i);
779         unlink (rbuf);
780     }
781     logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
782     logf (LOG_LOG, "Distinct words .%7d", no_diffs);
783     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
784     logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
785     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
786     
787     xmalloc_trav("unfreed"); /* while hunting leaks */     
788 }
789
790
791
792 /*
793  * $Log: kinput.c,v $
794  * Revision 1.38  1999-08-18 08:38:04  heikki
795  * Memory leak hunting
796  *
797  * Revision 1.37  1999/07/14 13:21:34  heikki
798  * Added isam-d files. Compiles (almost) clean. Doesn't work at all
799  *
800  * Revision 1.36  1999/07/14 10:59:26  adam
801  * Changed functions isc_getmethod, isams_getmethod.
802  * Improved fatal error handling (such as missing EXPLAIN schema).
803  *
804  * Revision 1.35  1999/06/30 15:07:23  heikki
805  * Adding isamh stuff
806  *
807  * Revision 1.34  1999/05/26 07:49:13  adam
808  * C++ compilation.
809  *
810  * Revision 1.33  1999/05/15 14:36:38  adam
811  * Updated dictionary. Implemented "compression" of dictionary.
812  *
813  * Revision 1.32  1999/05/12 13:08:06  adam
814  * First version of ISAMS.
815  *
816  * Revision 1.31  1999/02/02 14:50:56  adam
817  * Updated WIN32 code specific sections. Changed header.
818  *
819  * Revision 1.30  1998/10/28 10:53:57  adam
820  * Added type cast to prevent warning.
821  *
822  * Revision 1.29  1998/06/11 15:41:39  adam
823  * Minor changes.
824  *
825  * Revision 1.28  1998/03/05 08:45:12  adam
826  * New result set model and modular ranking system. Moved towards
827  * descent server API. System information stored as "SGML" records.
828  *
829  * Revision 1.27  1998/02/17 10:32:52  adam
830  * Fixed bug: binary files weren't opened with flag b on NT.
831  *
832  * Revision 1.26  1998/01/29 13:39:13  adam
833  * Compress ISAM is default.
834  *
835  * Revision 1.25  1997/09/17 12:19:14  adam
836  * Zebra version corresponds to YAZ version 1.4.
837  * Changed Zebra server so that it doesn't depend on global common_resource.
838  *
839  * Revision 1.24  1997/09/09 13:38:07  adam
840  * Partial port to WIN95/NT.
841  *
842  * Revision 1.23  1997/09/04 13:57:39  adam
843  * Added O_BINARY for open calls.
844  *
845  * Revision 1.22  1997/02/12 20:39:45  adam
846  * Implemented options -f <n> that limits the log to the first <n>
847  * records.
848  * Changed some log messages also.
849  *
850  * Revision 1.21  1996/11/08 11:10:23  adam
851  * Buffers used during file match got bigger.
852  * Compressed ISAM support everywhere.
853  * Bug fixes regarding masking characters in queries.
854  * Redesigned Regexp-2 queries.
855  *
856  * Revision 1.20  1996/11/01 08:58:41  adam
857  * Interface to isamc system now includes update and delete.
858  *
859  * Revision 1.19  1996/10/29 14:09:46  adam
860  * Use of cisam system - enabled if setting isamc is 1.
861  *
862  * Revision 1.18  1996/06/04 10:18:59  adam
863  * Minor changes - removed include of ctype.h.
864  *
865  * Revision 1.17  1996/05/14  15:47:07  adam
866  * Cleanup of various buffer size entities.
867  *
868  * Revision 1.16  1996/04/09  10:05:20  adam
869  * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
870  *
871  * Revision 1.15  1996/03/21  14:50:09  adam
872  * File update uses modify-time instead of change-time.
873  *
874  * Revision 1.14  1996/02/07  14:06:37  adam
875  * Better progress report during register merge.
876  * New command: clean - removes temporary shadow files.
877  *
878  * Revision 1.13  1996/02/05  12:30:00  adam
879  * Logging reduced a bit.
880  * The remaining running time is estimated during register merge.
881  *
882  * Revision 1.12  1995/12/06  17:49:19  adam
883  * Uses dict_delete now.
884  *
885  * Revision 1.11  1995/12/06  16:06:43  adam
886  * Better diagnostics. Work on 'real' dictionary deletion.
887  *
888  * Revision 1.10  1995/12/06  12:41:22  adam
889  * New command 'stat' for the index program.
890  * Filenames can be read from stdin by specifying '-'.
891  * Bug fix/enhancement of the transformation from terms to regular
892  * expressons in the search engine.
893  *
894  * Revision 1.9  1995/10/10  12:24:39  adam
895  * Temporary sort files are compressed.
896  *
897  * Revision 1.8  1995/10/04  16:57:19  adam
898  * Key input and merge sort in one pass.
899  *
900  * Revision 1.7  1995/10/02  15:18:52  adam
901  * New member in recRetrieveCtrl: diagnostic.
902  *
903  * Revision 1.6  1995/09/29  15:51:56  adam
904  * First work on multi-way read.
905  *
906  * Revision 1.5  1995/09/29  14:01:43  adam
907  * Bug fixes.
908  *
909  * Revision 1.4  1995/09/28  14:22:57  adam
910  * Sort uses smaller temporary files.
911  *
912  * Revision 1.3  1995/09/06  16:11:17  adam
913  * Option: only one word key per file.
914  *
915  * Revision 1.2  1995/09/04  12:33:42  adam
916  * Various cleanup. YAZ util used instead.
917  *
918  * Revision 1.1  1995/09/04  09:10:37  adam
919  * More work on index add/del/update.
920  * Merge sort implemented.
921  * Initial work on z39 server.
922  *
923  */
924