1 /* $Id: benchindex1.c,v 1.3 2006-12-11 10:02:14 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);
211 zebra_timing_stop(tim);
213 printf("%3d %8.6f %5.2f %5.2f\n",
215 zebra_timing_get_real(tim),
216 zebra_timing_get_user(tim),
217 zebra_timing_get_sys(tim));
218 zebra_timing_destroy(&tim);
221 void index_block_check_flush(struct index_block *b, ISAMB isb, Dict dict,
224 int total = nmem_total(b->nmem);
225 int max = b->current_max;
228 yaz_log(YLOG_LOG, "flush to disk total=%d max=%d", total, max);
229 index_block_flush(b, isb, dict, no_docs);
233 void index_block_add(struct index_block *b,
234 const char *term, zint docid, zint seqno)
236 struct index_term *t = nmem_malloc(b->nmem, sizeof(*t));
237 t->term = nmem_strdup(b->nmem, term);
245 void exit_usage(void)
247 fprintf(stderr, "benchindex1 [-m mem] [-i] [iso2709file]\n");
251 void index_term(struct index_block *b, const char *term,
252 zint docid, zint *seqno)
255 printf("%s " ZINT_FORMAT " " ZINT_FORMAT "\n", term,
258 index_block_add(b, term, docid, *seqno);
262 void index_wrbuf(struct index_block *b, WRBUF wrbuf, zint docid)
265 const char *cp = wrbuf_buf(wrbuf);
275 for (i = 0; i<6 && *cp; i++, cp++)
283 index_term(b, term, docid, &seqno);
289 else if (*cp == '$' && cp[1])
293 index_term(b, term, docid, &seqno);
298 else if (strchr("$/-;,.:[]\"&(){} ", *cp))
302 index_term(b, term, docid, &seqno);
309 unsigned ch = *(const unsigned char *)cp;
310 if (sz < sizeof(term))
312 term[sz] = tolower(ch);
320 index_term(b, term, docid, &seqno);
323 void index_marc_from_file(ISAMB isb,
327 int verbose, int print_offset)
329 yaz_marc_t mt = yaz_marc_create();
330 WRBUF wrbuf = wrbuf_alloc();
331 struct index_block *b = index_block_new(memory);
332 const char *dict_info = 0;
336 dict_info = dict_lookup(dict, "_s");
339 assert(*dict_info == sizeof(docid_seq));
340 memcpy(&docid_seq, dict_info+1, sizeof(docid_seq));
349 r = fread (buf, 1, 5, inf);
352 if (r && print_offset && verbose)
353 printf ("<!-- Extra %ld bytes at end of file -->\n",
357 while (*buf < '0' || *buf > '9')
360 long off = ftell(inf) - 5;
361 if (verbose || print_offset)
362 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
364 *buf & 0xff, *buf & 0xff,
366 for (i = 0; i<4; i++)
368 r = fread(buf+4, 1, 1, inf);
374 if (verbose || print_offset)
375 printf ("<!-- End of file with data -->\n");
378 len = atoi_n(buf, 5);
379 if (len < 25 || len > 100000)
381 long off = ftell(inf) - 5;
382 printf("Bad Length %ld read at offset %ld (%lx)\n",
383 (long)len, (long) off, (long) off);
387 r = fread (buf + 5, 1, rlen, inf);
390 yaz_marc_read_iso2709(mt, buf, len);
392 if (yaz_marc_write_line(mt, wrbuf))
395 index_wrbuf(b, wrbuf, docid_seq);
400 index_block_check_flush(b, isb, dict, no_docs);
402 index_block_flush(b, isb, dict, no_docs);
403 wrbuf_free(wrbuf, 1);
404 yaz_marc_destroy(mt);
405 index_block_destroy(&b);
406 yaz_log(YLOG_LOG, "Total " ZINT_FORMAT " documents", docid_seq);
407 dict_insert(dict, "_s", sizeof(docid_seq), &docid_seq);
410 int main(int argc, char **argv)
420 const char *fname = 0;
422 zebra_timing_t tim = 0;
424 while ((ret = options("im:", argv, argc, &arg)) != -2)
438 fprintf(stderr, "bad option.\n");
445 inf = fopen(fname, "rb");
448 fprintf(stderr, "Cannot open %s\n", fname);
452 /* setup method (attributes) */
453 method.compare_item = key_compare;
454 method.log_item = key_logdump_txt;
456 method.codec.start = iscz1_start;
457 method.codec.decode = iscz1_decode;
458 method.codec.encode = iscz1_encode;
459 method.codec.stop = iscz1_stop;
460 method.codec.reset = iscz1_reset;
464 /* create block system */
465 bfs = bfs_create(0, 0);
468 yaz_log(YLOG_WARN, "bfs_create failed");
475 tim = zebra_timing_create();
476 /* create isam handle */
477 isb = isamb_open (bfs, "isamb", 1, &method, 0);
480 yaz_log(YLOG_WARN, "isamb_open failed");
483 dict = dict_open(bfs, "dict", 50, 1, 0, 4096);
485 index_marc_from_file(isb, dict, inf, memory,
486 0 /* verbose */ , 0 /* print_offset */);
493 /* exit block system */
495 zebra_timing_stop(tim);
497 yaz_log(YLOG_LOG, "Total %8.6f %5.2f %5.2f\n",
498 zebra_timing_get_real(tim),
499 zebra_timing_get_user(tim),
500 zebra_timing_get_sys(tim));
502 zebra_timing_destroy(&tim);
510 * indent-tabs-mode: nil
512 * vim: shiftwidth=4 tabstop=8 expandtab