2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
6 * $Id: kinput.c,v 1.47 2002-04-04 14:14:13 adam Exp $
9 * - Allocates a lot of memory for the merge process, but never releases it.
10 * Doesn't matter, as the program terminates soon after.
27 #define KEY_SIZE (1+sizeof(struct it_key))
28 #define INP_NAME_MAX 768
29 #define INP_BUF_START 60000
30 #define INP_BUF_ADD 400000
32 static int no_diffs = 0;
33 static int no_updates = 0;
34 static int no_deletions = 0;
35 static int no_insertions = 0;
36 static int no_iterations = 0;
40 off_t offset; /* file offset */
41 unsigned char *buf; /* buffer block */
42 size_t buf_size; /* number of read bytes in block */
43 size_t chunk; /* number of bytes allocated */
44 size_t buf_ptr; /* current position in buffer */
45 char *prev_name; /* last word read */
46 int sysno; /* last sysno */
47 int seqno; /* last seqno */
48 off_t length; /* length of file */
49 /* handler invoked in each read */
50 void (*readHandler)(struct key_file *keyp, void *rinfo);
55 void getFnameTmp (Res res, char *fname, int no)
59 pre = res_get_def (res, "keyTmpDir", ".");
60 sprintf (fname, "%s/key%d.tmp", pre, no);
63 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
67 pre = res_get_def (zh->res, "keyTmpDir", ".");
68 sprintf (fname, "%s/key%d.tmp", pre, no);
71 void key_file_chunk_read (struct key_file *f)
73 int nr = 0, r = 0, fd;
75 getFnameTmp (f->res, fname, f->no);
76 fd = open (fname, O_BINARY|O_RDONLY);
82 logf (LOG_WARN|LOG_ERRNO, "cannot open %s", fname);
87 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
89 logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
94 if (lseek (fd, f->offset, SEEK_SET) == -1)
96 logf (LOG_WARN|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_WARN|LOG_ERRNO, "read of %s", fname);
115 (*f->readHandler)(f, f->readInfo);
119 void key_file_destroy (struct key_file *f)
122 xfree (f->prev_name);
126 struct key_file *key_file_init (int no, int chunk, Res res)
130 f = (struct key_file *) xmalloc (sizeof(*f));
138 f->readHandler = NULL;
139 f->buf = (unsigned char *) xmalloc (f->chunk);
140 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
141 *f->prev_name = '\0';
142 key_file_chunk_read (f);
146 int key_file_getc (struct key_file *f)
148 if (f->buf_ptr < f->buf_size)
149 return f->buf[(f->buf_ptr)++];
150 if (f->buf_size < f->chunk)
152 f->offset += f->buf_size;
153 key_file_chunk_read (f);
154 if (f->buf_ptr < f->buf_size)
155 return f->buf[(f->buf_ptr)++];
160 int key_file_decode (struct key_file *f)
164 c = key_file_getc (f);
171 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
174 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
175 d = (d << 8) + (key_file_getc (f) & 0xff);
178 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
179 d = (d << 8) + (key_file_getc (f) & 0xff);
180 d = (d << 8) + (key_file_getc (f) & 0xff);
186 int key_file_read (struct key_file *f, char *key)
191 c = key_file_getc (f);
194 strcpy (key, f->prev_name);
203 while ((key[i++] = key_file_getc (f)))
205 strcpy (f->prev_name, key);
208 d = key_file_decode (f);
211 itkey.sysno = d + f->sysno;
214 f->sysno = itkey.sysno;
217 d = key_file_decode (f);
218 itkey.seqno = d + f->seqno;
219 f->seqno = itkey.seqno;
220 memcpy (key + i, &itkey, sizeof(struct it_key));
221 return i + sizeof (struct it_key);
226 struct key_file **file;
231 int (*cmp)(const void *p1, const void *p2);
241 struct heap_info *key_heap_init (int nkeys,
242 int (*cmp)(const void *p1, const void *p2))
244 struct heap_info *hi;
247 hi = (struct heap_info *) xmalloc (sizeof(*hi));
248 hi->info.file = (struct key_file **)
249 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
250 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
252 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
254 for (i = 0; i<= nkeys; i++)
257 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
262 void key_heap_destroy (struct heap_info *hi, int nkeys)
265 yaz_log (LOG_LOG, "key_heap_destroy");
266 for (i = 0; i<=nkeys; i++)
267 xfree (hi->info.buf[i]);
269 xfree (hi->info.buf);
271 xfree (hi->info.file);
275 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
280 hi->ptr[i1] = hi->ptr[i2];
285 static void key_heap_delete (struct heap_info *hi)
287 int cur = 1, child = 2;
289 assert (hi->heapnum > 0);
291 key_heap_swap (hi, 1, hi->heapnum);
293 while (child <= hi->heapnum) {
294 if (child < hi->heapnum &&
295 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
296 &hi->info.buf[hi->ptr[child+1]]) > 0)
298 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
299 &hi->info.buf[hi->ptr[child]]) > 0)
301 key_heap_swap (hi, cur, child);
310 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
315 cur = ++(hi->heapnum);
316 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
317 hi->info.file[hi->ptr[cur]] = kf;
320 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
321 &hi->info.buf[hi->ptr[cur]]) > 0)
323 key_heap_swap (hi, cur, parent);
329 static int heap_read_one (struct heap_info *hi, char *name, char *key)
332 char rbuf[INP_NAME_MAX];
338 strcpy (name, hi->info.buf[n]);
339 kf = hi->info.file[n];
341 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
342 key_heap_delete (hi);
343 if ((r = key_file_read (kf, rbuf)))
344 key_heap_insert (hi, rbuf, r, kf);
349 struct heap_cread_info {
350 char prev_name[INP_NAME_MAX];
351 char cur_name[INP_NAME_MAX];
353 struct heap_info *hi;
358 int heap_cread_item (void *vp, char **dst, int *insertMode)
360 struct heap_cread_info *p = (struct heap_cread_info *) vp;
361 struct heap_info *hi = p->hi;
365 *insertMode = p->key[0];
366 memcpy (*dst, p->key+1, sizeof(struct it_key));
367 (*dst) += sizeof(struct it_key);
371 strcpy (p->prev_name, p->cur_name);
372 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
374 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
379 *insertMode = p->key[0];
380 memcpy (*dst, p->key+1, sizeof(struct it_key));
381 (*dst) += sizeof(struct it_key);
386 int heap_inpc (struct heap_info *hi)
388 struct heap_cread_info hci;
389 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
391 hci.key = (char *) xmalloc (KEY_SIZE);
394 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
396 isamc_i->clientData = &hci;
397 isamc_i->read_item = heap_cread_item;
401 char this_name[INP_NAME_MAX];
402 ISAMC_P isamc_p, isamc_p2;
405 strcpy (this_name, hci.cur_name);
406 assert (hci.cur_name[1]);
408 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
410 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
411 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
415 if (!dict_delete (hi->dict, this_name))
421 if (isamc_p2 != isamc_p)
422 dict_insert (hi->dict, this_name,
423 sizeof(ISAMC_P), &isamc_p2);
428 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
430 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
438 int heap_inpd (struct heap_info *hi)
440 struct heap_cread_info hci;
441 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
443 hci.key = (char *) xmalloc (KEY_SIZE);
446 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
448 isamd_i->clientData = &hci;
449 isamd_i->read_item = heap_cread_item;
453 char this_name[INP_NAME_MAX];
454 ISAMD_P isamd_p, isamd_p2;
457 strcpy (this_name, hci.cur_name);
458 assert (hci.cur_name[1]);
460 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
462 memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
463 isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
467 if (!dict_delete (hi->dict, this_name))
473 if (isamd_p2 != isamd_p)
474 dict_insert (hi->dict, this_name,
475 sizeof(ISAMD_P), &isamd_p2);
480 isamd_p = isamd_append (hi->isamd, 0, isamd_i);
482 dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
489 int heap_inp (struct heap_info *hi)
492 char next_name[INP_NAME_MAX];
493 char cur_name[INP_NAME_MAX];
494 int key_buf_size = INP_BUF_START;
500 next_key = (char *) xmalloc (KEY_SIZE);
501 key_buf = (char *) xmalloc (key_buf_size);
502 more = heap_read_one (hi, cur_name, key_buf);
503 while (more) /* EOF ? */
506 key_buf_ptr = KEY_SIZE;
509 if (!(more = heap_read_one (hi, next_name, next_key)))
511 if (*next_name && strcmp (next_name, cur_name))
513 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
514 key_buf_ptr += KEY_SIZE;
515 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
518 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
519 memcpy (new_key_buf, key_buf, key_buf_size);
520 key_buf_size += INP_BUF_ADD;
522 key_buf = new_key_buf;
526 nmemb = key_buf_ptr / KEY_SIZE;
527 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
528 if ((info = dict_lookup (hi->dict, cur_name)))
530 ISAM_P isam_p, isam_p2;
531 memcpy (&isam_p, info+1, sizeof(ISAM_P));
532 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
536 if (!dict_delete (hi->dict, cur_name))
542 if (isam_p2 != isam_p)
543 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
550 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
551 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
553 memcpy (key_buf, next_key, KEY_SIZE);
554 strcpy (cur_name, next_name);
561 int heap_inps (struct heap_info *hi)
563 struct heap_cread_info hci;
564 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
566 hci.key = (char *) xmalloc (KEY_SIZE);
569 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
571 isams_i->clientData = &hci;
572 isams_i->read_item = heap_cread_item;
576 char this_name[INP_NAME_MAX];
580 strcpy (this_name, hci.cur_name);
581 assert (hci.cur_name[1]);
583 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
585 isams_p = isams_merge (hi->isams, isams_i);
587 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
591 logf (LOG_FATAL, "isams doesn't support this kind of update");
599 struct progressInfo {
606 void progressFunc (struct key_file *keyp, void *info)
608 struct progressInfo *p = (struct progressInfo *) info;
609 time_t now, remaining;
611 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
615 if (now >= p->lastTime+10)
618 remaining = (time_t) ((now - p->startTime)*
619 ((double) p->totalBytes/p->totalOffset - 1.0));
620 if (remaining <= 130)
621 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
622 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
624 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
625 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
627 p->totalOffset += keyp->buf_size;
634 void zebra_index_merge (ZebraHandle zh)
636 struct key_file **kf;
639 struct heap_info *hi;
640 struct progressInfo progressInfo;
641 int nkeys = zh->reg->key_file_no;
649 extract_get_fname_tmp (zh, fname, nkeys+1);
650 if (access (fname, R_OK) == -1)
657 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
658 progressInfo.totalBytes = 0;
659 progressInfo.totalOffset = 0;
660 time (&progressInfo.startTime);
661 time (&progressInfo.lastTime);
662 for (i = 1; i<=nkeys; i++)
664 kf[i] = key_file_init (i, 8192, zh->res);
665 kf[i]->readHandler = progressFunc;
666 kf[i]->readInfo = &progressInfo;
667 progressInfo.totalBytes += kf[i]->length;
668 progressInfo.totalOffset += kf[i]->buf_size;
670 hi = key_heap_init (nkeys, key_qsort_compare);
671 hi->dict = zh->reg->dict;
672 hi->isams = zh->reg->isams;
674 hi->isam = zh->reg->isam;
675 hi->isamc = zh->reg->isamc;
676 hi->isamd = zh->reg->isamd;
679 for (i = 1; i<=nkeys; i++)
680 if ((r = key_file_read (kf[i], rbuf)))
681 key_heap_insert (hi, rbuf, r, kf[i]);
685 else if (zh->reg->isamc)
687 else if (zh->reg->isam)
689 else if (zh->reg->isamd)
693 for (i = 1; i<=nkeys; i++)
695 extract_get_fname_tmp (zh, rbuf, i);
698 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
699 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
700 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
701 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
702 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
703 zh->reg->key_file_no = 0;
705 key_heap_destroy (hi, nkeys);
706 for (i = 1; i<=nkeys; i++)
707 key_file_destroy (kf[i]);