2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1996-05-14 15:47:07 adam
8 * Cleanup of various buffer size entities.
10 * Revision 1.16 1996/04/09 10:05:20 adam
11 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
13 * Revision 1.15 1996/03/21 14:50:09 adam
14 * File update uses modify-time instead of change-time.
16 * Revision 1.14 1996/02/07 14:06:37 adam
17 * Better progress report during register merge.
18 * New command: clean - removes temporary shadow files.
20 * Revision 1.13 1996/02/05 12:30:00 adam
21 * Logging reduced a bit.
22 * The remaining running time is estimated during register merge.
24 * Revision 1.12 1995/12/06 17:49:19 adam
25 * Uses dict_delete now.
27 * Revision 1.11 1995/12/06 16:06:43 adam
28 * Better diagnostics. Work on 'real' dictionary deletion.
30 * Revision 1.10 1995/12/06 12:41:22 adam
31 * New command 'stat' for the index program.
32 * Filenames can be read from stdin by specifying '-'.
33 * Bug fix/enhancement of the transformation from terms to regular
34 * expressons in the search engine.
36 * Revision 1.9 1995/10/10 12:24:39 adam
37 * Temporary sort files are compressed.
39 * Revision 1.8 1995/10/04 16:57:19 adam
40 * Key input and merge sort in one pass.
42 * Revision 1.7 1995/10/02 15:18:52 adam
43 * New member in recRetrieveCtrl: diagnostic.
45 * Revision 1.6 1995/09/29 15:51:56 adam
46 * First work on multi-way read.
48 * Revision 1.5 1995/09/29 14:01:43 adam
51 * Revision 1.4 1995/09/28 14:22:57 adam
52 * Sort uses smaller temporary files.
54 * Revision 1.3 1995/09/06 16:11:17 adam
55 * Option: only one word key per file.
57 * Revision 1.2 1995/09/04 12:33:42 adam
58 * Various cleanup. YAZ util used instead.
60 * Revision 1.1 1995/09/04 09:10:37 adam
61 * More work on index add/del/update.
62 * Merge sort implemented.
63 * Initial work on z39 server.
77 #define KEY_SIZE (1+sizeof(struct it_key))
78 #define INP_NAME_MAX 768
79 #define INP_BUF_START 60000
80 #define INP_BUF_ADD 400000
82 static int no_diffs = 0;
83 static int no_updates = 0;
84 static int no_deletions = 0;
85 static int no_insertions = 0;
86 static int no_iterations = 0;
90 off_t offset; /* file offset */
91 unsigned char *buf; /* buffer block */
92 size_t buf_size; /* number of read bytes in block */
93 size_t chunk; /* number of bytes allocated */
94 size_t buf_ptr; /* current position in buffer */
95 char *prev_name; /* last word read */
96 int sysno; /* last sysno */
97 int seqno; /* last seqno */
98 off_t length; /* length of file */
99 /* handler invoked in each read */
100 void (*readHandler)(struct key_file *keyp, void *rinfo);
104 void getFnameTmp (char *fname, int no)
106 sprintf (fname, TEMP_FNAME, no);
109 void key_file_chunk_read (struct key_file *f)
113 getFnameTmp (fname, f->no);
114 fd = open (fname, O_RDONLY);
117 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
122 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
124 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
128 if (lseek (fd, f->offset, SEEK_SET) == -1)
130 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
133 while (f->chunk - nr > 0)
135 r = read (fd, f->buf + nr, f->chunk - nr);
142 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
148 (*f->readHandler)(f, f->readInfo);
152 struct key_file *key_file_init (int no, int chunk)
156 f = xmalloc (sizeof(*f));
163 f->readHandler = NULL;
164 f->buf = xmalloc (f->chunk);
165 f->prev_name = xmalloc (INP_NAME_MAX);
166 *f->prev_name = '\0';
167 key_file_chunk_read (f);
171 int key_file_getc (struct key_file *f)
173 if (f->buf_ptr < f->buf_size)
174 return f->buf[(f->buf_ptr)++];
175 if (f->buf_size < f->chunk)
177 f->offset += f->buf_size;
178 key_file_chunk_read (f);
179 if (f->buf_ptr < f->buf_size)
180 return f->buf[(f->buf_ptr)++];
185 int key_file_decode (struct key_file *f)
189 c = key_file_getc (f);
196 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
199 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
200 d = (d << 8) + (key_file_getc (f) & 0xff);
203 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
204 d = (d << 8) + (key_file_getc (f) & 0xff);
205 d = (d << 8) + (key_file_getc (f) & 0xff);
211 int key_file_read (struct key_file *f, char *key)
216 c = key_file_getc (f);
219 strcpy (key, f->prev_name);
228 while ((key[i++] = key_file_getc (f)))
230 strcpy (f->prev_name, key);
233 d = key_file_decode (f);
236 itkey.sysno = d + f->sysno;
239 f->sysno = itkey.sysno;
242 d = key_file_decode (f);
243 itkey.seqno = d + f->seqno;
244 f->seqno = itkey.seqno;
245 memcpy (key + i, &itkey, sizeof(struct it_key));
246 return i + sizeof (struct it_key);
251 struct key_file **file;
256 int (*cmp)(const void *p1, const void *p2);
259 struct heap_info *key_heap_init (int nkeys,
260 int (*cmp)(const void *p1, const void *p2))
262 struct heap_info *hi;
265 hi = xmalloc (sizeof(*hi));
266 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
267 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
269 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
271 for (i = 0; i<= nkeys; i++)
274 hi->info.buf[i] = xmalloc (INP_NAME_MAX);
279 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
284 hi->ptr[i1] = hi->ptr[i2];
289 static void key_heap_delete (struct heap_info *hi)
291 int cur = 1, child = 2;
293 assert (hi->heapnum > 0);
295 key_heap_swap (hi, 1, hi->heapnum);
297 while (child <= hi->heapnum) {
298 if (child < hi->heapnum &&
299 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
300 &hi->info.buf[hi->ptr[child+1]]) > 0)
302 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
303 &hi->info.buf[hi->ptr[child]]) > 0)
305 key_heap_swap (hi, cur, child);
314 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
319 cur = ++(hi->heapnum);
320 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
321 hi->info.file[hi->ptr[cur]] = kf;
324 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
325 &hi->info.buf[hi->ptr[cur]]) > 0)
327 key_heap_swap (hi, cur, parent);
333 static int heap_read_one (struct heap_info *hi, char *name, char *key)
336 char rbuf[INP_NAME_MAX];
342 strcpy (name, hi->info.buf[n]);
343 kf = hi->info.file[n];
345 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
346 key_heap_delete (hi);
347 if ((r = key_file_read (kf, rbuf)))
348 key_heap_insert (hi, rbuf, r, kf);
353 int heap_inp (Dict dict, ISAM isam, struct heap_info *hi)
356 char next_name[INP_NAME_MAX];
357 char cur_name[INP_NAME_MAX];
358 int key_buf_size = INP_BUF_START;
364 next_key = xmalloc (KEY_SIZE);
365 key_buf = xmalloc (key_buf_size);
366 more = heap_read_one (hi, cur_name, key_buf);
367 while (more) /* EOF ? */
370 key_buf_ptr = KEY_SIZE;
373 if (!(more = heap_read_one (hi, next_name, next_key)))
375 if (*next_name && strcmp (next_name, cur_name))
377 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
378 key_buf_ptr += KEY_SIZE;
379 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
382 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
383 memcpy (new_key_buf, key_buf, key_buf_size);
384 key_buf_size += INP_BUF_ADD;
386 key_buf = new_key_buf;
390 nmemb = key_buf_ptr / KEY_SIZE;
391 assert (nmemb*KEY_SIZE == key_buf_ptr);
392 if ((info = dict_lookup (dict, cur_name)))
394 ISAM_P isam_p, isam_p2;
395 logf (LOG_DEBUG, "updating %s", cur_name);
396 memcpy (&isam_p, info+1, sizeof(ISAM_P));
397 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
401 if (!dict_delete (dict, cur_name))
407 if (isam_p2 != isam_p)
408 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
414 logf (LOG_DEBUG, "inserting %s", cur_name);
416 isam_p = is_merge (isam, 0, nmemb, key_buf);
417 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
419 memcpy (key_buf, next_key, KEY_SIZE);
420 strcpy (cur_name, next_name);
425 struct progressInfo {
432 void progressFunc (struct key_file *keyp, void *info)
434 struct progressInfo *p = info;
435 time_t now, remaining;
437 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
441 if (now >= p->lastTime+10)
444 remaining = (now - p->startTime)*
445 ((double) p->totalBytes/p->totalOffset - 1.0);
446 if (remaining <= 130)
447 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
448 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
450 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
451 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
453 p->totalOffset += keyp->buf_size;
456 void key_input (const char *dict_fname, const char *isam_fname,
457 int nkeys, int cache)
462 struct key_file **kf;
465 struct heap_info *hi;
466 struct progressInfo progressInfo;
474 getFnameTmp (fname, nkeys+1);
475 if (access (fname, R_OK) == -1)
482 dict = dict_open (dict_fname, cache, 1);
485 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
488 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
491 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
495 kf = xmalloc ((1+nkeys) * sizeof(*kf));
496 progressInfo.totalBytes = 0;
497 progressInfo.totalOffset = 0;
498 time (&progressInfo.startTime);
499 time (&progressInfo.lastTime);
500 for (i = 1; i<=nkeys; i++)
502 kf[i] = key_file_init (i, 32768);
503 kf[i]->readHandler = progressFunc;
504 kf[i]->readInfo = &progressInfo;
505 progressInfo.totalBytes += kf[i]->length;
506 progressInfo.totalOffset += kf[i]->buf_size;
508 hi = key_heap_init (nkeys, key_qsort_compare);
509 for (i = 1; i<=nkeys; i++)
510 if ((r = key_file_read (kf[i], rbuf)))
511 key_heap_insert (hi, rbuf, r, kf[i]);
512 heap_inp (dict, isam, hi);
517 for (i = 1; i<=nkeys; i++)
519 getFnameTmp (rbuf, i);
522 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
523 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
524 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
525 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
526 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);