First work on multi-way read.
[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.6  1995-09-29 15:51:56  adam
8  * First work on multi-way read.
9  *
10  * Revision 1.5  1995/09/29  14:01:43  adam
11  * Bug fixes.
12  *
13  * Revision 1.4  1995/09/28  14:22:57  adam
14  * Sort uses smaller temporary files.
15  *
16  * Revision 1.3  1995/09/06  16:11:17  adam
17  * Option: only one word key per file.
18  *
19  * Revision 1.2  1995/09/04  12:33:42  adam
20  * Various cleanup. YAZ util used instead.
21  *
22  * Revision 1.1  1995/09/04  09:10:37  adam
23  * More work on index add/del/update.
24  * Merge sort implemented.
25  * Initial work on z39 server.
26  *
27  */
28
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <assert.h>
36
37 #include "index.h"
38
39 #define KEY_SIZE (1+sizeof(struct it_key))
40 #define INP_NAME_MAX 8192
41 #define INP_BUF_START 60000
42 #define INP_BUF_ADD  400000
43
44 static int no_diffs   = 0;
45 static int no_updates = 0;
46 static int no_insertions = 0;
47 static int no_iterations = 0;
48
49 static int read_one (FILE *inf, char *name, char *key)
50 {
51     int c;
52     int i = 0;
53     do
54     {
55         if ((c=getc(inf)) == EOF)
56             return 0;
57         name[i++] = c;
58     } while (c);
59     for (i = 0; i<KEY_SIZE; i++)
60         ((char *)key)[i] = getc (inf);
61     ++no_iterations;
62     return 1;
63 }
64
65 static int inp (Dict dict, ISAM isam, const char *name)
66 {
67     FILE *inf;
68     char *info;
69     char next_name[INP_NAME_MAX+1];
70     char cur_name[INP_NAME_MAX+1];
71     int key_buf_size = INP_BUF_START;
72     int key_buf_ptr;
73     char *next_key;
74     char *key_buf;
75     int more;
76     
77     next_key = xmalloc (KEY_SIZE);
78     key_buf = xmalloc (key_buf_size * (KEY_SIZE));
79     if (!(inf = fopen (name, "r")))
80     {
81         logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
82         exit (1);
83     }
84     more = read_one (inf, cur_name, key_buf);
85     while (more)                   /* EOF ? */
86     {
87         int nmemb;
88         key_buf_ptr = KEY_SIZE;
89         while (1)
90         {
91             if (!(more = read_one (inf, next_name, next_key)))
92                 break;
93             if (*next_name && strcmp (next_name, cur_name))
94                 break;
95             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
96             key_buf_ptr += KEY_SIZE;
97             if (key_buf_ptr+KEY_SIZE >= key_buf_size)
98             {
99                 char *new_key_buf;
100                 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
101                 memcpy (new_key_buf, key_buf, key_buf_size);
102                 key_buf_size += INP_BUF_ADD;
103                 xfree (key_buf);
104                 key_buf = new_key_buf;
105             }
106         }
107         no_diffs++;
108         nmemb = key_buf_ptr / KEY_SIZE;
109         assert (nmemb*KEY_SIZE == key_buf_ptr);
110         if ((info = dict_lookup (dict, cur_name)))
111         {
112             ISAM_P isam_p, isam_p2;
113             logf (LOG_DEBUG, "updating %s", cur_name);
114             no_updates++;
115             memcpy (&isam_p, info+1, sizeof(ISAM_P));
116             isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
117             if (isam_p2 != isam_p)
118                 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
119         }
120         else
121         {
122             ISAM_P isam_p;
123             logf (LOG_DEBUG, "inserting %s", cur_name);
124             no_insertions++;
125             isam_p = is_merge (isam, 0, nmemb, key_buf);
126             dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
127         }
128         memcpy (key_buf, next_key, KEY_SIZE);
129         strcpy (cur_name, next_name);
130     }
131     fclose (inf);
132     return 0;
133 }
134
135 void key_input (const char *dict_fname, const char *isam_fname,
136                 const char *key_fname, int cache)
137 {
138     Dict dict;
139     ISAM isam;
140
141     dict = dict_open (dict_fname, cache, 1);
142     if (!dict)
143     {
144         logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
145         exit (1);
146     }
147     isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
148     if (!isam)
149     {
150         logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
151         exit (1);
152     }
153     inp (dict, isam, key_fname);
154     dict_close (dict);
155     is_close (isam);
156     logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
157     logf (LOG_LOG, "Distinct words .%7d", no_diffs);
158     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
159     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
160 }
161
162
163 struct key_file {
164     int   no;
165     off_t offset;
166     char *buf;
167     size_t buf_size;
168     size_t chunk;
169     size_t buf_ptr;
170 };
171
172 void key_file_chunk_read (struct key_file *f)
173 {
174     int nr = 0, r, fd;
175     char  fname[256];
176     sprintf (fname, TEMP_FNAME, f->no);
177     fd = open (fname, O_RDONLY);
178     if (fd == -1)
179     {
180         logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
181         exit (1);
182     }
183     if (lseek (fd, f->offset, SEEK_SET) == -1)
184     {
185         logf (LOG_FATAL|LOG_ERRNO, "lseek to %ld in file %s",
186               (long) f->offset, fname);
187         exit (1);
188     }
189     f->buf = xmalloc (f->chunk);
190     while (f->chunk - nr > 0)
191     {
192         r = read (fd, f->buf + nr, f->chunk - nr);
193         if (r <= 0)
194             break;
195         nr += r;
196     }
197     if (r == -1)
198     {
199         logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
200         exit (1);
201     }
202     f->buf_size = nr;
203 }
204
205 void key_file_chunk_discard (struct key_file *f)
206 {
207
208
209 }
210
211 int inp2 (Dict dict, ISAM isam, const char *name)
212 {
213     FILE *inf;
214     char *info;
215     char next_name[INP_NAME_MAX+1];
216     char cur_name[INP_NAME_MAX+1];
217     int key_buf_size = INP_BUF_START;
218     int key_buf_ptr;
219     char *next_key;
220     char *key_buf;
221     int more;
222     
223     next_key = xmalloc (KEY_SIZE);
224     key_buf = xmalloc (key_buf_size * (KEY_SIZE));
225     if (!(inf = fopen (name, "r")))
226     {
227         logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
228         exit (1);
229     }
230     more = read_one (inf, cur_name, key_buf);
231     while (more)                   /* EOF ? */
232     {
233         int nmemb;
234         key_buf_ptr = KEY_SIZE;
235         while (1)
236         {
237             if (!(more = read_one (inf, next_name, next_key)))
238                 break;
239             if (*next_name && strcmp (next_name, cur_name))
240                 break;
241             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
242             key_buf_ptr += KEY_SIZE;
243             if (key_buf_ptr+KEY_SIZE >= key_buf_size)
244             {
245                 char *new_key_buf;
246                 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
247                 memcpy (new_key_buf, key_buf, key_buf_size);
248                 key_buf_size += INP_BUF_ADD;
249                 xfree (key_buf);
250                 key_buf = new_key_buf;
251             }
252         }
253         no_diffs++;
254         nmemb = key_buf_ptr / KEY_SIZE;
255         assert (nmemb*KEY_SIZE == key_buf_ptr);
256         if ((info = dict_lookup (dict, cur_name)))
257         {
258             ISAM_P isam_p, isam_p2;
259             logf (LOG_DEBUG, "updating %s", cur_name);
260             no_updates++;
261             memcpy (&isam_p, info+1, sizeof(ISAM_P));
262             isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
263             if (isam_p2 != isam_p)
264                 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
265         }
266         else
267         {
268             ISAM_P isam_p;
269             logf (LOG_DEBUG, "inserting %s", cur_name);
270             no_insertions++;
271             isam_p = is_merge (isam, 0, nmemb, key_buf);
272             dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
273         }
274         memcpy (key_buf, next_key, KEY_SIZE);
275         strcpy (cur_name, next_name);
276     }
277     fclose (inf);
278     return 0;
279 }
280
281 void key_input2 (const char *dict_fname, const char *isam_fname,
282                  int nkeys, int cache)
283 {
284     Dict dict;
285     ISAM isam;
286
287     dict = dict_open (dict_fname, cache, 1);
288     if (!dict)
289     {
290         logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
291         exit (1);
292     }
293     isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
294     if (!isam)
295     {
296         logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
297         exit (1);
298     }
299     inp2 (dict, isam, NULL);
300     dict_close (dict);
301     is_close (isam);
302     logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
303     logf (LOG_LOG, "Distinct words .%7d", no_diffs);
304     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
305     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
306 }
307
308