2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1997-02-12 20:39:45 adam
8 * Implemented options -f <n> that limits the log to the first <n>
10 * Changed some log messages also.
12 * Revision 1.21 1996/11/08 11:10:23 adam
13 * Buffers used during file match got bigger.
14 * Compressed ISAM support everywhere.
15 * Bug fixes regarding masking characters in queries.
16 * Redesigned Regexp-2 queries.
18 * Revision 1.20 1996/11/01 08:58:41 adam
19 * Interface to isamc system now includes update and delete.
21 * Revision 1.19 1996/10/29 14:09:46 adam
22 * Use of cisam system - enabled if setting isamc is 1.
24 * Revision 1.18 1996/06/04 10:18:59 adam
25 * Minor changes - removed include of ctype.h.
27 * Revision 1.17 1996/05/14 15:47:07 adam
28 * Cleanup of various buffer size entities.
30 * Revision 1.16 1996/04/09 10:05:20 adam
31 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
33 * Revision 1.15 1996/03/21 14:50:09 adam
34 * File update uses modify-time instead of change-time.
36 * Revision 1.14 1996/02/07 14:06:37 adam
37 * Better progress report during register merge.
38 * New command: clean - removes temporary shadow files.
40 * Revision 1.13 1996/02/05 12:30:00 adam
41 * Logging reduced a bit.
42 * The remaining running time is estimated during register merge.
44 * Revision 1.12 1995/12/06 17:49:19 adam
45 * Uses dict_delete now.
47 * Revision 1.11 1995/12/06 16:06:43 adam
48 * Better diagnostics. Work on 'real' dictionary deletion.
50 * Revision 1.10 1995/12/06 12:41:22 adam
51 * New command 'stat' for the index program.
52 * Filenames can be read from stdin by specifying '-'.
53 * Bug fix/enhancement of the transformation from terms to regular
54 * expressons in the search engine.
56 * Revision 1.9 1995/10/10 12:24:39 adam
57 * Temporary sort files are compressed.
59 * Revision 1.8 1995/10/04 16:57:19 adam
60 * Key input and merge sort in one pass.
62 * Revision 1.7 1995/10/02 15:18:52 adam
63 * New member in recRetrieveCtrl: diagnostic.
65 * Revision 1.6 1995/09/29 15:51:56 adam
66 * First work on multi-way read.
68 * Revision 1.5 1995/09/29 14:01:43 adam
71 * Revision 1.4 1995/09/28 14:22:57 adam
72 * Sort uses smaller temporary files.
74 * Revision 1.3 1995/09/06 16:11:17 adam
75 * Option: only one word key per file.
77 * Revision 1.2 1995/09/04 12:33:42 adam
78 * Various cleanup. YAZ util used instead.
80 * Revision 1.1 1995/09/04 09:10:37 adam
81 * More work on index add/del/update.
82 * Merge sort implemented.
83 * Initial work on z39 server.
96 #define KEY_SIZE (1+sizeof(struct it_key))
97 #define INP_NAME_MAX 768
98 #define INP_BUF_START 60000
99 #define INP_BUF_ADD 400000
101 static int no_diffs = 0;
102 static int no_updates = 0;
103 static int no_deletions = 0;
104 static int no_insertions = 0;
105 static int no_iterations = 0;
108 int no; /* file no */
109 off_t offset; /* file offset */
110 unsigned char *buf; /* buffer block */
111 size_t buf_size; /* number of read bytes in block */
112 size_t chunk; /* number of bytes allocated */
113 size_t buf_ptr; /* current position in buffer */
114 char *prev_name; /* last word read */
115 int sysno; /* last sysno */
116 int seqno; /* last seqno */
117 off_t length; /* length of file */
118 /* handler invoked in each read */
119 void (*readHandler)(struct key_file *keyp, void *rinfo);
123 void getFnameTmp (char *fname, int no)
127 pre = res_get_def (common_resource, "keyTmpDir", ".");
128 sprintf (fname, "%s/key%d.tmp", pre, no);
131 void key_file_chunk_read (struct key_file *f)
135 getFnameTmp (fname, f->no);
136 fd = open (fname, O_RDONLY);
139 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
144 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
146 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
150 if (lseek (fd, f->offset, SEEK_SET) == -1)
152 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
155 while (f->chunk - nr > 0)
157 r = read (fd, f->buf + nr, f->chunk - nr);
164 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
170 (*f->readHandler)(f, f->readInfo);
174 struct key_file *key_file_init (int no, int chunk)
178 f = xmalloc (sizeof(*f));
185 f->readHandler = NULL;
186 f->buf = xmalloc (f->chunk);
187 f->prev_name = xmalloc (INP_NAME_MAX);
188 *f->prev_name = '\0';
189 key_file_chunk_read (f);
193 int key_file_getc (struct key_file *f)
195 if (f->buf_ptr < f->buf_size)
196 return f->buf[(f->buf_ptr)++];
197 if (f->buf_size < f->chunk)
199 f->offset += f->buf_size;
200 key_file_chunk_read (f);
201 if (f->buf_ptr < f->buf_size)
202 return f->buf[(f->buf_ptr)++];
207 int key_file_decode (struct key_file *f)
211 c = key_file_getc (f);
218 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
221 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
222 d = (d << 8) + (key_file_getc (f) & 0xff);
225 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
226 d = (d << 8) + (key_file_getc (f) & 0xff);
227 d = (d << 8) + (key_file_getc (f) & 0xff);
233 int key_file_read (struct key_file *f, char *key)
238 c = key_file_getc (f);
241 strcpy (key, f->prev_name);
250 while ((key[i++] = key_file_getc (f)))
252 strcpy (f->prev_name, key);
255 d = key_file_decode (f);
258 itkey.sysno = d + f->sysno;
261 f->sysno = itkey.sysno;
264 d = key_file_decode (f);
265 itkey.seqno = d + f->seqno;
266 f->seqno = itkey.seqno;
267 memcpy (key + i, &itkey, sizeof(struct it_key));
268 return i + sizeof (struct it_key);
273 struct key_file **file;
278 int (*cmp)(const void *p1, const void *p2);
284 struct heap_info *key_heap_init (int nkeys,
285 int (*cmp)(const void *p1, const void *p2))
287 struct heap_info *hi;
290 hi = xmalloc (sizeof(*hi));
291 hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
292 hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
294 hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
296 for (i = 0; i<= nkeys; i++)
299 hi->info.buf[i] = xmalloc (INP_NAME_MAX);
304 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
309 hi->ptr[i1] = hi->ptr[i2];
314 static void key_heap_delete (struct heap_info *hi)
316 int cur = 1, child = 2;
318 assert (hi->heapnum > 0);
320 key_heap_swap (hi, 1, hi->heapnum);
322 while (child <= hi->heapnum) {
323 if (child < hi->heapnum &&
324 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
325 &hi->info.buf[hi->ptr[child+1]]) > 0)
327 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
328 &hi->info.buf[hi->ptr[child]]) > 0)
330 key_heap_swap (hi, cur, child);
339 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
344 cur = ++(hi->heapnum);
345 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
346 hi->info.file[hi->ptr[cur]] = kf;
349 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
350 &hi->info.buf[hi->ptr[cur]]) > 0)
352 key_heap_swap (hi, cur, parent);
358 static int heap_read_one (struct heap_info *hi, char *name, char *key)
361 char rbuf[INP_NAME_MAX];
367 strcpy (name, hi->info.buf[n]);
368 kf = hi->info.file[n];
370 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
371 key_heap_delete (hi);
372 if ((r = key_file_read (kf, rbuf)))
373 key_heap_insert (hi, rbuf, r, kf);
378 struct heap_cread_info {
379 char prev_name[INP_NAME_MAX];
380 char cur_name[INP_NAME_MAX];
382 struct heap_info *hi;
387 int heap_cread_item (void *vp, char **dst, int *insertMode)
389 struct heap_cread_info *p = vp;
390 struct heap_info *hi = p->hi;
394 *insertMode = p->key[0];
395 memcpy (*dst, p->key+1, sizeof(struct it_key));
396 (*dst) += sizeof(struct it_key);
400 strcpy (p->prev_name, p->cur_name);
401 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
403 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
408 *insertMode = p->key[0];
409 memcpy (*dst, p->key+1, sizeof(struct it_key));
410 (*dst) += sizeof(struct it_key);
414 int heap_inpc (struct heap_info *hi)
416 struct heap_cread_info hci;
417 ISAMC_I isamc_i = xmalloc (sizeof(*isamc_i));
419 hci.key = xmalloc (KEY_SIZE);
422 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
424 isamc_i->clientData = &hci;
425 isamc_i->read_item = heap_cread_item;
429 char this_name[INP_NAME_MAX];
430 ISAMC_P isamc_p, isamc_p2;
433 strcpy (this_name, hci.cur_name);
434 logf (LOG_DEBUG, "inserting %s", 1+hci.cur_name);
436 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
438 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
439 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
443 if (!dict_delete (hi->dict, this_name))
449 if (isamc_p2 != isamc_p)
450 dict_insert (hi->dict, this_name,
451 sizeof(ISAMC_P), &isamc_p2);
456 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
458 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
465 int heap_inp (struct heap_info *hi)
468 char next_name[INP_NAME_MAX];
469 char cur_name[INP_NAME_MAX];
470 int key_buf_size = INP_BUF_START;
476 next_key = xmalloc (KEY_SIZE);
477 key_buf = xmalloc (key_buf_size);
478 more = heap_read_one (hi, cur_name, key_buf);
479 while (more) /* EOF ? */
482 key_buf_ptr = KEY_SIZE;
485 if (!(more = heap_read_one (hi, next_name, next_key)))
487 if (*next_name && strcmp (next_name, cur_name))
489 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
490 key_buf_ptr += KEY_SIZE;
491 if (key_buf_ptr+KEY_SIZE >= key_buf_size)
494 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
495 memcpy (new_key_buf, key_buf, key_buf_size);
496 key_buf_size += INP_BUF_ADD;
498 key_buf = new_key_buf;
502 nmemb = key_buf_ptr / KEY_SIZE;
503 assert (nmemb*KEY_SIZE == key_buf_ptr);
504 if ((info = dict_lookup (hi->dict, cur_name)))
506 ISAM_P isam_p, isam_p2;
507 logf (LOG_DEBUG, "updating %s", 1+cur_name);
508 memcpy (&isam_p, info+1, sizeof(ISAM_P));
509 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
513 if (!dict_delete (hi->dict, cur_name))
519 if (isam_p2 != isam_p)
520 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
526 logf (LOG_DEBUG, "inserting %s", 1+cur_name);
528 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
529 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
531 memcpy (key_buf, next_key, KEY_SIZE);
532 strcpy (cur_name, next_name);
537 struct progressInfo {
544 void progressFunc (struct key_file *keyp, void *info)
546 struct progressInfo *p = info;
547 time_t now, remaining;
549 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
553 if (now >= p->lastTime+10)
556 remaining = (now - p->startTime)*
557 ((double) p->totalBytes/p->totalOffset - 1.0);
558 if (remaining <= 130)
559 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
560 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
562 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
563 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
565 p->totalOffset += keyp->buf_size;
568 void key_input (int nkeys, int cache)
574 struct key_file **kf;
577 struct heap_info *hi;
578 struct progressInfo progressInfo;
586 getFnameTmp (fname, nkeys+1);
587 if (access (fname, R_OK) == -1)
594 dict = dict_open (FNAME_DICT, cache, 1);
597 logf (LOG_FATAL, "dict_open fail");
600 if (res_get_match (common_resource, "isam", "c", NULL))
602 isamc = isc_open (FNAME_ISAMC, 1, key_isamc_m ());
605 logf (LOG_FATAL, "isc_open fail");
611 isam = is_open (FNAME_ISAM, key_compare, 1, sizeof(struct it_key));
614 logf (LOG_FATAL, "is_open fail");
618 kf = xmalloc ((1+nkeys) * sizeof(*kf));
619 progressInfo.totalBytes = 0;
620 progressInfo.totalOffset = 0;
621 time (&progressInfo.startTime);
622 time (&progressInfo.lastTime);
623 for (i = 1; i<=nkeys; i++)
625 kf[i] = key_file_init (i, 32768);
626 kf[i]->readHandler = progressFunc;
627 kf[i]->readInfo = &progressInfo;
628 progressInfo.totalBytes += kf[i]->length;
629 progressInfo.totalOffset += kf[i]->buf_size;
631 hi = key_heap_init (nkeys, key_qsort_compare);
636 for (i = 1; i<=nkeys; i++)
637 if ((r = key_file_read (kf[i], rbuf)))
638 key_heap_insert (hi, rbuf, r, kf[i]);
649 for (i = 1; i<=nkeys; i++)
651 getFnameTmp (rbuf, i);
654 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
655 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
656 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
657 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
658 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);