1 /* $Id: benchindex1.c,v 1.4 2006-12-11 15:08:55 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
23 #include <yaz/options.h>
29 #include <yaz/xmalloc.h>
30 #include <yaz/marcdisp.h>
32 #include <idzebra/isamb.h>
33 #include <idzebra/dict.h>
41 struct index_term *terms;
42 struct index_term **ar;
51 struct index_term *next;
54 struct index_block *index_block_new(int memory)
56 struct index_block *b = xmalloc(sizeof(*b));
58 b->current_max = memory * 1024 * 1024;
60 b->nmem = nmem_create();
65 void index_block_destroy(struct index_block **bp)
69 nmem_destroy((*bp)->nmem);
75 static int cmp_ar(const void *p1, const void *p2)
77 struct index_term *t1 = *(struct index_term **) p1;
78 struct index_term *t2 = *(struct index_term **) p2;
79 int d = strcmp(t1->term, t2->term);
83 if (t1->docid > t2->docid)
85 else if (t1->docid < t2->docid)
87 if (t1->seqno > t2->seqno)
89 else if (t1->seqno < t2->seqno)
95 int code_read(void *vp, char **dst, int *insertMode)
97 struct index_block *b = (struct index_block *)vp;
101 if (b->current_entry >= b->no_entries)
104 t = b->ar[b->current_entry];
108 key.mem[0] = t->word_id;
109 key.mem[1] = t->docid;
110 key.mem[2] = t->seqno;
113 memcpy(*dst, &key, sizeof(key));
115 (*dst) += sizeof(key);
118 yaz_log(YLOG_LOG, "returning " ZINT_FORMAT " " ZINT_FORMAT "\n",
119 key.mem[0], key.mem[1]);
124 void index_block_flush(struct index_block *b, ISAMB isb, Dict dict,
127 struct index_term *t = b->terms;
130 int no_words = 0, no_new_words = 0;
131 const char *dict_info = 0;
133 zebra_timing_t tim = zebra_timing_create();
135 b->ar = xmalloc(sizeof(*b->ar) * b->no_entries);
136 for (i = 0; i < b->no_entries; i++, t = t->next)
143 qsort(b->ar, b->no_entries, sizeof(*b->ar), cmp_ar);
145 for (i = 0; i < b->no_entries; i++)
147 printf("%s " ZINT_FORMAT " " ZINT_FORMAT "\n",
148 ar[i]->term, ar[i]->docid, ar[i]->seqno);
151 dict_info = dict_lookup(dict, "_w");
154 assert(*dict_info == sizeof(word_id_seq));
155 memcpy(&word_id_seq, dict_info+1, sizeof(word_id_seq));
158 dict_info = dict_lookup(dict, "_i");
161 assert(*dict_info == sizeof(isamc_p));
162 memcpy(&isamc_p, dict_info+1, sizeof(isamc_p));
165 for (i = 0; i < b->no_entries; i++)
167 if (i > 0 && strcmp(b->ar[i-1]->term, b->ar[i]->term) == 0)
168 b->ar[i]->word_id = b->ar[i-1]->word_id;
171 const char *dict_info = dict_lookup(dict, b->ar[i]->term);
174 memcpy(&b->ar[i]->word_id, dict_info+1, sizeof(int));
180 dict_insert(dict, b->ar[i]->term, sizeof(int), &word_id_seq);
181 b->ar[i]->word_id = word_id_seq;
186 dict_insert(dict, "_w", sizeof(word_id_seq), &word_id_seq);
188 b->current_entry = 0;
194 isamc_i.clientData = b;
195 isamc_i.read_item = code_read;
197 isamb_merge (isb, &isamc_p, &isamc_i);
200 dict_insert(dict, "_i", sizeof(isamc_p), &isamc_p);
203 yaz_log(YLOG_LOG, "Flushed %d postings, %d/%d words, %d records",
204 b->no_entries, no_words, no_new_words, no_docs);
212 zebra_timing_stop(tim);
214 printf("%3d %8.6f %5.2f %5.2f\n",
216 zebra_timing_get_real(tim),
217 zebra_timing_get_user(tim),
218 zebra_timing_get_sys(tim));
220 zebra_timing_destroy(&tim);
223 void index_block_check_flush(struct index_block *b, ISAMB isb, Dict dict,
226 int total = nmem_total(b->nmem);
227 int max = b->current_max;
230 yaz_log(YLOG_LOG, "flush to disk total=%d max=%d", total, max);
231 index_block_flush(b, isb, dict, no_docs);
235 void index_block_add(struct index_block *b,
236 const char *term, zint docid, zint seqno)
238 struct index_term *t = nmem_malloc(b->nmem, sizeof(*t));
239 t->term = nmem_strdup(b->nmem, term);
247 void exit_usage(void)
249 fprintf(stderr, "benchindex1 [-t type] [-m mem] [-i] [inputfile]\n");
253 void index_term(struct index_block *b, const char *term,
254 zint docid, zint *seqno)
257 printf("%s " ZINT_FORMAT " " ZINT_FORMAT "\n", term,
260 index_block_add(b, term, docid, *seqno);
264 void index_wrbuf(struct index_block *b, WRBUF wrbuf, zint docid,
268 const char *cp = wrbuf_buf(wrbuf);
279 { /* skip field+indicator (e.g. 245 00) */
280 for (i = 0; i<6 && *cp; i++, cp++)
284 { /* continuation line */
285 for (i = 0; i<4 && *cp; i++, cp++)
294 index_term(b, term, docid, &seqno);
300 else if (*cp == subfield_char && cp[1])
304 index_term(b, term, docid, &seqno);
309 else if (strchr("$*/-;,.:[]\"&(){} ", *cp))
313 index_term(b, term, docid, &seqno);
320 unsigned ch = *(const unsigned char *)cp;
321 if (sz < sizeof(term))
323 term[sz] = tolower(ch);
331 index_term(b, term, docid, &seqno);
334 void index_marc_line_records(ISAMB isb,
340 WRBUF wrbuf = wrbuf_alloc();
344 struct index_block *b = index_block_new(memory);
345 while(fgets(line, sizeof(line)-1, inf))
359 index_block_check_flush(b, isb, dict, no_docs);
366 wrbuf_puts(wrbuf, line);
371 /* index existing buffer (if any) */
372 if (wrbuf_len(wrbuf))
374 index_wrbuf(b, wrbuf, *docid_seq, '*');
377 if (line[0] != ' ' && line[1] != ' ' && line[2] != ' ' &&
380 /* normal field+indicator line */
381 wrbuf_puts(wrbuf, line);
385 if (wrbuf_len(wrbuf))
387 index_wrbuf(b, wrbuf, *docid_seq, '*');
392 index_block_check_flush(b, isb, dict, no_docs);
393 index_block_destroy(&b);
396 void index_marc_from_file(ISAMB isb,
401 int verbose, int print_offset)
403 yaz_marc_t mt = yaz_marc_create();
404 WRBUF wrbuf = wrbuf_alloc();
405 struct index_block *b = index_block_new(memory);
414 r = fread (buf, 1, 5, inf);
417 if (r && print_offset && verbose)
418 printf ("<!-- Extra %ld bytes at end of file -->\n",
422 while (*buf < '0' || *buf > '9')
425 long off = ftell(inf) - 5;
426 if (verbose || print_offset)
427 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
429 *buf & 0xff, *buf & 0xff,
431 for (i = 0; i<4; i++)
433 r = fread(buf+4, 1, 1, inf);
439 if (verbose || print_offset)
440 printf ("<!-- End of file with data -->\n");
443 len = atoi_n(buf, 5);
444 if (len < 25 || len > 100000)
446 long off = ftell(inf) - 5;
447 printf("Bad Length %ld read at offset %ld (%lx)\n",
448 (long)len, (long) off, (long) off);
452 r = fread (buf + 5, 1, rlen, inf);
455 yaz_marc_read_iso2709(mt, buf, len);
457 if (yaz_marc_write_line(mt, wrbuf))
460 index_wrbuf(b, wrbuf, *docid_seq, '$');
465 index_block_check_flush(b, isb, dict, no_docs);
467 index_block_flush(b, isb, dict, no_docs);
468 wrbuf_free(wrbuf, 1);
469 yaz_marc_destroy(mt);
470 index_block_destroy(&b);
473 int main(int argc, char **argv)
483 const char *fname = 0;
485 zebra_timing_t tim = 0;
487 const char *dict_info;
488 const char *type = "iso2709";
490 while ((ret = options("im:t:", argv, argc, &arg)) != -2)
501 if (!strcmp(arg, "iso2709"))
503 else if (!strcmp(arg, "line"))
507 fprintf(stderr, "bad type: %s.\n", arg);
515 fprintf(stderr, "bad option.\n");
522 inf = fopen(fname, "rb");
525 fprintf(stderr, "Cannot open %s\n", fname);
529 /* setup method (attributes) */
530 method.compare_item = key_compare;
531 method.log_item = key_logdump_txt;
533 method.codec.start = iscz1_start;
534 method.codec.decode = iscz1_decode;
535 method.codec.encode = iscz1_encode;
536 method.codec.stop = iscz1_stop;
537 method.codec.reset = iscz1_reset;
541 /* create block system */
542 bfs = bfs_create(0, 0);
545 yaz_log(YLOG_WARN, "bfs_create failed");
552 tim = zebra_timing_create();
553 /* create isam handle */
554 isb = isamb_open (bfs, "isamb", 1, &method, 0);
557 yaz_log(YLOG_WARN, "isamb_open failed");
560 dict = dict_open(bfs, "dict", 50, 1, 0, 4096);
562 dict_info = dict_lookup(dict, "_s");
565 assert(*dict_info == sizeof(docid_seq));
566 memcpy(&docid_seq, dict_info+1, sizeof(docid_seq));
569 if (!strcmp(type, "iso2709"))
570 index_marc_from_file(isb, dict, &docid_seq, inf, memory,
571 0 /* verbose */ , 0 /* print_offset */);
572 else if (!strcmp(type, "line"))
573 index_marc_line_records(isb, dict, &docid_seq, inf, memory);
575 yaz_log(YLOG_LOG, "Total " ZINT_FORMAT " documents", docid_seq);
576 dict_insert(dict, "_s", sizeof(docid_seq), &docid_seq);
583 /* exit block system */
585 zebra_timing_stop(tim);
587 yaz_log(YLOG_LOG, "Total %8.6f %5.2f %5.2f\n",
588 zebra_timing_get_real(tim),
589 zebra_timing_get_user(tim),
590 zebra_timing_get_sys(tim));
592 zebra_timing_destroy(&tim);
600 * indent-tabs-mode: nil
602 * vim: shiftwidth=4 tabstop=8 expandtab