1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #define KEY_SIZE (1+sizeof(struct it_key))
35 #define INP_NAME_MAX 768
39 off_t offset; /* file offset */
40 unsigned char *buf; /* buffer block */
41 size_t buf_size; /* number of read bytes in block */
42 size_t chunk; /* number of bytes allocated */
43 size_t buf_ptr; /* current position in buffer */
44 char *prev_name; /* last word read */
46 off_t length; /* length of file */
47 /* handler invoked in each read */
48 void (*readHandler)(struct key_file *keyp, void *rinfo);
57 #if PR_KEY_LOW || PR_KEY_TOP
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)))
193 if (i < INP_NAME_MAX-2)
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);
363 /* for debugging only */
364 void zebra_log_dict_entry(ZebraHandle zh, const char *s)
366 char dst[INP_NAME_MAX+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 yaz_log(YLOG_LOG, "TOP");
498 pkey(*dst, *insertMode);
504 int heap_cread_item(void *vp, char **dst, int *insertMode)
506 struct heap_cread_info *p = (struct heap_cread_info *) vp;
507 struct heap_info *hi = p->hi;
509 if (p->first_in_list)
511 *insertMode = p->key[0];
512 memcpy(*dst, p->key+1, sizeof(struct it_key));
514 zebra_log_dict_entry(hi->zh, p->cur_name);
515 pkey(*dst, *insertMode);
517 (*dst) += sizeof(struct it_key);
518 p->first_in_list = 0;
521 strcpy(p->prev_name, p->cur_name);
522 if (!(p->more = heap_read_one(hi, p->cur_name, p->key)))
524 if (*p->cur_name && strcmp(p->cur_name, p->prev_name))
526 p->first_in_list = 1;
529 *insertMode = p->key[0];
530 memcpy(*dst, p->key+1, sizeof(struct it_key));
532 zebra_log_dict_entry(hi->zh, p->cur_name);
533 pkey(*dst, *insertMode);
535 (*dst) += sizeof(struct it_key);
539 int heap_inpc(struct heap_cread_info *hci, struct heap_info *hi)
541 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
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[0]);
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);
580 dict_insert(hi->reg->dict, this_name,
581 sizeof(ISAM_P), &isamc_p);
588 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
590 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
592 isamc_i->clientData = hci;
593 isamc_i->read_item = heap_cread_item2;
597 char this_name[INP_NAME_MAX];
598 ISAM_P isamc_p, isamc_p2;
601 strcpy(this_name, hci->cur_name);
602 assert(hci->cur_name[0]);
607 zebra_log_dict_entry(hi->zh, hci->cur_name);
609 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
611 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
613 isamb_merge(hi->reg->isamb, &isamc_p2, isamc_i);
617 if (!dict_delete(hi->reg->dict, this_name))
623 if (isamc_p2 != isamc_p)
624 dict_insert(hi->reg->dict, this_name,
625 sizeof(ISAM_P), &isamc_p2);
631 isamb_merge(hi->reg->isamb, &isamc_p, isamc_i);
634 dict_insert(hi->reg->dict, this_name,
635 sizeof(ISAM_P), &isamc_p);
642 int heap_inps(struct heap_cread_info *hci, struct heap_info *hi)
644 ISAMS_I isams_i = (ISAMS_I) xmalloc(sizeof(*isams_i));
646 isams_i->clientData = hci;
647 isams_i->read_item = heap_cread_item;
651 char this_name[INP_NAME_MAX];
655 strcpy(this_name, hci->cur_name);
656 assert(hci->cur_name[0]);
658 if (!(dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
660 isams_p = isams_merge(hi->reg->isams, isams_i);
662 dict_insert(hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
666 yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
674 struct progressInfo {
681 void progressFunc(struct key_file *keyp, void *info)
683 struct progressInfo *p = (struct progressInfo *) info;
684 time_t now, remaining;
686 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
690 if (now >= p->lastTime+10)
693 remaining = (time_t) ((now - p->startTime)*
694 ((double) p->totalBytes/p->totalOffset - 1.0));
695 if (remaining <= 130)
696 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
697 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
699 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
700 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
702 p->totalOffset += keyp->buf_size;
709 void zebra_index_merge(ZebraHandle zh)
711 struct key_file **kf = 0;
714 struct heap_info *hi;
715 struct progressInfo progressInfo;
716 int nkeys = key_block_get_no_files(zh->reg->key_block);
727 extract_get_fname_tmp (zh, fname, nkeys+1);
728 if (access(fname, R_OK) == -1)
735 kf = (struct key_file **) xmalloc((1+nkeys) * sizeof(*kf));
736 progressInfo.totalBytes = 0;
737 progressInfo.totalOffset = 0;
738 time(&progressInfo.startTime);
739 time(&progressInfo.lastTime);
740 for (i = 1; i<=nkeys; i++)
742 kf[i] = key_file_init(i, 8192, zh->res);
743 kf[i]->readHandler = progressFunc;
744 kf[i]->readInfo = &progressInfo;
745 progressInfo.totalBytes += kf[i]->length;
746 progressInfo.totalOffset += kf[i]->buf_size;
748 hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
751 for (i = 1; i<=nkeys; i++)
752 if ((r = key_file_read(kf[i], rbuf)))
753 key_heap_insert(hi, rbuf, r, kf[i]);
757 struct heap_cread_info hci;
759 hci.key = (char *) xmalloc(KEY_SIZE);
760 hci.key_1 = (char *) xmalloc(KEY_SIZE);
761 hci.key_2 = (char *) xmalloc(KEY_SIZE);
763 hci.first_in_list = 1;
766 hci.more = heap_read_one(hi, hci.cur_name, hci.key);
780 for (i = 1; i<=nkeys; i++)
782 extract_get_fname_tmp (zh, rbuf, i);
785 for (i = 1; i<=nkeys; i++)
786 key_file_destroy(kf[i]);
788 if (hi->no_iterations)
789 { /* do not log if nothing happened */
790 yaz_log(YLOG_LOG, "Iterations: isam/dict "
791 ZINT_FORMAT "/" ZINT_FORMAT,
792 hi->no_iterations, hi->no_diffs);
793 yaz_log(YLOG_LOG, "Dict: inserts/updates/deletions: "
794 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
795 hi->no_insertions, hi->no_updates, hi->no_deletions);
797 key_block_destroy(&zh->reg->key_block);
798 key_heap_destroy(hi, nkeys);
803 * c-file-style: "Stroustrup"
804 * indent-tabs-mode: nil
806 * vim: shiftwidth=4 tabstop=8 expandtab