Fixed bug #472: dumpdict is broken.
[idzebra-moved-to-github.git] / index / invstat.c
1 /* $Id: invstat.c,v 1.47 2006-02-20 12:41:42 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
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27
28 #include "index.h"
29
30 struct inv_stat_info {
31     ZebraHandle zh;
32     zint no_isam_entries[9];
33     int no_dict_entries;
34     int no_dict_bytes;
35     int isam_bounds[20];
36     int isam_occurrences[20];
37     char tmp[128];
38     int isamb_levels[10][5];
39     zint isamb_sizes[10];
40     zint isamb_blocks[10];
41     unsigned long cksum;
42     int dumpwords;
43 };
44
45 #define SINGLETON_TYPE 8 /* the type to use for singletons that */ 
46                          /* have no block and no block type */
47
48 static void print_dict_item (ZebraHandle zh, const char *s, zint count,
49             int firstsys, int firstseq, int lastsys, int lastseq )
50 {
51     char dst[IT_MAX_WORD+1];
52     int ord;
53     int len = key_SU_decode(&ord, (const unsigned char *) s);
54     int index_type;
55     const char *db = 0;
56
57     if (!zh)
58         *dst = '\0';
59     else
60     {
61         zebraExplain_lookup_ord (zh->reg->zei, ord, &index_type, &db, 0, 0);
62
63         zebra_term_untrans(zh, index_type, dst, s + len);
64     }
65     printf("%02d:%10" ZINT_FORMAT0 " %s %d.%d - %d.%d\n", ord, count, dst,
66            firstsys, firstseq, lastsys, lastseq);
67 }
68
69 static int inv_stat_handle (char *name, const char *info, int pos,
70                             void *client)
71 {
72     zint occur = 0;
73     int i = 0;
74     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
75     ISAM_P isam_p;
76     int firstsys=-1;
77     int firstseq=-1;
78     int lastsys=-1;
79     int lastseq=-1;
80
81     stat_info->no_dict_entries++;
82     stat_info->no_dict_bytes += strlen(name);
83
84     assert (*info == sizeof(ISAM_P));
85     memcpy (&isam_p, info+1, sizeof(ISAM_P));
86
87     if (stat_info->zh->reg->isams)
88     {
89         ISAMS_PP pp;
90         int occurx = 0;
91         struct it_key key;
92
93         pp = isams_pp_open (stat_info->zh->reg->isams, isam_p);
94         occur = isams_pp_num (pp);
95         while (isams_pp_read(pp, &key))
96         {
97             occurx++;
98         }
99         assert (occurx == occur);
100         stat_info->no_isam_entries[0] += occur;
101         isams_pp_close (pp);
102     }
103     if (stat_info->zh->reg->isamc)
104     {
105         ISAMC_PP pp;
106         zint occurx = 0;
107         struct it_key key;
108
109         pp = isamc_pp_open (stat_info->zh->reg->isamc, isam_p);
110         occur = isamc_pp_num (pp);
111         while (isamc_pp_read(pp, &key))
112         {
113             occurx++;
114         }
115         assert (occurx == occur);
116         stat_info->no_isam_entries[isamc_type(isam_p)] += occur;
117         isamc_pp_close (pp);
118     }
119     if (stat_info->zh->reg->isamb)
120     {
121         ISAMB_PP pp;
122         struct it_key key;
123         int cat = (int) (isam_p & 3);
124         int level;
125         zint size;
126         zint blocks;
127         
128         pp = isamb_pp_open_x(stat_info->zh->reg->isamb, isam_p, &level, 0);
129
130         while (isamb_pp_read(pp, &key))
131         {
132             occur++;
133         }
134         isamb_pp_close_x (pp, &size, &blocks);
135         stat_info->isamb_blocks[cat] += blocks;
136         stat_info->isamb_sizes[cat] += size;
137         if (level > 4)
138             level = 4;
139         stat_info->isamb_levels[cat][level] ++;
140         stat_info->no_isam_entries[cat] += occur;
141     }
142     i=0;
143     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
144         i++;
145     ++(stat_info->isam_occurrences[i]);
146     if (stat_info->dumpwords)
147         print_dict_item(stat_info->zh, name, occur,
148                         firstsys, firstseq, lastsys, lastseq);
149     return 0;
150 }
151
152 int zebra_register_statistics (ZebraHandle zh, int dumpdict)
153 {
154     int i, prev;
155     int before = 0;
156     zint occur;
157     int after = 1000000000;
158     struct inv_stat_info stat_info;
159     char term_dict[2*IT_MAX_WORD+2];
160
161     if (zebra_begin_read (zh))
162         return 1;
163
164     stat_info.zh = zh;
165     stat_info.dumpwords=dumpdict;
166
167     term_dict[0] = 1;
168     term_dict[1] = 0;
169
170     for (i = 0; i<=SINGLETON_TYPE; i++)
171         stat_info.no_isam_entries[i] = 0;
172     stat_info.no_dict_entries = 0;
173     stat_info.no_dict_bytes = 0;
174     stat_info.isam_bounds[0] = 1;
175     stat_info.isam_bounds[1] = 2;
176     stat_info.isam_bounds[2] = 3;
177     stat_info.isam_bounds[3] = 6;
178     stat_info.isam_bounds[4] = 10;
179     stat_info.isam_bounds[5] = 20;
180     stat_info.isam_bounds[6] = 30;
181     stat_info.isam_bounds[7] = 50;
182     stat_info.isam_bounds[8] = 100;
183     stat_info.isam_bounds[9] = 200;
184     stat_info.isam_bounds[10] = 5000;
185     stat_info.isam_bounds[11] = 10000;
186     stat_info.isam_bounds[12] = 20000;
187     stat_info.isam_bounds[13] = 50000;
188     stat_info.isam_bounds[14] = 100000;
189     stat_info.isam_bounds[15] = 200000;
190     stat_info.isam_bounds[16] = 500000;
191     stat_info.isam_bounds[17] = 1000000;
192     stat_info.isam_bounds[18] = 0;
193
194     stat_info.cksum = 0;
195
196     for (i = 0; i<20; i++)
197         stat_info.isam_occurrences[i] = 0;
198
199     for (i = 0; i<10; i++)
200     {
201         int j;
202         for (j = 0; j<5; j++)
203             stat_info.isamb_levels[i][j] = 0;
204         stat_info.isamb_sizes[i] = 0;
205         stat_info.isamb_blocks[i] = 0;
206     }
207
208     dict_scan (zh->reg->dict, term_dict, &before, &after, &stat_info,
209                inv_stat_handle);
210
211     if (zh->reg->isamc)
212     {
213         fprintf (stdout, "   Blocks    Occur  Size KB   Bytes/Entry\n");
214         for (i = 0; isamc_block_used (zh->reg->isamc, i) >= 0; i++)
215         {
216             fprintf (stdout, " %8" ZINT_FORMAT0 " %8" ZINT_FORMAT0,
217                      isamc_block_used (zh->reg->isamc, i),
218                      stat_info.no_isam_entries[i]);
219
220             if (stat_info.no_isam_entries[i])
221                 fprintf (stdout, " %8d   %f",
222                          (int) ((1023.0 + (double)
223                                  isamc_block_used(zh->reg->isamc, i) *
224                                  isamc_block_size(zh->reg->isamc,i))/1024),
225                          ((double) isamc_block_used(zh->reg->isamc, i) *
226                           isamc_block_size(zh->reg->isamc,i))/
227                          stat_info.no_isam_entries[i]);
228             fprintf (stdout, "\n");
229         }
230     }
231
232     if (zh->reg->isamb)
233     {
234         for (i = 0; i<4; i++)
235         {
236             int j;
237             int bsize = isamb_block_info(zh->reg->isamb, i);
238             if (bsize < 0)
239                 break;
240             fprintf (stdout, "Category   %d\n", i);
241             fprintf (stdout, "Block size %d\n", bsize);
242             fprintf (stdout, "Blocks:    " ZINT_FORMAT "\n", stat_info.isamb_blocks[i]);
243             fprintf (stdout, "Size:      " ZINT_FORMAT "\n", stat_info.isamb_sizes[i]);
244             fprintf (stdout, "Entries:   " ZINT_FORMAT "\n",
245                      stat_info.no_isam_entries[i]);
246             fprintf (stdout, "Total      " ZINT_FORMAT "\n", stat_info.isamb_blocks[i]*
247                      bsize);
248             for (j = 0; j<5; j++)
249                 if (stat_info.isamb_levels[i][j])
250                     fprintf (stdout, "Level%d     %d\n", j,
251                              stat_info.isamb_levels[i][j]);
252             fprintf (stdout, "\n");
253         }
254     }
255     fprintf (stdout, "Checksum       %08lX\n", stat_info.cksum);
256
257     fprintf (stdout, "Distinct words %d\n", stat_info.no_dict_entries);
258     occur = 0;
259     for (i = 0; i<9; i++)
260         occur += stat_info.no_isam_entries[i];
261     fprintf (stdout, "Word pos       " ZINT_FORMAT "\n", occur);
262     fprintf (stdout, "    Occurrences     Words\n");
263     prev = 1;
264     for (i = 0; stat_info.isam_bounds[i]; i++)
265     {
266         int here = stat_info.isam_bounds[i];
267         fprintf (stdout, "%7d-%-7d %7d\n",
268                  prev, here, stat_info.isam_occurrences[i]);
269         prev = here+1;
270     }
271     fprintf (stdout, "%7d-        %7d\n",
272              prev, stat_info.isam_occurrences[i]);
273     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
274     zebra_end_read (zh);
275     return 0;
276 }
277