2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-12-06 17:49:19 adam
8 * Uses dict_delete now.
10 * Revision 1.11 1995/12/06 16:06:43 adam
11 * Better diagnostics. Work on 'real' dictionary deletion.
13 * Revision 1.10 1995/12/06 12:41:22 adam
14 * New command 'stat' for the index program.
15 * Filenames can be read from stdin by specifying '-'.
16 * Bug fix/enhancement of the transformation from terms to regular
17 * expressons in the search engine.
19 * Revision 1.9 1995/10/10 12:24:39 adam
20 * Temporary sort files are compressed.
22 * Revision 1.8 1995/10/04 16:57:19 adam
23 * Key input and merge sort in one pass.
25 * Revision 1.7 1995/10/02 15:18:52 adam
26 * New member in recRetrieveCtrl: diagnostic.
28 * Revision 1.6 1995/09/29 15:51:56 adam
29 * First work on multi-way read.
31 * Revision 1.5 1995/09/29 14:01:43 adam
34 * Revision 1.4 1995/09/28 14:22:57 adam
35 * Sort uses smaller temporary files.
37 * Revision 1.3 1995/09/06 16:11:17 adam
38 * Option: only one word key per file.
40 * Revision 1.2 1995/09/04 12:33:42 adam
41 * Various cleanup. YAZ util used instead.
43 * Revision 1.1 1995/09/04 09:10:37 adam
44 * More work on index add/del/update.
45 * Merge sort implemented.
46 * Initial work on z39 server.
60 #define KEY_SIZE (1+sizeof(struct it_key))
61 #define INP_NAME_MAX 8192
62 #define INP_BUF_START 60000
63 #define INP_BUF_ADD 400000
65 static int no_diffs = 0;
66 static int no_updates = 0;
67 static int no_deletions = 0;
68 static int no_insertions = 0;
69 static int no_iterations = 0;
73 off_t offset; /* file offset */
74 unsigned char *buf; /* buffer block */
75 size_t buf_size; /* number of read bytes in block */
76 size_t chunk; /* number of bytes allocated */
77 size_t buf_ptr; /* current position in buffer */
78 char *prev_name; /* last word read */
79 int sysno; /* last sysno */
80 int seqno; /* last seqno */
83 void key_file_chunk_read (struct key_file *f)
87 sprintf (fname, TEMP_FNAME, f->no);
88 fd = open (fname, O_RDONLY);
91 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
94 logf (LOG_LOG, "reading chunk from %s", fname);
95 if (lseek (fd, f->offset, SEEK_SET) == -1)
97 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
100 while (f->chunk - nr > 0)
102 r = read (fd, f->buf + nr, f->chunk - nr);
109 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
117 struct key_file *key_file_init (int no, int chunk)
121 f = xmalloc (sizeof(*f));
127 f->buf = xmalloc (f->chunk);
128 f->prev_name = xmalloc (256);
129 *f->prev_name = '\0';
130 key_file_chunk_read (f);
134 int key_file_getc (struct key_file *f)
136 if (f->buf_ptr < f->buf_size)
137 return f->buf[(f->buf_ptr)++];
138 if (f->buf_size < f->chunk)
140 f->offset += f->buf_size;
141 key_file_chunk_read (f);
142 if (f->buf_ptr < f->buf_size)
143 return f->buf[(f->buf_ptr)++];
148 int key_file_decode (struct key_file *f)
152 c = key_file_getc (f);
159 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
162 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
163 d = (d << 8) + (key_file_getc (f) & 0xff);
166 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
167 d = (d << 8) + (key_file_getc (f) & 0xff);
168 d = (d << 8) + (key_file_getc (f) & 0xff);
174 int key_file_read (struct key_file *f, char *key)
179 c = key_file_getc (f);
182 strcpy (key, f->prev_name);
191 while ((key[i++] = key_file_getc (f)))
193 strcpy (f->prev_name, key);
196 d = key_file_decode (f);
199 itkey.sysno = d + f->sysno;
202 f->sysno = itkey.sysno;
205 d = key_file_decode (f);
206 itkey.seqno = d + f->seqno;
207 f->seqno = itkey.seqno;
208 memcpy (key + i, &itkey, sizeof(struct it_key));
209 return i + sizeof (struct it_key);
214 struct key_file **file;
219 int (*cmp)(const void *p1, const void *p2);
222 struct heap_info *key_heap_init (int nkeys,
223 int (*cmp)(const void *p1, const void *p2))
225 struct heap_info *hi;
228 hi = xmalloc (sizeof(*hi));
229 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
230 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
232 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
234 for (i = 0; i<= nkeys; i++)
237 hi->info.buf[i] = xmalloc (512);
242 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
247 hi->ptr[i1] = hi->ptr[i2];
252 static void key_heap_delete (struct heap_info *hi)
254 int cur = 1, child = 2;
256 assert (hi->heapnum > 0);
258 key_heap_swap (hi, 1, hi->heapnum);
260 while (child <= hi->heapnum) {
261 if (child < hi->heapnum &&
262 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
263 &hi->info.buf[hi->ptr[child+1]]) > 0)
265 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
266 &hi->info.buf[hi->ptr[child]]) > 0)
268 key_heap_swap (hi, cur, child);
277 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
282 cur = ++(hi->heapnum);
283 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
284 hi->info.file[hi->ptr[cur]] = kf;
287 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
288 &hi->info.buf[hi->ptr[cur]]) > 0)
290 key_heap_swap (hi, cur, parent);
296 static int heap_read_one (struct heap_info *hi, char *name, char *key)
305 strcpy (name, hi->info.buf[n]);
306 kf = hi->info.file[n];
308 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
309 key_heap_delete (hi);
310 if ((r = key_file_read (kf, rbuf)))
311 key_heap_insert (hi, rbuf, r, kf);
316 int heap_inp (Dict dict, ISAM isam, struct heap_info *hi)
319 char next_name[INP_NAME_MAX+1];
320 char cur_name[INP_NAME_MAX+1];
321 int key_buf_size = INP_BUF_START;
327 next_key = xmalloc (KEY_SIZE);
328 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
329 more = heap_read_one (hi, cur_name, key_buf);
330 while (more) /* EOF ? */
333 key_buf_ptr = KEY_SIZE;
336 if (!(more = heap_read_one (hi, next_name, next_key)))
338 if (*next_name && strcmp (next_name, cur_name))
340 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
341 key_buf_ptr += KEY_SIZE;
342 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
345 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
346 memcpy (new_key_buf, key_buf, key_buf_size);
347 key_buf_size += INP_BUF_ADD;
349 key_buf = new_key_buf;
353 nmemb = key_buf_ptr / KEY_SIZE;
354 assert (nmemb*KEY_SIZE == key_buf_ptr);
355 if ((info = dict_lookup (dict, cur_name)))
357 ISAM_P isam_p, isam_p2;
358 logf (LOG_DEBUG, "updating %s", cur_name);
359 memcpy (&isam_p, info+1, sizeof(ISAM_P));
360 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
364 if (!dict_delete (dict, cur_name))
370 if (isam_p2 != isam_p)
371 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
377 logf (LOG_DEBUG, "inserting %s", cur_name);
379 isam_p = is_merge (isam, 0, nmemb, key_buf);
380 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
382 memcpy (key_buf, next_key, KEY_SIZE);
383 strcpy (cur_name, next_name);
388 void key_input (const char *dict_fname, const char *isam_fname,
389 int nkeys, int cache)
394 struct key_file **kf;
397 struct heap_info *hi;
399 dict = dict_open (dict_fname, cache, 1);
402 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
405 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
408 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
412 kf = xmalloc ((1+nkeys) * sizeof(*kf));
413 for (i = 1; i<=nkeys; i++)
414 kf[i] = key_file_init (i, 32768);
416 hi = key_heap_init (nkeys, key_qsort_compare);
417 for (i = 1; i<=nkeys; i++)
418 if ((r = key_file_read (kf[i], rbuf)))
419 key_heap_insert (hi, rbuf, r, kf[i]);
420 heap_inp (dict, isam, hi);
425 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
426 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
427 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
428 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
429 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);