1 /* $Id: kinput.c,v 1.60 2004-08-04 08:35:23 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
36 #define KEY_SIZE (1+sizeof(struct it_key))
37 #define INP_NAME_MAX 768
38 #define INP_BUF_START 60000
39 #define INP_BUF_ADD 400000
44 off_t offset; /* file offset */
45 unsigned char *buf; /* buffer block */
46 size_t buf_size; /* number of read bytes in block */
47 size_t chunk; /* number of bytes allocated */
48 size_t buf_ptr; /* current position in buffer */
49 char *prev_name; /* last word read */
53 int sysno; /* last sysno */
54 int seqno; /* last seqno */
56 off_t length; /* length of file */
57 /* handler invoked in each read */
58 void (*readHandler)(struct key_file *keyp, void *rinfo);
63 void getFnameTmp (Res res, char *fname, int no)
67 pre = res_get_def (res, "keyTmpDir", ".");
68 sprintf (fname, "%s/key%d.tmp", pre, no);
71 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
75 pre = res_get_def (zh->res, "keyTmpDir", ".");
76 sprintf (fname, "%s/key%d.tmp", pre, no);
79 void key_file_chunk_read (struct key_file *f)
81 int nr = 0, r = 0, fd;
83 getFnameTmp (f->res, fname, f->no);
84 fd = open (fname, O_BINARY|O_RDONLY);
90 logf (LOG_WARN|LOG_ERRNO, "cannot open %s", fname);
95 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
97 logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
102 if (lseek (fd, f->offset, SEEK_SET) == -1)
104 logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
108 while (f->chunk - nr > 0)
110 r = read (fd, f->buf + nr, f->chunk - nr);
117 logf (LOG_WARN|LOG_ERRNO, "read of %s", fname);
123 (*f->readHandler)(f, f->readInfo);
127 void key_file_destroy (struct key_file *f)
130 iscz1_stop(f->decode_handle);
133 xfree (f->prev_name);
137 struct key_file *key_file_init (int no, int chunk, Res res)
141 f = (struct key_file *) xmalloc (sizeof(*f));
144 f->decode_handle = iscz1_start();
153 f->readHandler = NULL;
154 f->buf = (unsigned char *) xmalloc (f->chunk);
155 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
156 *f->prev_name = '\0';
157 key_file_chunk_read (f);
161 int key_file_getc (struct key_file *f)
163 if (f->buf_ptr < f->buf_size)
164 return f->buf[(f->buf_ptr)++];
165 if (f->buf_size < f->chunk)
167 f->offset += f->buf_size;
168 key_file_chunk_read (f);
169 if (f->buf_ptr < f->buf_size)
170 return f->buf[(f->buf_ptr)++];
175 int key_file_decode (struct key_file *f)
179 c = key_file_getc (f);
186 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
189 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
190 d = (d << 8) + (key_file_getc (f) & 0xff);
193 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
194 d = (d << 8) + (key_file_getc (f) & 0xff);
195 d = (d << 8) + (key_file_getc (f) & 0xff);
201 int key_file_read (struct key_file *f, char *key)
206 const char *src = srcbuf;
214 c = key_file_getc (f);
217 strcpy (key, f->prev_name);
226 while ((key[i++] = key_file_getc (f)))
228 strcpy (f->prev_name, key);
230 iscz1_reset(f->decode_handle);
234 c = key_file_getc(f); /* length + insert/delete combined */
237 for (j = 0; j < c; j++)
238 srcbuf[j] = key_file_getc(f);
240 iscz1_decode(f->decode_handle, &dst, &src);
241 return i + sizeof(struct it_key);
243 d = key_file_decode (f);
246 itkey.sysno = d + f->sysno;
249 f->sysno = itkey.sysno;
252 d = key_file_decode (f);
253 itkey.seqno = d + f->seqno;
254 f->seqno = itkey.seqno;
255 memcpy (key + i, &itkey, sizeof(struct it_key));
256 return i + sizeof (struct it_key);
262 struct key_file **file;
267 int (*cmp)(const void *p1, const void *p2);
268 struct zebra_register *reg;
269 ZebraHandle zh; /* only used for raw reading that bypasses the heaps */
277 static struct heap_info *key_heap_malloc()
278 { /* malloc and clear it */
279 struct heap_info *hi;
280 hi = (struct heap_info *) xmalloc (sizeof(*hi));
289 hi->no_deletions = 0;
290 hi->no_insertions = 0;
291 hi->no_iterations = 0;
295 struct heap_info *key_heap_init (int nkeys,
296 int (*cmp)(const void *p1, const void *p2))
298 struct heap_info *hi;
301 hi = key_heap_malloc();
302 hi->info.file = (struct key_file **)
303 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
304 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
305 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
307 for (i = 0; i<= nkeys; i++)
310 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
315 struct heap_info *key_heap_init_buff ( ZebraHandle zh,
316 int (*cmp)(const void *p1, const void *p2))
318 struct heap_info *hi=key_heap_malloc();
324 void key_heap_destroy (struct heap_info *hi, int nkeys)
327 yaz_log (LOG_DEBUG, "key_heap_destroy");
328 yaz_log (LOG_DEBUG, "key_heap_destroy nk=%d",nkeys);
330 for (i = 0; i<=nkeys; i++)
331 xfree (hi->info.buf[i]);
333 xfree (hi->info.buf);
335 xfree (hi->info.file);
339 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
344 hi->ptr[i1] = hi->ptr[i2];
349 static void key_heap_delete (struct heap_info *hi)
351 int cur = 1, child = 2;
353 assert (hi->heapnum > 0);
355 key_heap_swap (hi, 1, hi->heapnum);
357 while (child <= hi->heapnum) {
358 if (child < hi->heapnum &&
359 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
360 &hi->info.buf[hi->ptr[child+1]]) > 0)
362 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
363 &hi->info.buf[hi->ptr[child]]) > 0)
365 key_heap_swap (hi, cur, child);
374 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
379 cur = ++(hi->heapnum);
380 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
381 hi->info.file[hi->ptr[cur]] = kf;
384 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
385 &hi->info.buf[hi->ptr[cur]]) > 0)
387 key_heap_swap (hi, cur, parent);
393 static int heap_read_one_raw (struct heap_info *hi, char *name, char *key)
395 ZebraHandle zh=hi->zh;
396 size_t ptr_i = zh->reg->ptr_i;
401 cp=(zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
402 logf (LOG_DEBUG, " raw: i=%ld top=%ld cp=%p", (long) ptr_i,
403 (long) zh->reg->ptr_top, cp);
405 memcpy(key, cp+strlen(name)+1, KEY_SIZE);
410 static int heap_read_one (struct heap_info *hi, char *name, char *key)
413 char rbuf[INP_NAME_MAX];
416 if (hi->zh) /* bypass the heap stuff, we have a readymade buffer */
417 return heap_read_one_raw(hi, name, key);
422 strcpy (name, hi->info.buf[n]);
423 kf = hi->info.file[n];
425 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
426 key_heap_delete (hi);
427 if ((r = key_file_read (kf, rbuf)))
428 key_heap_insert (hi, rbuf, r, kf);
436 static void pkey(const char *b, int mode)
438 struct it_key *key = (struct it_key *) b;
439 printf ("%c %d:%d\n", mode + 48, key->sysno, key->seqno);
443 struct heap_cread_info {
444 char prev_name[INP_NAME_MAX];
445 char cur_name[INP_NAME_MAX];
450 struct heap_info *hi;
456 static int heap_cread_item (void *vp, char **dst, int *insertMode);
458 int heap_cread_item2 (void *vp, char **dst, int *insertMode)
460 struct heap_cread_info *p = (struct heap_cread_info *) vp;
463 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
468 else if (p->ret == -1) /* Must read new item ? */
470 char *dst_1 = p->key_1;
471 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
472 p->sz_1 = dst_1 - p->key_1;
475 { /* lookahead in 2 . Now in 1. */
477 p->mode_1 = p->mode_2;
478 memcpy (p->key_1, p->key_2, p->sz_2);
481 level = 1; /* insert */
483 level = -1; /* delete */
486 char *dst_2 = p->key_2;
487 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
495 p->sz_2 = dst_2 - p->key_2;
496 if (p->sz_1 == p->sz_2 && memcmp(p->key_1, p->key_2, p->sz_1) == 0)
498 if (p->mode_2) /* adjust level according to deletes/inserts */
507 /* all the same. new round .. */
509 p->mode_1 = p->mode_2;
510 memcpy (p->key_1, p->key_2, p->sz_1);
512 level = 1; /* insert */
514 level = -1; /* delete */
517 /* outcome is insert (1) or delete (0) depending on final level */
522 memcpy (*dst, p->key_1, p->sz_1);
525 pkey(*dst, *insertMode); fflush(stdout);
531 int heap_cread_item (void *vp, char **dst, int *insertMode)
533 struct heap_cread_info *p = (struct heap_cread_info *) vp;
534 struct heap_info *hi = p->hi;
536 if (p->first_in_list)
538 *insertMode = p->key[0];
539 memcpy (*dst, p->key+1, sizeof(struct it_key));
542 pkey(*dst, *insertMode);
544 (*dst) += sizeof(struct it_key);
545 p->first_in_list = 0;
548 strcpy (p->prev_name, p->cur_name);
549 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
551 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
553 p->first_in_list = 1;
556 *insertMode = p->key[0];
557 memcpy (*dst, p->key+1, sizeof(struct it_key));
560 pkey(*dst, *insertMode);
562 (*dst) += sizeof(struct it_key);
566 int heap_inpc (struct heap_info *hi)
568 struct heap_cread_info hci;
569 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
571 hci.key = (char *) xmalloc (KEY_SIZE);
572 hci.key_1 = (char *) xmalloc (KEY_SIZE);
573 hci.key_2 = (char *) xmalloc (KEY_SIZE);
575 hci.first_in_list = 1;
577 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
579 isamc_i->clientData = &hci;
580 isamc_i->read_item = heap_cread_item2;
584 char this_name[INP_NAME_MAX];
585 ISAMC_P isamc_p, isamc_p2;
588 strcpy (this_name, hci.cur_name);
589 assert (hci.cur_name[1]);
591 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
593 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
594 isamc_p2 = isc_merge (hi->reg->isamc, isamc_p, isamc_i);
598 if (!dict_delete (hi->reg->dict, this_name))
604 if (isamc_p2 != isamc_p)
605 dict_insert (hi->reg->dict, this_name,
606 sizeof(ISAMC_P), &isamc_p2);
611 isamc_p = isc_merge (hi->reg->isamc, 0, isamc_i);
613 dict_insert (hi->reg->dict, this_name, sizeof(ISAMC_P), &isamc_p);
624 /* for debugging only */
625 static void print_dict_item (ZebraMaps zm, const char *s)
628 char keybuf[IT_MAX_WORD+1];
630 const char *from = s + 2;
634 const char *res = zebra_maps_output (zm, reg_type, &from);
642 yaz_log (LOG_LOG, "%s", keybuf);
646 int heap_inpb (struct heap_info *hi)
648 struct heap_cread_info hci;
649 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
651 hci.key = (char *) xmalloc (KEY_SIZE);
652 hci.key_1 = (char *) xmalloc (KEY_SIZE);
653 hci.key_2 = (char *) xmalloc (KEY_SIZE);
655 hci.first_in_list = 1;
657 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
659 isamc_i->clientData = &hci;
660 isamc_i->read_item = heap_cread_item2;
664 char this_name[INP_NAME_MAX];
665 ISAMC_P isamc_p, isamc_p2;
668 strcpy (this_name, hci.cur_name);
669 assert (hci.cur_name[1]);
673 print_dict_item (hi->reg->zebra_maps, hci.cur_name);
675 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
677 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
678 isamc_p2 = isamb_merge (hi->reg->isamb, isamc_p, isamc_i);
682 if (!dict_delete (hi->reg->dict, this_name))
688 if (isamc_p2 != isamc_p)
689 dict_insert (hi->reg->dict, this_name,
690 sizeof(ISAMC_P), &isamc_p2);
695 isamc_p = isamb_merge (hi->reg->isamb, 0, isamc_i);
697 dict_insert (hi->reg->dict, this_name, sizeof(ISAMC_P), &isamc_p);
707 int heap_inp (struct heap_info *hi)
710 char next_name[INP_NAME_MAX];
711 char cur_name[INP_NAME_MAX];
712 int key_buf_size = INP_BUF_START;
718 next_key = (char *) xmalloc (KEY_SIZE);
719 key_buf = (char *) xmalloc (key_buf_size);
720 more = heap_read_one (hi, cur_name, key_buf);
721 while (more) /* EOF ? */
724 key_buf_ptr = KEY_SIZE;
727 if (!(more = heap_read_one (hi, next_name, next_key)))
729 if (*next_name && strcmp (next_name, cur_name))
731 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
732 key_buf_ptr += KEY_SIZE;
733 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
736 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
737 memcpy (new_key_buf, key_buf, key_buf_size);
738 key_buf_size += INP_BUF_ADD;
740 key_buf = new_key_buf;
744 nmemb = key_buf_ptr / KEY_SIZE;
745 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
746 if ((info = dict_lookup (hi->reg->dict, cur_name)))
748 ISAM_P isam_p, isam_p2;
749 memcpy (&isam_p, info+1, sizeof(ISAM_P));
750 isam_p2 = is_merge (hi->reg->isam, isam_p, nmemb, key_buf);
754 if (!dict_delete (hi->reg->dict, cur_name))
760 if (isam_p2 != isam_p)
761 dict_insert (hi->reg->dict, cur_name,
762 sizeof(ISAM_P), &isam_p2);
769 isam_p = is_merge (hi->reg->isam, 0, nmemb, key_buf);
770 dict_insert (hi->reg->dict, cur_name, sizeof(ISAM_P), &isam_p);
772 memcpy (key_buf, next_key, KEY_SIZE);
773 strcpy (cur_name, next_name);
778 int heap_inps (struct heap_info *hi)
780 struct heap_cread_info hci;
781 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
783 hci.key = (char *) xmalloc (KEY_SIZE);
784 hci.key_1 = (char *) xmalloc (KEY_SIZE);
785 hci.key_2 = (char *) xmalloc (KEY_SIZE);
786 hci.first_in_list = 1;
789 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
791 isams_i->clientData = &hci;
792 isams_i->read_item = heap_cread_item;
796 char this_name[INP_NAME_MAX];
800 strcpy (this_name, hci.cur_name);
801 assert (hci.cur_name[1]);
803 if (!(dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
805 isams_p = isams_merge (hi->reg->isams, isams_i);
807 dict_insert (hi->reg->dict, this_name, sizeof(ISAMS_P), &isams_p);
811 logf (LOG_FATAL, "isams doesn't support this kind of update");
819 struct progressInfo {
826 void progressFunc (struct key_file *keyp, void *info)
828 struct progressInfo *p = (struct progressInfo *) info;
829 time_t now, remaining;
831 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
835 if (now >= p->lastTime+10)
838 remaining = (time_t) ((now - p->startTime)*
839 ((double) p->totalBytes/p->totalOffset - 1.0));
840 if (remaining <= 130)
841 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
842 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
844 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
845 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
847 p->totalOffset += keyp->buf_size;
854 void zebra_index_merge (ZebraHandle zh)
856 struct key_file **kf = 0;
859 struct heap_info *hi;
860 struct progressInfo progressInfo;
861 int nkeys = zh->reg->key_file_no;
864 logf (LOG_DEBUG, " index_merge called with nk=%d b=%p",
865 nkeys, zh->reg->key_buf);
866 if ( (nkeys==0) && (zh->reg->key_buf==0) )
867 return; /* nothing to merge - probably flush after end-trans */
869 usefile = (nkeys!=0);
879 extract_get_fname_tmp (zh, fname, nkeys+1);
880 if (access (fname, R_OK) == -1)
887 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
888 progressInfo.totalBytes = 0;
889 progressInfo.totalOffset = 0;
890 time (&progressInfo.startTime);
891 time (&progressInfo.lastTime);
892 for (i = 1; i<=nkeys; i++)
894 kf[i] = key_file_init (i, 8192, zh->res);
895 kf[i]->readHandler = progressFunc;
896 kf[i]->readInfo = &progressInfo;
897 progressInfo.totalBytes += kf[i]->length;
898 progressInfo.totalOffset += kf[i]->buf_size;
900 hi = key_heap_init (nkeys, key_qsort_compare);
903 for (i = 1; i<=nkeys; i++)
904 if ((r = key_file_read (kf[i], rbuf)))
905 key_heap_insert (hi, rbuf, r, kf[i]);
908 { /* do not use file, read straight from buffer */
909 hi = key_heap_init_buff (zh,key_qsort_compare);
923 for (i = 1; i<=nkeys; i++)
925 extract_get_fname_tmp (zh, rbuf, i);
928 for (i = 1; i<=nkeys; i++)
929 key_file_destroy (kf[i]);
932 if (hi->no_iterations)
933 { /* do not log if nothing happened */
934 logf (LOG_LOG, "Iterations . . .%7d", hi->no_iterations);
935 logf (LOG_LOG, "Distinct words .%7d", hi->no_diffs);
936 logf (LOG_LOG, "Updates. . . . .%7d", hi->no_updates);
937 logf (LOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
938 logf (LOG_LOG, "Insertions . . .%7d", hi->no_insertions);
940 zh->reg->key_file_no = 0;
942 key_heap_destroy (hi, nkeys);