1 /* $Id: kinput.c,v 1.57 2004-01-22 15:40:25 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
38 #define KEY_SIZE (1+sizeof(struct it_key))
39 #define INP_NAME_MAX 768
40 #define INP_BUF_START 60000
41 #define INP_BUF_ADD 400000
46 off_t offset; /* file offset */
47 unsigned char *buf; /* buffer block */
48 size_t buf_size; /* number of read bytes in block */
49 size_t chunk; /* number of bytes allocated */
50 size_t buf_ptr; /* current position in buffer */
51 char *prev_name; /* last word read */
52 int sysno; /* last sysno */
53 int seqno; /* last seqno */
54 off_t length; /* length of file */
55 /* handler invoked in each read */
56 void (*readHandler)(struct key_file *keyp, void *rinfo);
61 void getFnameTmp (Res res, char *fname, int no)
65 pre = res_get_def (res, "keyTmpDir", ".");
66 sprintf (fname, "%s/key%d.tmp", pre, no);
69 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
73 pre = res_get_def (zh->res, "keyTmpDir", ".");
74 sprintf (fname, "%s/key%d.tmp", pre, no);
77 void key_file_chunk_read (struct key_file *f)
79 int nr = 0, r = 0, fd;
81 getFnameTmp (f->res, fname, f->no);
82 fd = open (fname, O_BINARY|O_RDONLY);
88 logf (LOG_WARN|LOG_ERRNO, "cannot open %s", fname);
93 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
95 logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
100 if (lseek (fd, f->offset, SEEK_SET) == -1)
102 logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
106 while (f->chunk - nr > 0)
108 r = read (fd, f->buf + nr, f->chunk - nr);
115 logf (LOG_WARN|LOG_ERRNO, "read of %s", fname);
121 (*f->readHandler)(f, f->readInfo);
125 void key_file_destroy (struct key_file *f)
128 xfree (f->prev_name);
132 struct key_file *key_file_init (int no, int chunk, Res res)
136 f = (struct key_file *) xmalloc (sizeof(*f));
144 f->readHandler = NULL;
145 f->buf = (unsigned char *) xmalloc (f->chunk);
146 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
147 *f->prev_name = '\0';
148 key_file_chunk_read (f);
152 int key_file_getc (struct key_file *f)
154 if (f->buf_ptr < f->buf_size)
155 return f->buf[(f->buf_ptr)++];
156 if (f->buf_size < f->chunk)
158 f->offset += f->buf_size;
159 key_file_chunk_read (f);
160 if (f->buf_ptr < f->buf_size)
161 return f->buf[(f->buf_ptr)++];
166 int key_file_decode (struct key_file *f)
170 c = key_file_getc (f);
177 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
180 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
181 d = (d << 8) + (key_file_getc (f) & 0xff);
184 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
185 d = (d << 8) + (key_file_getc (f) & 0xff);
186 d = (d << 8) + (key_file_getc (f) & 0xff);
192 int key_file_read (struct key_file *f, char *key)
197 c = key_file_getc (f);
200 strcpy (key, f->prev_name);
209 while ((key[i++] = key_file_getc (f)))
211 strcpy (f->prev_name, key);
214 d = key_file_decode (f);
217 itkey.sysno = d + f->sysno;
220 f->sysno = itkey.sysno;
223 d = key_file_decode (f);
224 itkey.seqno = d + f->seqno;
225 f->seqno = itkey.seqno;
226 memcpy (key + i, &itkey, sizeof(struct it_key));
227 return i + sizeof (struct it_key);
232 struct key_file **file;
237 int (*cmp)(const void *p1, const void *p2);
238 struct zebra_register *reg;
239 ZebraHandle zh; /* only used for raw reading that bypasses the heaps */
247 static struct heap_info *key_heap_malloc()
248 { /* malloc and clear it */
249 struct heap_info *hi;
250 hi = (struct heap_info *) xmalloc (sizeof(*hi));
259 hi->no_deletions = 0;
260 hi->no_insertions = 0;
261 hi->no_iterations = 0;
265 struct heap_info *key_heap_init (int nkeys,
266 int (*cmp)(const void *p1, const void *p2))
268 struct heap_info *hi;
271 hi = key_heap_malloc();
272 hi->info.file = (struct key_file **)
273 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
274 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
275 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
277 for (i = 0; i<= nkeys; i++)
280 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
285 struct heap_info *key_heap_init_buff ( ZebraHandle zh,
286 int (*cmp)(const void *p1, const void *p2))
288 struct heap_info *hi=key_heap_malloc();
294 void key_heap_destroy (struct heap_info *hi, int nkeys)
297 yaz_log (LOG_DEBUG, "key_heap_destroy");
298 yaz_log (LOG_DEBUG, "key_heap_destroy nk=%d",nkeys);
300 for (i = 0; i<=nkeys; i++)
301 xfree (hi->info.buf[i]);
303 xfree (hi->info.buf);
305 xfree (hi->info.file);
309 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
314 hi->ptr[i1] = hi->ptr[i2];
319 static void key_heap_delete (struct heap_info *hi)
321 int cur = 1, child = 2;
323 assert (hi->heapnum > 0);
325 key_heap_swap (hi, 1, hi->heapnum);
327 while (child <= hi->heapnum) {
328 if (child < hi->heapnum &&
329 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
330 &hi->info.buf[hi->ptr[child+1]]) > 0)
332 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
333 &hi->info.buf[hi->ptr[child]]) > 0)
335 key_heap_swap (hi, cur, child);
344 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
349 cur = ++(hi->heapnum);
350 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
351 hi->info.file[hi->ptr[cur]] = kf;
354 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
355 &hi->info.buf[hi->ptr[cur]]) > 0)
357 key_heap_swap (hi, cur, parent);
363 static int heap_read_one_raw (struct heap_info *hi, char *name, char *key)
365 ZebraHandle zh=hi->zh;
366 int ptr_i = zh->reg->ptr_i--;
370 cp=(zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
371 logf (LOG_DEBUG, " raw: i=%d top=%d cp=%p", ptr_i, zh->reg->ptr_top,cp);
373 memcpy(key, cp+strlen(name)+1, KEY_SIZE);
378 static int heap_read_one (struct heap_info *hi, char *name, char *key)
381 char rbuf[INP_NAME_MAX];
384 if (hi->zh) /* bypass the heap stuff, we have a readymade buffer */
385 return heap_read_one_raw(hi, name, key);
390 strcpy (name, hi->info.buf[n]);
391 kf = hi->info.file[n];
393 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
394 key_heap_delete (hi);
395 if ((r = key_file_read (kf, rbuf)))
396 key_heap_insert (hi, rbuf, r, kf);
404 static void pkey(const char *b, int mode)
406 struct it_key *key = (struct it_key *) b;
407 printf ("%c %d:%d\n", mode + 48, key->sysno, key->seqno);
411 struct heap_cread_info {
412 char prev_name[INP_NAME_MAX];
413 char cur_name[INP_NAME_MAX];
418 struct heap_info *hi;
424 static int heap_cread_item (void *vp, char **dst, int *insertMode);
426 int heap_cread_item2 (void *vp, char **dst, int *insertMode)
428 struct heap_cread_info *p = (struct heap_cread_info *) vp;
431 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
436 else if (p->ret == -1) /* Must read new item ? */
438 char *dst_1 = p->key_1;
439 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
440 p->sz_1 = dst_1 - p->key_1;
443 { /* lookahead in 2 . Now in 1. */
445 p->mode_1 = p->mode_2;
446 memcpy (p->key_1, p->key_2, p->sz_2);
449 level = 1; /* insert */
451 level = -1; /* delete */
454 char *dst_2 = p->key_2;
455 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
463 p->sz_2 = dst_2 - p->key_2;
464 if (p->sz_1 == p->sz_2 && memcmp(p->key_1, p->key_2, p->sz_1) == 0)
466 if (p->mode_2) /* adjust level according to deletes/inserts */
475 /* all the same. new round .. */
477 p->mode_1 = p->mode_2;
478 memcpy (p->key_1, p->key_2, p->sz_1);
480 level = 1; /* insert */
482 level = -1; /* delete */
485 /* outcome is insert (1) or delete (0) depending on final level */
490 memcpy (*dst, p->key_1, p->sz_1);
493 pkey(*dst, *insertMode); fflush(stdout);
499 int heap_cread_item (void *vp, char **dst, int *insertMode)
501 struct heap_cread_info *p = (struct heap_cread_info *) vp;
502 struct heap_info *hi = p->hi;
504 if (p->first_in_list)
506 *insertMode = p->key[0];
507 memcpy (*dst, p->key+1, sizeof(struct it_key));
510 pkey(*dst, *insertMode);
512 (*dst) += sizeof(struct it_key);
513 p->first_in_list = 0;
516 strcpy (p->prev_name, p->cur_name);
517 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
519 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
521 p->first_in_list = 1;
524 *insertMode = p->key[0];
525 memcpy (*dst, p->key+1, sizeof(struct it_key));
528 pkey(*dst, *insertMode);
530 (*dst) += sizeof(struct it_key);
534 int heap_inpc (struct heap_info *hi)
536 struct heap_cread_info hci;
537 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
539 hci.key = (char *) xmalloc (KEY_SIZE);
540 hci.key_1 = (char *) xmalloc (KEY_SIZE);
541 hci.key_2 = (char *) xmalloc (KEY_SIZE);
543 hci.first_in_list = 1;
545 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
547 isamc_i->clientData = &hci;
548 isamc_i->read_item = heap_cread_item2;
552 char this_name[INP_NAME_MAX];
553 ISAMC_P isamc_p, isamc_p2;
556 strcpy (this_name, hci.cur_name);
557 assert (hci.cur_name[1]);
559 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
561 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
562 isamc_p2 = isc_merge (hi->reg->isamc, isamc_p, isamc_i);
566 if (!dict_delete (hi->reg->dict, this_name))
572 if (isamc_p2 != isamc_p)
573 dict_insert (hi->reg->dict, this_name,
574 sizeof(ISAMC_P), &isamc_p2);
579 isamc_p = isc_merge (hi->reg->isamc, 0, isamc_i);
581 dict_insert (hi->reg->dict, this_name, sizeof(ISAMC_P), &isamc_p);
592 /* for debugging only */
593 static void print_dict_item (ZebraMaps zm, const char *s)
596 char keybuf[IT_MAX_WORD+1];
598 const char *from = s + 2;
602 const char *res = zebra_maps_output (zm, reg_type, &from);
610 yaz_log (LOG_LOG, "%s", keybuf);
614 int heap_inpb (struct heap_info *hi)
616 struct heap_cread_info hci;
617 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
619 hci.key = (char *) xmalloc (KEY_SIZE);
620 hci.key_1 = (char *) xmalloc (KEY_SIZE);
621 hci.key_2 = (char *) xmalloc (KEY_SIZE);
623 hci.first_in_list = 1;
625 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
627 isamc_i->clientData = &hci;
628 isamc_i->read_item = heap_cread_item2;
632 char this_name[INP_NAME_MAX];
633 ISAMC_P isamc_p, isamc_p2;
636 strcpy (this_name, hci.cur_name);
637 assert (hci.cur_name[1]);
641 print_dict_item (hi->reg->zebra_maps, hci.cur_name);
643 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
645 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
646 isamc_p2 = isamb_merge (hi->reg->isamb, isamc_p, isamc_i);
650 if (!dict_delete (hi->reg->dict, this_name))
656 if (isamc_p2 != isamc_p)
657 dict_insert (hi->reg->dict, this_name,
658 sizeof(ISAMC_P), &isamc_p2);
663 isamc_p = isamb_merge (hi->reg->isamb, 0, isamc_i);
665 dict_insert (hi->reg->dict, this_name, sizeof(ISAMC_P), &isamc_p);
675 int heap_inpd (struct heap_info *hi)
677 struct heap_cread_info hci;
678 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
680 hci.key = (char *) xmalloc (KEY_SIZE);
681 hci.key_1 = (char *) xmalloc (KEY_SIZE);
682 hci.key_2 = (char *) xmalloc (KEY_SIZE);
684 hci.first_in_list = 1;
686 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
688 isamd_i->clientData = &hci;
689 isamd_i->read_item = heap_cread_item;
693 char this_name[INP_NAME_MAX];
695 char dictentry[ISAMD_MAX_DICT_LEN+1];
698 strcpy (this_name, hci.cur_name);
700 /* print_dict_item (hi->reg->zebra_maps, hci.cur_name); */
701 /*!*/ /* FIXME: depend on isamd-debug */
703 assert (hci.cur_name[1]);
705 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
707 dictlen=dict_info[0];
708 memcpy (dictentry, dict_info+1, dictlen );
710 logf(LOG_LOG,"dictentry before. len=%d: %d %d %d %d %d %d %d %d %d",
711 dictlen,dictentry[0], dictentry[1], dictentry[2],
712 dictentry[3], dictentry[4], dictentry[5],
713 dictentry[6], dictentry[7], dictentry[8]); /*!*/
715 dictlen= isamd_append(hi->reg->isamd, dictentry, dictlen, isamd_i);
716 /* logf dictentry after */
720 if ( (dictlen!=dict_info[0]) ||
721 (0!=memcmp(dictentry, dict_info+1, dictlen)) )
723 dict_insert(hi->reg->dict, this_name,
730 if (!dict_delete (hi->reg->dict, this_name))
732 logf (LOG_FATAL, "dict_delete failed");
740 memset (dictentry, '\0', ISAMD_MAX_DICT_LEN);
741 dictlen= isamd_append(hi->reg->isamd, dictentry, dictlen, isamd_i);
742 /* logf dictentry first */
745 dict_insert(hi->reg->dict, this_name,
756 int heap_inp (struct heap_info *hi)
759 char next_name[INP_NAME_MAX];
760 char cur_name[INP_NAME_MAX];
761 int key_buf_size = INP_BUF_START;
767 next_key = (char *) xmalloc (KEY_SIZE);
768 key_buf = (char *) xmalloc (key_buf_size);
769 more = heap_read_one (hi, cur_name, key_buf);
770 while (more) /* EOF ? */
773 key_buf_ptr = KEY_SIZE;
776 if (!(more = heap_read_one (hi, next_name, next_key)))
778 if (*next_name && strcmp (next_name, cur_name))
780 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
781 key_buf_ptr += KEY_SIZE;
782 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
785 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
786 memcpy (new_key_buf, key_buf, key_buf_size);
787 key_buf_size += INP_BUF_ADD;
789 key_buf = new_key_buf;
793 nmemb = key_buf_ptr / KEY_SIZE;
794 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
795 if ((info = dict_lookup (hi->reg->dict, cur_name)))
797 ISAM_P isam_p, isam_p2;
798 memcpy (&isam_p, info+1, sizeof(ISAM_P));
799 isam_p2 = is_merge (hi->reg->isam, isam_p, nmemb, key_buf);
803 if (!dict_delete (hi->reg->dict, cur_name))
809 if (isam_p2 != isam_p)
810 dict_insert (hi->reg->dict, cur_name,
811 sizeof(ISAM_P), &isam_p2);
818 isam_p = is_merge (hi->reg->isam, 0, nmemb, key_buf);
819 dict_insert (hi->reg->dict, cur_name, sizeof(ISAM_P), &isam_p);
821 memcpy (key_buf, next_key, KEY_SIZE);
822 strcpy (cur_name, next_name);
827 int heap_inps (struct heap_info *hi)
829 struct heap_cread_info hci;
830 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
832 hci.key = (char *) xmalloc (KEY_SIZE);
833 hci.key_1 = (char *) xmalloc (KEY_SIZE);
834 hci.key_2 = (char *) xmalloc (KEY_SIZE);
835 hci.first_in_list = 1;
838 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
840 isams_i->clientData = &hci;
841 isams_i->read_item = heap_cread_item;
845 char this_name[INP_NAME_MAX];
849 strcpy (this_name, hci.cur_name);
850 assert (hci.cur_name[1]);
852 if (!(dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
854 isams_p = isams_merge (hi->reg->isams, isams_i);
856 dict_insert (hi->reg->dict, this_name, sizeof(ISAMS_P), &isams_p);
860 logf (LOG_FATAL, "isams doesn't support this kind of update");
868 struct progressInfo {
875 void progressFunc (struct key_file *keyp, void *info)
877 struct progressInfo *p = (struct progressInfo *) info;
878 time_t now, remaining;
880 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
884 if (now >= p->lastTime+10)
887 remaining = (time_t) ((now - p->startTime)*
888 ((double) p->totalBytes/p->totalOffset - 1.0));
889 if (remaining <= 130)
890 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
891 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
893 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
894 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
896 p->totalOffset += keyp->buf_size;
903 void zebra_index_merge (ZebraHandle zh)
905 struct key_file **kf;
908 struct heap_info *hi;
909 struct progressInfo progressInfo;
910 int nkeys = zh->reg->key_file_no;
913 logf (LOG_DEBUG, " index_merge called with nk=%d b=%p",
914 nkeys, zh->reg->key_buf);
915 if ( (nkeys==0) && (zh->reg->key_buf==0) )
916 return; /* nothing to merge - probably flush after end-trans */
918 usefile = (nkeys!=0);
928 extract_get_fname_tmp (zh, fname, nkeys+1);
929 if (access (fname, R_OK) == -1)
936 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
937 progressInfo.totalBytes = 0;
938 progressInfo.totalOffset = 0;
939 time (&progressInfo.startTime);
940 time (&progressInfo.lastTime);
941 for (i = 1; i<=nkeys; i++)
943 kf[i] = key_file_init (i, 8192, zh->res);
944 kf[i]->readHandler = progressFunc;
945 kf[i]->readInfo = &progressInfo;
946 progressInfo.totalBytes += kf[i]->length;
947 progressInfo.totalOffset += kf[i]->buf_size;
949 hi = key_heap_init (nkeys, key_qsort_compare);
952 for (i = 1; i<=nkeys; i++)
953 if ((r = key_file_read (kf[i], rbuf)))
954 key_heap_insert (hi, rbuf, r, kf[i]);
957 { /* do not use file, read straight from buffer */
958 hi = key_heap_init_buff (zh,key_qsort_compare);
974 for (i = 1; i<=nkeys; i++)
976 extract_get_fname_tmp (zh, rbuf, i);
979 for (i = 1; i<=nkeys; i++)
980 key_file_destroy (kf[i]);
983 if (hi->no_iterations)
984 { /* do not log if nothing happened */
985 logf (LOG_LOG, "Iterations . . .%7d", hi->no_iterations);
986 logf (LOG_LOG, "Distinct words .%7d", hi->no_diffs);
987 logf (LOG_LOG, "Updates. . . . .%7d", hi->no_updates);
988 logf (LOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
989 logf (LOG_LOG, "Insertions . . .%7d", hi->no_insertions);
991 zh->reg->key_file_no = 0;
993 key_heap_destroy (hi, nkeys);