2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
6 * $Id: kinput.c,v 1.46 2002-02-20 23:07:54 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 void key_file_destroy (struct key_file *f)
123 xfree (f->prev_name);
127 struct key_file *key_file_init (int no, int chunk, Res res)
131 f = (struct key_file *) xmalloc (sizeof(*f));
139 f->readHandler = NULL;
140 f->buf = (unsigned char *) xmalloc (f->chunk);
141 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
142 *f->prev_name = '\0';
143 key_file_chunk_read (f);
147 int key_file_getc (struct key_file *f)
149 if (f->buf_ptr < f->buf_size)
150 return f->buf[(f->buf_ptr)++];
151 if (f->buf_size < f->chunk)
153 f->offset += f->buf_size;
154 key_file_chunk_read (f);
155 if (f->buf_ptr < f->buf_size)
156 return f->buf[(f->buf_ptr)++];
161 int key_file_decode (struct key_file *f)
165 c = key_file_getc (f);
172 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
175 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
176 d = (d << 8) + (key_file_getc (f) & 0xff);
179 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
180 d = (d << 8) + (key_file_getc (f) & 0xff);
181 d = (d << 8) + (key_file_getc (f) & 0xff);
187 int key_file_read (struct key_file *f, char *key)
192 c = key_file_getc (f);
195 strcpy (key, f->prev_name);
204 while ((key[i++] = key_file_getc (f)))
206 strcpy (f->prev_name, key);
209 d = key_file_decode (f);
212 itkey.sysno = d + f->sysno;
215 f->sysno = itkey.sysno;
218 d = key_file_decode (f);
219 itkey.seqno = d + f->seqno;
220 f->seqno = itkey.seqno;
221 memcpy (key + i, &itkey, sizeof(struct it_key));
222 return i + sizeof (struct it_key);
227 struct key_file **file;
232 int (*cmp)(const void *p1, const void *p2);
242 struct heap_info *key_heap_init (int nkeys,
243 int (*cmp)(const void *p1, const void *p2))
245 struct heap_info *hi;
248 hi = (struct heap_info *) xmalloc (sizeof(*hi));
249 hi->info.file = (struct key_file **)
250 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
251 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
253 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
255 for (i = 0; i<= nkeys; i++)
258 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
263 void key_heap_destroy (struct heap_info *hi, int nkeys)
266 yaz_log (LOG_LOG, "key_heap_destroy");
267 for (i = 0; i<=nkeys; i++)
268 xfree (hi->info.buf[i]);
270 xfree (hi->info.buf);
272 xfree (hi->info.file);
276 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
281 hi->ptr[i1] = hi->ptr[i2];
286 static void key_heap_delete (struct heap_info *hi)
288 int cur = 1, child = 2;
290 assert (hi->heapnum > 0);
292 key_heap_swap (hi, 1, hi->heapnum);
294 while (child <= hi->heapnum) {
295 if (child < hi->heapnum &&
296 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
297 &hi->info.buf[hi->ptr[child+1]]) > 0)
299 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
300 &hi->info.buf[hi->ptr[child]]) > 0)
302 key_heap_swap (hi, cur, child);
311 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
316 cur = ++(hi->heapnum);
317 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
318 hi->info.file[hi->ptr[cur]] = kf;
321 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
322 &hi->info.buf[hi->ptr[cur]]) > 0)
324 key_heap_swap (hi, cur, parent);
330 static int heap_read_one (struct heap_info *hi, char *name, char *key)
333 char rbuf[INP_NAME_MAX];
339 strcpy (name, hi->info.buf[n]);
340 kf = hi->info.file[n];
342 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
343 key_heap_delete (hi);
344 if ((r = key_file_read (kf, rbuf)))
345 key_heap_insert (hi, rbuf, r, kf);
350 struct heap_cread_info {
351 char prev_name[INP_NAME_MAX];
352 char cur_name[INP_NAME_MAX];
354 struct heap_info *hi;
359 int heap_cread_item (void *vp, char **dst, int *insertMode)
361 struct heap_cread_info *p = (struct heap_cread_info *) vp;
362 struct heap_info *hi = p->hi;
366 *insertMode = p->key[0];
367 memcpy (*dst, p->key+1, sizeof(struct it_key));
368 (*dst) += sizeof(struct it_key);
372 strcpy (p->prev_name, p->cur_name);
373 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
375 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
380 *insertMode = p->key[0];
381 memcpy (*dst, p->key+1, sizeof(struct it_key));
382 (*dst) += sizeof(struct it_key);
387 int heap_inpc (struct heap_info *hi)
389 struct heap_cread_info hci;
390 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
392 hci.key = (char *) xmalloc (KEY_SIZE);
395 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
397 isamc_i->clientData = &hci;
398 isamc_i->read_item = heap_cread_item;
402 char this_name[INP_NAME_MAX];
403 ISAMC_P isamc_p, isamc_p2;
406 strcpy (this_name, hci.cur_name);
407 assert (hci.cur_name[1]);
409 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
411 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
412 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
416 if (!dict_delete (hi->dict, this_name))
422 if (isamc_p2 != isamc_p)
423 dict_insert (hi->dict, this_name,
424 sizeof(ISAMC_P), &isamc_p2);
429 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
431 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
439 int heap_inpd (struct heap_info *hi)
441 struct heap_cread_info hci;
442 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
444 hci.key = (char *) xmalloc (KEY_SIZE);
447 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
449 isamd_i->clientData = &hci;
450 isamd_i->read_item = heap_cread_item;
454 char this_name[INP_NAME_MAX];
455 ISAMD_P isamd_p, isamd_p2;
458 strcpy (this_name, hci.cur_name);
459 assert (hci.cur_name[1]);
461 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
463 memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
464 isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
468 if (!dict_delete (hi->dict, this_name))
474 if (isamd_p2 != isamd_p)
475 dict_insert (hi->dict, this_name,
476 sizeof(ISAMD_P), &isamd_p2);
481 isamd_p = isamd_append (hi->isamd, 0, isamd_i);
483 dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
490 int heap_inp (struct heap_info *hi)
493 char next_name[INP_NAME_MAX];
494 char cur_name[INP_NAME_MAX];
495 int key_buf_size = INP_BUF_START;
501 next_key = (char *) xmalloc (KEY_SIZE);
502 key_buf = (char *) xmalloc (key_buf_size);
503 more = heap_read_one (hi, cur_name, key_buf);
504 while (more) /* EOF ? */
507 key_buf_ptr = KEY_SIZE;
510 if (!(more = heap_read_one (hi, next_name, next_key)))
512 if (*next_name && strcmp (next_name, cur_name))
514 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
515 key_buf_ptr += KEY_SIZE;
516 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
519 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
520 memcpy (new_key_buf, key_buf, key_buf_size);
521 key_buf_size += INP_BUF_ADD;
523 key_buf = new_key_buf;
527 nmemb = key_buf_ptr / KEY_SIZE;
528 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
529 if ((info = dict_lookup (hi->dict, cur_name)))
531 ISAM_P isam_p, isam_p2;
532 memcpy (&isam_p, info+1, sizeof(ISAM_P));
533 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
537 if (!dict_delete (hi->dict, cur_name))
543 if (isam_p2 != isam_p)
544 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
551 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
552 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
554 memcpy (key_buf, next_key, KEY_SIZE);
555 strcpy (cur_name, next_name);
562 int heap_inps (struct heap_info *hi)
564 struct heap_cread_info hci;
565 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
567 hci.key = (char *) xmalloc (KEY_SIZE);
570 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
572 isams_i->clientData = &hci;
573 isams_i->read_item = heap_cread_item;
577 char this_name[INP_NAME_MAX];
581 strcpy (this_name, hci.cur_name);
582 assert (hci.cur_name[1]);
584 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
586 isams_p = isams_merge (hi->isams, isams_i);
588 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
592 logf (LOG_FATAL, "isams doesn't support this kind of update");
600 struct progressInfo {
607 void progressFunc (struct key_file *keyp, void *info)
609 struct progressInfo *p = (struct progressInfo *) info;
610 time_t now, remaining;
612 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
616 if (now >= p->lastTime+10)
619 remaining = (time_t) ((now - p->startTime)*
620 ((double) p->totalBytes/p->totalOffset - 1.0));
621 if (remaining <= 130)
622 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
623 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
625 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
626 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
628 p->totalOffset += keyp->buf_size;
635 void zebra_index_merge (ZebraHandle zh)
637 struct key_file **kf;
640 struct heap_info *hi;
641 struct progressInfo progressInfo;
642 int nkeys = zh->key_file_no;
650 extract_get_fname_tmp (zh, fname, nkeys+1);
651 if (access (fname, R_OK) == -1)
658 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
659 progressInfo.totalBytes = 0;
660 progressInfo.totalOffset = 0;
661 time (&progressInfo.startTime);
662 time (&progressInfo.lastTime);
663 for (i = 1; i<=nkeys; i++)
665 kf[i] = key_file_init (i, 8192, zh->service->res);
666 kf[i]->readHandler = progressFunc;
667 kf[i]->readInfo = &progressInfo;
668 progressInfo.totalBytes += kf[i]->length;
669 progressInfo.totalOffset += kf[i]->buf_size;
671 hi = key_heap_init (nkeys, key_qsort_compare);
672 hi->dict = zh->service->dict;
673 hi->isams = zh->service->isams;
675 hi->isam = zh->service->isam;
676 hi->isamc = zh->service->isamc;
677 hi->isamd = zh->service->isamd;
680 for (i = 1; i<=nkeys; i++)
681 if ((r = key_file_read (kf[i], rbuf)))
682 key_heap_insert (hi, rbuf, r, kf[i]);
683 if (zh->service->isams)
686 else if (zh->service->isamc)
688 else if (zh->service->isam)
690 else if (zh->service->isamd)
694 for (i = 1; i<=nkeys; i++)
696 extract_get_fname_tmp (zh, rbuf, i);
699 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
700 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
701 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
702 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
703 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
706 key_heap_destroy (hi, nkeys);
707 for (i = 1; i<=nkeys; i++)
708 key_file_destroy (kf[i]);