1 /* $Id: dicttest.c,v 1.36 2006-05-10 08:13:18 adam Exp $
2 Copyright (C) 1995-2005
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>
29 #include <idzebra/util.h>
30 #include <idzebra/res.h>
31 #include <yaz/yaz-util.h>
38 static int grep_handler (char *name, const char *info, void *client)
41 printf ("%s\n", name);
45 static int scan_handler (char *name, const char *info, int pos, void *client)
47 printf ("%s\n", name);
51 int main (int argc, char **argv)
55 const char *name = NULL;
56 const char *inputfile = NULL;
57 const char *config = NULL;
58 const char *delete_term = NULL;
59 int scan_the_thing = 0;
68 char *grep_pattern = NULL;
70 int no_of_iterations = 0;
71 int no_of_new = 0, no_of_same = 0, no_of_change = 0;
72 int no_of_hits = 0, no_of_misses = 0, no_not_found = 0, no_of_deleted = 0;
78 fprintf (stderr, "usage:\n "
79 " %s [-d] [-D t] [-S] [-r n] [-p n] [-u] [-g pat] [-s n] "
80 "[-v n] [-i f] [-w] [-c n] config file\n\n",
82 fprintf (stderr, " -d delete instead of insert\n");
83 fprintf (stderr, " -D t delete subtree instead of insert\n");
84 fprintf (stderr, " -r n set regular match range\n");
85 fprintf (stderr, " -p n set regular match start range\n");
86 fprintf (stderr, " -u report if keys change during insert\n");
87 fprintf (stderr, " -g p try pattern n (see -r)\n");
88 fprintf (stderr, " -s n set info size to n (instead of 4)\n");
89 fprintf (stderr, " -v n set logging level\n");
90 fprintf (stderr, " -i f read file with words\n");
91 fprintf (stderr, " -w insert/delete instead of lookup\n");
92 fprintf (stderr, " -c n cache size (number of pages)\n");
93 fprintf (stderr, " -S scan the dictionary\n");
96 while ((ret = options ("D:Sdr:p:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
106 yaz_log (YLOG_FATAL, "too many files specified\n");
146 infosize = atoi(arg);
150 yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
154 yaz_log (YLOG_FATAL, "Unknown option '-%s'", arg);
158 if (!config || !name)
160 yaz_log (YLOG_FATAL, "no config and/or dictionary specified");
163 my_resource = res_open(0, 0);
166 yaz_log (YLOG_FATAL, "cannot open resource `%s'", config);
169 res_read_file(my_resource, config);
171 bfs = bfs_create (res_get(my_resource, "register"), 0);
174 yaz_log (YLOG_FATAL, "bfs_create fail");
177 dict = dict_open (bfs, name, cache, rw, 0, 4096);
180 yaz_log (YLOG_FATAL, "dict_open fail of `%s'", name);
189 memset (infobytes, 0, 120);
191 if (!(ipf = fopen(inputfile, "r")))
193 yaz_log (YLOG_FATAL|YLOG_ERRNO, "cannot open %s", inputfile);
197 while (fgets (ipf_buf, 1023, ipf))
199 char *ipf_ptr = ipf_buf;
200 sprintf (infobytes, "%d", line);
201 for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
203 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
206 while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
214 switch (dict_delete (dict, ipf_ptr))
223 switch(dict_insert (dict, ipf_ptr,
224 infosize, infobytes))
232 yaz_log (YLOG_LOG, "%s change\n", ipf_ptr);
236 yaz_log (YLOG_LOG, "%s duplicate\n", ipf_ptr);
245 cp = dict_lookup (dict, ipf_ptr);
254 dict_lookup_grep (dict, ipf_ptr, range, NULL,
255 &max_pos, srange, grep_handler);
262 if ((no_of_iterations % 10000) == 0)
264 printf ("."); fflush(stdout);
273 if (rw && delete_term)
275 yaz_log (YLOG_LOG, "dict_delete_subtree %s", delete_term);
276 dict_delete_subtree (dict, delete_term, 0, 0);
282 yaz_log (YLOG_LOG, "Grepping '%s'", grep_pattern);
283 dict_lookup_grep (dict, grep_pattern, range, NULL, &max_pos,
284 srange, grep_handler);
288 yaz_log (YLOG_LOG, "Iterations.... %d", no_of_iterations);
291 yaz_log (YLOG_LOG, "No of deleted. %d", no_of_deleted);
292 yaz_log (YLOG_LOG, "No not found.. %d", no_not_found);
296 yaz_log (YLOG_LOG, "No of new..... %d", no_of_new);
297 yaz_log (YLOG_LOG, "No of change.. %d", no_of_change);
302 yaz_log (YLOG_LOG, "Lookups....... %d", no_of_iterations);
303 yaz_log (YLOG_LOG, "No of hits.... %d", no_of_hits);
304 yaz_log (YLOG_LOG, "No of misses.. %d", no_of_misses);
308 char term_dict[1024];
310 int before = 1000000;
312 yaz_log (YLOG_LOG, "dict_scan");
315 dict_scan (dict, term_dict, &before, &after, 0, scan_handler);
319 res_close (my_resource);
325 * indent-tabs-mode: nil
327 * vim: shiftwidth=4 tabstop=8 expandtab