1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 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
25 #include <idzebra/dict.h>
26 #include <idzebra/util.h>
27 #include <idzebra/res.h>
28 #include <yaz/yaz-util.h>
35 static int grep_handler (char *name, const char *info, void *client)
38 printf ("%s\n", name);
42 static int scan_handler (char *name, const char *info, int pos, void *client)
44 printf ("%s\n", name);
48 int main (int argc, char **argv)
52 const char *name = NULL;
53 const char *inputfile = NULL;
54 const char *config = NULL;
55 const char *delete_term = NULL;
56 int scan_the_thing = 0;
65 char *grep_pattern = NULL;
67 int no_of_iterations = 0;
68 int no_of_new = 0, no_of_same = 0, no_of_change = 0;
69 int no_of_hits = 0, no_of_misses = 0, no_not_found = 0, no_of_deleted = 0;
75 fprintf (stderr, "usage:\n "
76 " %s [-d] [-D t] [-S] [-r n] [-p n] [-u] [-g pat] [-s n] "
77 "[-v n] [-i f] [-w] [-c n] config file\n\n",
79 fprintf (stderr, " -d delete instead of insert\n");
80 fprintf (stderr, " -D t delete subtree instead of insert\n");
81 fprintf (stderr, " -r n set regular match range\n");
82 fprintf (stderr, " -p n set regular match start range\n");
83 fprintf (stderr, " -u report if keys change during insert\n");
84 fprintf (stderr, " -g p try pattern n (see -r)\n");
85 fprintf (stderr, " -s n set info size to n (instead of 4)\n");
86 fprintf (stderr, " -v n set logging level\n");
87 fprintf (stderr, " -i f read file with words\n");
88 fprintf (stderr, " -w insert/delete instead of lookup\n");
89 fprintf (stderr, " -c n cache size (number of pages)\n");
90 fprintf (stderr, " -S scan the dictionary\n");
93 while ((ret = options ("D:Sdr:p:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
103 yaz_log (YLOG_FATAL, "too many files specified\n");
143 infosize = atoi(arg);
147 yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
151 yaz_log (YLOG_FATAL, "Unknown option '-%s'", arg);
155 if (!config || !name)
157 yaz_log (YLOG_FATAL, "no config and/or dictionary specified");
160 my_resource = res_open(0, 0);
163 yaz_log (YLOG_FATAL, "cannot open resource `%s'", config);
166 res_read_file(my_resource, config);
168 bfs = bfs_create (res_get(my_resource, "register"), 0);
171 yaz_log (YLOG_FATAL, "bfs_create fail");
174 dict = dict_open (bfs, name, cache, rw, 0, 4096);
177 yaz_log (YLOG_FATAL, "dict_open fail of `%s'", name);
186 memset (infobytes, 0, 120);
188 if (!(ipf = fopen(inputfile, "r")))
190 yaz_log (YLOG_FATAL|YLOG_ERRNO, "cannot open %s", inputfile);
194 while (fgets (ipf_buf, 1023, ipf))
196 char *ipf_ptr = ipf_buf;
197 sprintf (infobytes, "%d", line);
198 for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
200 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
203 while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
211 switch (dict_delete (dict, ipf_ptr))
220 switch(dict_insert (dict, ipf_ptr,
221 infosize, infobytes))
229 yaz_log (YLOG_LOG, "%s change\n", ipf_ptr);
233 yaz_log (YLOG_LOG, "%s duplicate\n", ipf_ptr);
242 cp = dict_lookup (dict, ipf_ptr);
251 dict_lookup_grep (dict, ipf_ptr, range, NULL,
252 &max_pos, srange, grep_handler);
259 if ((no_of_iterations % 10000) == 0)
261 printf ("."); fflush(stdout);
270 if (rw && delete_term)
272 yaz_log (YLOG_LOG, "dict_delete_subtree %s", delete_term);
273 dict_delete_subtree (dict, delete_term, 0, 0);
279 yaz_log (YLOG_LOG, "Grepping '%s'", grep_pattern);
280 dict_lookup_grep (dict, grep_pattern, range, NULL, &max_pos,
281 srange, grep_handler);
285 yaz_log (YLOG_LOG, "Iterations.... %d", no_of_iterations);
288 yaz_log (YLOG_LOG, "No of deleted. %d", no_of_deleted);
289 yaz_log (YLOG_LOG, "No not found.. %d", no_not_found);
293 yaz_log (YLOG_LOG, "No of new..... %d", no_of_new);
294 yaz_log (YLOG_LOG, "No of change.. %d", no_of_change);
299 yaz_log (YLOG_LOG, "Lookups....... %d", no_of_iterations);
300 yaz_log (YLOG_LOG, "No of hits.... %d", no_of_hits);
301 yaz_log (YLOG_LOG, "No of misses.. %d", no_of_misses);
305 char term_dict[1024];
307 int before = 1000000;
309 yaz_log (YLOG_LOG, "dict_scan");
312 dict_scan (dict, term_dict, &before, &after, 0, scan_handler);
316 res_close (my_resource);
322 * c-file-style: "Stroustrup"
323 * indent-tabs-mode: nil
325 * vim: shiftwidth=4 tabstop=8 expandtab