1 /* $Id: kinput.c,v 1.85 2007-10-29 16:57:52 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 ((key[i++] = key_file_getc(f)))
193 strcpy(f->prev_name, key);
194 iscz1_reset(f->decode_handle);
196 c = key_file_getc(f); /* length + insert/delete combined */
199 for (j = 0; j < c; j++)
200 srcbuf[j] = key_file_getc(f);
202 iscz1_decode(f->decode_handle, &dst, &src);
209 memcpy(&k, key+i, sizeof(k));
211 yaz_log(YLOG_LOG, "00 KEY");
214 return i + sizeof(struct it_key);
219 struct key_file **file;
224 int (*cmp)(const void *p1, const void *p2);
225 struct zebra_register *reg;
227 int raw_reading; /* 1=raw /mem read. 0=file reading */
235 static struct heap_info *key_heap_malloc(void)
236 { /* malloc and clear it */
237 struct heap_info *hi;
238 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 struct heap_info *key_heap_init_raw(ZebraHandle zh,
276 int (*cmp)(const void *p1, const void *p2))
278 struct heap_info *hi=key_heap_malloc();
285 void key_heap_destroy(struct heap_info *hi, int nkeys)
288 if (!hi->raw_reading)
289 for (i = 0; i<=nkeys; i++)
290 xfree(hi->info.buf[i]);
294 xfree(hi->info.file);
298 static void key_heap_swap(struct heap_info *hi, int i1, int i2)
303 hi->ptr[i1] = hi->ptr[i2];
308 static void key_heap_delete(struct heap_info *hi)
310 int cur = 1, child = 2;
312 assert(hi->heapnum > 0);
314 key_heap_swap(hi, 1, hi->heapnum);
316 while (child <= hi->heapnum) {
317 if (child < hi->heapnum &&
318 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
319 &hi->info.buf[hi->ptr[child+1]]) > 0)
321 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
322 &hi->info.buf[hi->ptr[child]]) > 0)
324 key_heap_swap(hi, cur, child);
333 static void key_heap_insert(struct heap_info *hi, const char *buf, int nbytes,
338 cur = ++(hi->heapnum);
339 memcpy(hi->info.buf[hi->ptr[cur]], buf, nbytes);
340 hi->info.file[hi->ptr[cur]] = kf;
343 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
344 &hi->info.buf[hi->ptr[cur]]) > 0)
346 key_heap_swap(hi, cur, parent);
352 static int heap_read_one(struct heap_info *hi, char *name, char *key)
355 char rbuf[INP_NAME_MAX];
361 strcpy(name, hi->info.buf[n]);
362 kf = hi->info.file[n];
364 memcpy(key, hi->info.buf[n] + r+1, KEY_SIZE);
366 if ((r = key_file_read(kf, rbuf)))
367 key_heap_insert(hi, rbuf, r, kf);
375 /* for debugging only */
376 void zebra_log_dict_entry(ZebraHandle zh, const char *s)
378 char dst[IT_MAX_WORD+1];
380 int len = key_SU_decode(&ord, (const unsigned char *) s);
381 const char *index_type;
384 yaz_log(YLOG_LOG, "ord=%d", ord);
387 const char *string_index;
389 zebraExplain_lookup_ord(zh->reg->zei,
390 ord, &index_type, &db, &string_index);
392 zebra_term_untrans(zh, *index_type, dst, s + len);
394 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s term=%s",
395 ord, index_type, string_index, dst);
399 struct heap_cread_info {
400 char prev_name[INP_NAME_MAX];
401 char cur_name[INP_NAME_MAX];
406 struct heap_info *hi;
413 static int heap_cread_item(void *vp, char **dst, int *insertMode);
415 int heap_cread_item2(void *vp, char **dst, int *insertMode)
417 struct heap_cread_info *p = (struct heap_cread_info *) vp;
422 if (p->look_level > 0)
432 memcpy(*dst, p->key_1, p->sz_1);
434 yaz_log(YLOG_LOG, "DUP level=%d", p->look_level);
435 pkey(*dst, *insertMode);
440 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
445 else if (p->ret == -1) /* Must read new item ? */
447 char *dst_1 = p->key_1;
448 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
449 p->sz_1 = dst_1 - p->key_1;
452 { /* lookahead in 2 . Now in 1. */
454 p->mode_1 = p->mode_2;
455 memcpy(p->key_1, p->key_2, p->sz_2);
458 level = 1; /* insert */
460 level = -1; /* delete */
463 char *dst_2 = p->key_2;
464 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
472 p->sz_2 = dst_2 - p->key_2;
474 if (key_compare(p->key_1, p->key_2) == 0)
476 if (p->mode_2) /* adjust level according to deletes/inserts */
485 /* all the same. new round .. */
487 p->mode_1 = p->mode_2;
488 memcpy(p->key_1, p->key_2, p->sz_1);
490 level = 1; /* insert */
492 level = -1; /* delete */
495 /* outcome is insert (1) or delete (0) depending on final level */
506 p->look_level = level;
507 memcpy(*dst, p->key_1, p->sz_1);
509 pkey(*dst, *insertMode);
515 int heap_cread_item(void *vp, char **dst, int *insertMode)
517 struct heap_cread_info *p = (struct heap_cread_info *) vp;
518 struct heap_info *hi = p->hi;
520 if (p->first_in_list)
522 *insertMode = p->key[0];
523 memcpy(*dst, p->key+1, sizeof(struct it_key));
525 zebra_log_dict_entry(hi->zh, p->cur_name);
526 pkey(*dst, *insertMode);
528 (*dst) += sizeof(struct it_key);
529 p->first_in_list = 0;
532 strcpy(p->prev_name, p->cur_name);
533 if (!(p->more = heap_read_one(hi, p->cur_name, p->key)))
535 if (*p->cur_name && strcmp(p->cur_name, p->prev_name))
537 p->first_in_list = 1;
540 *insertMode = p->key[0];
541 memcpy(*dst, p->key+1, sizeof(struct it_key));
543 zebra_log_dict_entry(hi->zh, p->cur_name);
544 pkey(*dst, *insertMode);
546 (*dst) += sizeof(struct it_key);
550 int heap_inpc(struct heap_cread_info *hci, struct heap_info *hi)
552 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
554 isamc_i->clientData = hci;
555 isamc_i->read_item = heap_cread_item2;
559 char this_name[INP_NAME_MAX];
560 ISAM_P isamc_p, isamc_p2;
563 strcpy(this_name, hci->cur_name);
564 assert(hci->cur_name[0]);
566 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
568 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
570 isamc_merge(hi->reg->isamc, &isamc_p2, isamc_i);
574 if (!dict_delete(hi->reg->dict, this_name))
580 if (isamc_p2 != isamc_p)
581 dict_insert(hi->reg->dict, this_name,
582 sizeof(ISAM_P), &isamc_p2);
588 isamc_merge(hi->reg->isamc, &isamc_p, isamc_i);
591 dict_insert(hi->reg->dict, this_name,
592 sizeof(ISAM_P), &isamc_p);
599 int heap_inp0(struct heap_cread_info *hci, struct heap_info *hi)
603 char this_name[INP_NAME_MAX];
608 strcpy(this_name, hci->cur_name);
609 assert(hci->cur_name[0]);
612 while (heap_cread_item2(hci, &dst, &mode))
619 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
621 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
623 isamc_i->clientData = hci;
624 isamc_i->read_item = heap_cread_item2;
628 char this_name[INP_NAME_MAX];
629 ISAM_P isamc_p, isamc_p2;
632 strcpy(this_name, hci->cur_name);
633 assert(hci->cur_name[0]);
638 zebra_log_dict_entry(hi->zh, hci->cur_name);
640 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
642 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
644 isamb_merge(hi->reg->isamb, &isamc_p2, isamc_i);
648 if (!dict_delete(hi->reg->dict, this_name))
654 if (isamc_p2 != isamc_p)
655 dict_insert(hi->reg->dict, this_name,
656 sizeof(ISAM_P), &isamc_p2);
662 isamb_merge(hi->reg->isamb, &isamc_p, isamc_i);
665 dict_insert(hi->reg->dict, this_name,
666 sizeof(ISAM_P), &isamc_p);
673 int heap_inps(struct heap_cread_info *hci, struct heap_info *hi)
675 ISAMS_I isams_i = (ISAMS_I) xmalloc(sizeof(*isams_i));
677 isams_i->clientData = hci;
678 isams_i->read_item = heap_cread_item;
682 char this_name[INP_NAME_MAX];
686 strcpy(this_name, hci->cur_name);
687 assert(hci->cur_name[0]);
689 if (!(dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
691 isams_p = isams_merge(hi->reg->isams, isams_i);
693 dict_insert(hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
697 yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
705 struct progressInfo {
712 void progressFunc(struct key_file *keyp, void *info)
714 struct progressInfo *p = (struct progressInfo *) info;
715 time_t now, remaining;
717 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
721 if (now >= p->lastTime+10)
724 remaining = (time_t) ((now - p->startTime)*
725 ((double) p->totalBytes/p->totalOffset - 1.0));
726 if (remaining <= 130)
727 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
728 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
730 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
731 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
733 p->totalOffset += keyp->buf_size;
740 void zebra_index_merge(ZebraHandle zh)
742 struct key_file **kf = 0;
745 struct heap_info *hi;
746 struct progressInfo progressInfo;
747 int nkeys = key_block_get_no_files(zh->reg->key_block);
758 extract_get_fname_tmp (zh, fname, nkeys+1);
759 if (access(fname, R_OK) == -1)
766 kf = (struct key_file **) xmalloc((1+nkeys) * sizeof(*kf));
767 progressInfo.totalBytes = 0;
768 progressInfo.totalOffset = 0;
769 time(&progressInfo.startTime);
770 time(&progressInfo.lastTime);
771 for (i = 1; i<=nkeys; i++)
773 kf[i] = key_file_init(i, 8192, zh->res);
774 kf[i]->readHandler = progressFunc;
775 kf[i]->readInfo = &progressInfo;
776 progressInfo.totalBytes += kf[i]->length;
777 progressInfo.totalOffset += kf[i]->buf_size;
779 hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
782 for (i = 1; i<=nkeys; i++)
783 if ((r = key_file_read(kf[i], rbuf)))
784 key_heap_insert(hi, rbuf, r, kf[i]);
788 struct heap_cread_info hci;
790 hci.key = (char *) xmalloc(KEY_SIZE);
791 hci.key_1 = (char *) xmalloc(KEY_SIZE);
792 hci.key_2 = (char *) xmalloc(KEY_SIZE);
794 hci.first_in_list = 1;
797 hci.more = heap_read_one(hi, hci.cur_name, hci.key);
811 for (i = 1; i<=nkeys; i++)
813 extract_get_fname_tmp (zh, rbuf, i);
816 for (i = 1; i<=nkeys; i++)
817 key_file_destroy(kf[i]);
819 if (hi->no_iterations)
820 { /* do not log if nothing happened */
821 yaz_log(YLOG_LOG, "Iterations: isam/dict "
822 ZINT_FORMAT "/" ZINT_FORMAT,
823 hi->no_iterations, hi->no_diffs);
824 yaz_log(YLOG_LOG, "Dict: inserts/updates/deletions: "
825 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
826 hi->no_insertions, hi->no_updates, hi->no_deletions);
828 key_block_destroy(&zh->reg->key_block);
829 key_heap_destroy(hi, nkeys);
834 * indent-tabs-mode: nil
836 * vim: shiftwidth=4 tabstop=8 expandtab