2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
6 * $Id: kinput.c,v 1.48 2002-04-05 08:46:26 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);
239 struct heap_info *key_heap_init (int nkeys,
240 int (*cmp)(const void *p1, const void *p2))
242 struct heap_info *hi;
245 hi = (struct heap_info *) xmalloc (sizeof(*hi));
246 hi->info.file = (struct key_file **)
247 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
248 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
250 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
252 for (i = 0; i<= nkeys; i++)
255 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
260 void key_heap_destroy (struct heap_info *hi, int nkeys)
263 yaz_log (LOG_LOG, "key_heap_destroy");
264 for (i = 0; i<=nkeys; i++)
265 xfree (hi->info.buf[i]);
267 xfree (hi->info.buf);
269 xfree (hi->info.file);
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)
330 char rbuf[INP_NAME_MAX];
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 struct heap_cread_info {
348 char prev_name[INP_NAME_MAX];
349 char cur_name[INP_NAME_MAX];
351 struct heap_info *hi;
356 int heap_cread_item (void *vp, char **dst, int *insertMode)
358 struct heap_cread_info *p = (struct heap_cread_info *) vp;
359 struct heap_info *hi = p->hi;
363 *insertMode = p->key[0];
364 memcpy (*dst, p->key+1, sizeof(struct it_key));
365 (*dst) += sizeof(struct it_key);
369 strcpy (p->prev_name, p->cur_name);
370 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
372 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
377 *insertMode = p->key[0];
378 memcpy (*dst, p->key+1, sizeof(struct it_key));
379 (*dst) += sizeof(struct it_key);
383 int heap_inpc (struct heap_info *hi)
385 struct heap_cread_info hci;
386 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
388 hci.key = (char *) xmalloc (KEY_SIZE);
391 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
393 isamc_i->clientData = &hci;
394 isamc_i->read_item = heap_cread_item;
398 char this_name[INP_NAME_MAX];
399 ISAMC_P isamc_p, isamc_p2;
402 strcpy (this_name, hci.cur_name);
403 assert (hci.cur_name[1]);
405 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
407 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
408 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
412 if (!dict_delete (hi->dict, this_name))
418 if (isamc_p2 != isamc_p)
419 dict_insert (hi->dict, this_name,
420 sizeof(ISAMC_P), &isamc_p2);
425 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
427 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
435 int heap_inpd (struct heap_info *hi)
437 struct heap_cread_info hci;
438 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
440 hci.key = (char *) xmalloc (KEY_SIZE);
443 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
445 isamd_i->clientData = &hci;
446 isamd_i->read_item = heap_cread_item;
450 char this_name[INP_NAME_MAX];
451 ISAMD_P isamd_p, isamd_p2;
454 strcpy (this_name, hci.cur_name);
455 assert (hci.cur_name[1]);
457 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
459 memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
460 isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
464 if (!dict_delete (hi->dict, this_name))
470 if (isamd_p2 != isamd_p)
471 dict_insert (hi->dict, this_name,
472 sizeof(ISAMD_P), &isamd_p2);
477 isamd_p = isamd_append (hi->isamd, 0, isamd_i);
479 dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
486 int heap_inp (struct heap_info *hi)
489 char next_name[INP_NAME_MAX];
490 char cur_name[INP_NAME_MAX];
491 int key_buf_size = INP_BUF_START;
497 next_key = (char *) xmalloc (KEY_SIZE);
498 key_buf = (char *) xmalloc (key_buf_size);
499 more = heap_read_one (hi, cur_name, key_buf);
500 while (more) /* EOF ? */
503 key_buf_ptr = KEY_SIZE;
506 if (!(more = heap_read_one (hi, next_name, next_key)))
508 if (*next_name && strcmp (next_name, cur_name))
510 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
511 key_buf_ptr += KEY_SIZE;
512 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
515 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
516 memcpy (new_key_buf, key_buf, key_buf_size);
517 key_buf_size += INP_BUF_ADD;
519 key_buf = new_key_buf;
523 nmemb = key_buf_ptr / KEY_SIZE;
524 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
525 if ((info = dict_lookup (hi->dict, cur_name)))
527 ISAM_P isam_p, isam_p2;
528 memcpy (&isam_p, info+1, sizeof(ISAM_P));
529 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
533 if (!dict_delete (hi->dict, cur_name))
539 if (isam_p2 != isam_p)
540 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
547 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
548 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
550 memcpy (key_buf, next_key, KEY_SIZE);
551 strcpy (cur_name, next_name);
556 int heap_inps (struct heap_info *hi)
558 struct heap_cread_info hci;
559 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
561 hci.key = (char *) xmalloc (KEY_SIZE);
564 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
566 isams_i->clientData = &hci;
567 isams_i->read_item = heap_cread_item;
571 char this_name[INP_NAME_MAX];
575 strcpy (this_name, hci.cur_name);
576 assert (hci.cur_name[1]);
578 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
580 isams_p = isams_merge (hi->isams, isams_i);
582 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
586 logf (LOG_FATAL, "isams doesn't support this kind of update");
594 struct progressInfo {
601 void progressFunc (struct key_file *keyp, void *info)
603 struct progressInfo *p = (struct progressInfo *) info;
604 time_t now, remaining;
606 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
610 if (now >= p->lastTime+10)
613 remaining = (time_t) ((now - p->startTime)*
614 ((double) p->totalBytes/p->totalOffset - 1.0));
615 if (remaining <= 130)
616 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
617 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
619 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
620 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
622 p->totalOffset += keyp->buf_size;
629 void zebra_index_merge (ZebraHandle zh)
631 struct key_file **kf;
634 struct heap_info *hi;
635 struct progressInfo progressInfo;
636 int nkeys = zh->reg->key_file_no;
644 extract_get_fname_tmp (zh, fname, nkeys+1);
645 if (access (fname, R_OK) == -1)
652 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
653 progressInfo.totalBytes = 0;
654 progressInfo.totalOffset = 0;
655 time (&progressInfo.startTime);
656 time (&progressInfo.lastTime);
657 for (i = 1; i<=nkeys; i++)
659 kf[i] = key_file_init (i, 8192, zh->res);
660 kf[i]->readHandler = progressFunc;
661 kf[i]->readInfo = &progressInfo;
662 progressInfo.totalBytes += kf[i]->length;
663 progressInfo.totalOffset += kf[i]->buf_size;
665 hi = key_heap_init (nkeys, key_qsort_compare);
666 hi->dict = zh->reg->dict;
667 hi->isams = zh->reg->isams;
668 hi->isam = zh->reg->isam;
669 hi->isamc = zh->reg->isamc;
670 hi->isamd = zh->reg->isamd;
672 for (i = 1; i<=nkeys; i++)
673 if ((r = key_file_read (kf[i], rbuf)))
674 key_heap_insert (hi, rbuf, r, kf[i]);
677 else if (zh->reg->isamc)
679 else if (zh->reg->isam)
681 else if (zh->reg->isamd)
684 for (i = 1; i<=nkeys; i++)
686 extract_get_fname_tmp (zh, rbuf, i);
689 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
690 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
691 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
692 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
693 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
694 zh->reg->key_file_no = 0;
696 key_heap_destroy (hi, nkeys);
697 for (i = 1; i<=nkeys; i++)
698 key_file_destroy (kf[i]);