2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1996-03-21 14:50:09 adam
8 * File update uses modify-time instead of change-time.
10 * Revision 1.14 1996/02/07 14:06:37 adam
11 * Better progress report during register merge.
12 * New command: clean - removes temporary shadow files.
14 * Revision 1.13 1996/02/05 12:30:00 adam
15 * Logging reduced a bit.
16 * The remaining running time is estimated during register merge.
18 * Revision 1.12 1995/12/06 17:49:19 adam
19 * Uses dict_delete now.
21 * Revision 1.11 1995/12/06 16:06:43 adam
22 * Better diagnostics. Work on 'real' dictionary deletion.
24 * Revision 1.10 1995/12/06 12:41:22 adam
25 * New command 'stat' for the index program.
26 * Filenames can be read from stdin by specifying '-'.
27 * Bug fix/enhancement of the transformation from terms to regular
28 * expressons in the search engine.
30 * Revision 1.9 1995/10/10 12:24:39 adam
31 * Temporary sort files are compressed.
33 * Revision 1.8 1995/10/04 16:57:19 adam
34 * Key input and merge sort in one pass.
36 * Revision 1.7 1995/10/02 15:18:52 adam
37 * New member in recRetrieveCtrl: diagnostic.
39 * Revision 1.6 1995/09/29 15:51:56 adam
40 * First work on multi-way read.
42 * Revision 1.5 1995/09/29 14:01:43 adam
45 * Revision 1.4 1995/09/28 14:22:57 adam
46 * Sort uses smaller temporary files.
48 * Revision 1.3 1995/09/06 16:11:17 adam
49 * Option: only one word key per file.
51 * Revision 1.2 1995/09/04 12:33:42 adam
52 * Various cleanup. YAZ util used instead.
54 * Revision 1.1 1995/09/04 09:10:37 adam
55 * More work on index add/del/update.
56 * Merge sort implemented.
57 * Initial work on z39 server.
71 #define KEY_SIZE (1+sizeof(struct it_key))
72 #define INP_NAME_MAX 8192
73 #define INP_BUF_START 60000
74 #define INP_BUF_ADD 400000
76 static int no_diffs = 0;
77 static int no_updates = 0;
78 static int no_deletions = 0;
79 static int no_insertions = 0;
80 static int no_iterations = 0;
84 off_t offset; /* file offset */
85 unsigned char *buf; /* buffer block */
86 size_t buf_size; /* number of read bytes in block */
87 size_t chunk; /* number of bytes allocated */
88 size_t buf_ptr; /* current position in buffer */
89 char *prev_name; /* last word read */
90 int sysno; /* last sysno */
91 int seqno; /* last seqno */
92 off_t length; /* length of file */
93 /* handler invoked in each read */
94 void (*readHandler)(struct key_file *keyp, void *rinfo);
98 void getFnameTmp (char *fname, int no)
100 sprintf (fname, TEMP_FNAME, no);
103 void key_file_chunk_read (struct key_file *f)
107 getFnameTmp (fname, f->no);
108 fd = open (fname, O_RDONLY);
111 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
116 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
118 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
122 if (lseek (fd, f->offset, SEEK_SET) == -1)
124 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
127 while (f->chunk - nr > 0)
129 r = read (fd, f->buf + nr, f->chunk - nr);
136 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
142 (*f->readHandler)(f, f->readInfo);
146 struct key_file *key_file_init (int no, int chunk)
150 f = xmalloc (sizeof(*f));
157 f->readHandler = NULL;
158 f->buf = xmalloc (f->chunk);
159 f->prev_name = xmalloc (256);
160 *f->prev_name = '\0';
161 key_file_chunk_read (f);
165 int key_file_getc (struct key_file *f)
167 if (f->buf_ptr < f->buf_size)
168 return f->buf[(f->buf_ptr)++];
169 if (f->buf_size < f->chunk)
171 f->offset += f->buf_size;
172 key_file_chunk_read (f);
173 if (f->buf_ptr < f->buf_size)
174 return f->buf[(f->buf_ptr)++];
179 int key_file_decode (struct key_file *f)
183 c = key_file_getc (f);
190 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
193 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
194 d = (d << 8) + (key_file_getc (f) & 0xff);
197 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
198 d = (d << 8) + (key_file_getc (f) & 0xff);
199 d = (d << 8) + (key_file_getc (f) & 0xff);
205 int key_file_read (struct key_file *f, char *key)
210 c = key_file_getc (f);
213 strcpy (key, f->prev_name);
222 while ((key[i++] = key_file_getc (f)))
224 strcpy (f->prev_name, key);
227 d = key_file_decode (f);
230 itkey.sysno = d + f->sysno;
233 f->sysno = itkey.sysno;
236 d = key_file_decode (f);
237 itkey.seqno = d + f->seqno;
238 f->seqno = itkey.seqno;
239 memcpy (key + i, &itkey, sizeof(struct it_key));
240 return i + sizeof (struct it_key);
245 struct key_file **file;
250 int (*cmp)(const void *p1, const void *p2);
253 struct heap_info *key_heap_init (int nkeys,
254 int (*cmp)(const void *p1, const void *p2))
256 struct heap_info *hi;
259 hi = xmalloc (sizeof(*hi));
260 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
261 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
263 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
265 for (i = 0; i<= nkeys; i++)
268 hi->info.buf[i] = xmalloc (512);
273 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
278 hi->ptr[i1] = hi->ptr[i2];
283 static void key_heap_delete (struct heap_info *hi)
285 int cur = 1, child = 2;
287 assert (hi->heapnum > 0);
289 key_heap_swap (hi, 1, hi->heapnum);
291 while (child <= hi->heapnum) {
292 if (child < hi->heapnum &&
293 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
294 &hi->info.buf[hi->ptr[child+1]]) > 0)
296 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
297 &hi->info.buf[hi->ptr[child]]) > 0)
299 key_heap_swap (hi, cur, child);
308 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
313 cur = ++(hi->heapnum);
314 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
315 hi->info.file[hi->ptr[cur]] = kf;
318 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
319 &hi->info.buf[hi->ptr[cur]]) > 0)
321 key_heap_swap (hi, cur, parent);
327 static int heap_read_one (struct heap_info *hi, char *name, char *key)
336 strcpy (name, hi->info.buf[n]);
337 kf = hi->info.file[n];
339 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
340 key_heap_delete (hi);
341 if ((r = key_file_read (kf, rbuf)))
342 key_heap_insert (hi, rbuf, r, kf);
347 int heap_inp (Dict dict, ISAM isam, struct heap_info *hi)
350 char next_name[INP_NAME_MAX+1];
351 char cur_name[INP_NAME_MAX+1];
352 int key_buf_size = INP_BUF_START;
358 next_key = xmalloc (KEY_SIZE);
359 key_buf = xmalloc (key_buf_size * (KEY_SIZE));
360 more = heap_read_one (hi, cur_name, key_buf);
361 while (more) /* EOF ? */
364 key_buf_ptr = KEY_SIZE;
367 if (!(more = heap_read_one (hi, next_name, next_key)))
369 if (*next_name && strcmp (next_name, cur_name))
371 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
372 key_buf_ptr += KEY_SIZE;
373 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
376 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
377 memcpy (new_key_buf, key_buf, key_buf_size);
378 key_buf_size += INP_BUF_ADD;
380 key_buf = new_key_buf;
384 nmemb = key_buf_ptr / KEY_SIZE;
385 assert (nmemb*KEY_SIZE == key_buf_ptr);
386 if ((info = dict_lookup (dict, cur_name)))
388 ISAM_P isam_p, isam_p2;
389 logf (LOG_DEBUG, "updating %s", cur_name);
390 memcpy (&isam_p, info+1, sizeof(ISAM_P));
391 isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
395 if (!dict_delete (dict, cur_name))
401 if (isam_p2 != isam_p)
402 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
408 logf (LOG_DEBUG, "inserting %s", cur_name);
410 isam_p = is_merge (isam, 0, nmemb, key_buf);
411 dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
413 memcpy (key_buf, next_key, KEY_SIZE);
414 strcpy (cur_name, next_name);
419 struct progressInfo {
426 void progressFunc (struct key_file *keyp, void *info)
428 struct progressInfo *p = info;
429 time_t now, remaining;
431 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
435 if (now >= p->lastTime+10)
438 remaining = (now - p->startTime)*
439 ((double) p->totalBytes/p->totalOffset - 1.0);
440 if (remaining <= 130)
441 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
442 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
444 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
445 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
447 p->totalOffset += keyp->buf_size;
450 void key_input (const char *dict_fname, const char *isam_fname,
451 int nkeys, int cache)
456 struct key_file **kf;
459 struct heap_info *hi;
460 struct progressInfo progressInfo;
462 dict = dict_open (dict_fname, cache, 1);
465 logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
468 isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
471 logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
475 kf = xmalloc ((1+nkeys) * sizeof(*kf));
476 progressInfo.totalBytes = 0;
477 progressInfo.totalOffset = 0;
478 time (&progressInfo.startTime);
479 time (&progressInfo.lastTime);
480 for (i = 1; i<=nkeys; i++)
482 kf[i] = key_file_init (i, 32768);
483 kf[i]->readHandler = progressFunc;
484 kf[i]->readInfo = &progressInfo;
485 progressInfo.totalBytes += kf[i]->length;
486 progressInfo.totalOffset += kf[i]->buf_size;
488 hi = key_heap_init (nkeys, key_qsort_compare);
489 for (i = 1; i<=nkeys; i++)
490 if ((r = key_file_read (kf[i], rbuf)))
491 key_heap_insert (hi, rbuf, r, kf[i]);
492 heap_inp (dict, isam, hi);
497 for (i = 1; i<=nkeys; i++)
499 getFnameTmp (rbuf, i);
502 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
503 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
504 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
505 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
506 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);