16d8187f259a49234d5172989b8c5261e14ab736
[idzebra-moved-to-github.git] / index / invstat.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
5  * log at eof
6  *
7  */
8 #include <stdio.h>
9 #include <assert.h>
10 #include <string.h>
11
12 #include "index.h"
13 #include "recindex.h"
14 #include "../isamc/isamh-p.h"
15 #include "../isamc/isamd-p.h"
16
17 struct inv_stat_info {
18     ISAM isam;
19     ISAMC isamc;
20     ISAMS isams;
21     ISAMH isamh;
22     ISAMD isamd;
23     int no_isam_entries[8];
24     int no_dict_entries;
25     int no_dict_bytes;
26     int isam_bounds[20];
27     int isam_occurrences[20];
28     char tmp[128];
29 };
30
31 static int inv_stat_handle (char *name, const char *info, int pos,
32                             void *client)
33 {
34     int occur = 0;
35     int i = 0;
36     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
37     ISAM_P isam_p;
38
39     stat_info->no_dict_entries++;
40     stat_info->no_dict_bytes += strlen(name);
41
42     assert (*info == sizeof(ISAM_P));
43     memcpy (&isam_p, info+1, sizeof(ISAM_P));
44
45     //printf ("---\n");
46     if (stat_info->isam)
47     {
48         ISPT ispt;
49
50         ispt = is_position (stat_info->isam, isam_p);
51         occur = is_numkeys (ispt);
52         is_pt_free (ispt);
53     }
54     if (stat_info->isamc)
55     {
56         ISAMC_PP pp;
57         int occurx = 0;
58         struct it_key key;
59
60         pp = isc_pp_open (stat_info->isamc, isam_p);
61         occur = isc_pp_num (pp);
62         while (isc_pp_read(pp, &key))
63         {
64             printf ("sysno=%d seqno=%d\n", key.sysno, key.seqno);
65             occurx++;
66         }
67         assert (occurx == occur);
68         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
69         isc_pp_close (pp);
70     }
71     if (stat_info->isamh)
72     {
73         ISAMH_PP pp;
74         int occurx = 0;
75         struct it_key key;
76
77         pp = isamh_pp_open (stat_info->isamh, isam_p);
78         
79         occur = isamh_pp_num (pp);
80           //  printf ("  opening item %d=%d:%d \n",
81           //    isam_p, isamh_type(isam_p),isamh_block(isam_p));
82         while (isamh_pp_read(pp, &key))
83         {
84             occurx++;
85             logf (LOG_LOG,"sysno=%d seqno=%d (%x/%x) oc=%d/%d ofs=%d ",
86                    key.sysno, key.seqno,
87                    key.sysno, key.seqno,
88                    occur,occurx, pp->offset);
89         }
90         if (occurx != occur) 
91           logf(LOG_LOG,"Count error!!! read %d, counted %d", occur, occurx);
92         assert (occurx == occur);
93         stat_info->no_isam_entries[isamh_type(isam_p)] += occur;
94         isamh_pp_close (pp);
95     }
96     if (stat_info->isamd)
97     {
98         ISAMD_PP pp;
99         int occurx = 0;
100         struct it_key key;
101
102         pp = isamd_pp_open (stat_info->isamd, isam_p);
103         
104         occur = isamd_pp_num (pp);
105           //  printf ("  opening item %d=%d:%d \n",
106           //    isam_p, isamh_type(isam_p),isamh_block(isam_p));
107         while (isamd_pp_read(pp, &key))
108         {
109             occurx++;
110             logf (LOG_LOG,"sysno=%d seqno=%d (%x/%x) oc=%d/%d ofs=%d ",
111                    key.sysno, key.seqno,
112                    key.sysno, key.seqno,
113                    occur,occurx, pp->offset);
114         }
115         if (occurx != occur) 
116           logf(LOG_LOG,"Count error!!! read %d, counted %d", occur, occurx);
117         assert (occurx == occur);
118         stat_info->no_isam_entries[isamd_type(isam_p)] += occur;
119         isamd_pp_close (pp);
120     }
121     if (stat_info->isams)
122     {
123         ISAMS_PP pp;
124         int occurx = 0;
125         struct it_key key;
126
127         pp = isams_pp_open (stat_info->isams, isam_p);
128         occur = isams_pp_num (pp);
129         while (isams_pp_read(pp, &key))
130         {
131             //printf ("sysno=%d seqno=%d\n", key.sysno, key.seqno);
132             occurx++;
133         }
134         assert (occurx == occur);
135         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
136         isams_pp_close (pp);
137     }
138
139     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
140         i++;
141     ++(stat_info->isam_occurrences[i]);
142     return 0;
143 }
144
145 void inv_prstat (BFiles bfs)
146 {
147     Dict dict;
148     ISAM  isam  = NULL;
149     ISAMC isamc = NULL;
150     ISAMS isams = NULL;
151     ISAMH isamh = NULL;
152     ISAMD isamd = NULL;
153     Records records;
154     int i, prev;
155     int before = 0;
156     int after = 1000000000;
157     struct inv_stat_info stat_info;
158     char term_dict[2*IT_MAX_WORD+2];
159
160     term_dict[0] = 1;
161     term_dict[1] = 0;
162
163     dict = dict_open (bfs, FNAME_DICT, 100, 0, 0);
164     if (!dict)
165     {
166         logf (LOG_FATAL, "dict_open fail");
167         exit (1);
168     }
169     if (res_get_match (common_resource, "isam", "i", NULL))
170     {
171         isam = is_open (bfs, FNAME_ISAM, key_compare, 0,
172                         sizeof(struct it_key), common_resource);
173         if (!isam)
174         {
175             logf (LOG_FATAL, "is_open fail");
176             exit (1);
177         }
178     }
179     else if (res_get_match (common_resource, "isam", "s", NULL))
180     {
181         struct ISAMS_M_s isams_m;
182         isams = isams_open (bfs, FNAME_ISAMS, 0,
183                             key_isams_m(common_resource, &isams_m));
184         if (!isams)
185         {
186             logf (LOG_FATAL, "isams_open fail");
187             exit (1);
188         }
189     }
190     else if (res_get_match (common_resource, "isam", "h", NULL))
191     {
192         isamh = isamh_open (bfs, FNAME_ISAMH, 0, key_isamh_m(common_resource));
193         if (!isamh)
194         {
195             logf (LOG_FATAL, "isamh_open fail");
196             exit (1);
197         }
198     }
199     else if (res_get_match (common_resource, "isam", "d", NULL))
200     {
201         struct ISAMD_M_s isamd_m;
202         isamd = isamd_open (bfs, FNAME_ISAMD, 0, 
203                             key_isamd_m(common_resource,&isamd_m));
204         if (!isamd)
205         {
206             logf (LOG_FATAL, "isamd_open fail");
207             exit (1);
208         }
209     }
210     else
211     {
212         struct ISAMC_M_s isamc_m;
213         isamc = isc_open (bfs, FNAME_ISAMC, 0,
214                           key_isamc_m (common_resource, &isamc_m));
215         if (!isamc)
216         {
217             logf (LOG_FATAL, "isc_open fail");
218             exit (1);
219         }
220     }
221     records = rec_open (bfs, 0, 0);
222
223     for (i = 0; i<8; i++)
224         stat_info.no_isam_entries[i] = 0;
225     stat_info.no_dict_entries = 0;
226     stat_info.no_dict_bytes = 0;
227     stat_info.isam = isam;
228     stat_info.isamc = isamc;
229     stat_info.isams = isams;
230     stat_info.isamh = isamh;
231     stat_info.isamd = isamd;
232     stat_info.isam_bounds[0] = 1;
233     stat_info.isam_bounds[1] = 2;
234     stat_info.isam_bounds[2] = 3;
235     stat_info.isam_bounds[3] = 6;
236     stat_info.isam_bounds[4] = 10;
237     stat_info.isam_bounds[5] = 20;
238     stat_info.isam_bounds[6] = 30;
239     stat_info.isam_bounds[7] = 50;
240     stat_info.isam_bounds[8] = 100;
241     stat_info.isam_bounds[9] = 200;
242     stat_info.isam_bounds[10] = 5000;
243     stat_info.isam_bounds[11] = 10000;
244     stat_info.isam_bounds[12] = 20000;
245     stat_info.isam_bounds[13] = 50000;
246     stat_info.isam_bounds[14] = 100000;
247     stat_info.isam_bounds[15] = 200000;
248     stat_info.isam_bounds[16] = 500000;
249     stat_info.isam_bounds[17] = 1000000;
250     stat_info.isam_bounds[18] = 0;
251
252     for (i = 0; i<20; i++)
253         stat_info.isam_occurrences[i] = 0;
254
255     dict_scan (dict, term_dict, &before, &after, &stat_info, inv_stat_handle);
256
257     if (isamc)
258     {
259         fprintf (stderr, "   Blocks    Occur  Size KB   Bytes/Entry\n");
260         for (i = 0; isc_block_used (isamc, i) >= 0; i++)
261         {
262             fprintf (stderr, " %8d %8d", isc_block_used (isamc, i),
263                      stat_info.no_isam_entries[i]);
264
265             if (stat_info.no_isam_entries[i])
266                 fprintf (stderr, " %8d   %f",
267                          (int) ((1023.0 + (double) isc_block_used(isamc, i) *
268                                  isc_block_size(isamc,i))/1024),
269                          ((double) isc_block_used(isamc, i) *
270                           isc_block_size(isamc,i))/
271                          stat_info.no_isam_entries[i]);
272             fprintf (stderr, "\n");
273         }
274     }
275         
276     fprintf (stderr, "\n%d words using %d bytes\n",
277              stat_info.no_dict_entries, stat_info.no_dict_bytes);
278     fprintf (stderr, "    Occurrences     Words\n");
279     prev = 1;
280     for (i = 0; stat_info.isam_bounds[i]; i++)
281     {
282         int here = stat_info.isam_bounds[i];
283         fprintf (stderr, "%7d-%-7d %7d\n",
284                  prev, here, stat_info.isam_occurrences[i]);
285         prev = here+1;
286     }
287     fprintf (stderr, "%7d-        %7d\n",
288              prev, stat_info.isam_occurrences[i]);
289
290     rec_close (&records);
291     dict_close (dict);
292
293     if (isam)
294         is_close (isam);
295     if (isamc)
296         isc_close (isamc);
297     if (isams)
298         isams_close (isams);
299     if (isamh)
300         isamh_close (isamh);
301     if (isamd)
302         isamd_close (isamd);
303
304     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
305 }
306
307
308 /*
309  *
310  * $Log: invstat.c,v $
311  * Revision 1.16  1999-08-18 08:38:22  heikki
312  * Memory leak hunting
313  *
314  * Revision 1.15  1999/08/18 08:34:53  heikki
315  * isamd
316  *
317  * Revision 1.14  1999/07/14 10:59:26  adam
318  * Changed functions isc_getmethod, isams_getmethod.
319  * Improved fatal error handling (such as missing EXPLAIN schema).
320  *
321  * Revision 1.13  1999/07/08 14:23:27  heikki
322  * Fixed a bug in isamh_pp_read and cleaned up a bit
323  *
324  * Revision 1.12  1999/07/06 12:28:04  adam
325  * Updated record index structure. Format includes version ID. Compression
326  * algorithm ID is stored for each record block.
327  *
328  * Revision 1.11  1999/05/15 14:36:38  adam
329  * Updated dictionary. Implemented "compression" of dictionary.
330  *
331  * Revision 1.10  1999/05/12 13:08:06  adam
332  * First version of ISAMS.
333  *
334  * Revision 1.9  1999/02/12 13:29:23  adam
335  * Implemented position-flag for registers.
336  *
337  * Revision 1.8  1999/02/02 14:50:53  adam
338  * Updated WIN32 code specific sections. Changed header.
339  *
340  * Revision 1.7  1998/03/13 15:30:50  adam
341  * New functions isc_block_used and isc_block_size. Fixed 'leak'
342  * in isc_alloc_block.
343  *
344  * Revision 1.6  1998/03/06 13:54:02  adam
345  * Fixed two nasty bugs in isc_merge.
346  *
347  * Revision 1.5  1997/09/17 12:19:13  adam
348  * Zebra version corresponds to YAZ version 1.4.
349  * Changed Zebra server so that it doesn't depend on global common_resource.
350  *
351  * Revision 1.4  1996/11/08 11:10:21  adam
352  * Buffers used during file match got bigger.
353  * Compressed ISAM support everywhere.
354  * Bug fixes regarding masking characters in queries.
355  * Redesigned Regexp-2 queries.
356  *
357  * Revision 1.3  1996/06/04 10:18:58  adam
358  * Minor changes - removed include of ctype.h.
359  *
360  * Revision 1.2  1996/05/22  08:25:56  adam
361  * Minor change.
362  *
363  * Revision 1.1  1996/05/14 14:04:34  adam
364  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
365  * is collected.
366  */