1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 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);
54 static void pkey(const char *b, int mode)
56 key_logdump_txt(YLOG_LOG, b, mode ? "i" : "d");
61 void getFnameTmp(Res res, char *fname, int no)
65 pre = res_get_def(res, "keyTmpDir", ".");
66 sprintf(fname, "%s/key%d.tmp", pre, no);
69 void extract_get_fname_tmp(ZebraHandle zh, char *fname, int no)
73 pre = res_get_def(zh->res, "keyTmpDir", ".");
74 sprintf(fname, "%s/key%d.tmp", pre, no);
77 void key_file_chunk_read(struct key_file *f)
79 int nr = 0, r = 0, fd;
81 getFnameTmp(f->res, fname, f->no);
82 fd = open(fname, O_BINARY|O_RDONLY);
88 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot open %s", fname);
93 if ((f->length = lseek(fd, 0L, SEEK_END)) == (off_t) -1)
95 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
100 if (lseek(fd, f->offset, SEEK_SET) == -1)
102 yaz_log(YLOG_WARN|YLOG_ERRNO, "cannot seek %s", fname);
106 while (f->chunk - nr > 0)
108 r = read(fd, f->buf + nr, f->chunk - nr);
115 yaz_log(YLOG_WARN|YLOG_ERRNO, "read of %s", fname);
121 (*f->readHandler)(f, f->readInfo);
125 void key_file_destroy(struct key_file *f)
127 iscz1_stop(f->decode_handle);
133 struct key_file *key_file_init(int no, int chunk, Res res)
137 f = (struct key_file *) xmalloc(sizeof(*f));
139 f->decode_handle = iscz1_start();
144 f->readHandler = NULL;
145 f->buf = (unsigned char *) xmalloc(f->chunk);
146 f->prev_name = (char *) xmalloc(INP_NAME_MAX);
147 *f->prev_name = '\0';
148 key_file_chunk_read(f);
152 int key_file_getc(struct key_file *f)
154 if (f->buf_ptr < f->buf_size)
155 return f->buf[(f->buf_ptr)++];
156 if (f->buf_size < f->chunk)
158 f->offset += f->buf_size;
159 key_file_chunk_read(f);
160 if (f->buf_ptr < f->buf_size)
161 return f->buf[(f->buf_ptr)++];
166 int key_file_read(struct key_file *f, char *key)
170 const char *src = srcbuf;
174 c = key_file_getc(f);
177 strcpy(key, f->prev_name);
186 while ((c = key_file_getc(f)))
188 if (i <= IT_MAX_WORD)
192 strcpy(f->prev_name, key);
193 iscz1_reset(f->decode_handle);
195 c = key_file_getc(f); /* length + insert/delete combined */
198 for (j = 0; j < c; j++)
199 srcbuf[j] = key_file_getc(f);
201 iscz1_decode(f->decode_handle, &dst, &src);
208 memcpy(&k, key+i, sizeof(k));
210 yaz_log(YLOG_LOG, "00 KEY");
213 return i + sizeof(struct it_key);
218 struct key_file **file;
223 int (*cmp)(const void *p1, const void *p2);
224 struct zebra_register *reg;
233 static struct heap_info *key_heap_malloc(void)
234 { /* malloc and clear it */
235 struct heap_info *hi;
236 hi = (struct heap_info *) xmalloc(sizeof(*hi));
244 hi->no_deletions = 0;
245 hi->no_insertions = 0;
246 hi->no_iterations = 0;
250 struct heap_info *key_heap_init_file(ZebraHandle zh,
252 int (*cmp)(const void *p1, const void *p2))
254 struct heap_info *hi;
257 hi = key_heap_malloc();
259 hi->info.file = (struct key_file **)
260 xmalloc(sizeof(*hi->info.file) * (1+nkeys));
261 hi->info.buf = (char **) xmalloc(sizeof(*hi->info.buf) * (1+nkeys));
262 hi->ptr = (int *) xmalloc(sizeof(*hi->ptr) * (1+nkeys));
264 for (i = 0; i<= nkeys; i++)
267 hi->info.buf[i] = (char *) xmalloc(INP_NAME_MAX);
272 void key_heap_destroy(struct heap_info *hi, int nkeys)
275 for (i = 0; i<=nkeys; i++)
276 xfree(hi->info.buf[i]);
279 xfree(hi->info.file);
283 static void key_heap_swap(struct heap_info *hi, int i1, int i2)
288 hi->ptr[i1] = hi->ptr[i2];
293 static void key_heap_delete(struct heap_info *hi)
295 int cur = 1, child = 2;
297 assert(hi->heapnum > 0);
299 key_heap_swap(hi, 1, hi->heapnum);
301 while (child <= hi->heapnum) {
302 if (child < hi->heapnum &&
303 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
304 &hi->info.buf[hi->ptr[child+1]]) > 0)
306 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
307 &hi->info.buf[hi->ptr[child]]) > 0)
309 key_heap_swap(hi, cur, child);
318 static void key_heap_insert(struct heap_info *hi, const char *buf, int nbytes,
323 cur = ++(hi->heapnum);
324 memcpy(hi->info.buf[hi->ptr[cur]], buf, nbytes);
325 hi->info.file[hi->ptr[cur]] = kf;
328 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
329 &hi->info.buf[hi->ptr[cur]]) > 0)
331 key_heap_swap(hi, cur, parent);
337 static int heap_read_one(struct heap_info *hi, char *name, char *key)
340 char rbuf[INP_NAME_MAX];
346 strcpy(name, hi->info.buf[n]);
347 kf = hi->info.file[n];
349 memcpy(key, hi->info.buf[n] + r+1, KEY_SIZE);
351 if ((r = key_file_read(kf, rbuf)))
352 key_heap_insert(hi, rbuf, r, kf);
360 /* for debugging only */
361 void zebra_log_dict_entry(ZebraHandle zh, const char *s)
363 char dst[IT_MAX_WORD+1];
365 int len = key_SU_decode(&ord, (const unsigned char *) s);
366 const char *index_type;
369 yaz_log(YLOG_LOG, "ord=%d", ord);
372 const char *string_index;
374 zebraExplain_lookup_ord(zh->reg->zei,
375 ord, &index_type, &db, &string_index);
377 zebra_term_untrans(zh, index_type, dst, s + len);
379 yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s term=%s",
380 ord, index_type, string_index, dst);
384 struct heap_cread_info {
385 char prev_name[INP_NAME_MAX];
386 char cur_name[INP_NAME_MAX];
391 struct heap_info *hi;
398 static int heap_cread_item(void *vp, char **dst, int *insertMode);
400 int heap_cread_item2(void *vp, char **dst, int *insertMode)
402 struct heap_cread_info *p = (struct heap_cread_info *) vp;
407 if (p->look_level > 0)
417 memcpy(*dst, p->key_1, p->sz_1);
419 yaz_log(YLOG_LOG, "DUP level=%d", p->look_level);
420 pkey(*dst, *insertMode);
425 if (p->ret == 0) /* lookahead was 0?. Return that in read next round */
430 else if (p->ret == -1) /* Must read new item ? */
432 char *dst_1 = p->key_1;
433 p->ret = heap_cread_item(vp, &dst_1, &p->mode_1);
434 p->sz_1 = dst_1 - p->key_1;
437 { /* lookahead in 2 . Now in 1. */
439 p->mode_1 = p->mode_2;
440 memcpy(p->key_1, p->key_2, p->sz_2);
443 level = 1; /* insert */
445 level = -1; /* delete */
448 char *dst_2 = p->key_2;
449 p->ret = heap_cread_item(vp, &dst_2, &p->mode_2);
457 p->sz_2 = dst_2 - p->key_2;
459 if (key_compare(p->key_1, p->key_2) == 0)
461 if (p->mode_2) /* adjust level according to deletes/inserts */
470 /* all the same. new round .. */
472 p->mode_1 = p->mode_2;
473 memcpy(p->key_1, p->key_2, p->sz_1);
475 level = 1; /* insert */
477 level = -1; /* delete */
480 /* outcome is insert (1) or delete (0) depending on final level */
491 p->look_level = level;
492 memcpy(*dst, p->key_1, p->sz_1);
494 pkey(*dst, *insertMode);
500 int heap_cread_item(void *vp, char **dst, int *insertMode)
502 struct heap_cread_info *p = (struct heap_cread_info *) vp;
503 struct heap_info *hi = p->hi;
505 if (p->first_in_list)
507 *insertMode = p->key[0];
508 memcpy(*dst, p->key+1, sizeof(struct it_key));
510 zebra_log_dict_entry(hi->zh, p->cur_name);
511 pkey(*dst, *insertMode);
513 (*dst) += sizeof(struct it_key);
514 p->first_in_list = 0;
517 strcpy(p->prev_name, p->cur_name);
518 if (!(p->more = heap_read_one(hi, p->cur_name, p->key)))
520 if (*p->cur_name && strcmp(p->cur_name, p->prev_name))
522 p->first_in_list = 1;
525 *insertMode = p->key[0];
526 memcpy(*dst, p->key+1, sizeof(struct it_key));
528 zebra_log_dict_entry(hi->zh, p->cur_name);
529 pkey(*dst, *insertMode);
531 (*dst) += sizeof(struct it_key);
535 int heap_inpc(struct heap_cread_info *hci, struct heap_info *hi)
537 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
539 isamc_i->clientData = hci;
540 isamc_i->read_item = heap_cread_item2;
544 char this_name[INP_NAME_MAX];
545 ISAM_P isamc_p, isamc_p2;
548 strcpy(this_name, hci->cur_name);
549 assert(hci->cur_name[0]);
551 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
553 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
555 isamc_merge(hi->reg->isamc, &isamc_p2, isamc_i);
559 if (!dict_delete(hi->reg->dict, this_name))
565 if (isamc_p2 != isamc_p)
566 dict_insert(hi->reg->dict, this_name,
567 sizeof(ISAM_P), &isamc_p2);
573 isamc_merge(hi->reg->isamc, &isamc_p, isamc_i);
576 dict_insert(hi->reg->dict, this_name,
577 sizeof(ISAM_P), &isamc_p);
584 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
586 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc(sizeof(*isamc_i));
588 isamc_i->clientData = hci;
589 isamc_i->read_item = heap_cread_item2;
593 char this_name[INP_NAME_MAX];
594 ISAM_P isamc_p, isamc_p2;
597 strcpy(this_name, hci->cur_name);
598 assert(hci->cur_name[0]);
603 zebra_log_dict_entry(hi->zh, hci->cur_name);
605 if ((dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
607 memcpy(&isamc_p, dict_info+1, sizeof(ISAM_P));
609 isamb_merge(hi->reg->isamb, &isamc_p2, isamc_i);
613 if (!dict_delete(hi->reg->dict, this_name))
619 if (isamc_p2 != isamc_p)
620 dict_insert(hi->reg->dict, this_name,
621 sizeof(ISAM_P), &isamc_p2);
627 isamb_merge(hi->reg->isamb, &isamc_p, isamc_i);
630 dict_insert(hi->reg->dict, this_name,
631 sizeof(ISAM_P), &isamc_p);
638 int heap_inps(struct heap_cread_info *hci, struct heap_info *hi)
640 ISAMS_I isams_i = (ISAMS_I) xmalloc(sizeof(*isams_i));
642 isams_i->clientData = hci;
643 isams_i->read_item = heap_cread_item;
647 char this_name[INP_NAME_MAX];
651 strcpy(this_name, hci->cur_name);
652 assert(hci->cur_name[0]);
654 if (!(dict_info = dict_lookup(hi->reg->dict, hci->cur_name)))
656 isams_p = isams_merge(hi->reg->isams, isams_i);
658 dict_insert(hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
662 yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
670 struct progressInfo {
677 void progressFunc(struct key_file *keyp, void *info)
679 struct progressInfo *p = (struct progressInfo *) info;
680 time_t now, remaining;
682 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
686 if (now >= p->lastTime+10)
689 remaining = (time_t) ((now - p->startTime)*
690 ((double) p->totalBytes/p->totalOffset - 1.0));
691 if (remaining <= 130)
692 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
693 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
695 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
696 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
698 p->totalOffset += keyp->buf_size;
705 void zebra_index_merge(ZebraHandle zh)
707 struct key_file **kf = 0;
710 struct heap_info *hi;
711 struct progressInfo progressInfo;
712 int nkeys = key_block_get_no_files(zh->reg->key_block);
723 extract_get_fname_tmp (zh, fname, nkeys+1);
724 if (access(fname, R_OK) == -1)
731 kf = (struct key_file **) xmalloc((1+nkeys) * sizeof(*kf));
732 progressInfo.totalBytes = 0;
733 progressInfo.totalOffset = 0;
734 time(&progressInfo.startTime);
735 time(&progressInfo.lastTime);
736 for (i = 1; i<=nkeys; i++)
738 kf[i] = key_file_init(i, 8192, zh->res);
739 kf[i]->readHandler = progressFunc;
740 kf[i]->readInfo = &progressInfo;
741 progressInfo.totalBytes += kf[i]->length;
742 progressInfo.totalOffset += kf[i]->buf_size;
744 hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
747 for (i = 1; i<=nkeys; i++)
748 if ((r = key_file_read(kf[i], rbuf)))
749 key_heap_insert(hi, rbuf, r, kf[i]);
753 struct heap_cread_info hci;
755 hci.key = (char *) xmalloc(KEY_SIZE);
756 hci.key_1 = (char *) xmalloc(KEY_SIZE);
757 hci.key_2 = (char *) xmalloc(KEY_SIZE);
759 hci.first_in_list = 1;
762 hci.more = heap_read_one(hi, hci.cur_name, hci.key);
776 for (i = 1; i<=nkeys; i++)
778 extract_get_fname_tmp (zh, rbuf, i);
781 for (i = 1; i<=nkeys; i++)
782 key_file_destroy(kf[i]);
784 if (hi->no_iterations)
785 { /* do not log if nothing happened */
786 yaz_log(YLOG_LOG, "Iterations: isam/dict "
787 ZINT_FORMAT "/" ZINT_FORMAT,
788 hi->no_iterations, hi->no_diffs);
789 yaz_log(YLOG_LOG, "Dict: inserts/updates/deletions: "
790 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
791 hi->no_insertions, hi->no_updates, hi->no_deletions);
793 key_block_destroy(&zh->reg->key_block);
794 key_heap_destroy(hi, nkeys);
799 * indent-tabs-mode: nil
801 * vim: shiftwidth=4 tabstop=8 expandtab