Fixed a bug in isamd, failed to store a single key when its bits
[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 "../isamc/isamd-p.h"
14
15 struct inv_stat_info {
16     ZebraHandle zh;
17     int no_isam_entries[9];
18     int no_dict_entries;
19     int no_dict_bytes;
20     int isam_bounds[20];
21     int isam_occurrences[20];
22     char tmp[128];
23     int isamb_levels[10][5];
24     int isamb_sizes[10];
25     int isamb_blocks[10];
26     unsigned long cksum;
27     int dumpwords;
28 };
29
30 #define SINGLETON_TYPE 8 /* the type to use for singletons that */ 
31                          /* have no block and no block type */
32
33 static void print_dict_item (ZebraMaps zm, const char *s, int count,
34             int firstsys, int firstseq, int lastsys, int lastseq )
35 {
36     int reg_type = s[1];
37     char keybuf[IT_MAX_WORD+1];
38     char *to = keybuf;
39     const char *from = s + 2;
40
41     while (*from)
42     {
43         const char *res = zebra_maps_output (zm, reg_type, &from);
44         if (!res)
45             *to++ = *from++;
46         else
47             while (*res)
48                 *to++ = *res++;
49     }
50     *to = '\0';
51     /* yaz_log (LOG_LOG, "%s", keybuf); */
52     printf("%10d %s %d.%d - %d.%d\n",count, keybuf,
53               firstsys,firstseq, lastsys,lastseq);
54 }
55
56 static int inv_stat_handle (char *name, const char *info, int pos,
57                             void *client)
58 {
59     int occur = 0;
60     int i = 0;
61     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
62     ISAMS_P isam_p;
63     int firstsys=-1;
64     int firstseq=-1;
65     int lastsys=-1;
66     int lastseq=-1;
67
68     stat_info->no_dict_entries++;
69     stat_info->no_dict_bytes += strlen(name);
70
71     assert (*info == sizeof(ISAMS_P));
72     memcpy (&isam_p, info+1, sizeof(ISAMS_P));
73
74
75     if (stat_info->zh->reg->isams)
76     {
77         ISAMS_PP pp;
78         int occurx = 0;
79         struct it_key key;
80
81         pp = isams_pp_open (stat_info->zh->reg->isams, isam_p);
82         occur = isams_pp_num (pp);
83         while (isams_pp_read(pp, &key))
84         {
85             stat_info->cksum = stat_info->cksum * 65509 + 
86                 key.sysno + 11 * key.seqno;
87             occurx++;
88             if (-1==firstsys)
89             {
90                 firstseq=key.seqno;
91                 firstsys=key.sysno;
92             }
93             lastsys=key.sysno;
94             lastseq=key.seqno;
95         }
96         assert (occurx == occur);
97         stat_info->no_isam_entries[0] += occur;
98         isams_pp_close (pp);
99     }
100     if (stat_info->zh->reg->isam)
101     {
102         ISPT ispt;
103
104         ispt = is_position (stat_info->zh->reg->isam, isam_p);
105         occur = is_numkeys (ispt);
106         stat_info->no_isam_entries[is_type(isam_p)] += occur;
107         is_pt_free (ispt);
108     }
109     if (stat_info->zh->reg->isamc)
110     {
111         ISAMC_PP pp;
112         int occurx = 0;
113         struct it_key key;
114
115         pp = isc_pp_open (stat_info->zh->reg->isamc, isam_p);
116         occur = isc_pp_num (pp);
117         while (isc_pp_read(pp, &key))
118         {
119             stat_info->cksum = stat_info->cksum * 65509 + 
120                 key.sysno + 11 * key.seqno;
121             occurx++;
122             if (-1==firstsys)
123             {
124                 firstseq=key.seqno;
125                 firstsys=key.sysno;
126             }
127             lastsys=key.sysno;
128             lastseq=key.seqno;
129         }
130         assert (occurx == occur);
131         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
132         isc_pp_close (pp);
133     }
134     if (stat_info->zh->reg->isamd)
135     {
136         ISAMD_PP pp;
137         int occurx = 0;
138         struct it_key key;
139
140         pp = isamd_pp_open (stat_info->zh->reg->isamd, isam_p);
141         
142         occur = isamd_pp_num (pp);
143         while (isamd_pp_read(pp, &key))
144         {
145             stat_info->cksum = stat_info->cksum * 65509 + 
146                 key.sysno + 11 * key.seqno;
147             occurx++;
148             if (-1==firstsys)
149             {
150                 firstseq=key.seqno;
151                 firstsys=key.sysno;
152             }
153             lastsys=key.sysno;
154             lastseq=key.seqno;
155             if ( pp->is->method->debug >8 )
156                logf (LOG_LOG,"sysno=%d seqno=%d (%x/%x) oc=%d/%d ofs=%d ",
157                    key.sysno, key.seqno,
158                    key.sysno, key.seqno,
159                    occur,occurx, pp->offset);
160         }
161         if ( pp->is->method->debug >7 )
162            logf(LOG_LOG,"item %d=%d:%d says %d keys, counted %d",
163               isam_p, isamd_type(isam_p), isamd_block(isam_p),
164               occur, occurx); 
165         if (occurx != occur) 
166           logf(LOG_LOG,"Count error!!! read %d, counted %d", occur, occurx);
167         assert (occurx == occur);
168         if ( is_singleton(isam_p) )
169             stat_info->no_isam_entries[SINGLETON_TYPE] += occur;
170         else
171             stat_info->no_isam_entries[isamd_type(isam_p)] += occur;
172         isamd_pp_close (pp);
173     }
174     if (stat_info->zh->reg->isamb)
175     {
176         ISAMB_PP pp;
177         struct it_key key;
178         int cat = isam_p & 3;
179         int level;
180         int size;
181         int blocks;
182         
183         pp = isamb_pp_open_x(stat_info->zh->reg->isamb, isam_p, &level);
184
185         while (isamb_pp_read(pp, &key))
186         {
187             stat_info->cksum = stat_info->cksum * 65509 + 
188                 key.sysno + 11 * key.seqno;
189             occur++;
190             if (-1==firstsys)
191             {
192                 firstseq=key.seqno;
193                 firstsys=key.sysno;
194             }
195             lastsys=key.sysno;
196             lastseq=key.seqno;
197         }
198         isamb_pp_close_x (pp, &size, &blocks);
199         stat_info->isamb_blocks[cat] += blocks;
200         stat_info->isamb_sizes[cat] += size;
201         if (level > 4)
202             level = 4;
203         stat_info->isamb_levels[cat][level] ++;
204         stat_info->no_isam_entries[cat] += occur;
205     }
206
207     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
208         i++;
209     ++(stat_info->isam_occurrences[i]);
210     if (stat_info->dumpwords)
211        print_dict_item(stat_info->zh->reg->zebra_maps, name, occur,
212           firstsys,firstseq, lastsys, lastseq);
213     return 0;
214 }
215
216 void zebra_register_statistics (ZebraHandle zh, int dumpdict)
217 {
218     int blocks;
219     int size;
220     int count;
221     int i, prev;
222     int before = 0;
223     int occur;
224     int after = 1000000000;
225     struct inv_stat_info stat_info;
226     char term_dict[2*IT_MAX_WORD+2];
227
228     if (zebra_begin_read (zh))
229         return;
230
231     stat_info.zh = zh;
232     stat_info.dumpwords=dumpdict;
233
234     term_dict[0] = 1;
235     term_dict[1] = 0;
236
237     for (i = 0; i<=SINGLETON_TYPE; i++)
238         stat_info.no_isam_entries[i] = 0;
239     stat_info.no_dict_entries = 0;
240     stat_info.no_dict_bytes = 0;
241     stat_info.isam_bounds[0] = 1;
242     stat_info.isam_bounds[1] = 2;
243     stat_info.isam_bounds[2] = 3;
244     stat_info.isam_bounds[3] = 6;
245     stat_info.isam_bounds[4] = 10;
246     stat_info.isam_bounds[5] = 20;
247     stat_info.isam_bounds[6] = 30;
248     stat_info.isam_bounds[7] = 50;
249     stat_info.isam_bounds[8] = 100;
250     stat_info.isam_bounds[9] = 200;
251     stat_info.isam_bounds[10] = 5000;
252     stat_info.isam_bounds[11] = 10000;
253     stat_info.isam_bounds[12] = 20000;
254     stat_info.isam_bounds[13] = 50000;
255     stat_info.isam_bounds[14] = 100000;
256     stat_info.isam_bounds[15] = 200000;
257     stat_info.isam_bounds[16] = 500000;
258     stat_info.isam_bounds[17] = 1000000;
259     stat_info.isam_bounds[18] = 0;
260
261     stat_info.cksum = 0;
262
263     for (i = 0; i<20; i++)
264         stat_info.isam_occurrences[i] = 0;
265
266     for (i = 0; i<10; i++)
267     {
268         int j;
269         for (j = 0; j<5; j++)
270             stat_info.isamb_levels[i][j] = 0;
271         stat_info.isamb_sizes[i] = 0;
272         stat_info.isamb_blocks[i] = 0;
273     }
274
275     dict_scan (zh->reg->dict, term_dict, &before, &after, &stat_info,
276                inv_stat_handle);
277
278     if (zh->reg->isamc)
279     {
280         fprintf (stdout, "   Blocks    Occur  Size KB   Bytes/Entry\n");
281         for (i = 0; isc_block_used (zh->reg->isamc, i) >= 0; i++)
282         {
283             fprintf (stdout, " %8d %8d", isc_block_used (zh->reg->isamc, i),
284                      stat_info.no_isam_entries[i]);
285
286             if (stat_info.no_isam_entries[i])
287                 fprintf (stdout, " %8d   %f",
288                          (int) ((1023.0 + (double)
289                                  isc_block_used(zh->reg->isamc, i) *
290                                  isc_block_size(zh->reg->isamc,i))/1024),
291                          ((double) isc_block_used(zh->reg->isamc, i) *
292                           isc_block_size(zh->reg->isamc,i))/
293                          stat_info.no_isam_entries[i]);
294             fprintf (stdout, "\n");
295         }
296     }
297     if (zh->reg->isamd)
298     {
299         fprintf (stdout, "   Blocks   Occur      KB Bytes/Entry\n");
300         if (zh->reg->isamd->method->debug >0) 
301             logf(LOG_LOG,"   Blocks   Occur      KB Bytes/Entry");
302         for (i = 0; i<=SINGLETON_TYPE; i++)
303         {
304             blocks= isamd_block_used(zh->reg->isamd,i);
305             size= isamd_block_size(zh->reg->isamd,i);
306             count=stat_info.no_isam_entries[i];
307             if (i==SINGLETON_TYPE) 
308                 blocks=size=0;
309             if (stat_info.no_isam_entries[i]) 
310             {
311                 fprintf (stdout, "%c %7d %7d %7d %5.2f\n",
312                          (i==SINGLETON_TYPE)?('z'):('A'+i),
313                          blocks,
314                          count,
315                          (int) ((1023.0 + (double) blocks * size)/1024),
316                          ((double) blocks * size)/count);
317                 if (zh->reg->isamd->method->debug >0) 
318                     logf(LOG_LOG, "%c %7d %7d %7d %5.2f",
319                          (i==SINGLETON_TYPE)?('z'):('A'+i),
320                          blocks,
321                          count,
322                          (int) ((1023.0 + (double) blocks * size)/1024),
323                          ((double) blocks * size)/count);
324             } /* entries */
325         } /* for */
326     } /* isamd */
327     if ( (zh->reg->isamd) && (zh->reg->isamd->method->debug>0))
328         fprintf (stdout, "\n%d words using %d bytes\n",
329              stat_info.no_dict_entries, stat_info.no_dict_bytes);
330
331     if (zh->reg->isamb)
332     {
333         for (i = 0; i<4; i++)
334         {
335             int j;
336             int bsize = isamb_block_info(zh->reg->isamb, i);
337             if (bsize < 0)
338                 break;
339             fprintf (stdout, "Category   %d\n", i);
340             fprintf (stdout, "Block size %d\n", bsize);
341             fprintf (stdout, "Blocks:    %d\n", stat_info.isamb_blocks[i]);
342             fprintf (stdout, "Size:      %d\n", stat_info.isamb_sizes[i]);
343             fprintf (stdout, "Entries:   %d\n", stat_info.no_isam_entries[i]);
344             fprintf (stdout, "Total      %d\n", stat_info.isamb_blocks[i]*
345                      bsize);
346             for (j = 0; j<5; j++)
347                 if (stat_info.isamb_levels[i][j])
348                     fprintf (stdout, "Level%d     %d\n", j,
349                              stat_info.isamb_levels[i][j]);
350             fprintf (stdout, "\n");
351         }
352     }
353     fprintf (stdout, "Checksum       %08lX\n", stat_info.cksum);
354
355     fprintf (stdout, "Distinct words %d\n", stat_info.no_dict_entries);
356     occur = 0;
357     for (i = 0; i<9; i++)
358         occur += stat_info.no_isam_entries[i];
359     fprintf (stdout, "Word pos       %d\n", occur);
360     fprintf (stdout, "    Occurrences     Words\n");
361     prev = 1;
362     for (i = 0; stat_info.isam_bounds[i]; i++)
363     {
364         int here = stat_info.isam_bounds[i];
365         fprintf (stdout, "%7d-%-7d %7d\n",
366                  prev, here, stat_info.isam_occurrences[i]);
367         prev = here+1;
368     }
369     fprintf (stdout, "%7d-        %7d\n",
370              prev, stat_info.isam_occurrences[i]);
371     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
372     zebra_end_read (zh);
373 }
374
375
376 /*
377  *
378  * $Log: invstat.c,v $
379  * Revision 1.31  2002-07-11 16:16:00  heikki
380  * Fixed a bug in isamd, failed to store a single key when its bits
381  * did not fit into a singleton.
382  *
383  * Revision 1.30  2002/07/11 13:03:01  heikki
384  * Added dumpdict command line option to dump the
385  * dictionary before doing the usual stats
386  *
387  * Revision 1.29  2002/06/19 10:29:17  adam
388  * align block sizes for isam sys. Better plot for test
389  *
390  * Revision 1.28  2002/04/30 19:31:09  adam
391  * isamb delete; more statistics
392  *
393  * Revision 1.27  2002/04/30 08:28:37  adam
394  * isamb fixes for pp_read. Statistics
395  *
396  * Revision 1.26  2002/04/29 18:03:46  adam
397  * More isamb statistics
398  *
399  * Revision 1.25  2002/04/26 08:44:47  adam
400  * Index statistics working again
401  *
402  * Revision 1.24  2002/04/05 08:46:26  adam
403  * Zebra with full functionality
404  *
405  * Revision 1.23  2002/04/04 14:14:13  adam
406  * Multiple registers (alpha early)
407  *
408  * Revision 1.22  2002/02/20 17:30:01  adam
409  * Work on new API. Locking system re-implemented
410  *
411  * Revision 1.21  2000/07/13 10:14:20  heikki
412  * Removed compiler warnings when making zebra
413  *
414  * Revision 1.20  1999/12/01 13:30:30  adam
415  * Updated configure for Zmbol/Zebra dependent settings.
416  *
417  * Revision 1.19  1999/11/30 13:48:03  adam
418  * Improved installation. Updated for inclusion of YAZ header files.
419  *
420  * Revision 1.18  1999/10/06 11:46:36  heikki
421  * mproved statistics on isam-d
422  *
423  * Revision 1.17  1999/08/20 08:28:37  heikki
424  * Log levels
425  *
426  * Revision 1.16  1999/08/18 08:38:22  heikki
427  * Memory leak hunting
428  *
429  * Revision 1.15  1999/08/18 08:34:53  heikki
430  * isamd
431  *
432  * Revision 1.14  1999/07/14 10:59:26  adam
433  * Changed functions isc_getmethod, isams_getmethod.
434  * Improved fatal error handling (such as missing EXPLAIN schema).
435  *
436  * Revision 1.13  1999/07/08 14:23:27  heikki
437  * Fixed a bug in isamh_pp_read and cleaned up a bit
438  *
439  * Revision 1.12  1999/07/06 12:28:04  adam
440  * Updated record index structure. Format includes version ID. Compression
441  * algorithm ID is stored for each record block.
442  *
443  * Revision 1.11  1999/05/15 14:36:38  adam
444  * Updated dictionary. Implemented "compression" of dictionary.
445  *
446  * Revision 1.10  1999/05/12 13:08:06  adam
447  * First version of ISAMS.
448  *
449  * Revision 1.9  1999/02/12 13:29:23  adam
450  * Implemented position-flag for registers.
451  *
452  * Revision 1.8  1999/02/02 14:50:53  adam
453  * Updated WIN32 code specific sections. Changed header.
454  *
455  * Revision 1.7  1998/03/13 15:30:50  adam
456  * New functions isc_block_used and isc_block_size. Fixed 'leak'
457  * in isc_alloc_block.
458  *
459  * Revision 1.6  1998/03/06 13:54:02  adam
460  * Fixed two nasty bugs in isc_merge.
461  *
462  * Revision 1.5  1997/09/17 12:19:13  adam
463  * Zebra version corresponds to YAZ version 1.4.
464  * Changed Zebra server so that it doesn't depend on global common_resource.
465  *
466  * Revision 1.4  1996/11/08 11:10:21  adam
467  * Buffers used during file match got bigger.
468  * Compressed ISAM support everywhere.
469  * Bug fixes regarding masking characters in queries.
470  * Redesigned Regexp-2 queries.
471  *
472  * Revision 1.3  1996/06/04 10:18:58  adam
473  * Minor changes - removed include of ctype.h.
474  *
475  * Revision 1.2  1996/05/22  08:25:56  adam
476  * Minor change.
477  *
478  * Revision 1.1  1996/05/14 14:04:34  adam
479  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
480  * is collected.
481  */