1 /* $Id: kinput.c,v 1.87 2008-01-09 14:53:26 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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
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);
59 static void pkey(const char *b, int mode)
61 key_logdump_txt(YLOG_LOG, b, mode ? "i" : "d");
66 void getFnameTmp(Res res, char *fname, int no)
70 pre = res_get_def(res, "keyTmpDir", ".");
71 sprintf(fname, "%s/key%d.tmp", pre, no);
74 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no)
78 pre = res_get_def(zh->res, "keyTmpDir", ".");
79 sprintf(fname, "%s/key%d.tmp", pre, no);
82 void key_file_chunk_read(struct key_file *f)
84 int nr = 0, r = 0, fd;
86 getFnameTmp(f->res, fname, f->no);
87 fd = open(fname, O_BINARY|O_RDONLY);
93 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot open %s", fname);
98 if ((f->length = lseek(fd, 0L, SEEK_END)) == (off_t) -1)
100 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
105 if (lseek(fd, f->offset, SEEK_SET) == -1)
107 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
111 while (f->chunk - nr > 0)
113 r = read(fd, f->buf + nr, f->chunk - nr);
120 yaz_log(YLOG_WARN|YLOG_ERRNO, "read of %s", fname);
126 (*f->readHandler)(f, f->readInfo);
130 void key_file_destroy(struct key_file *f)
132 iscz1_stop(f->decode_handle);
138 struct key_file *key_file_init(int no, int chunk, Res res)
142 f = (struct key_file *) xmalloc(sizeof(*f));
144 f->decode_handle = iscz1_start();
149 f->readHandler = NULL;
150 f->buf = (unsigned char *) xmalloc(f->chunk);
151 f->prev_name = (char *) xmalloc(INP_NAME_MAX);
152 *f->prev_name = '\0';
153 key_file_chunk_read(f);
157 int key_file_getc(struct key_file *f)
159 if (f->buf_ptr < f->buf_size)
160 return f->buf[(f->buf_ptr)++];
161 if (f->buf_size < f->chunk)
163 f->offset += f->buf_size;
164 key_file_chunk_read(f);
165 if (f->buf_ptr < f->buf_size)
166 return f->buf[(f->buf_ptr)++];
171 int key_file_read(struct key_file *f, char *key)
175 const char *src = srcbuf;
179 c = key_file_getc(f);
182 strcpy(key, f->prev_name);
191 while ((c = key_file_getc(f)))
197 strcpy(f->prev_name, key);
198 iscz1_reset(f->decode_handle);
200 c = key_file_getc(f); /* length + insert/delete combined */
203 for (j = 0; j < c; j++)
204 srcbuf[j] = key_file_getc(f);
206 iscz1_decode(f->decode_handle, &dst, &src);
213 memcpy(&k, key+i, sizeof(k));
215 yaz_log(YLOG_LOG, "00 KEY");
218 return i + sizeof(struct it_key);
223 struct key_file **file;
228 int (*cmp)(const void *p1, const void *p2);
229 struct zebra_register *reg;
231 int raw_reading; /* 1=raw /mem read. 0=file reading */
239 static struct heap_info *key_heap_malloc(void)
240 { /* malloc and clear it */
241 struct heap_info *hi;
242 hi = (struct heap_info *) xmalloc(sizeof(*hi));
251 hi->no_deletions = 0;
252 hi->no_insertions = 0;
253 hi->no_iterations = 0;
257 struct heap_info *key_heap_init_file(ZebraHandle zh,
259 int (*cmp)(const void *p1, const void *p2))
261 struct heap_info *hi;
264 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_raw(ZebraHandle zh,
280 int (*cmp)(const void *p1, const void *p2))
282 struct heap_info *hi=key_heap_malloc();
289 void key_heap_destroy(struct heap_info *hi, int nkeys)
292 if (!hi->raw_reading)
293 for (i = 0; i<=nkeys; i++)
294 xfree(hi->info.buf[i]);
298 xfree(hi->info.file);
302 static void key_heap_swap(struct heap_info *hi, int i1, int i2)
307 hi->ptr[i1] = hi->ptr[i2];
312 static void key_heap_delete(struct heap_info *hi)
314 int cur = 1, child = 2;
316 assert(hi->heapnum > 0);
318 key_heap_swap(hi, 1, hi->heapnum);
320 while (child <= hi->heapnum) {
321 if (child < hi->heapnum &&
322 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
323 &hi->info.buf[hi->ptr[child+1]]) > 0)
325 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
326 &hi->info.buf[hi->ptr[child]]) > 0)
328 key_heap_swap(hi, cur, child);
337 static void key_heap_insert(struct heap_info *hi, const char *buf, int nbytes,
342 cur = ++(hi->heapnum);
343 memcpy(hi->info.buf[hi->ptr[cur]], buf, nbytes);
344 hi->info.file[hi->ptr[cur]] = kf;
347 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
348 &hi->info.buf[hi->ptr[cur]]) > 0)
350 key_heap_swap(hi, cur, parent);
356 static int heap_read_one(struct heap_info *hi, char *name, char *key)
359 char rbuf[INP_NAME_MAX];
365 strcpy(name, hi->info.buf[n]);
366 kf = hi->info.file[n];
368 memcpy(key, hi->info.buf[n] + r+1, KEY_SIZE);
370 if ((r = key_file_read(kf, rbuf)))
371 key_heap_insert(hi, rbuf, r, kf);
379 /* for debugging only */
380 void zebra_log_dict_entry(ZebraHandle zh, const char *s)
382 char dst[IT_MAX_WORD+1];
384 int len = key_SU_decode(&ord, (const unsigned char *) s);
385 const char *index_type;
388 yaz_log(YLOG_LOG, "ord=%d", ord);
391 const char *string_index;
393 zebraExplain_lookup_ord(zh->reg->zei,
394 ord, &index_type, &db, &string_index);
396 zebra_term_untrans(zh, index_type, dst, s + len);
398 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s term=%s",
399 ord, index_type, string_index, dst);
403 struct heap_cread_info {
404 char prev_name[INP_NAME_MAX];
405 char cur_name[INP_NAME_MAX];
410 struct heap_info *hi;
417 static int heap_cread_item(void *vp, char **dst, int *insertMode);
419 int heap_cread_item2(void *vp, char **dst, int *insertMode)
421 struct heap_cread_info *p = (struct heap_cread_info *) vp;
426 if (p->look_level > 0)
436 memcpy(*dst, p->key_1, p->sz_1);
438 yaz_log(YLOG_LOG, "DUP level=%d", p->look_level);
439 pkey(*dst, *insertMode);
444 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
449 else if (p->ret == -1) /* Must read new item ? */
451 char *dst_1 = p->key_1;
452 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
453 p->sz_1 = dst_1 - p->key_1;
456 { /* lookahead in 2 . Now in 1. */
458 p->mode_1 = p->mode_2;
459 memcpy(p->key_1, p->key_2, p->sz_2);
462 level = 1; /* insert */
464 level = -1; /* delete */
467 char *dst_2 = p->key_2;
468 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
476 p->sz_2 = dst_2 - p->key_2;
478 if (key_compare(p->key_1, p->key_2) == 0)
480 if (p->mode_2) /* adjust level according to deletes/inserts */
489 /* all the same. new round .. */
491 p->mode_1 = p->mode_2;
492 memcpy(p->key_1, p->key_2, p->sz_1);
494 level = 1; /* insert */
496 level = -1; /* delete */
499 /* outcome is insert (1) or delete (0) depending on final level */
510 p->look_level = level;
511 memcpy(*dst, p->key_1, p->sz_1);
513 pkey(*dst, *insertMode);
519 int heap_cread_item(void *vp, char **dst, int *insertMode)
521 struct heap_cread_info *p = (struct heap_cread_info *) vp;
522 struct heap_info *hi = p->hi;
524 if (p->first_in_list)
526 *insertMode = p->key[0];
527 memcpy(*dst, p->key+1, sizeof(struct it_key));
529 zebra_log_dict_entry(hi->zh, p->cur_name);
530 pkey(*dst, *insertMode);
532 (*dst) += sizeof(struct it_key);
533 p->first_in_list = 0;
536 strcpy(p->prev_name, p->cur_name);
537 if (!(p->more = heap_read_one(hi, p->cur_name, p->key)))
539 if (*p->cur_name && strcmp(p->cur_name, p->prev_name))
541 p->first_in_list = 1;
544 *insertMode = p->key[0];
545 memcpy(*dst, p->key+1, sizeof(struct it_key));
547 zebra_log_dict_entry(hi->zh, p->cur_name);
548 pkey(*dst, *insertMode);
550 (*dst) += sizeof(struct it_key);
554 int heap_inpc(struct heap_cread_info *hci, struct heap_info *hi)
556 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
558 isamc_i->clientData = hci;
559 isamc_i->read_item = heap_cread_item2;
563 char this_name[INP_NAME_MAX];
564 ISAM_P isamc_p, isamc_p2;
567 strcpy(this_name, hci->cur_name);
568 assert(hci->cur_name[0]);
570 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
572 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
574 isamc_merge(hi->reg->isamc, &isamc_p2, isamc_i);
578 if (!dict_delete(hi->reg->dict, this_name))
584 if (isamc_p2 != isamc_p)
585 dict_insert(hi->reg->dict, this_name,
586 sizeof(ISAM_P), &isamc_p2);
592 isamc_merge(hi->reg->isamc, &isamc_p, isamc_i);
595 dict_insert(hi->reg->dict, this_name,
596 sizeof(ISAM_P), &isamc_p);
603 int heap_inp0(struct heap_cread_info *hci, struct heap_info *hi)
607 char this_name[INP_NAME_MAX];
612 strcpy(this_name, hci->cur_name);
613 assert(hci->cur_name[0]);
616 while (heap_cread_item2(hci, &dst, &mode))
623 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
625 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
627 isamc_i->clientData = hci;
628 isamc_i->read_item = heap_cread_item2;
632 char this_name[INP_NAME_MAX];
633 ISAM_P isamc_p, isamc_p2;
636 strcpy(this_name, hci->cur_name);
637 assert(hci->cur_name[0]);
642 zebra_log_dict_entry(hi->zh, hci->cur_name);
644 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
646 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
648 isamb_merge(hi->reg->isamb, &isamc_p2, isamc_i);
652 if (!dict_delete(hi->reg->dict, this_name))
658 if (isamc_p2 != isamc_p)
659 dict_insert(hi->reg->dict, this_name,
660 sizeof(ISAM_P), &isamc_p2);
666 isamb_merge(hi->reg->isamb, &isamc_p, isamc_i);
669 dict_insert(hi->reg->dict, this_name,
670 sizeof(ISAM_P), &isamc_p);
677 int heap_inps(struct heap_cread_info *hci, struct heap_info *hi)
679 ISAMS_I isams_i = (ISAMS_I) xmalloc(sizeof(*isams_i));
681 isams_i->clientData = hci;
682 isams_i->read_item = heap_cread_item;
686 char this_name[INP_NAME_MAX];
690 strcpy(this_name, hci->cur_name);
691 assert(hci->cur_name[0]);
693 if (!(dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
695 isams_p = isams_merge(hi->reg->isams, isams_i);
697 dict_insert(hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
701 yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
709 struct progressInfo {
716 void progressFunc(struct key_file *keyp, void *info)
718 struct progressInfo *p = (struct progressInfo *) info;
719 time_t now, remaining;
721 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
725 if (now >= p->lastTime+10)
728 remaining = (time_t) ((now - p->startTime)*
729 ((double) p->totalBytes/p->totalOffset - 1.0));
730 if (remaining <= 130)
731 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
732 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
734 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
735 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
737 p->totalOffset += keyp->buf_size;
744 void zebra_index_merge(ZebraHandle zh)
746 struct key_file **kf = 0;
749 struct heap_info *hi;
750 struct progressInfo progressInfo;
751 int nkeys = key_block_get_no_files(zh->reg->key_block);
762 extract_get_fname_tmp (zh, fname, nkeys+1);
763 if (access(fname, R_OK) == -1)
770 kf = (struct key_file **) xmalloc((1+nkeys) * sizeof(*kf));
771 progressInfo.totalBytes = 0;
772 progressInfo.totalOffset = 0;
773 time(&progressInfo.startTime);
774 time(&progressInfo.lastTime);
775 for (i = 1; i<=nkeys; i++)
777 kf[i] = key_file_init(i, 8192, zh->res);
778 kf[i]->readHandler = progressFunc;
779 kf[i]->readInfo = &progressInfo;
780 progressInfo.totalBytes += kf[i]->length;
781 progressInfo.totalOffset += kf[i]->buf_size;
783 hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
786 for (i = 1; i<=nkeys; i++)
787 if ((r = key_file_read(kf[i], rbuf)))
788 key_heap_insert(hi, rbuf, r, kf[i]);
792 struct heap_cread_info hci;
794 hci.key = (char *) xmalloc(KEY_SIZE);
795 hci.key_1 = (char *) xmalloc(KEY_SIZE);
796 hci.key_2 = (char *) xmalloc(KEY_SIZE);
798 hci.first_in_list = 1;
801 hci.more = heap_read_one(hi, hci.cur_name, hci.key);
815 for (i = 1; i<=nkeys; i++)
817 extract_get_fname_tmp (zh, rbuf, i);
820 for (i = 1; i<=nkeys; i++)
821 key_file_destroy(kf[i]);
823 if (hi->no_iterations)
824 { /* do not log if nothing happened */
825 yaz_log(YLOG_LOG, "Iterations: isam/dict "
826 ZINT_FORMAT "/" ZINT_FORMAT,
827 hi->no_iterations, hi->no_diffs);
828 yaz_log(YLOG_LOG, "Dict: inserts/updates/deletions: "
829 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
830 hi->no_insertions, hi->no_updates, hi->no_deletions);
832 key_block_destroy(&zh->reg->key_block);
833 key_heap_destroy(hi, nkeys);
838 * indent-tabs-mode: nil
840 * vim: shiftwidth=4 tabstop=8 expandtab