1 /* This file is part of the Zebra server.
2 Copyright (C) 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
23 #include <yaz/options.h>
29 #include <yaz/xmalloc.h>
30 #include <yaz/marcdisp.h>
31 #include <yaz/timing.h>
33 #include <idzebra/isamb.h>
34 #include <idzebra/dict.h>
42 struct index_term *terms;
43 struct index_term **ar;
52 struct index_term *next;
55 struct index_block *index_block_new(int memory)
57 struct index_block *b = xmalloc(sizeof(*b));
59 b->current_max = memory * 1024 * 1024;
61 b->nmem = nmem_create();
66 void index_block_destroy(struct index_block **bp)
70 nmem_destroy((*bp)->nmem);
76 static int cmp_ar(const void *p1, const void *p2)
78 struct index_term *t1 = *(struct index_term **) p1;
79 struct index_term *t2 = *(struct index_term **) p2;
80 int d = strcmp(t1->term, t2->term);
84 if (t1->docid > t2->docid)
86 else if (t1->docid < t2->docid)
88 if (t1->seqno > t2->seqno)
90 else if (t1->seqno < t2->seqno)
96 int code_read(void *vp, char **dst, int *insertMode)
98 struct index_block *b = (struct index_block *)vp;
102 if (b->current_entry >= b->no_entries)
105 t = b->ar[b->current_entry];
109 key.mem[0] = t->word_id;
110 key.mem[1] = t->docid;
111 key.mem[2] = t->seqno;
114 memcpy(*dst, &key, sizeof(key));
116 (*dst) += sizeof(key);
119 yaz_log(YLOG_LOG, "returning " ZINT_FORMAT " " ZINT_FORMAT "\n",
120 key.mem[0], key.mem[1]);
125 void index_block_flush(struct index_block *b, ISAMB isb, Dict dict,
128 struct index_term *t = b->terms;
131 int no_words = 0, no_new_words = 0;
132 const char *dict_info = 0;
134 yaz_timing_t tim_dict = 0;
135 yaz_timing_t tim_isamb = 0;
136 zint number_of_int_splits = isamb_get_int_splits(isb);
137 zint number_of_leaf_splits = isamb_get_leaf_splits(isb);
138 zint number_of_dict_splits = dict_get_no_split(dict);
140 b->ar = xmalloc(sizeof(*b->ar) * b->no_entries);
141 for (i = 0; i < b->no_entries; i++, t = t->next)
148 qsort(b->ar, b->no_entries, sizeof(*b->ar), cmp_ar);
149 tim_dict = yaz_timing_create();
151 for (i = 0; i < b->no_entries; i++)
153 printf("%s " ZINT_FORMAT " " ZINT_FORMAT "\n",
154 ar[i]->term, ar[i]->docid, ar[i]->seqno);
157 dict_info = dict_lookup(dict, "_w");
160 assert(*dict_info == sizeof(word_id_seq));
161 memcpy(&word_id_seq, dict_info+1, sizeof(word_id_seq));
164 dict_info = dict_lookup(dict, "_i");
167 assert(*dict_info == sizeof(isamc_p));
168 memcpy(&isamc_p, dict_info+1, sizeof(isamc_p));
171 for (i = 0; i < b->no_entries; i++)
173 if (i > 0 && strcmp(b->ar[i-1]->term, b->ar[i]->term) == 0)
174 b->ar[i]->word_id = b->ar[i-1]->word_id;
177 const char *dict_info = dict_lookup(dict, b->ar[i]->term);
180 memcpy(&b->ar[i]->word_id, dict_info+1, sizeof(int));
186 dict_insert(dict, b->ar[i]->term, sizeof(int), &word_id_seq);
187 b->ar[i]->word_id = word_id_seq;
192 dict_insert(dict, "_w", sizeof(word_id_seq), &word_id_seq);
194 yaz_timing_stop(tim_dict);
195 tim_isamb = yaz_timing_create();
197 b->current_entry = 0;
203 isamc_i.clientData = b;
204 isamc_i.read_item = code_read;
206 isamb_merge (isb, &isamc_p, &isamc_i);
209 dict_insert(dict, "_i", sizeof(isamc_p), &isamc_p);
212 yaz_timing_stop(tim_isamb);
214 number_of_int_splits = isamb_get_int_splits(isb) - number_of_int_splits;
215 number_of_leaf_splits = isamb_get_leaf_splits(isb) - number_of_leaf_splits;
216 number_of_dict_splits = dict_get_no_split(dict) - number_of_dict_splits;
220 printf("# run total dict-real user sys isam-real user sys "
221 " intsp leafsp docs postings words new d-spl\n");
224 printf("%5d %9.6f %9.6f %5.2f %5.2f %9.6f %5.2f %5.2f "
225 "%6" ZINT_FORMAT0 " %6" ZINT_FORMAT0
226 " %8d %8d %6d %6d" " %5" ZINT_FORMAT0 "\n",
228 yaz_timing_get_real(tim_dict) + yaz_timing_get_real(tim_isamb),
229 yaz_timing_get_real(tim_dict),
230 yaz_timing_get_user(tim_dict),
231 yaz_timing_get_sys(tim_dict),
232 yaz_timing_get_real(tim_isamb),
233 yaz_timing_get_user(tim_isamb),
234 yaz_timing_get_sys(tim_isamb),
235 number_of_int_splits,
236 number_of_leaf_splits,
241 number_of_dict_splits
251 yaz_timing_destroy(&tim_isamb);
252 yaz_timing_destroy(&tim_dict);
255 void index_block_check_flush(struct index_block *b, ISAMB isb, Dict dict,
258 int total = nmem_total(b->nmem);
259 int max = b->current_max;
262 index_block_flush(b, isb, dict, no_docs);
266 void index_block_add(struct index_block *b,
267 const char *term, zint docid, zint seqno)
269 struct index_term *t = nmem_malloc(b->nmem, sizeof(*t));
270 t->term = nmem_strdup(b->nmem, term);
278 void index_term(struct index_block *b, const char *term,
279 zint docid, zint *seqno)
282 printf("%s " ZINT_FORMAT " " ZINT_FORMAT "\n", term,
285 index_block_add(b, term, docid, *seqno);
289 void index_wrbuf(struct index_block *b, WRBUF wrbuf, zint docid,
293 const char *cp = wrbuf_buf(wrbuf);
304 { /* skip field+indicator (e.g. 245 00) */
305 for (i = 0; i<6 && *cp; i++, cp++)
309 { /* continuation line */
310 for (i = 0; i<4 && *cp; i++, cp++)
319 index_term(b, term, docid, &seqno);
325 else if (*cp == subfield_char && cp[1])
329 index_term(b, term, docid, &seqno);
334 else if (strchr("$*/-;,.:[]\"&(){} ", *cp))
338 index_term(b, term, docid, &seqno);
345 unsigned ch = *(const unsigned char *)cp;
346 if (sz < sizeof(term))
348 term[sz] = tolower(ch);
356 index_term(b, term, docid, &seqno);
359 void index_marc_line_records(ISAMB isb,
365 WRBUF wrbuf = wrbuf_alloc();
369 struct index_block *b = index_block_new(memory);
370 while(fgets(line, sizeof(line)-1, inf))
384 index_block_check_flush(b, isb, dict, no_docs);
391 wrbuf_puts(wrbuf, line);
396 /* index existing buffer (if any) */
397 if (wrbuf_len(wrbuf))
399 index_wrbuf(b, wrbuf, *docid_seq, '*');
402 if (line[0] != ' ' && line[1] != ' ' && line[2] != ' ' &&
405 /* normal field+indicator line */
406 wrbuf_puts(wrbuf, line);
410 if (wrbuf_len(wrbuf))
412 index_wrbuf(b, wrbuf, *docid_seq, '*');
417 index_block_flush(b, isb, dict, no_docs);
418 index_block_destroy(&b);
421 void index_marc_from_file(ISAMB isb,
426 int verbose, int print_offset)
428 yaz_marc_t mt = yaz_marc_create();
429 WRBUF wrbuf = wrbuf_alloc();
430 struct index_block *b = index_block_new(memory);
439 r = fread (buf, 1, 5, inf);
442 if (r && print_offset && verbose)
443 printf ("<!-- Extra %ld bytes at end of file -->\n",
447 while (*buf < '0' || *buf > '9')
450 long off = ftell(inf) - 5;
451 if (verbose || print_offset)
452 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
454 *buf & 0xff, *buf & 0xff,
456 for (i = 0; i<4; i++)
458 r = fread(buf+4, 1, 1, inf);
464 if (verbose || print_offset)
465 printf ("<!-- End of file with data -->\n");
468 len = atoi_n(buf, 5);
469 if (len < 25 || len > 100000)
471 long off = ftell(inf) - 5;
472 printf("Bad Length %ld read at offset %ld (%lx)\n",
473 (long)len, (long) off, (long) off);
477 r = fread (buf + 5, 1, rlen, inf);
480 yaz_marc_read_iso2709(mt, buf, len);
482 if (yaz_marc_write_line(mt, wrbuf))
485 index_wrbuf(b, wrbuf, *docid_seq, '$');
490 index_block_check_flush(b, isb, dict, no_docs);
492 index_block_flush(b, isb, dict, no_docs);
493 wrbuf_destroy(wrbuf);
494 yaz_marc_destroy(mt);
495 index_block_destroy(&b);
498 void exit_usage(void)
500 fprintf(stderr, "benchindex1 [-t type] [-c d:i] [-m mem] [-i] [inputfile]\n");
504 int main(int argc, char **argv)
508 ISAMC_M method_postings;
514 int isam_cache_size = 40;
515 int dict_cache_size = 50;
516 const char *fname = 0;
518 yaz_timing_t tim = 0;
520 const char *dict_info;
521 const char *type = "iso2709";
522 int int_count_enable = 1;
524 while ((ret = options("im:t:c:N", argv, argc, &arg)) != -2)
535 if (!strcmp(arg, "iso2709"))
537 else if (!strcmp(arg, "line"))
541 fprintf(stderr, "bad type: %s.\n", arg);
546 if (sscanf(arg, "%d:%d", &dict_cache_size, &isam_cache_size)
549 fprintf(stderr, "bad cache sizes for -c\n");
557 int_count_enable = 0;
560 fprintf(stderr, "bad option.\n");
567 inf = fopen(fname, "rb");
570 fprintf(stderr, "Cannot open %s\n", fname);
574 printf("# benchindex1 %s %s\n", __DATE__, __TIME__);
575 printf("# isam_cache_size = %d\n", isam_cache_size);
576 printf("# dict_cache_size = %d\n", dict_cache_size);
577 printf("# int_count_enable = %d\n", int_count_enable);
578 printf("# memory = %d\n", memory);
580 /* setup postings isamb attributes */
581 method_postings.compare_item = key_compare;
582 method_postings.log_item = key_logdump_txt;
584 method_postings.codec.start = iscz1_start;
585 method_postings.codec.decode = iscz1_decode;
586 method_postings.codec.encode = iscz1_encode;
587 method_postings.codec.stop = iscz1_stop;
588 method_postings.codec.reset = iscz1_reset;
590 method_postings.debug = 0;
592 /* create block system */
593 bfs = bfs_create(0, 0);
596 yaz_log(YLOG_WARN, "bfs_create failed");
603 tim = yaz_timing_create();
604 /* create isam handle */
605 isb_postings = isamb_open (bfs, "isamb", isam_cache_size ? 1 : 0,
606 &method_postings, 0);
609 yaz_log(YLOG_WARN, "isamb_open failed");
612 isamb_set_cache_size(isb_postings, isam_cache_size);
613 isamb_set_int_count(isb_postings, int_count_enable);
614 dict = dict_open(bfs, "dict", dict_cache_size, 1, 0, 4096);
616 dict_info = dict_lookup(dict, "_s");
619 assert(*dict_info == sizeof(docid_seq));
620 memcpy(&docid_seq, dict_info+1, sizeof(docid_seq));
623 if (!strcmp(type, "iso2709"))
624 index_marc_from_file(isb_postings, dict, &docid_seq, inf, memory,
625 0 /* verbose */ , 0 /* print_offset */);
626 else if (!strcmp(type, "line"))
627 index_marc_line_records(isb_postings, dict, &docid_seq, inf, memory);
629 printf("# Total " ZINT_FORMAT " documents\n", docid_seq);
630 dict_insert(dict, "_s", sizeof(docid_seq), &docid_seq);
633 isamb_close(isb_postings);
637 /* exit block system */
639 yaz_timing_stop(tim);
641 printf("# Total timings real=%8.6f user=%3.2f system=%3.2f\n",
642 yaz_timing_get_real(tim),
643 yaz_timing_get_user(tim),
644 yaz_timing_get_sys(tim));
646 yaz_timing_destroy(&tim);
654 * c-file-style: "Stroustrup"
655 * indent-tabs-mode: nil
657 * vim: shiftwidth=4 tabstop=8 expandtab