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