1 /* $Id: kinput.c,v 1.78 2006-11-21 14:32:38 adam Exp $
2 Copyright (C) 1995-2006
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);
134 xfree (f->prev_name);
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]);
292 xfree (hi->info.buf);
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);
365 key_heap_delete (hi);
366 if ((r = key_file_read (kf, rbuf)))
367 key_heap_insert (hi, rbuf, r, kf);
376 /* for debugging only */
377 static void print_dict_item(ZebraHandle zh, const char *s)
379 char dst[IT_MAX_WORD+1];
381 int len = key_SU_decode(&ord, (const unsigned char *) s);
386 yaz_log(YLOG_LOG, "ord=%d", ord);
389 zebraExplain_lookup_ord (zh->reg->zei,
390 ord, &index_type, &db, 0, 0, 0);
392 zebra_term_untrans(zh, index_type, dst, s + len);
394 yaz_log(YLOG_LOG, "ord=%d term=%s", ord, 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 pkey(*dst, *insertMode);
527 (*dst) += sizeof(struct it_key);
528 p->first_in_list = 0;
531 strcpy (p->prev_name, p->cur_name);
532 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
534 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
536 p->first_in_list = 1;
539 *insertMode = p->key[0];
540 memcpy (*dst, p->key+1, sizeof(struct it_key));
542 pkey(*dst, *insertMode);
544 (*dst) += sizeof(struct it_key);
548 int heap_inpc (struct heap_cread_info *hci, struct heap_info *hi)
550 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
552 isamc_i->clientData = hci;
553 isamc_i->read_item = heap_cread_item2;
557 char this_name[INP_NAME_MAX];
558 ISAM_P isamc_p, isamc_p2;
561 strcpy (this_name, hci->cur_name);
562 assert (hci->cur_name[0]);
564 if ((dict_info = dict_lookup (hi->reg->dict, hci->cur_name)))
566 memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
568 isamc_merge (hi->reg->isamc, &isamc_p2, isamc_i);
572 if (!dict_delete (hi->reg->dict, this_name))
578 if (isamc_p2 != isamc_p)
579 dict_insert (hi->reg->dict, this_name,
580 sizeof(ISAM_P), &isamc_p2);
586 isamc_merge (hi->reg->isamc, &isamc_p, isamc_i);
589 dict_insert (hi->reg->dict, this_name,
590 sizeof(ISAM_P), &isamc_p);
597 int heap_inp0(struct heap_cread_info *hci, struct heap_info *hi)
601 char this_name[INP_NAME_MAX];
606 strcpy (this_name, hci->cur_name);
607 assert (hci->cur_name[0]);
610 while (heap_cread_item2(hci, &dst, &mode))
617 int heap_inpb(struct heap_cread_info *hci, struct heap_info *hi)
619 ISAMC_I *isamc_i = (ISAMC_I *) xmalloc (sizeof(*isamc_i));
621 isamc_i->clientData = hci;
622 isamc_i->read_item = heap_cread_item2;
626 char this_name[INP_NAME_MAX];
627 ISAM_P isamc_p, isamc_p2;
630 strcpy (this_name, hci->cur_name);
631 assert (hci->cur_name[0]);
636 print_dict_item(hi->zh, hci->cur_name);
638 if ((dict_info = dict_lookup (hi->reg->dict, hci->cur_name)))
640 memcpy (&isamc_p, dict_info+1, sizeof(ISAM_P));
642 isamb_merge (hi->reg->isamb, &isamc_p2, isamc_i);
646 if (!dict_delete (hi->reg->dict, this_name))
652 if (isamc_p2 != isamc_p)
653 dict_insert (hi->reg->dict, this_name,
654 sizeof(ISAM_P), &isamc_p2);
660 isamb_merge (hi->reg->isamb, &isamc_p, isamc_i);
663 dict_insert (hi->reg->dict, this_name,
664 sizeof(ISAM_P), &isamc_p);
671 int heap_inps (struct heap_cread_info *hci, struct heap_info *hi)
673 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
675 isams_i->clientData = hci;
676 isams_i->read_item = heap_cread_item;
680 char this_name[INP_NAME_MAX];
684 strcpy (this_name, hci->cur_name);
685 assert (hci->cur_name[0]);
687 if (!(dict_info = dict_lookup (hi->reg->dict, hci->cur_name)))
689 isams_p = isams_merge (hi->reg->isams, isams_i);
691 dict_insert (hi->reg->dict, this_name, sizeof(ISAM_P), &isams_p);
695 yaz_log(YLOG_FATAL, "isams doesn't support this kind of update");
703 struct progressInfo {
710 void progressFunc (struct key_file *keyp, void *info)
712 struct progressInfo *p = (struct progressInfo *) info;
713 time_t now, remaining;
715 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
719 if (now >= p->lastTime+10)
722 remaining = (time_t) ((now - p->startTime)*
723 ((double) p->totalBytes/p->totalOffset - 1.0));
724 if (remaining <= 130)
725 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
726 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
728 yaz_log(YLOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
729 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
731 p->totalOffset += keyp->buf_size;
738 void zebra_index_merge (ZebraHandle zh)
740 struct key_file **kf = 0;
743 struct heap_info *hi;
744 struct progressInfo progressInfo;
745 int nkeys = key_block_get_no_files(zh->reg->key_block);
756 extract_get_fname_tmp (zh, fname, nkeys+1);
757 if (access (fname, R_OK) == -1)
764 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
765 progressInfo.totalBytes = 0;
766 progressInfo.totalOffset = 0;
767 time (&progressInfo.startTime);
768 time (&progressInfo.lastTime);
769 for (i = 1; i<=nkeys; i++)
771 kf[i] = key_file_init (i, 8192, zh->res);
772 kf[i]->readHandler = progressFunc;
773 kf[i]->readInfo = &progressInfo;
774 progressInfo.totalBytes += kf[i]->length;
775 progressInfo.totalOffset += kf[i]->buf_size;
777 hi = key_heap_init_file(zh, nkeys, key_qsort_compare);
780 for (i = 1; i<=nkeys; i++)
781 if ((r = key_file_read (kf[i], rbuf)))
782 key_heap_insert (hi, rbuf, r, kf[i]);
786 struct heap_cread_info hci;
788 hci.key = (char *) xmalloc (KEY_SIZE);
789 hci.key_1 = (char *) xmalloc (KEY_SIZE);
790 hci.key_2 = (char *) xmalloc (KEY_SIZE);
792 hci.first_in_list = 1;
795 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
809 for (i = 1; i<=nkeys; i++)
811 extract_get_fname_tmp (zh, rbuf, i);
814 for (i = 1; i<=nkeys; i++)
815 key_file_destroy (kf[i]);
817 if (hi->no_iterations)
818 { /* do not log if nothing happened */
819 yaz_log(YLOG_LOG, "Iterations . . .%7d", hi->no_iterations);
820 yaz_log(YLOG_LOG, "Distinct words .%7d", hi->no_diffs);
821 yaz_log(YLOG_LOG, "Updates. . . . .%7d", hi->no_updates);
822 yaz_log(YLOG_LOG, "Deletions. . . .%7d", hi->no_deletions);
823 yaz_log(YLOG_LOG, "Insertions . . .%7d", hi->no_insertions);
825 key_block_destroy(&zh->reg->key_block);
826 key_heap_destroy(hi, nkeys);
831 * indent-tabs-mode: nil
833 * vim: shiftwidth=4 tabstop=8 expandtab