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