1 /* $Id: kinput.c,v 1.89 2008-01-09 22:18:04 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
42 off_t offset; /* file offset */
43 unsigned char *buf; /* buffer block */
44 size_t buf_size; /* number of read bytes in block */
45 size_t chunk; /* number of bytes allocated */
46 size_t buf_ptr; /* current position in buffer */
47 char *prev_name; /* last word read */
49 off_t length; /* length of file */
50 /* handler invoked in each read */
51 void (*readHandler)(struct key_file *keyp, void *rinfo);
57 static void pkey(const char *b, int mode)
59 key_logdump_txt(YLOG_LOG, b, mode ? "i" : "d");
64 void getFnameTmp(Res res, char *fname, int no)
68 pre = res_get_def(res, "keyTmpDir", ".");
69 sprintf(fname, "%s/key%d.tmp", pre, no);
72 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no)
76 pre = res_get_def(zh->res, "keyTmpDir", ".");
77 sprintf(fname, "%s/key%d.tmp", pre, no);
80 void key_file_chunk_read(struct key_file *f)
82 int nr = 0, r = 0, fd;
84 getFnameTmp(f->res, fname, f->no);
85 fd = open(fname, O_BINARY|O_RDONLY);
91 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot open %s", fname);
96 if ((f->length = lseek(fd, 0L, SEEK_END)) == (off_t) -1)
98 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
103 if (lseek(fd, f->offset, SEEK_SET) == -1)
105 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
109 while (f->chunk - nr > 0)
111 r = read(fd, f->buf + nr, f->chunk - nr);
118 yaz_log(YLOG_WARN|YLOG_ERRNO, "read of %s", fname);
124 (*f->readHandler)(f, f->readInfo);
128 void key_file_destroy(struct key_file *f)
130 iscz1_stop(f->decode_handle);
136 struct key_file *key_file_init(int no, int chunk, Res res)
140 f = (struct key_file *) xmalloc(sizeof(*f));
142 f->decode_handle = iscz1_start();
147 f->readHandler = NULL;
148 f->buf = (unsigned char *) xmalloc(f->chunk);
149 f->prev_name = (char *) xmalloc(INP_NAME_MAX);
150 *f->prev_name = '\0';
151 key_file_chunk_read(f);
155 int key_file_getc(struct key_file *f)
157 if (f->buf_ptr < f->buf_size)
158 return f->buf[(f->buf_ptr)++];
159 if (f->buf_size < f->chunk)
161 f->offset += f->buf_size;
162 key_file_chunk_read(f);
163 if (f->buf_ptr < f->buf_size)
164 return f->buf[(f->buf_ptr)++];
169 int key_file_read(struct key_file *f, char *key)
173 const char *src = srcbuf;
177 c = key_file_getc(f);
180 strcpy(key, f->prev_name);
189 while ((c = key_file_getc(f)))
195 strcpy(f->prev_name, key);
196 iscz1_reset(f->decode_handle);
198 c = key_file_getc(f); /* length + insert/delete combined */
201 for (j = 0; j < c; j++)
202 srcbuf[j] = key_file_getc(f);
204 iscz1_decode(f->decode_handle, &dst, &src);
211 memcpy(&k, key+i, sizeof(k));
213 yaz_log(YLOG_LOG, "00 KEY");
216 return i + sizeof(struct it_key);
221 struct key_file **file;
226 int (*cmp)(const void *p1, const void *p2);
227 struct zebra_register *reg;
236 static struct heap_info *key_heap_malloc(void)
237 { /* malloc and clear it */
238 struct heap_info *hi;
239 hi = (struct heap_info *) xmalloc(sizeof(*hi));
247 hi->no_deletions = 0;
248 hi->no_insertions = 0;
249 hi->no_iterations = 0;
253 struct heap_info *key_heap_init_file(ZebraHandle zh,
255 int (*cmp)(const void *p1, const void *p2))
257 struct heap_info *hi;
260 hi = key_heap_malloc();
262 hi->info.file = (struct key_file **)
263 xmalloc(sizeof(*hi->info.file) * (1+nkeys));
264 hi->info.buf = (char **) xmalloc(sizeof(*hi->info.buf) * (1+nkeys));
265 hi->ptr = (int *) xmalloc(sizeof(*hi->ptr) * (1+nkeys));
267 for (i = 0; i<= nkeys; i++)
270 hi->info.buf[i] = (char *) xmalloc(INP_NAME_MAX);
275 void key_heap_destroy(struct heap_info *hi, int nkeys)
278 for (i = 0; i<=nkeys; i++)
279 xfree(hi->info.buf[i]);
282 xfree(hi->info.file);
286 static void key_heap_swap(struct heap_info *hi, int i1, int i2)
291 hi->ptr[i1] = hi->ptr[i2];
296 static void key_heap_delete(struct heap_info *hi)
298 int cur = 1, child = 2;
300 assert(hi->heapnum > 0);
302 key_heap_swap(hi, 1, hi->heapnum);
304 while (child <= hi->heapnum) {
305 if (child < hi->heapnum &&
306 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
307 &hi->info.buf[hi->ptr[child+1]]) > 0)
309 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
310 &hi->info.buf[hi->ptr[child]]) > 0)
312 key_heap_swap(hi, cur, child);
321 static void key_heap_insert(struct heap_info *hi, const char *buf, int nbytes,
326 cur = ++(hi->heapnum);
327 memcpy(hi->info.buf[hi->ptr[cur]], buf, nbytes);
328 hi->info.file[hi->ptr[cur]] = kf;
331 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
332 &hi->info.buf[hi->ptr[cur]]) > 0)
334 key_heap_swap(hi, cur, parent);
340 static int heap_read_one(struct heap_info *hi, char *name, char *key)
343 char rbuf[INP_NAME_MAX];
349 strcpy(name, hi->info.buf[n]);
350 kf = hi->info.file[n];
352 memcpy(key, hi->info.buf[n] + r+1, KEY_SIZE);
354 if ((r = key_file_read(kf, rbuf)))
355 key_heap_insert(hi, rbuf, r, kf);
363 /* for debugging only */
364 void zebra_log_dict_entry(ZebraHandle zh, const char *s)
366 char dst[IT_MAX_WORD+1];
368 int len = key_SU_decode(&ord, (const unsigned char *) s);
369 const char *index_type;
372 yaz_log(YLOG_LOG, "ord=%d", ord);
375 const char *string_index;
377 zebraExplain_lookup_ord(zh->reg->zei,
378 ord, &index_type, &db, &string_index);
380 zebra_term_untrans(zh, index_type, dst, s + len);
382 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s term=%s",
383 ord, index_type, string_index, dst);
387 struct heap_cread_info {
388 char prev_name[INP_NAME_MAX];
389 char cur_name[INP_NAME_MAX];
394 struct heap_info *hi;
401 static int heap_cread_item(void *vp, char **dst, int *insertMode);
403 int heap_cread_item2(void *vp, char **dst, int *insertMode)
405 struct heap_cread_info *p = (struct heap_cread_info *) vp;
410 if (p->look_level > 0)
420 memcpy(*dst, p->key_1, p->sz_1);
422 yaz_log(YLOG_LOG, "DUP level=%d", p->look_level);
423 pkey(*dst, *insertMode);
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;
462 if (key_compare(p->key_1, p->key_2) == 0)
464 if (p->mode_2) /* adjust level according to deletes/inserts */
473 /* all the same. new round .. */
475 p->mode_1 = p->mode_2;
476 memcpy(p->key_1, p->key_2, p->sz_1);
478 level = 1; /* insert */
480 level = -1; /* delete */
483 /* outcome is insert (1) or delete (0) depending on final level */
494 p->look_level = level;
495 memcpy(*dst, p->key_1, p->sz_1);
497 pkey(*dst, *insertMode);
503 int heap_cread_item(void *vp, char **dst, int *insertMode)
505 struct heap_cread_info *p = (struct heap_cread_info *) vp;
506 struct heap_info *hi = p->hi;
508 if (p->first_in_list)
510 *insertMode = p->key[0];
511 memcpy(*dst, p->key+1, sizeof(struct it_key));
513 zebra_log_dict_entry(hi->zh, p->cur_name);
514 pkey(*dst, *insertMode);
516 (*dst) += sizeof(struct it_key);
517 p->first_in_list = 0;
520 strcpy(p->prev_name, p->cur_name);
521 if (!(p->more = heap_read_one(hi, p->cur_name, p->key)))
523 if (*p->cur_name && strcmp(p->cur_name, p->prev_name))
525 p->first_in_list = 1;
528 *insertMode = p->key[0];
529 memcpy(*dst, p->key+1, sizeof(struct it_key));
531 zebra_log_dict_entry(hi->zh, p->cur_name);
532 pkey(*dst, *insertMode);
534 (*dst) += sizeof(struct it_key);
538 int heap_inpc(struct heap_cread_info *hci, struct heap_info *hi)
540 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
542 isamc_i->clientData = hci;
543 isamc_i->read_item = heap_cread_item2;
547 char this_name[INP_NAME_MAX];
548 ISAM_P isamc_p, isamc_p2;
551 strcpy(this_name, hci->cur_name);
552 assert(hci->cur_name[0]);
554 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
556 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
558 isamc_merge(hi->reg->isamc, &isamc_p2, isamc_i);
562 if (!dict_delete(hi->reg->dict, this_name))
568 if (isamc_p2 != isamc_p)
569 dict_insert(hi->reg->dict, this_name,
570 sizeof(ISAM_P), &isamc_p2);
576 isamc_merge(hi->reg->isamc, &isamc_p, isamc_i);
579 dict_insert(hi->reg->dict, this_name,
580 sizeof(ISAM_P), &isamc_p);
587 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
589 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
591 isamc_i->clientData = hci;
592 isamc_i->read_item = heap_cread_item2;
596 char this_name[INP_NAME_MAX];
597 ISAM_P isamc_p, isamc_p2;
600 strcpy(this_name, hci->cur_name);
601 assert(hci->cur_name[0]);
606 zebra_log_dict_entry(hi->zh, hci->cur_name);
608 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
610 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
612 isamb_merge(hi->reg->isamb, &isamc_p2, isamc_i);
616 if (!dict_delete(hi->reg->dict, this_name))
622 if (isamc_p2 != isamc_p)
623 dict_insert(hi->reg->dict, this_name,
624 sizeof(ISAM_P), &isamc_p2);
630 isamb_merge(hi->reg->isamb, &isamc_p, isamc_i);
633 dict_insert(hi->reg->dict, this_name,
634 sizeof(ISAM_P), &isamc_p);
641 int heap_inps(struct heap_cread_info *hci, struct heap_info *hi)
643 ISAMS_I isams_i = (ISAMS_I) xmalloc(sizeof(*isams_i));
645 isams_i->clientData = hci;
646 isams_i->read_item = heap_cread_item;
650 char this_name[INP_NAME_MAX];
654 strcpy(this_name, hci->cur_name);
655 assert(hci->cur_name[0]);
657 if (!(dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
659 isams_p = isams_merge(hi->reg->isams, isams_i);
661 dict_insert(hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
665 yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
673 struct progressInfo {
680 void progressFunc(struct key_file *keyp, void *info)
682 struct progressInfo *p = (struct progressInfo *) info;
683 time_t now, remaining;
685 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
689 if (now >= p->lastTime+10)
692 remaining = (time_t) ((now - p->startTime)*
693 ((double) p->totalBytes/p->totalOffset - 1.0));
694 if (remaining <= 130)
695 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
696 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
698 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
699 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
701 p->totalOffset += keyp->buf_size;
708 void zebra_index_merge(ZebraHandle zh)
710 struct key_file **kf = 0;
713 struct heap_info *hi;
714 struct progressInfo progressInfo;
715 int nkeys = key_block_get_no_files(zh->reg->key_block);
726 extract_get_fname_tmp (zh, fname, nkeys+1);
727 if (access(fname, R_OK) == -1)
734 kf = (struct key_file **) xmalloc((1+nkeys) * sizeof(*kf));
735 progressInfo.totalBytes = 0;
736 progressInfo.totalOffset = 0;
737 time(&progressInfo.startTime);
738 time(&progressInfo.lastTime);
739 for (i = 1; i<=nkeys; i++)
741 kf[i] = key_file_init(i, 8192, zh->res);
742 kf[i]->readHandler = progressFunc;
743 kf[i]->readInfo = &progressInfo;
744 progressInfo.totalBytes += kf[i]->length;
745 progressInfo.totalOffset += kf[i]->buf_size;
747 hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
750 for (i = 1; i<=nkeys; i++)
751 if ((r = key_file_read(kf[i], rbuf)))
752 key_heap_insert(hi, rbuf, r, kf[i]);
756 struct heap_cread_info hci;
758 hci.key = (char *) xmalloc(KEY_SIZE);
759 hci.key_1 = (char *) xmalloc(KEY_SIZE);
760 hci.key_2 = (char *) xmalloc(KEY_SIZE);
762 hci.first_in_list = 1;
765 hci.more = heap_read_one(hi, hci.cur_name, hci.key);
779 for (i = 1; i<=nkeys; i++)
781 extract_get_fname_tmp (zh, rbuf, i);
784 for (i = 1; i<=nkeys; i++)
785 key_file_destroy(kf[i]);
787 if (hi->no_iterations)
788 { /* do not log if nothing happened */
789 yaz_log(YLOG_LOG, "Iterations: isam/dict "
790 ZINT_FORMAT "/" ZINT_FORMAT,
791 hi->no_iterations, hi->no_diffs);
792 yaz_log(YLOG_LOG, "Dict: inserts/updates/deletions: "
793 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
794 hi->no_insertions, hi->no_updates, hi->no_deletions);
796 key_block_destroy(&zh->reg->key_block);
797 key_heap_destroy(hi, nkeys);
802 * indent-tabs-mode: nil
804 * vim: shiftwidth=4 tabstop=8 expandtab