1 /* $Id: kinput.c,v 1.65 2005-04-13 13:03:47 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
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 */
51 off_t length; /* length of file */
52 /* handler invoked in each read */
53 void (*readHandler)(struct key_file *keyp, void *rinfo);
58 void getFnameTmp (Res res, char *fname, int no)
62 pre = res_get_def (res, "keyTmpDir", ".");
63 sprintf (fname, "%s/key%d.tmp", pre, no);
66 void extract_get_fname_tmp (ZebraHandle zh, char *fname, int no)
70 pre = res_get_def (zh->res, "keyTmpDir", ".");
71 sprintf (fname, "%s/key%d.tmp", pre, no);
74 void key_file_chunk_read (struct key_file *f)
76 int nr = 0, r = 0, fd;
78 getFnameTmp (f->res, fname, f->no);
79 fd = open (fname, O_BINARY|O_RDONLY);
85 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot open %s", fname);
90 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
92 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
97 if (lseek (fd, f->offset, SEEK_SET) == -1)
99 yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
103 while (f->chunk - nr > 0)
105 r = read (fd, f->buf + nr, f->chunk - nr);
112 yaz_log (YLOG_WARN|YLOG_ERRNO, "read of %s", fname);
118 (*f->readHandler)(f, f->readInfo);
122 void key_file_destroy (struct key_file *f)
124 iscz1_stop(f->decode_handle);
126 xfree (f->prev_name);
130 struct key_file *key_file_init (int no, int chunk, Res res)
134 f = (struct key_file *) xmalloc (sizeof(*f));
136 f->decode_handle = iscz1_start();
141 f->readHandler = NULL;
142 f->buf = (unsigned char *) xmalloc (f->chunk);
143 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
144 *f->prev_name = '\0';
145 key_file_chunk_read (f);
149 int key_file_getc (struct key_file *f)
151 if (f->buf_ptr < f->buf_size)
152 return f->buf[(f->buf_ptr)++];
153 if (f->buf_size < f->chunk)
155 f->offset += f->buf_size;
156 key_file_chunk_read (f);
157 if (f->buf_ptr < f->buf_size)
158 return f->buf[(f->buf_ptr)++];
163 int key_file_decode (struct key_file *f)
167 c = key_file_getc (f);
174 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
177 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
178 d = (d << 8) + (key_file_getc (f) & 0xff);
181 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
182 d = (d << 8) + (key_file_getc (f) & 0xff);
183 d = (d << 8) + (key_file_getc (f) & 0xff);
189 int key_file_read (struct key_file *f, char *key)
193 const char *src = srcbuf;
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);
212 iscz1_reset(f->decode_handle);
214 c = key_file_getc(f); /* length + insert/delete combined */
217 for (j = 0; j < c; j++)
218 srcbuf[j] = key_file_getc(f);
220 iscz1_decode(f->decode_handle, &dst, &src);
221 return i + sizeof(struct it_key);
226 struct key_file **file;
231 int (*cmp)(const void *p1, const void *p2);
232 struct zebra_register *reg;
233 ZebraHandle zh; /* only used for raw reading that bypasses the heaps */
241 static struct heap_info *key_heap_malloc()
242 { /* malloc and clear it */
243 struct heap_info *hi;
244 hi = (struct heap_info *) xmalloc (sizeof(*hi));
253 hi->no_deletions = 0;
254 hi->no_insertions = 0;
255 hi->no_iterations = 0;
259 struct heap_info *key_heap_init (int nkeys,
260 int (*cmp)(const void *p1, const void *p2))
262 struct heap_info *hi;
265 hi = key_heap_malloc();
266 hi->info.file = (struct key_file **)
267 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
268 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
269 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
271 for (i = 0; i<= nkeys; i++)
274 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
279 struct heap_info *key_heap_init_buff ( ZebraHandle zh,
280 int (*cmp)(const void *p1, const void *p2))
282 struct heap_info *hi=key_heap_malloc();
288 void key_heap_destroy (struct heap_info *hi, int nkeys)
291 yaz_log (YLOG_DEBUG, "key_heap_destroy");
292 yaz_log (YLOG_DEBUG, "key_heap_destroy nk=%d",nkeys);
294 for (i = 0; i<=nkeys; i++)
295 xfree (hi->info.buf[i]);
297 xfree (hi->info.buf);
299 xfree (hi->info.file);
303 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
308 hi->ptr[i1] = hi->ptr[i2];
313 static void key_heap_delete (struct heap_info *hi)
315 int cur = 1, child = 2;
317 assert (hi->heapnum > 0);
319 key_heap_swap (hi, 1, hi->heapnum);
321 while (child <= hi->heapnum) {
322 if (child < hi->heapnum &&
323 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
324 &hi->info.buf[hi->ptr[child+1]]) > 0)
326 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
327 &hi->info.buf[hi->ptr[child]]) > 0)
329 key_heap_swap (hi, cur, child);
338 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
343 cur = ++(hi->heapnum);
344 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
345 hi->info.file[hi->ptr[cur]] = kf;
348 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
349 &hi->info.buf[hi->ptr[cur]]) > 0)
351 key_heap_swap (hi, cur, parent);
357 static int heap_read_one_raw (struct heap_info *hi, char *name, char *key)
359 ZebraHandle zh=hi->zh;
360 size_t ptr_i = zh->reg->ptr_i;
365 cp=(zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
366 yaz_log (YLOG_DEBUG, " raw: i=%ld top=%ld cp=%p", (long) ptr_i,
367 (long) zh->reg->ptr_top, cp);
369 memcpy(key, cp+strlen(name)+1, KEY_SIZE);
374 static int heap_read_one (struct heap_info *hi, char *name, char *key)
377 char rbuf[INP_NAME_MAX];
380 if (hi->zh) /* bypass the heap stuff, we have a readymade buffer */
381 return heap_read_one_raw(hi, name, key);
386 strcpy (name, hi->info.buf[n]);
387 kf = hi->info.file[n];
389 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
390 key_heap_delete (hi);
391 if ((r = key_file_read (kf, rbuf)))
392 key_heap_insert (hi, rbuf, r, kf);
400 static void pkey(const char *b, int mode)
402 struct it_key *key = (struct it_key *) b;
403 printf ("%c %d:%d\n", mode + 48, key->sysno, key->seqno);
407 struct heap_cread_info {
408 char prev_name[INP_NAME_MAX];
409 char cur_name[INP_NAME_MAX];
414 struct heap_info *hi;
420 static int heap_cread_item (void *vp, char **dst, int *insertMode);
422 int heap_cread_item2 (void *vp, char **dst, int *insertMode)
424 struct heap_cread_info *p = (struct heap_cread_info *) vp;
427 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
432 else if (p->ret == -1) /* Must read new item ? */
434 char *dst_1 = p->key_1;
435 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
436 p->sz_1 = dst_1 - p->key_1;
439 { /* lookahead in 2 . Now in 1. */
441 p->mode_1 = p->mode_2;
442 memcpy (p->key_1, p->key_2, p->sz_2);
445 level = 1; /* insert */
447 level = -1; /* delete */
450 char *dst_2 = p->key_2;
451 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
459 p->sz_2 = dst_2 - p->key_2;
460 if (p->sz_1 == p->sz_2 && memcmp(p->key_1, p->key_2, p->sz_1) == 0)
462 if (p->mode_2) /* adjust level according to deletes/inserts */
471 /* all the same. new round .. */
473 p->mode_1 = p->mode_2;
474 memcpy (p->key_1, p->key_2, p->sz_1);
476 level = 1; /* insert */
478 level = -1; /* delete */
481 /* outcome is insert (1) or delete (0) depending on final level */
486 memcpy (*dst, p->key_1, p->sz_1);
489 pkey(*dst, *insertMode); fflush(stdout);
495 int heap_cread_item (void *vp, char **dst, int *insertMode)
497 struct heap_cread_info *p = (struct heap_cread_info *) vp;
498 struct heap_info *hi = p->hi;
500 if (p->first_in_list)
502 *insertMode = p->key[0];
503 memcpy (*dst, p->key+1, sizeof(struct it_key));
506 pkey(*dst, *insertMode);
508 (*dst) += sizeof(struct it_key);
509 p->first_in_list = 0;
512 strcpy (p->prev_name, p->cur_name);
513 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
515 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
517 p->first_in_list = 1;
520 *insertMode = p->key[0];
521 memcpy (*dst, p->key+1, sizeof(struct it_key));
524 pkey(*dst, *insertMode);
526 (*dst) += sizeof(struct it_key);
530 int heap_inpc (struct heap_info *hi)
532 struct heap_cread_info hci;
533 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
535 hci.key = (char *) xmalloc (KEY_SIZE);
536 hci.key_1 = (char *) xmalloc (KEY_SIZE);
537 hci.key_2 = (char *) xmalloc (KEY_SIZE);
539 hci.first_in_list = 1;
541 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
543 isamc_i->clientData = &hci;
544 isamc_i->read_item = heap_cread_item2;
548 char this_name[INP_NAME_MAX];
549 ISAM_P isamc_p, isamc_p2;
552 strcpy (this_name, hci.cur_name);
553 assert (hci.cur_name[1]);
555 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
557 memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
559 isamc_merge (hi->reg->isamc, &isamc_p2, isamc_i);
563 if (!dict_delete (hi->reg->dict, this_name))
569 if (isamc_p2 != isamc_p)
570 dict_insert (hi->reg->dict, this_name,
571 sizeof(ISAM_P), &isamc_p2);
577 isamc_merge (hi->reg->isamc, &isamc_p, isamc_i);
579 dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isamc_p);
590 /* for debugging only */
591 static void print_dict_item (ZebraMaps zm, const char *s)
594 char keybuf[IT_MAX_WORD+1];
596 const char *from = s + 2;
600 const char *res = zebra_maps_output (zm, reg_type, &from);
608 yaz_log (YLOG_LOG, "%s", keybuf);
612 int heap_inpb (struct heap_info *hi)
614 struct heap_cread_info hci;
615 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
617 hci.key = (char *) xmalloc (KEY_SIZE);
618 hci.key_1 = (char *) xmalloc (KEY_SIZE);
619 hci.key_2 = (char *) xmalloc (KEY_SIZE);
621 hci.first_in_list = 1;
623 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
625 isamc_i->clientData = &hci;
626 isamc_i->read_item = heap_cread_item2;
630 char this_name[INP_NAME_MAX];
631 ISAM_P isamc_p, isamc_p2;
634 strcpy (this_name, hci.cur_name);
635 assert (hci.cur_name[1]);
639 print_dict_item (hi->reg->zebra_maps, hci.cur_name);
641 if ((dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
643 memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
645 isamb_merge (hi->reg->isamb, &isamc_p2, isamc_i);
649 if (!dict_delete (hi->reg->dict, this_name))
655 if (isamc_p2 != isamc_p)
656 dict_insert (hi->reg->dict, this_name,
657 sizeof(ISAM_P), &isamc_p2);
663 isamb_merge (hi->reg->isamb, &isamc_p, isamc_i);
665 dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isamc_p);
675 int heap_inps (struct heap_info *hi)
677 struct heap_cread_info hci;
678 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
680 hci.key = (char *) xmalloc (KEY_SIZE);
681 hci.key_1 = (char *) xmalloc (KEY_SIZE);
682 hci.key_2 = (char *) xmalloc (KEY_SIZE);
683 hci.first_in_list = 1;
686 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
688 isams_i->clientData = &hci;
689 isams_i->read_item = heap_cread_item;
693 char this_name[INP_NAME_MAX];
697 strcpy (this_name, hci.cur_name);
698 assert (hci.cur_name[1]);
700 if (!(dict_info = dict_lookup (hi->reg->dict, hci.cur_name)))
702 isams_p = isams_merge (hi->reg->isams, isams_i);
704 dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
708 yaz_log (YLOG_FATAL, "isams doesn't support this kind of update");
716 struct progressInfo {
723 void progressFunc (struct key_file *keyp, void *info)
725 struct progressInfo *p = (struct progressInfo *) info;
726 time_t now, remaining;
728 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
732 if (now >= p->lastTime+10)
735 remaining = (time_t) ((now - p->startTime)*
736 ((double) p->totalBytes/p->totalOffset - 1.0));
737 if (remaining <= 130)
738 yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
739 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
741 yaz_log (YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
742 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
744 p->totalOffset += keyp->buf_size;
751 void zebra_index_merge (ZebraHandle zh)
753 struct key_file **kf = 0;
756 struct heap_info *hi;
757 struct progressInfo progressInfo;
758 int nkeys = zh->reg->key_file_no;
761 yaz_log (YLOG_DEBUG, " index_merge called with nk=%d b=%p",
762 nkeys, zh->reg->key_buf);
763 if ( (nkeys==0) && (zh->reg->key_buf==0) )
764 return; /* nothing to merge - probably flush after end-trans */
766 usefile = (nkeys!=0);
776 extract_get_fname_tmp (zh, fname, nkeys+1);
777 if (access (fname, R_OK) == -1)
784 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
785 progressInfo.totalBytes = 0;
786 progressInfo.totalOffset = 0;
787 time (&progressInfo.startTime);
788 time (&progressInfo.lastTime);
789 for (i = 1; i<=nkeys; i++)
791 kf[i] = key_file_init (i, 8192, zh->res);
792 kf[i]->readHandler = progressFunc;
793 kf[i]->readInfo = &progressInfo;
794 progressInfo.totalBytes += kf[i]->length;
795 progressInfo.totalOffset += kf[i]->buf_size;
797 hi = key_heap_init (nkeys, key_qsort_compare);
800 for (i = 1; i<=nkeys; i++)
801 if ((r = key_file_read (kf[i], rbuf)))
802 key_heap_insert (hi, rbuf, r, kf[i]);
805 { /* do not use file, read straight from buffer */
806 hi = key_heap_init_buff (zh,key_qsort_compare);
818 for (i = 1; i<=nkeys; i++)
820 extract_get_fname_tmp (zh, rbuf, i);
823 for (i = 1; i<=nkeys; i++)
824 key_file_destroy (kf[i]);
827 if (hi->no_iterations)
828 { /* do not log if nothing happened */
829 yaz_log (YLOG_LOG, "Iterations . . .%7d", hi->no_iterations);
830 yaz_log (YLOG_LOG, "Distinct words .%7d", hi->no_diffs);
831 yaz_log (YLOG_LOG, "Updates. . . . .%7d", hi->no_updates);
832 yaz_log (YLOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
833 yaz_log (YLOG_LOG, "Insertions . . .%7d", hi->no_insertions);
835 zh->reg->key_file_no = 0;
837 key_heap_destroy (hi, nkeys);