2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
6 * $Id: kinput.c,v 1.45 2002-02-20 17:30:01 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.
28 #define KEY_SIZE (1+sizeof(struct it_key))
29 #define INP_NAME_MAX 768
30 #define INP_BUF_START 60000
31 #define INP_BUF_ADD 400000
33 static int no_diffs = 0;
34 static int no_updates = 0;
35 static int no_deletions = 0;
36 static int no_insertions = 0;
37 static int no_iterations = 0;
41 off_t offset; /* file offset */
42 unsigned char *buf; /* buffer block */
43 size_t buf_size; /* number of read bytes in block */
44 size_t chunk; /* number of bytes allocated */
45 size_t buf_ptr; /* current position in buffer */
46 char *prev_name; /* last word read */
47 int sysno; /* last sysno */
48 int seqno; /* last seqno */
49 off_t length; /* length of file */
50 /* handler invoked in each read */
51 void (*readHandler)(struct key_file *keyp, void *rinfo);
56 void getFnameTmp (Res res, char *fname, int no)
60 pre = res_get_def (res, "keyTmpDir", ".");
61 sprintf (fname, "%s/key%d.tmp", pre, no);
64 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
68 pre = res_get_def (zh->service->res, "keyTmpDir", ".");
69 sprintf (fname, "%s/key%d.tmp", pre, no);
72 void key_file_chunk_read (struct key_file *f)
74 int nr = 0, r = 0, fd;
76 getFnameTmp (f->res, fname, f->no);
77 fd = open (fname, O_BINARY|O_RDONLY);
83 logf (LOG_WARN|LOG_ERRNO, "cannot open %s", fname);
88 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
90 logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
95 if (lseek (fd, f->offset, SEEK_SET) == -1)
97 logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
101 while (f->chunk - nr > 0)
103 r = read (fd, f->buf + nr, f->chunk - nr);
110 logf (LOG_WARN|LOG_ERRNO, "read of %s", fname);
116 (*f->readHandler)(f, f->readInfo);
120 struct key_file *key_file_init (int no, int chunk, Res res)
124 f = (struct key_file *) xmalloc (sizeof(*f));
132 f->readHandler = NULL;
133 f->buf = (unsigned char *) xmalloc (f->chunk);
134 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
135 *f->prev_name = '\0';
136 key_file_chunk_read (f);
140 int key_file_getc (struct key_file *f)
142 if (f->buf_ptr < f->buf_size)
143 return f->buf[(f->buf_ptr)++];
144 if (f->buf_size < f->chunk)
146 f->offset += f->buf_size;
147 key_file_chunk_read (f);
148 if (f->buf_ptr < f->buf_size)
149 return f->buf[(f->buf_ptr)++];
154 int key_file_decode (struct key_file *f)
158 c = key_file_getc (f);
165 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
168 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
169 d = (d << 8) + (key_file_getc (f) & 0xff);
172 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
173 d = (d << 8) + (key_file_getc (f) & 0xff);
174 d = (d << 8) + (key_file_getc (f) & 0xff);
180 int key_file_read (struct key_file *f, char *key)
185 c = key_file_getc (f);
188 strcpy (key, f->prev_name);
197 while ((key[i++] = key_file_getc (f)))
199 strcpy (f->prev_name, key);
202 d = key_file_decode (f);
205 itkey.sysno = d + f->sysno;
208 f->sysno = itkey.sysno;
211 d = key_file_decode (f);
212 itkey.seqno = d + f->seqno;
213 f->seqno = itkey.seqno;
214 memcpy (key + i, &itkey, sizeof(struct it_key));
215 return i + sizeof (struct it_key);
220 struct key_file **file;
225 int (*cmp)(const void *p1, const void *p2);
235 struct heap_info *key_heap_init (int nkeys,
236 int (*cmp)(const void *p1, const void *p2))
238 struct heap_info *hi;
241 hi = (struct heap_info *) xmalloc (sizeof(*hi));
242 hi->info.file = (struct key_file **)
243 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
244 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
246 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
248 for (i = 0; i<= nkeys; i++)
251 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
256 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
261 hi->ptr[i1] = hi->ptr[i2];
266 static void key_heap_delete (struct heap_info *hi)
268 int cur = 1, child = 2;
270 assert (hi->heapnum > 0);
272 key_heap_swap (hi, 1, hi->heapnum);
274 while (child <= hi->heapnum) {
275 if (child < hi->heapnum &&
276 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
277 &hi->info.buf[hi->ptr[child+1]]) > 0)
279 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
280 &hi->info.buf[hi->ptr[child]]) > 0)
282 key_heap_swap (hi, cur, child);
291 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
296 cur = ++(hi->heapnum);
297 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
298 hi->info.file[hi->ptr[cur]] = kf;
301 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
302 &hi->info.buf[hi->ptr[cur]]) > 0)
304 key_heap_swap (hi, cur, parent);
310 static int heap_read_one (struct heap_info *hi, char *name, char *key)
313 char rbuf[INP_NAME_MAX];
319 strcpy (name, hi->info.buf[n]);
320 kf = hi->info.file[n];
322 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
323 key_heap_delete (hi);
324 if ((r = key_file_read (kf, rbuf)))
325 key_heap_insert (hi, rbuf, r, kf);
330 struct heap_cread_info {
331 char prev_name[INP_NAME_MAX];
332 char cur_name[INP_NAME_MAX];
334 struct heap_info *hi;
339 int heap_cread_item (void *vp, char **dst, int *insertMode)
341 struct heap_cread_info *p = (struct heap_cread_info *) vp;
342 struct heap_info *hi = p->hi;
346 *insertMode = p->key[0];
347 memcpy (*dst, p->key+1, sizeof(struct it_key));
348 (*dst) += sizeof(struct it_key);
352 strcpy (p->prev_name, p->cur_name);
353 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
355 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
360 *insertMode = p->key[0];
361 memcpy (*dst, p->key+1, sizeof(struct it_key));
362 (*dst) += sizeof(struct it_key);
367 int heap_inpc (struct heap_info *hi)
369 struct heap_cread_info hci;
370 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
372 hci.key = (char *) xmalloc (KEY_SIZE);
375 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
377 isamc_i->clientData = &hci;
378 isamc_i->read_item = heap_cread_item;
382 char this_name[INP_NAME_MAX];
383 ISAMC_P isamc_p, isamc_p2;
386 strcpy (this_name, hci.cur_name);
387 assert (hci.cur_name[1]);
389 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
391 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
392 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
396 if (!dict_delete (hi->dict, this_name))
402 if (isamc_p2 != isamc_p)
403 dict_insert (hi->dict, this_name,
404 sizeof(ISAMC_P), &isamc_p2);
409 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
411 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
419 int heap_inpd (struct heap_info *hi)
421 struct heap_cread_info hci;
422 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
424 hci.key = (char *) xmalloc (KEY_SIZE);
427 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
429 isamd_i->clientData = &hci;
430 isamd_i->read_item = heap_cread_item;
434 char this_name[INP_NAME_MAX];
435 ISAMD_P isamd_p, isamd_p2;
438 strcpy (this_name, hci.cur_name);
439 assert (hci.cur_name[1]);
441 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
443 memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
444 isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
448 if (!dict_delete (hi->dict, this_name))
454 if (isamd_p2 != isamd_p)
455 dict_insert (hi->dict, this_name,
456 sizeof(ISAMD_P), &isamd_p2);
461 isamd_p = isamd_append (hi->isamd, 0, isamd_i);
463 dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
470 int heap_inp (struct heap_info *hi)
473 char next_name[INP_NAME_MAX];
474 char cur_name[INP_NAME_MAX];
475 int key_buf_size = INP_BUF_START;
481 next_key = (char *) xmalloc (KEY_SIZE);
482 key_buf = (char *) xmalloc (key_buf_size);
483 more = heap_read_one (hi, cur_name, key_buf);
484 while (more) /* EOF ? */
487 key_buf_ptr = KEY_SIZE;
490 if (!(more = heap_read_one (hi, next_name, next_key)))
492 if (*next_name && strcmp (next_name, cur_name))
494 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
495 key_buf_ptr += KEY_SIZE;
496 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
499 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
500 memcpy (new_key_buf, key_buf, key_buf_size);
501 key_buf_size += INP_BUF_ADD;
503 key_buf = new_key_buf;
507 nmemb = key_buf_ptr / KEY_SIZE;
508 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
509 if ((info = dict_lookup (hi->dict, cur_name)))
511 ISAM_P isam_p, isam_p2;
512 memcpy (&isam_p, info+1, sizeof(ISAM_P));
513 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
517 if (!dict_delete (hi->dict, cur_name))
523 if (isam_p2 != isam_p)
524 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
531 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
532 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
534 memcpy (key_buf, next_key, KEY_SIZE);
535 strcpy (cur_name, next_name);
542 int heap_inps (struct heap_info *hi)
544 struct heap_cread_info hci;
545 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
547 hci.key = (char *) xmalloc (KEY_SIZE);
550 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
552 isams_i->clientData = &hci;
553 isams_i->read_item = heap_cread_item;
557 char this_name[INP_NAME_MAX];
561 strcpy (this_name, hci.cur_name);
562 assert (hci.cur_name[1]);
564 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
566 isams_p = isams_merge (hi->isams, isams_i);
568 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
572 logf (LOG_FATAL, "isams doesn't support this kind of update");
580 struct progressInfo {
587 void progressFunc (struct key_file *keyp, void *info)
589 struct progressInfo *p = (struct progressInfo *) info;
590 time_t now, remaining;
592 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
596 if (now >= p->lastTime+10)
599 remaining = (time_t) ((now - p->startTime)*
600 ((double) p->totalBytes/p->totalOffset - 1.0));
601 if (remaining <= 130)
602 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
603 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
605 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
606 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
608 p->totalOffset += keyp->buf_size;
615 void zebra_index_merge (ZebraHandle zh)
617 struct key_file **kf;
620 struct heap_info *hi;
621 struct progressInfo progressInfo;
622 int nkeys = zh->key_file_no;
630 extract_get_fname_tmp (zh, fname, nkeys+1);
631 if (access (fname, R_OK) == -1)
638 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
639 progressInfo.totalBytes = 0;
640 progressInfo.totalOffset = 0;
641 time (&progressInfo.startTime);
642 time (&progressInfo.lastTime);
643 for (i = 1; i<=nkeys; i++)
645 kf[i] = key_file_init (i, 8192, zh->service->res);
646 kf[i]->readHandler = progressFunc;
647 kf[i]->readInfo = &progressInfo;
648 progressInfo.totalBytes += kf[i]->length;
649 progressInfo.totalOffset += kf[i]->buf_size;
651 hi = key_heap_init (nkeys, key_qsort_compare);
652 hi->dict = zh->service->dict;
653 hi->isams = zh->service->isams;
655 hi->isam = zh->service->isam;
656 hi->isamc = zh->service->isamc;
657 hi->isamd = zh->service->isamd;
660 for (i = 1; i<=nkeys; i++)
661 if ((r = key_file_read (kf[i], rbuf)))
662 key_heap_insert (hi, rbuf, r, kf[i]);
663 if (zh->service->isams)
666 else if (zh->service->isamc)
668 else if (zh->service->isam)
670 else if (zh->service->isamd)
674 for (i = 1; i<=nkeys; i++)
676 extract_get_fname_tmp (zh, rbuf, i);
679 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
680 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
681 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
682 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
683 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
687 void key_input (ZebraHandle zh, int nkeys, int cache, Res res)
690 struct key_file **kf;
693 struct heap_info *hi;
694 struct progressInfo progressInfo;
702 getFnameTmp (res, fname, nkeys+1);
703 if (access (fname, R_OK) == -1)
710 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
711 progressInfo.totalBytes = 0;
712 progressInfo.totalOffset = 0;
713 time (&progressInfo.startTime);
714 time (&progressInfo.lastTime);
715 for (i = 1; i<=nkeys; i++)
717 kf[i] = key_file_init (i, 8192, res);
718 kf[i]->readHandler = progressFunc;
719 kf[i]->readInfo = &progressInfo;
720 progressInfo.totalBytes += kf[i]->length;
721 progressInfo.totalOffset += kf[i]->buf_size;
723 hi = key_heap_init (nkeys, key_qsort_compare);
724 hi->dict = zh->service->dict;
725 hi->isams = zh->service->isams;
727 hi->isam = zh->service->isam;
728 hi->isamc = zh->service->isamc;
729 hi->isamd = zh->service->isamd;
732 for (i = 1; i<=nkeys; i++)
733 if ((r = key_file_read (kf[i], rbuf)))
734 key_heap_insert (hi, rbuf, r, kf[i]);
746 for (i = 1; i<=nkeys; i++)
748 getFnameTmp (res, rbuf, i);
751 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
752 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
753 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
754 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
755 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
757 /* xmalloc_trav("unfreed"); while hunting leaks */