2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-09-29 15:51:56 adam
8 * First work on multi-way read.
10 * Revision 1.5 1995/09/29 14:01:43 adam
13 * Revision 1.4 1995/09/28 14:22:57 adam
14 * Sort uses smaller temporary files.
16 * Revision 1.3 1995/09/06 16:11:17 adam
17 * Option: only one word key per file.
19 * Revision 1.2 1995/09/04 12:33:42 adam
20 * Various cleanup. YAZ util used instead.
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.
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
44 static int no_diffs = 0;
45 static int no_updates = 0;
46 static int no_insertions = 0;
47 static int no_iterations = 0;
49 static int read_one (FILE *inf, char *name, char *key)
55 if ((c=getc(inf)) == EOF)
59 for (i = 0; i<KEY_SIZE; i++)
60 ((char *)key)[i] = getc (inf);
65 static int inp (Dict dict, ISAM isam, const char *name)
69 char next_name[INP_NAME_MAX+1];
70 char cur_name[INP_NAME_MAX+1];
71 int key_buf_size = INP_BUF_START;
77 next_key = xmalloc (KEY_SIZE);
78 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
79 if (!(inf = fopen (name, "r")))
81 logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
84 more = read_one (inf, cur_name, key_buf);
85 while (more) /* EOF ? */
88 key_buf_ptr = KEY_SIZE;
91 if (!(more = read_one (inf, next_name, next_key)))
93 if (*next_name && strcmp (next_name, cur_name))
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)
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;
104 key_buf = new_key_buf;
108 nmemb = key_buf_ptr / KEY_SIZE;
109 assert (nmemb*KEY_SIZE == key_buf_ptr);
110 if ((info = dict_lookup (dict, cur_name)))
112 ISAM_P isam_p, isam_p2;
113 logf (LOG_DEBUG, "updating %s", cur_name);
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);
123 logf (LOG_DEBUG, "inserting %s", cur_name);
125 isam_p = is_merge (isam, 0, nmemb, key_buf);
126 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
128 memcpy (key_buf, next_key, KEY_SIZE);
129 strcpy (cur_name, next_name);
135 void key_input (const char *dict_fname, const char *isam_fname,
136 const char *key_fname, int cache)
141 dict = dict_open (dict_fname, cache, 1);
144 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
147 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
150 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
153 inp (dict, isam, key_fname);
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);
172 void key_file_chunk_read (struct key_file *f)
176 sprintf (fname, TEMP_FNAME, f->no);
177 fd = open (fname, O_RDONLY);
180 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
183 if (lseek (fd, f->offset, SEEK_SET) == -1)
185 logf (LOG_FATAL|LOG_ERRNO, "lseek to %ld in file %s",
186 (long) f->offset, fname);
189 f->buf = xmalloc (f->chunk);
190 while (f->chunk - nr > 0)
192 r = read (fd, f->buf + nr, f->chunk - nr);
199 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
205 void key_file_chunk_discard (struct key_file *f)
211 int inp2 (Dict dict, ISAM isam, const char *name)
215 char next_name[INP_NAME_MAX+1];
216 char cur_name[INP_NAME_MAX+1];
217 int key_buf_size = INP_BUF_START;
223 next_key = xmalloc (KEY_SIZE);
224 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
225 if (!(inf = fopen (name, "r")))
227 logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
230 more = read_one (inf, cur_name, key_buf);
231 while (more) /* EOF ? */
234 key_buf_ptr = KEY_SIZE;
237 if (!(more = read_one (inf, next_name, next_key)))
239 if (*next_name && strcmp (next_name, cur_name))
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)
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;
250 key_buf = new_key_buf;
254 nmemb = key_buf_ptr / KEY_SIZE;
255 assert (nmemb*KEY_SIZE == key_buf_ptr);
256 if ((info = dict_lookup (dict, cur_name)))
258 ISAM_P isam_p, isam_p2;
259 logf (LOG_DEBUG, "updating %s", cur_name);
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);
269 logf (LOG_DEBUG, "inserting %s", cur_name);
271 isam_p = is_merge (isam, 0, nmemb, key_buf);
272 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
274 memcpy (key_buf, next_key, KEY_SIZE);
275 strcpy (cur_name, next_name);
281 void key_input2 (const char *dict_fname, const char *isam_fname,
282 int nkeys, int cache)
287 dict = dict_open (dict_fname, cache, 1);
290 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
293 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
296 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
299 inp2 (dict, isam, NULL);
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);