2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1996-06-04 10:18:59 adam
8 * Minor changes - removed include of ctype.h.
10 * Revision 1.17 1996/05/14 15:47:07 adam
11 * Cleanup of various buffer size entities.
13 * Revision 1.16 1996/04/09 10:05:20 adam
14 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
16 * Revision 1.15 1996/03/21 14:50:09 adam
17 * File update uses modify-time instead of change-time.
19 * Revision 1.14 1996/02/07 14:06:37 adam
20 * Better progress report during register merge.
21 * New command: clean - removes temporary shadow files.
23 * Revision 1.13 1996/02/05 12:30:00 adam
24 * Logging reduced a bit.
25 * The remaining running time is estimated during register merge.
27 * Revision 1.12 1995/12/06 17:49:19 adam
28 * Uses dict_delete now.
30 * Revision 1.11 1995/12/06 16:06:43 adam
31 * Better diagnostics. Work on 'real' dictionary deletion.
33 * Revision 1.10 1995/12/06 12:41:22 adam
34 * New command 'stat' for the index program.
35 * Filenames can be read from stdin by specifying '-'.
36 * Bug fix/enhancement of the transformation from terms to regular
37 * expressons in the search engine.
39 * Revision 1.9 1995/10/10 12:24:39 adam
40 * Temporary sort files are compressed.
42 * Revision 1.8 1995/10/04 16:57:19 adam
43 * Key input and merge sort in one pass.
45 * Revision 1.7 1995/10/02 15:18:52 adam
46 * New member in recRetrieveCtrl: diagnostic.
48 * Revision 1.6 1995/09/29 15:51:56 adam
49 * First work on multi-way read.
51 * Revision 1.5 1995/09/29 14:01:43 adam
54 * Revision 1.4 1995/09/28 14:22:57 adam
55 * Sort uses smaller temporary files.
57 * Revision 1.3 1995/09/06 16:11:17 adam
58 * Option: only one word key per file.
60 * Revision 1.2 1995/09/04 12:33:42 adam
61 * Various cleanup. YAZ util used instead.
63 * Revision 1.1 1995/09/04 09:10:37 adam
64 * More work on index add/del/update.
65 * Merge sort implemented.
66 * Initial work on z39 server.
79 #define KEY_SIZE (1+sizeof(struct it_key))
80 #define INP_NAME_MAX 768
81 #define INP_BUF_START 60000
82 #define INP_BUF_ADD 400000
84 static int no_diffs = 0;
85 static int no_updates = 0;
86 static int no_deletions = 0;
87 static int no_insertions = 0;
88 static int no_iterations = 0;
92 off_t offset; /* file offset */
93 unsigned char *buf; /* buffer block */
94 size_t buf_size; /* number of read bytes in block */
95 size_t chunk; /* number of bytes allocated */
96 size_t buf_ptr; /* current position in buffer */
97 char *prev_name; /* last word read */
98 int sysno; /* last sysno */
99 int seqno; /* last seqno */
100 off_t length; /* length of file */
101 /* handler invoked in each read */
102 void (*readHandler)(struct key_file *keyp, void *rinfo);
106 void getFnameTmp (char *fname, int no)
108 sprintf (fname, TEMP_FNAME, no);
111 void key_file_chunk_read (struct key_file *f)
115 getFnameTmp (fname, f->no);
116 fd = open (fname, O_RDONLY);
119 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
124 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
126 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
130 if (lseek (fd, f->offset, SEEK_SET) == -1)
132 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
135 while (f->chunk - nr > 0)
137 r = read (fd, f->buf + nr, f->chunk - nr);
144 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
150 (*f->readHandler)(f, f->readInfo);
154 struct key_file *key_file_init (int no, int chunk)
158 f = xmalloc (sizeof(*f));
165 f->readHandler = NULL;
166 f->buf = xmalloc (f->chunk);
167 f->prev_name = xmalloc (INP_NAME_MAX);
168 *f->prev_name = '\0';
169 key_file_chunk_read (f);
173 int key_file_getc (struct key_file *f)
175 if (f->buf_ptr < f->buf_size)
176 return f->buf[(f->buf_ptr)++];
177 if (f->buf_size < f->chunk)
179 f->offset += f->buf_size;
180 key_file_chunk_read (f);
181 if (f->buf_ptr < f->buf_size)
182 return f->buf[(f->buf_ptr)++];
187 int key_file_decode (struct key_file *f)
191 c = key_file_getc (f);
198 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
201 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
202 d = (d << 8) + (key_file_getc (f) & 0xff);
205 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
206 d = (d << 8) + (key_file_getc (f) & 0xff);
207 d = (d << 8) + (key_file_getc (f) & 0xff);
213 int key_file_read (struct key_file *f, char *key)
218 c = key_file_getc (f);
221 strcpy (key, f->prev_name);
230 while ((key[i++] = key_file_getc (f)))
232 strcpy (f->prev_name, key);
235 d = key_file_decode (f);
238 itkey.sysno = d + f->sysno;
241 f->sysno = itkey.sysno;
244 d = key_file_decode (f);
245 itkey.seqno = d + f->seqno;
246 f->seqno = itkey.seqno;
247 memcpy (key + i, &itkey, sizeof(struct it_key));
248 return i + sizeof (struct it_key);
253 struct key_file **file;
258 int (*cmp)(const void *p1, const void *p2);
261 struct heap_info *key_heap_init (int nkeys,
262 int (*cmp)(const void *p1, const void *p2))
264 struct heap_info *hi;
267 hi = xmalloc (sizeof(*hi));
268 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
269 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
271 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
273 for (i = 0; i<= nkeys; i++)
276 hi->info.buf[i] = xmalloc (INP_NAME_MAX);
281 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
286 hi->ptr[i1] = hi->ptr[i2];
291 static void key_heap_delete (struct heap_info *hi)
293 int cur = 1, child = 2;
295 assert (hi->heapnum > 0);
297 key_heap_swap (hi, 1, hi->heapnum);
299 while (child <= hi->heapnum) {
300 if (child < hi->heapnum &&
301 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
302 &hi->info.buf[hi->ptr[child+1]]) > 0)
304 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
305 &hi->info.buf[hi->ptr[child]]) > 0)
307 key_heap_swap (hi, cur, child);
316 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
321 cur = ++(hi->heapnum);
322 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
323 hi->info.file[hi->ptr[cur]] = kf;
326 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
327 &hi->info.buf[hi->ptr[cur]]) > 0)
329 key_heap_swap (hi, cur, parent);
335 static int heap_read_one (struct heap_info *hi, char *name, char *key)
338 char rbuf[INP_NAME_MAX];
344 strcpy (name, hi->info.buf[n]);
345 kf = hi->info.file[n];
347 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
348 key_heap_delete (hi);
349 if ((r = key_file_read (kf, rbuf)))
350 key_heap_insert (hi, rbuf, r, kf);
355 int heap_inp (Dict dict, ISAM isam, struct heap_info *hi)
358 char next_name[INP_NAME_MAX];
359 char cur_name[INP_NAME_MAX];
360 int key_buf_size = INP_BUF_START;
366 next_key = xmalloc (KEY_SIZE);
367 key_buf = xmalloc (key_buf_size);
368 more = heap_read_one (hi, cur_name, key_buf);
369 while (more) /* EOF ? */
372 key_buf_ptr = KEY_SIZE;
375 if (!(more = heap_read_one (hi, next_name, next_key)))
377 if (*next_name && strcmp (next_name, cur_name))
379 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
380 key_buf_ptr += KEY_SIZE;
381 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
384 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
385 memcpy (new_key_buf, key_buf, key_buf_size);
386 key_buf_size += INP_BUF_ADD;
388 key_buf = new_key_buf;
392 nmemb = key_buf_ptr / KEY_SIZE;
393 assert (nmemb*KEY_SIZE == key_buf_ptr);
394 if ((info = dict_lookup (dict, cur_name)))
396 ISAM_P isam_p, isam_p2;
397 logf (LOG_DEBUG, "updating %s", cur_name);
398 memcpy (&isam_p, info+1, sizeof(ISAM_P));
399 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
403 if (!dict_delete (dict, cur_name))
409 if (isam_p2 != isam_p)
410 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
416 logf (LOG_DEBUG, "inserting %s", cur_name);
418 isam_p = is_merge (isam, 0, nmemb, key_buf);
419 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
421 memcpy (key_buf, next_key, KEY_SIZE);
422 strcpy (cur_name, next_name);
427 struct progressInfo {
434 void progressFunc (struct key_file *keyp, void *info)
436 struct progressInfo *p = info;
437 time_t now, remaining;
439 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
443 if (now >= p->lastTime+10)
446 remaining = (now - p->startTime)*
447 ((double) p->totalBytes/p->totalOffset - 1.0);
448 if (remaining <= 130)
449 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
450 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
452 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
453 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
455 p->totalOffset += keyp->buf_size;
458 void key_input (const char *dict_fname, const char *isam_fname,
459 int nkeys, int cache)
464 struct key_file **kf;
467 struct heap_info *hi;
468 struct progressInfo progressInfo;
476 getFnameTmp (fname, nkeys+1);
477 if (access (fname, R_OK) == -1)
484 dict = dict_open (dict_fname, cache, 1);
487 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
490 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
493 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
497 kf = xmalloc ((1+nkeys) * sizeof(*kf));
498 progressInfo.totalBytes = 0;
499 progressInfo.totalOffset = 0;
500 time (&progressInfo.startTime);
501 time (&progressInfo.lastTime);
502 for (i = 1; i<=nkeys; i++)
504 kf[i] = key_file_init (i, 32768);
505 kf[i]->readHandler = progressFunc;
506 kf[i]->readInfo = &progressInfo;
507 progressInfo.totalBytes += kf[i]->length;
508 progressInfo.totalOffset += kf[i]->buf_size;
510 hi = key_heap_init (nkeys, key_qsort_compare);
511 for (i = 1; i<=nkeys; i++)
512 if ((r = key_file_read (kf[i], rbuf)))
513 key_heap_insert (hi, rbuf, r, kf[i]);
514 heap_inp (dict, isam, hi);
519 for (i = 1; i<=nkeys; i++)
521 getFnameTmp (rbuf, i);
524 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
525 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
526 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
527 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
528 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);