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