1 /* $Id: kinput.c,v 1.88 2008-01-09 14:57:07 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;
238 static struct heap_info *key_heap_malloc(void)
239 { /* malloc and clear it */
240 struct heap_info *hi;
241 hi = (struct heap_info *) xmalloc(sizeof(*hi));
249 hi->no_deletions = 0;
250 hi->no_insertions = 0;
251 hi->no_iterations = 0;
255 struct heap_info *key_heap_init_file(ZebraHandle zh,
257 int (*cmp)(const void *p1, const void *p2))
259 struct heap_info *hi;
262 hi = key_heap_malloc();
264 hi->info.file = (struct key_file **)
265 xmalloc(sizeof(*hi->info.file) * (1+nkeys));
266 hi->info.buf = (char **) xmalloc(sizeof(*hi->info.buf) * (1+nkeys));
267 hi->ptr = (int *) xmalloc(sizeof(*hi->ptr) * (1+nkeys));
269 for (i = 0; i<= nkeys; i++)
272 hi->info.buf[i] = (char *) xmalloc(INP_NAME_MAX);
277 void key_heap_destroy(struct heap_info *hi, int nkeys)
280 for (i = 0; i<=nkeys; i++)
281 xfree(hi->info.buf[i]);
284 xfree(hi->info.file);
288 static void key_heap_swap(struct heap_info *hi, int i1, int i2)
293 hi->ptr[i1] = hi->ptr[i2];
298 static void key_heap_delete(struct heap_info *hi)
300 int cur = 1, child = 2;
302 assert(hi->heapnum > 0);
304 key_heap_swap(hi, 1, hi->heapnum);
306 while (child <= hi->heapnum) {
307 if (child < hi->heapnum &&
308 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
309 &hi->info.buf[hi->ptr[child+1]]) > 0)
311 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
312 &hi->info.buf[hi->ptr[child]]) > 0)
314 key_heap_swap(hi, cur, child);
323 static void key_heap_insert(struct heap_info *hi, const char *buf, int nbytes,
328 cur = ++(hi->heapnum);
329 memcpy(hi->info.buf[hi->ptr[cur]], buf, nbytes);
330 hi->info.file[hi->ptr[cur]] = kf;
333 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
334 &hi->info.buf[hi->ptr[cur]]) > 0)
336 key_heap_swap(hi, cur, parent);
342 static int heap_read_one(struct heap_info *hi, char *name, char *key)
345 char rbuf[INP_NAME_MAX];
351 strcpy(name, hi->info.buf[n]);
352 kf = hi->info.file[n];
354 memcpy(key, hi->info.buf[n] + r+1, KEY_SIZE);
356 if ((r = key_file_read(kf, rbuf)))
357 key_heap_insert(hi, rbuf, r, kf);
365 /* for debugging only */
366 void zebra_log_dict_entry(ZebraHandle zh, const char *s)
368 char dst[IT_MAX_WORD+1];
370 int len = key_SU_decode(&ord, (const unsigned char *) s);
371 const char *index_type;
374 yaz_log(YLOG_LOG, "ord=%d", ord);
377 const char *string_index;
379 zebraExplain_lookup_ord(zh->reg->zei,
380 ord, &index_type, &db, &string_index);
382 zebra_term_untrans(zh, index_type, dst, s + len);
384 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s term=%s",
385 ord, index_type, string_index, dst);
389 struct heap_cread_info {
390 char prev_name[INP_NAME_MAX];
391 char cur_name[INP_NAME_MAX];
396 struct heap_info *hi;
403 static int heap_cread_item(void *vp, char **dst, int *insertMode);
405 int heap_cread_item2(void *vp, char **dst, int *insertMode)
407 struct heap_cread_info *p = (struct heap_cread_info *) vp;
412 if (p->look_level > 0)
422 memcpy(*dst, p->key_1, p->sz_1);
424 yaz_log(YLOG_LOG, "DUP level=%d", p->look_level);
425 pkey(*dst, *insertMode);
430 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
435 else if (p->ret == -1) /* Must read new item ? */
437 char *dst_1 = p->key_1;
438 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
439 p->sz_1 = dst_1 - p->key_1;
442 { /* lookahead in 2 . Now in 1. */
444 p->mode_1 = p->mode_2;
445 memcpy(p->key_1, p->key_2, p->sz_2);
448 level = 1; /* insert */
450 level = -1; /* delete */
453 char *dst_2 = p->key_2;
454 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
462 p->sz_2 = dst_2 - p->key_2;
464 if (key_compare(p->key_1, p->key_2) == 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 */
496 p->look_level = level;
497 memcpy(*dst, p->key_1, p->sz_1);
499 pkey(*dst, *insertMode);
505 int heap_cread_item(void *vp, char **dst, int *insertMode)
507 struct heap_cread_info *p = (struct heap_cread_info *) vp;
508 struct heap_info *hi = p->hi;
510 if (p->first_in_list)
512 *insertMode = p->key[0];
513 memcpy(*dst, p->key+1, sizeof(struct it_key));
515 zebra_log_dict_entry(hi->zh, p->cur_name);
516 pkey(*dst, *insertMode);
518 (*dst) += sizeof(struct it_key);
519 p->first_in_list = 0;
522 strcpy(p->prev_name, p->cur_name);
523 if (!(p->more = heap_read_one(hi, p->cur_name, p->key)))
525 if (*p->cur_name && strcmp(p->cur_name, p->prev_name))
527 p->first_in_list = 1;
530 *insertMode = p->key[0];
531 memcpy(*dst, p->key+1, sizeof(struct it_key));
533 zebra_log_dict_entry(hi->zh, p->cur_name);
534 pkey(*dst, *insertMode);
536 (*dst) += sizeof(struct it_key);
540 int heap_inpc(struct heap_cread_info *hci, struct heap_info *hi)
542 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
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[0]);
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);
581 dict_insert(hi->reg->dict, this_name,
582 sizeof(ISAM_P), &isamc_p);
589 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
591 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
593 isamc_i->clientData = hci;
594 isamc_i->read_item = heap_cread_item2;
598 char this_name[INP_NAME_MAX];
599 ISAM_P isamc_p, isamc_p2;
602 strcpy(this_name, hci->cur_name);
603 assert(hci->cur_name[0]);
608 zebra_log_dict_entry(hi->zh, hci->cur_name);
610 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
612 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
614 isamb_merge(hi->reg->isamb, &isamc_p2, isamc_i);
618 if (!dict_delete(hi->reg->dict, this_name))
624 if (isamc_p2 != isamc_p)
625 dict_insert(hi->reg->dict, this_name,
626 sizeof(ISAM_P), &isamc_p2);
632 isamb_merge(hi->reg->isamb, &isamc_p, isamc_i);
635 dict_insert(hi->reg->dict, this_name,
636 sizeof(ISAM_P), &isamc_p);
643 int heap_inps(struct heap_cread_info *hci, struct heap_info *hi)
645 ISAMS_I isams_i = (ISAMS_I) xmalloc(sizeof(*isams_i));
647 isams_i->clientData = hci;
648 isams_i->read_item = heap_cread_item;
652 char this_name[INP_NAME_MAX];
656 strcpy(this_name, hci->cur_name);
657 assert(hci->cur_name[0]);
659 if (!(dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
661 isams_p = isams_merge(hi->reg->isams, isams_i);
663 dict_insert(hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
667 yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
675 struct progressInfo {
682 void progressFunc(struct key_file *keyp, void *info)
684 struct progressInfo *p = (struct progressInfo *) info;
685 time_t now, remaining;
687 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
691 if (now >= p->lastTime+10)
694 remaining = (time_t) ((now - p->startTime)*
695 ((double) p->totalBytes/p->totalOffset - 1.0));
696 if (remaining <= 130)
697 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
698 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
700 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
701 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
703 p->totalOffset += keyp->buf_size;
710 void zebra_index_merge(ZebraHandle zh)
712 struct key_file **kf = 0;
715 struct heap_info *hi;
716 struct progressInfo progressInfo;
717 int nkeys = key_block_get_no_files(zh->reg->key_block);
728 extract_get_fname_tmp (zh, fname, nkeys+1);
729 if (access(fname, R_OK) == -1)
736 kf = (struct key_file **) xmalloc((1+nkeys) * sizeof(*kf));
737 progressInfo.totalBytes = 0;
738 progressInfo.totalOffset = 0;
739 time(&progressInfo.startTime);
740 time(&progressInfo.lastTime);
741 for (i = 1; i<=nkeys; i++)
743 kf[i] = key_file_init(i, 8192, zh->res);
744 kf[i]->readHandler = progressFunc;
745 kf[i]->readInfo = &progressInfo;
746 progressInfo.totalBytes += kf[i]->length;
747 progressInfo.totalOffset += kf[i]->buf_size;
749 hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
752 for (i = 1; i<=nkeys; i++)
753 if ((r = key_file_read(kf[i], rbuf)))
754 key_heap_insert(hi, rbuf, r, kf[i]);
758 struct heap_cread_info hci;
760 hci.key = (char *) xmalloc(KEY_SIZE);
761 hci.key_1 = (char *) xmalloc(KEY_SIZE);
762 hci.key_2 = (char *) xmalloc(KEY_SIZE);
764 hci.first_in_list = 1;
767 hci.more = heap_read_one(hi, hci.cur_name, hci.key);
781 for (i = 1; i<=nkeys; i++)
783 extract_get_fname_tmp (zh, rbuf, i);
786 for (i = 1; i<=nkeys; i++)
787 key_file_destroy(kf[i]);
789 if (hi->no_iterations)
790 { /* do not log if nothing happened */
791 yaz_log(YLOG_LOG, "Iterations: isam/dict "
792 ZINT_FORMAT "/" ZINT_FORMAT,
793 hi->no_iterations, hi->no_diffs);
794 yaz_log(YLOG_LOG, "Dict: inserts/updates/deletions: "
795 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
796 hi->no_insertions, hi->no_updates, hi->no_deletions);
798 key_block_destroy(&zh->reg->key_block);
799 key_heap_destroy(hi, nkeys);
804 * indent-tabs-mode: nil
806 * vim: shiftwidth=4 tabstop=8 expandtab