2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-10-04 16:57:19 adam
8 * Key input and merge sort in one pass.
10 * Revision 1.7 1995/10/02 15:18:52 adam
11 * New member in recRetrieveCtrl: diagnostic.
13 * Revision 1.6 1995/09/29 15:51:56 adam
14 * First work on multi-way read.
16 * Revision 1.5 1995/09/29 14:01:43 adam
19 * Revision 1.4 1995/09/28 14:22:57 adam
20 * Sort uses smaller temporary files.
22 * Revision 1.3 1995/09/06 16:11:17 adam
23 * Option: only one word key per file.
25 * Revision 1.2 1995/09/04 12:33:42 adam
26 * Various cleanup. YAZ util used instead.
28 * Revision 1.1 1995/09/04 09:10:37 adam
29 * More work on index add/del/update.
30 * Merge sort implemented.
31 * Initial work on z39 server.
45 #define KEY_SIZE (1+sizeof(struct it_key))
46 #define INP_NAME_MAX 8192
47 #define INP_BUF_START 60000
48 #define INP_BUF_ADD 400000
50 static int no_diffs = 0;
51 static int no_updates = 0;
52 static int no_insertions = 0;
53 static int no_iterations = 0;
55 static int read_one (FILE *inf, char *name, char *key)
61 if ((c=getc(inf)) == EOF)
65 for (i = 0; i<KEY_SIZE; i++)
66 ((char *)key)[i] = getc (inf);
71 static int inp (Dict dict, ISAM isam, const char *name)
75 char next_name[INP_NAME_MAX+1];
76 char cur_name[INP_NAME_MAX+1];
77 int key_buf_size = INP_BUF_START;
83 next_key = xmalloc (KEY_SIZE);
84 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
85 if (!(inf = fopen (name, "r")))
87 logf (LOG_FATAL|LOG_ERRNO, "cannot open `%s'", name);
90 more = read_one (inf, cur_name, key_buf);
91 while (more) /* EOF ? */
94 key_buf_ptr = KEY_SIZE;
97 if (!(more = read_one (inf, next_name, next_key)))
99 if (*next_name && strcmp (next_name, cur_name))
101 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
102 key_buf_ptr += KEY_SIZE;
103 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
106 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
107 memcpy (new_key_buf, key_buf, key_buf_size);
108 key_buf_size += INP_BUF_ADD;
110 key_buf = new_key_buf;
114 nmemb = key_buf_ptr / KEY_SIZE;
115 assert (nmemb*KEY_SIZE == key_buf_ptr);
116 if ((info = dict_lookup (dict, cur_name)))
118 ISAM_P isam_p, isam_p2;
119 logf (LOG_DEBUG, "updating %s", cur_name);
121 memcpy (&isam_p, info+1, sizeof(ISAM_P));
122 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
123 if (isam_p2 != isam_p)
124 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
129 logf (LOG_DEBUG, "inserting %s", cur_name);
131 isam_p = is_merge (isam, 0, nmemb, key_buf);
132 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
134 memcpy (key_buf, next_key, KEY_SIZE);
135 strcpy (cur_name, next_name);
141 void key_input (const char *dict_fname, const char *isam_fname,
142 const char *key_fname, int cache)
147 dict = dict_open (dict_fname, cache, 1);
150 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
153 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
156 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
159 inp (dict, isam, key_fname);
162 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
163 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
164 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
165 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
170 int no; /* file no */
171 off_t offset; /* file offset */
172 unsigned char *buf; /* buffer block */
173 size_t buf_size; /* number of read bytes in block */
174 size_t chunk; /* number of bytes allocated */
175 size_t buf_ptr; /* current position in buffer */
176 char *prev_name; /* last word read */
179 void key_file_chunk_read (struct key_file *f)
183 sprintf (fname, TEMP_FNAME, f->no);
184 fd = open (fname, O_RDONLY);
187 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
190 logf (LOG_LOG, "reading chunk from %s", fname);
191 if (lseek (fd, f->offset, SEEK_SET) == -1)
193 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
196 while (f->chunk - nr > 0)
198 r = read (fd, f->buf + nr, f->chunk - nr);
205 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
213 struct key_file *key_file_init (int no, int chunk)
217 f = xmalloc (sizeof(*f));
221 f->buf = xmalloc (f->chunk);
222 f->prev_name = xmalloc (256);
223 *f->prev_name = '\0';
224 key_file_chunk_read (f);
228 int key_file_getc (struct key_file *f)
230 if (f->buf_ptr < f->buf_size)
231 return f->buf[(f->buf_ptr)++];
232 if (f->buf_size < f->chunk)
234 f->offset += f->buf_size;
235 key_file_chunk_read (f);
236 if (f->buf_ptr < f->buf_size)
237 return f->buf[(f->buf_ptr)++];
242 int key_file_read (struct key_file *f, char *key)
246 c = key_file_getc (f);
249 strcpy (key, f->prev_name);
258 while ((key[i++] = key_file_getc (f)))
260 strcpy (f->prev_name, key);
262 for (j = KEY_SIZE; --j >= 0; )
263 key[i++] = key_file_getc (f);
269 struct key_file **file;
274 int (*cmp)(const void *p1, const void *p2);
277 struct heap_info *key_heap_init (int nkeys,
278 int (*cmp)(const void *p1, const void *p2))
280 struct heap_info *hi;
283 hi = xmalloc (sizeof(*hi));
284 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
285 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
287 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
289 for (i = 0; i<= nkeys; i++)
292 hi->info.buf[i] = xmalloc (512);
297 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
302 hi->ptr[i1] = hi->ptr[i2];
307 static void key_heap_delete (struct heap_info *hi)
309 int cur = 1, child = 2;
311 assert (hi->heapnum > 0);
314 key_heap_swap (hi, 1, hi->heapnum);
317 hi->ptr[1] = hi->ptr[hi->heapnum--];
319 while (child <= hi->heapnum) {
320 if (child < hi->heapnum &&
321 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
322 &hi->info.buf[hi->ptr[child+1]]) > 0)
324 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
325 &hi->info.buf[hi->ptr[child]]) > 0)
327 key_heap_swap (hi, cur, child);
336 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
341 cur = ++(hi->heapnum);
342 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
343 hi->info.file[hi->ptr[cur]] = kf;
346 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
347 &hi->info.buf[hi->ptr[cur]]) > 0)
349 key_heap_swap (hi, cur, parent);
355 static int heap_read_one (struct heap_info *hi, char *name, char *key)
364 strcpy (name, hi->info.buf[n]);
365 kf = hi->info.file[n];
367 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
368 key_heap_delete (hi);
369 if ((r = key_file_read (kf, rbuf)))
370 key_heap_insert (hi, rbuf, r, kf);
375 int heap_inp (Dict dict, ISAM isam, struct heap_info *hi)
378 char next_name[INP_NAME_MAX+1];
379 char cur_name[INP_NAME_MAX+1];
380 int key_buf_size = INP_BUF_START;
386 next_key = xmalloc (KEY_SIZE);
387 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
388 more = heap_read_one (hi, cur_name, key_buf);
389 while (more) /* EOF ? */
392 key_buf_ptr = KEY_SIZE;
395 if (!(more = heap_read_one (hi, next_name, next_key)))
397 if (*next_name && strcmp (next_name, cur_name))
399 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
400 key_buf_ptr += KEY_SIZE;
401 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
404 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
405 memcpy (new_key_buf, key_buf, key_buf_size);
406 key_buf_size += INP_BUF_ADD;
408 key_buf = new_key_buf;
412 nmemb = key_buf_ptr / KEY_SIZE;
413 assert (nmemb*KEY_SIZE == key_buf_ptr);
414 if ((info = dict_lookup (dict, cur_name)))
416 ISAM_P isam_p, isam_p2;
417 logf (LOG_DEBUG, "updating %s", cur_name);
419 memcpy (&isam_p, info+1, sizeof(ISAM_P));
420 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
421 if (isam_p2 != isam_p)
422 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
427 logf (LOG_DEBUG, "inserting %s", cur_name);
429 isam_p = is_merge (isam, 0, nmemb, key_buf);
430 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
432 memcpy (key_buf, next_key, KEY_SIZE);
433 strcpy (cur_name, next_name);
438 void key_input2 (const char *dict_fname, const char *isam_fname,
439 int nkeys, int cache)
443 struct key_file **kf;
446 struct heap_info *hi;
448 dict = dict_open (dict_fname, cache, 1);
451 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
454 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
457 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
461 kf = xmalloc ((1+nkeys) * sizeof(*kf));
462 for (i = 1; i<=nkeys; i++)
463 kf[i] = key_file_init (i, 32768);
465 hi = key_heap_init (nkeys, key_qsort_compare);
466 for (i = 1; i<=nkeys; i++)
467 if ((r = key_file_read (kf[i], rbuf)))
468 key_heap_insert (hi, rbuf, r, kf[i]);
469 heap_inp (dict, isam, hi);
474 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
475 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
476 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
477 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);