Bug fixes.
[idzebra-moved-to-github.git] / index / kinput.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: kinput.c,v $
7  * Revision 1.5  1995-09-29 14:01:43  adam
8  * Bug fixes.
9  *
10  * Revision 1.4  1995/09/28  14:22:57  adam
11  * Sort uses smaller temporary files.
12  *
13  * Revision 1.3  1995/09/06  16:11:17  adam
14  * Option: only one word key per file.
15  *
16  * Revision 1.2  1995/09/04  12:33:42  adam
17  * Various cleanup. YAZ util used instead.
18  *
19  * Revision 1.1  1995/09/04  09:10:37  adam
20  * More work on index add/del/update.
21  * Merge sort implemented.
22  * Initial work on z39 server.
23  *
24  */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <assert.h>
31
32 #include "index.h"
33
34 #define KEY_SIZE (1+sizeof(struct it_key))
35 #define INP_NAME_MAX 8192
36 #define INP_BUF_START 60000
37 #define INP_BUF_ADD  400000
38
39 static int no_diffs   = 0;
40 static int no_updates = 0;
41 static int no_insertions = 0;
42 static int no_iterations = 0;
43
44 static int read_one (FILE *inf, char *name, char *key)
45 {
46     int c;
47     int i = 0;
48     do
49     {
50         if ((c=getc(inf)) == EOF)
51             return 0;
52         name[i++] = c;
53     } while (c);
54     for (i = 0; i<KEY_SIZE; i++)
55         ((char *)key)[i] = getc (inf);
56     ++no_iterations;
57     return 1;
58 }
59
60 static int inp (Dict dict, ISAM isam, const char *name)
61 {
62     FILE *inf;
63     char *info;
64     char next_name[INP_NAME_MAX+1];
65     char cur_name[INP_NAME_MAX+1];
66     int key_buf_size = INP_BUF_START;
67     int key_buf_ptr;
68     char *next_key;
69     char *key_buf;
70     int more;
71     
72     next_key = xmalloc (KEY_SIZE);
73     key_buf = xmalloc (key_buf_size * (KEY_SIZE));
74     if (!(inf = fopen (name, "r")))
75     {
76         logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
77         exit (1);
78     }
79     more = read_one (inf, cur_name, key_buf);
80     while (more)                   /* EOF ? */
81     {
82         int nmemb;
83         key_buf_ptr = KEY_SIZE;
84         while (1)
85         {
86             if (!(more = read_one (inf, next_name, next_key)))
87                 break;
88             if (*next_name && strcmp (next_name, cur_name))
89                 break;
90             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
91             key_buf_ptr += KEY_SIZE;
92             if (key_buf_ptr+KEY_SIZE >= key_buf_size)
93             {
94                 char *new_key_buf;
95                 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
96                 memcpy (new_key_buf, key_buf, key_buf_size);
97                 key_buf_size += INP_BUF_ADD;
98                 xfree (key_buf);
99                 key_buf = new_key_buf;
100             }
101         }
102         no_diffs++;
103         nmemb = key_buf_ptr / KEY_SIZE;
104         assert (nmemb*KEY_SIZE == key_buf_ptr);
105         if ((info = dict_lookup (dict, cur_name)))
106         {
107             ISAM_P isam_p, isam_p2;
108             logf (LOG_DEBUG, "updating %s", cur_name);
109             no_updates++;
110             memcpy (&isam_p, info+1, sizeof(ISAM_P));
111             isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
112             if (isam_p2 != isam_p)
113                 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
114         }
115         else
116         {
117             ISAM_P isam_p;
118             logf (LOG_DEBUG, "inserting %s", cur_name);
119             no_insertions++;
120             isam_p = is_merge (isam, 0, nmemb, key_buf);
121             dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
122         }
123         memcpy (key_buf, next_key, KEY_SIZE);
124         strcpy (cur_name, next_name);
125     }
126     fclose (inf);
127     return 0;
128 }
129
130 void key_input (const char *dict_fname, const char *isam_fname,
131                 const char *key_fname, int cache)
132 {
133     Dict dict;
134     ISAM isam;
135
136     dict = dict_open (dict_fname, cache, 1);
137     if (!dict)
138     {
139         logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
140         exit (1);
141     }
142     isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
143     if (!isam)
144     {
145         logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
146         exit (1);
147     }
148     inp (dict, isam, key_fname);
149     dict_close (dict);
150     is_close (isam);
151     logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
152     logf (LOG_LOG, "Distinct words .%7d", no_diffs);
153     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
154     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
155 }