1 /* $Id: kinput.c,v 1.66 2005-06-14 20:28:54 adam Exp $
2 Copyright (C) 1995-2005
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
37 #define KEY_SIZE (1+sizeof(struct it_key))
38 #define INP_NAME_MAX 768
39 #define INP_BUF_START 60000
40 #define INP_BUF_ADD 400000
45 off_t offset; /* file offset */
46 unsigned char *buf; /* buffer block */
47 size_t buf_size; /* number of read bytes in block */
48 size_t chunk; /* number of bytes allocated */
49 size_t buf_ptr; /* current position in buffer */
50 char *prev_name; /* last word read */
52 off_t length; /* length of file */
53 /* handler invoked in each read */
54 void (*readHandler)(struct key_file *keyp, void *rinfo);
59 void getFnameTmp (Res res, char *fname, int no)
63 pre = res_get_def (res, "keyTmpDir", ".");
64 sprintf (fname, "%s/key%d.tmp", pre, no);
67 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
71 pre = res_get_def (zh->res, "keyTmpDir", ".");
72 sprintf (fname, "%s/key%d.tmp", pre, no);
75 void key_file_chunk_read (struct key_file *f)
77 int nr = 0, r = 0, fd;
79 getFnameTmp (f->res, fname, f->no);
80 fd = open (fname, O_BINARY|O_RDONLY);
86 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot open %s", fname);
91 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
93 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
98 if (lseek (fd, f->offset, SEEK_SET) == -1)
100 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
104 while (f->chunk - nr > 0)
106 r = read (fd, f->buf + nr, f->chunk - nr);
113 yaz_log (YLOG_WARN|YLOG_ERRNO, "read of %s", fname);
119 (*f->readHandler)(f, f->readInfo);
123 void key_file_destroy (struct key_file *f)
125 iscz1_stop(f->decode_handle);
127 xfree (f->prev_name);
131 struct key_file *key_file_init (int no, int chunk, Res res)
135 f = (struct key_file *) xmalloc (sizeof(*f));
137 f->decode_handle = iscz1_start();
142 f->readHandler = NULL;
143 f->buf = (unsigned char *) xmalloc (f->chunk);
144 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
145 *f->prev_name = '\0';
146 key_file_chunk_read (f);
150 int key_file_getc (struct key_file *f)
152 if (f->buf_ptr < f->buf_size)
153 return f->buf[(f->buf_ptr)++];
154 if (f->buf_size < f->chunk)
156 f->offset += f->buf_size;
157 key_file_chunk_read (f);
158 if (f->buf_ptr < f->buf_size)
159 return f->buf[(f->buf_ptr)++];
164 int key_file_decode (struct key_file *f)
168 c = key_file_getc (f);
175 d = ((c&63) << 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);
182 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
183 d = (d << 8) + (key_file_getc (f) & 0xff);
184 d = (d << 8) + (key_file_getc (f) & 0xff);
190 int key_file_read (struct key_file *f, char *key)
194 const char *src = srcbuf;
198 c = key_file_getc (f);
201 strcpy (key, f->prev_name);
210 while ((key[i++] = key_file_getc (f)))
212 strcpy (f->prev_name, key);
213 iscz1_reset(f->decode_handle);
215 c = key_file_getc(f); /* length + insert/delete combined */
218 for (j = 0; j < c; j++)
219 srcbuf[j] = key_file_getc(f);
221 iscz1_decode(f->decode_handle, &dst, &src);
222 return i + sizeof(struct it_key);
227 struct key_file **file;
232 int (*cmp)(const void *p1, const void *p2);
233 struct zebra_register *reg;
234 ZebraHandle zh; /* only used for raw reading that bypasses the heaps */
242 static struct heap_info *key_heap_malloc()
243 { /* malloc and clear it */
244 struct heap_info *hi;
245 hi = (struct heap_info *) xmalloc (sizeof(*hi));
254 hi->no_deletions = 0;
255 hi->no_insertions = 0;
256 hi->no_iterations = 0;
260 struct heap_info *key_heap_init (int nkeys,
261 int (*cmp)(const void *p1, const void *p2))
263 struct heap_info *hi;
266 hi = key_heap_malloc();
267 hi->info.file = (struct key_file **)
268 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
269 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
270 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
272 for (i = 0; i<= nkeys; i++)
275 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
280 struct heap_info *key_heap_init_buff ( ZebraHandle zh,
281 int (*cmp)(const void *p1, const void *p2))
283 struct heap_info *hi=key_heap_malloc();
289 void key_heap_destroy (struct heap_info *hi, int nkeys)
292 yaz_log (YLOG_DEBUG, "key_heap_destroy");
293 yaz_log (YLOG_DEBUG, "key_heap_destroy nk=%d",nkeys);
295 for (i = 0; i<=nkeys; i++)
296 xfree (hi->info.buf[i]);
298 xfree (hi->info.buf);
300 xfree (hi->info.file);
304 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
309 hi->ptr[i1] = hi->ptr[i2];
314 static void key_heap_delete (struct heap_info *hi)
316 int cur = 1, child = 2;
318 assert (hi->heapnum > 0);
320 key_heap_swap (hi, 1, hi->heapnum);
322 while (child <= hi->heapnum) {
323 if (child < hi->heapnum &&
324 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
325 &hi->info.buf[hi->ptr[child+1]]) > 0)
327 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
328 &hi->info.buf[hi->ptr[child]]) > 0)
330 key_heap_swap (hi, cur, child);
339 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
344 cur = ++(hi->heapnum);
345 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
346 hi->info.file[hi->ptr[cur]] = kf;
349 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
350 &hi->info.buf[hi->ptr[cur]]) > 0)
352 key_heap_swap (hi, cur, parent);
358 static int heap_read_one_raw (struct heap_info *hi, char *name, char *key)
360 ZebraHandle zh=hi->zh;
361 size_t ptr_i = zh->reg->ptr_i;
366 cp=(zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
367 yaz_log (YLOG_DEBUG, " raw: i=%ld top=%ld cp=%p", (long) ptr_i,
368 (long) zh->reg->ptr_top, cp);
370 memcpy(key, cp+strlen(name)+1, KEY_SIZE);
375 static int heap_read_one (struct heap_info *hi, char *name, char *key)
378 char rbuf[INP_NAME_MAX];
381 if (hi->zh) /* bypass the heap stuff, we have a readymade buffer */
382 return heap_read_one_raw(hi, name, key);
387 strcpy (name, hi->info.buf[n]);
388 kf = hi->info.file[n];
390 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
391 key_heap_delete (hi);
392 if ((r = key_file_read (kf, rbuf)))
393 key_heap_insert (hi, rbuf, r, kf);
401 static void pkey(const char *b, int mode)
403 struct it_key *key = (struct it_key *) b;
404 printf ("%c %d:%d\n", mode + 48, key->sysno, key->seqno);
408 struct heap_cread_info {
409 char prev_name[INP_NAME_MAX];
410 char cur_name[INP_NAME_MAX];
415 struct heap_info *hi;
421 static int heap_cread_item (void *vp, char **dst, int *insertMode);
423 int heap_cread_item2 (void *vp, char **dst, int *insertMode)
425 struct heap_cread_info *p = (struct heap_cread_info *) vp;
428 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
433 else if (p->ret == -1) /* Must read new item ? */
435 char *dst_1 = p->key_1;
436 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
437 p->sz_1 = dst_1 - p->key_1;
440 { /* lookahead in 2 . Now in 1. */
442 p->mode_1 = p->mode_2;
443 memcpy (p->key_1, p->key_2, p->sz_2);
446 level = 1; /* insert */
448 level = -1; /* delete */
451 char *dst_2 = p->key_2;
452 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
460 p->sz_2 = dst_2 - p->key_2;
461 if (p->sz_1 == p->sz_2 && memcmp(p->key_1, p->key_2, p->sz_1) == 0)
463 if (p->mode_2) /* adjust level according to deletes/inserts */
472 /* all the same. new round .. */
474 p->mode_1 = p->mode_2;
475 memcpy (p->key_1, p->key_2, p->sz_1);
477 level = 1; /* insert */
479 level = -1; /* delete */
482 /* outcome is insert (1) or delete (0) depending on final level */
487 memcpy (*dst, p->key_1, p->sz_1);
490 pkey(*dst, *insertMode); fflush(stdout);
496 int heap_cread_item (void *vp, char **dst, int *insertMode)
498 struct heap_cread_info *p = (struct heap_cread_info *) vp;
499 struct heap_info *hi = p->hi;
501 if (p->first_in_list)
503 *insertMode = p->key[0];
504 memcpy (*dst, p->key+1, sizeof(struct it_key));
507 pkey(*dst, *insertMode);
509 (*dst) += sizeof(struct it_key);
510 p->first_in_list = 0;
513 strcpy (p->prev_name, p->cur_name);
514 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
516 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
518 p->first_in_list = 1;
521 *insertMode = p->key[0];
522 memcpy (*dst, p->key+1, sizeof(struct it_key));
525 pkey(*dst, *insertMode);
527 (*dst) += sizeof(struct it_key);
531 int heap_inpc (struct heap_info *hi)
533 struct heap_cread_info hci;
534 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
536 hci.key = (char *) xmalloc (KEY_SIZE);
537 hci.key_1 = (char *) xmalloc (KEY_SIZE);
538 hci.key_2 = (char *) xmalloc (KEY_SIZE);
540 hci.first_in_list = 1;
542 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
544 isamc_i->clientData = &hci;
545 isamc_i->read_item = heap_cread_item2;
549 char this_name[INP_NAME_MAX];
550 ISAM_P isamc_p, isamc_p2;
553 strcpy (this_name, hci.cur_name);
554 assert (hci.cur_name[1]);
556 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
558 memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
560 isamc_merge (hi->reg->isamc, &isamc_p2, isamc_i);
564 if (!dict_delete (hi->reg->dict, this_name))
570 if (isamc_p2 != isamc_p)
571 dict_insert (hi->reg->dict, this_name,
572 sizeof(ISAM_P), &isamc_p2);
578 isamc_merge (hi->reg->isamc, &isamc_p, isamc_i);
580 dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isamc_p);
591 /* for debugging only */
592 static void print_dict_item (ZebraMaps zm, const char *s)
595 char keybuf[IT_MAX_WORD+1];
597 const char *from = s + 2;
601 const char *res = zebra_maps_output (zm, reg_type, &from);
609 yaz_log (YLOG_LOG, "%s", keybuf);
613 int heap_inpb (struct heap_info *hi)
615 struct heap_cread_info hci;
616 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
618 hci.key = (char *) xmalloc (KEY_SIZE);
619 hci.key_1 = (char *) xmalloc (KEY_SIZE);
620 hci.key_2 = (char *) xmalloc (KEY_SIZE);
622 hci.first_in_list = 1;
624 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
626 isamc_i->clientData = &hci;
627 isamc_i->read_item = heap_cread_item2;
631 char this_name[INP_NAME_MAX];
632 ISAM_P isamc_p, isamc_p2;
635 strcpy (this_name, hci.cur_name);
636 assert (hci.cur_name[1]);
640 print_dict_item (hi->reg->zebra_maps, hci.cur_name);
642 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
644 memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
646 isamb_merge (hi->reg->isamb, &isamc_p2, 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(ISAM_P), &isamc_p2);
664 isamb_merge (hi->reg->isamb, &isamc_p, isamc_i);
666 dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isamc_p);
676 int heap_inps (struct heap_info *hi)
678 struct heap_cread_info hci;
679 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
681 hci.key = (char *) xmalloc (KEY_SIZE);
682 hci.key_1 = (char *) xmalloc (KEY_SIZE);
683 hci.key_2 = (char *) xmalloc (KEY_SIZE);
684 hci.first_in_list = 1;
687 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
689 isams_i->clientData = &hci;
690 isams_i->read_item = heap_cread_item;
694 char this_name[INP_NAME_MAX];
698 strcpy (this_name, hci.cur_name);
699 assert (hci.cur_name[1]);
701 if (!(dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
703 isams_p = isams_merge (hi->reg->isams, isams_i);
705 dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
709 yaz_log (YLOG_FATAL, "isams doesn't support this kind of update");
717 struct progressInfo {
724 void progressFunc (struct key_file *keyp, void *info)
726 struct progressInfo *p = (struct progressInfo *) info;
727 time_t now, remaining;
729 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
733 if (now >= p->lastTime+10)
736 remaining = (time_t) ((now - p->startTime)*
737 ((double) p->totalBytes/p->totalOffset - 1.0));
738 if (remaining <= 130)
739 yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
740 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
742 yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
743 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
745 p->totalOffset += keyp->buf_size;
752 void zebra_index_merge (ZebraHandle zh)
754 struct key_file **kf = 0;
757 struct heap_info *hi;
758 struct progressInfo progressInfo;
759 int nkeys = zh->reg->key_file_no;
762 yaz_log (YLOG_DEBUG, " index_merge called with nk=%d b=%p",
763 nkeys, zh->reg->key_buf);
764 if ( (nkeys==0) && (zh->reg->key_buf==0) )
765 return; /* nothing to merge - probably flush after end-trans */
767 usefile = (nkeys!=0);
777 extract_get_fname_tmp (zh, fname, nkeys+1);
778 if (access (fname, R_OK) == -1)
785 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
786 progressInfo.totalBytes = 0;
787 progressInfo.totalOffset = 0;
788 time (&progressInfo.startTime);
789 time (&progressInfo.lastTime);
790 for (i = 1; i<=nkeys; i++)
792 kf[i] = key_file_init (i, 8192, zh->res);
793 kf[i]->readHandler = progressFunc;
794 kf[i]->readInfo = &progressInfo;
795 progressInfo.totalBytes += kf[i]->length;
796 progressInfo.totalOffset += kf[i]->buf_size;
798 hi = key_heap_init (nkeys, key_qsort_compare);
801 for (i = 1; i<=nkeys; i++)
802 if ((r = key_file_read (kf[i], rbuf)))
803 key_heap_insert (hi, rbuf, r, kf[i]);
806 { /* do not use file, read straight from buffer */
807 hi = key_heap_init_buff (zh,key_qsort_compare);
819 for (i = 1; i<=nkeys; i++)
821 extract_get_fname_tmp (zh, rbuf, i);
824 for (i = 1; i<=nkeys; i++)
825 key_file_destroy (kf[i]);
828 if (hi->no_iterations)
829 { /* do not log if nothing happened */
830 yaz_log (YLOG_LOG, "Iterations . . .%7d", hi->no_iterations);
831 yaz_log (YLOG_LOG, "Distinct words .%7d", hi->no_diffs);
832 yaz_log (YLOG_LOG, "Updates. . . . .%7d", hi->no_updates);
833 yaz_log (YLOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
834 yaz_log (YLOG_LOG, "Insertions . . .%7d", hi->no_insertions);
836 zh->reg->key_file_no = 0;
838 key_heap_destroy (hi, nkeys);