1 /* $Id: dicttest.c,v 1.31 2004-12-08 12:23:08 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include <idzebra/dict.h>
36 static int grep_handler (char *name, const char *info, void *client)
39 printf ("%s\n", name);
43 static int scan_handler (char *name, const char *info, int pos, void *client)
45 printf ("%s\n", name);
49 int main (int argc, char **argv)
53 const char *name = NULL;
54 const char *inputfile = NULL;
55 const char *config = NULL;
56 const char *delete_term = NULL;
57 int scan_the_thing = 0;
66 char *grep_pattern = NULL;
68 int no_of_iterations = 0;
69 int no_of_new = 0, no_of_same = 0, no_of_change = 0;
70 int no_of_hits = 0, no_of_misses = 0, no_not_found = 0, no_of_deleted = 0;
76 fprintf (stderr, "usage:\n "
77 " %s [-d] [-D t] [-S] [-r n] [-p n] [-u] [-g pat] [-s n] "
78 "[-v n] [-i f] [-w] [-c n] config file\n\n",
80 fprintf (stderr, " -d delete instead of insert\n");
81 fprintf (stderr, " -D t delete subtree instead of insert\n");
82 fprintf (stderr, " -r n set regular match range\n");
83 fprintf (stderr, " -p n set regular match start range\n");
84 fprintf (stderr, " -u report if keys change during insert\n");
85 fprintf (stderr, " -g p try pattern n (see -r)\n");
86 fprintf (stderr, " -s n set info size to n (instead of 4)\n");
87 fprintf (stderr, " -v n set logging level\n");
88 fprintf (stderr, " -i f read file with words\n");
89 fprintf (stderr, " -w insert/delete instead of lookup\n");
90 fprintf (stderr, " -c n cache size (number of pages)\n");
91 fprintf (stderr, " -S scan the dictionary\n");
94 while ((ret = options ("D:Sdr:p:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
104 yaz_log (YLOG_FATAL, "too many files specified\n");
144 infosize = atoi(arg);
148 yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
152 yaz_log (YLOG_FATAL, "Unknown option '-%s'", arg);
156 if (!config || !name)
158 yaz_log (YLOG_FATAL, "no config and/or dictionary specified");
161 my_resource = res_open (config, 0, 0);
164 yaz_log (YLOG_FATAL, "cannot open resource `%s'", config);
167 bfs = bfs_create (res_get(my_resource, "register"), 0);
170 yaz_log (YLOG_FATAL, "bfs_create fail");
173 dict = dict_open (bfs, name, cache, rw, 0, 4096);
176 yaz_log (YLOG_FATAL, "dict_open fail of `%s'", name);
185 memset (infobytes, 0, 120);
187 if (!(ipf = fopen(inputfile, "r")))
189 yaz_log (YLOG_FATAL|YLOG_ERRNO, "cannot open %s", inputfile);
193 while (fgets (ipf_buf, 1023, ipf))
195 char *ipf_ptr = ipf_buf;
196 sprintf (infobytes, "%d", line);
197 for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
199 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
202 while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
210 switch (dict_delete (dict, ipf_ptr))
219 switch(dict_insert (dict, ipf_ptr,
220 infosize, infobytes))
228 yaz_log (YLOG_LOG, "%s change\n", ipf_ptr);
232 yaz_log (YLOG_LOG, "%s duplicate\n", ipf_ptr);
241 cp = dict_lookup (dict, ipf_ptr);
250 dict_lookup_grep (dict, ipf_ptr, range, NULL,
251 &max_pos, srange, grep_handler);
258 if ((no_of_iterations % 10000) == 0)
260 printf ("."); fflush(stdout);
269 if (rw && delete_term)
271 yaz_log (YLOG_LOG, "dict_delete_subtree %s", delete_term);
272 dict_delete_subtree (dict, delete_term, 0, 0);
278 yaz_log (YLOG_LOG, "Grepping '%s'", grep_pattern);
279 dict_lookup_grep (dict, grep_pattern, range, NULL, &max_pos,
280 srange, grep_handler);
284 yaz_log (YLOG_LOG, "Iterations.... %d", no_of_iterations);
287 yaz_log (YLOG_LOG, "No of deleted. %d", no_of_deleted);
288 yaz_log (YLOG_LOG, "No not found.. %d", no_not_found);
292 yaz_log (YLOG_LOG, "No of new..... %d", no_of_new);
293 yaz_log (YLOG_LOG, "No of change.. %d", no_of_change);
298 yaz_log (YLOG_LOG, "Lookups....... %d", no_of_iterations);
299 yaz_log (YLOG_LOG, "No of hits.... %d", no_of_hits);
300 yaz_log (YLOG_LOG, "No of misses.. %d", no_of_misses);
304 char term_dict[1024];
306 int before = 1000000;
308 yaz_log (YLOG_LOG, "dict_scan");
311 dict_scan (dict, term_dict, &before, &after, 0, scan_handler);
315 res_close (my_resource);