Using the new ylog.h everywhere, and fixing what that breaks!
[idzebra-moved-to-github.git] / dict / dicttest.c
1 /* $Id: dicttest.c,v 1.30 2004-11-19 10:26:55 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <ctype.h>
29
30 #include <dict.h>
31 #include <zebrautl.h>
32
33 char *prog;
34 static Dict dict;
35
36 static int look_hits;
37
38 static int grep_handler (char *name, const char *info, void *client)
39 {
40     look_hits++;
41     printf ("%s\n", name);
42     return 0;
43 }
44
45 static int scan_handler (char *name, const char *info, int pos, void *client)
46 {
47     printf ("%s\n", name);
48     return 0;
49 }
50
51 int main (int argc, char **argv)
52 {
53     Res my_resource = 0;
54     BFiles bfs;
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;
60     int do_delete = 0;
61     int range = -1;
62     int srange = 0;
63     int rw = 0;
64     int infosize = 4;
65     int cache = 10;
66     int ret;
67     int unique = 0;
68     char *grep_pattern = NULL;
69     char *arg;
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;
73     int max_pos;
74     
75     prog = argv[0];
76     if (argc < 2)
77     {
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",
81                  prog);
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");
94         exit (1);
95     }
96     while ((ret = options ("D:Sdr:p:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
97     {
98         if (ret == 0)
99         {
100             if (!config)
101                 config = arg;
102             else if (!name)
103                 name = arg;
104             else
105             {
106                 yaz_log (YLOG_FATAL, "too many files specified\n");
107                 exit (1);
108             }
109         }
110         else if (ret == 'D')
111         {
112             delete_term = arg;
113         }
114         else if (ret == 'd')
115             do_delete = 1;
116         else if (ret == 'g')
117         {
118             grep_pattern = arg;
119         }
120         else if (ret == 'r')
121         {
122             range = atoi (arg);
123         }
124         else if (ret == 'p')
125         {
126             srange = atoi (arg);
127         }
128         else if (ret == 'u')
129         {
130             unique = 1;
131         }
132         else if (ret == 'c')
133         {
134             cache = atoi(arg);
135             if (cache<2)
136                 cache = 2;
137         }
138         else if (ret == 'w')
139             rw = 1;
140         else if (ret == 'i')
141             inputfile = arg;
142         else if (ret == 'S')
143             scan_the_thing = 1;
144         else if (ret == 's')
145         {
146             infosize = atoi(arg);
147         }
148         else if (ret == 'v')
149         {
150             yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
151         }
152         else
153         {
154             yaz_log (YLOG_FATAL, "Unknown option '-%s'", arg);
155             exit (1);
156         }
157     }
158     if (!config || !name)
159     {
160         yaz_log (YLOG_FATAL, "no config and/or dictionary specified");
161         exit (1);
162     }
163     my_resource = res_open (config, 0, 0);
164     if (!my_resource)
165     {
166         yaz_log (YLOG_FATAL, "cannot open resource `%s'", config);
167         exit (1);
168     }
169     bfs = bfs_create (res_get(my_resource, "register"), 0);
170     if (!bfs)
171     {
172         yaz_log (YLOG_FATAL, "bfs_create fail");
173         exit (1);
174     }
175     dict = dict_open (bfs, name, cache, rw, 0, 4096);
176     if (!dict)
177     {
178         yaz_log (YLOG_FATAL, "dict_open fail of `%s'", name);
179         exit (1);
180     }
181     if (inputfile)
182     {
183         FILE *ipf;
184         char ipf_buf[1024];
185         int line = 1;
186         char infobytes[120];
187         memset (infobytes, 0, 120);
188
189         if (!(ipf = fopen(inputfile, "r")))
190         {
191             yaz_log (YLOG_FATAL|YLOG_ERRNO, "cannot open %s", inputfile);
192             exit (1);
193         }
194         
195         while (fgets (ipf_buf, 1023, ipf))
196         {
197             char *ipf_ptr = ipf_buf;
198             sprintf (infobytes, "%d", line);
199             for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
200             {
201                 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
202                 {
203                     int i = 1;
204                     while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
205                                           ipf_ptr[i] == '_'))
206                         i++;
207                     if (ipf_ptr[i])
208                         ipf_ptr[i++] = '\0';
209                     if (rw)
210                     {
211                         if (do_delete)
212                             switch (dict_delete (dict, ipf_ptr))
213                             {
214                             case 0:
215                                 no_not_found++;
216                                 break;
217                             case 1:
218                                 no_of_deleted++;
219                             }
220                         else
221                             switch(dict_insert (dict, ipf_ptr,
222                                                 infosize, infobytes))
223                             {
224                             case 0:
225                                 no_of_new++;
226                                 break;
227                             case 1:
228                                 no_of_change++;
229                                 if (unique)
230                                     yaz_log (YLOG_LOG, "%s change\n", ipf_ptr);
231                                 break;
232                             case 2:
233                                 if (unique)
234                                     yaz_log (YLOG_LOG, "%s duplicate\n", ipf_ptr);
235                                 no_of_same++;
236                                 break;
237                             }
238                     }
239                     else if(range < 0)
240                     {
241                         char *cp;
242
243                         cp = dict_lookup (dict, ipf_ptr);
244                         if (cp && *cp)
245                             no_of_hits++;
246                         else
247                             no_of_misses++;
248                     }
249                     else
250                     {
251                         look_hits = 0;
252                         dict_lookup_grep (dict, ipf_ptr, range, NULL,
253                                           &max_pos, srange, grep_handler);
254                         if (look_hits)
255                             no_of_hits++;
256                         else
257                             no_of_misses++;
258                     }
259                     ++no_of_iterations;
260                     if ((no_of_iterations % 10000) == 0)
261                     {
262                         printf ("."); fflush(stdout);
263                     }
264                     ipf_ptr += (i-1);
265                 }
266             }
267             ++line;
268         }
269         fclose (ipf);
270     }
271     if (rw && delete_term)
272     {
273         yaz_log (YLOG_LOG, "dict_delete_subtree %s", delete_term);
274         dict_delete_subtree (dict, delete_term, 0, 0);
275     }
276     if (grep_pattern)
277     {
278         if (range < 0)
279             range = 0;
280         yaz_log (YLOG_LOG, "Grepping '%s'", grep_pattern);
281         dict_lookup_grep (dict, grep_pattern, range, NULL, &max_pos,
282                           srange, grep_handler);
283     }
284     if (rw)
285     {
286         yaz_log (YLOG_LOG, "Iterations.... %d", no_of_iterations);            
287         if (do_delete)
288         {
289             yaz_log (YLOG_LOG, "No of deleted. %d", no_of_deleted);
290             yaz_log (YLOG_LOG, "No not found.. %d", no_not_found);
291         }
292         else
293         {
294             yaz_log (YLOG_LOG, "No of new..... %d", no_of_new);
295             yaz_log (YLOG_LOG, "No of change.. %d", no_of_change);
296         }
297     }
298     else
299     {
300         yaz_log (YLOG_LOG, "Lookups....... %d", no_of_iterations);
301         yaz_log (YLOG_LOG, "No of hits.... %d", no_of_hits);
302         yaz_log (YLOG_LOG, "No of misses.. %d", no_of_misses);
303     }
304     if (scan_the_thing)
305     {
306         char term_dict[1024];
307         
308         int before = 1000000;
309         int after = 1000000;
310         yaz_log (YLOG_LOG, "dict_scan");
311         term_dict[0] = 1;
312         term_dict[1] = 0;
313         dict_scan (dict, term_dict, &before, &after, 0, scan_handler);
314     }
315     dict_close (dict);
316     bfs_destroy (bfs);
317     res_close (my_resource);
318     return 0;
319 }