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