New functions isc_block_used and isc_block_size. Fixed 'leak'
[idzebra-moved-to-github.git] / index / invstat.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: invstat.c,v $
7  * Revision 1.7  1998-03-13 15:30:50  adam
8  * New functions isc_block_used and isc_block_size. Fixed 'leak'
9  * in isc_alloc_block.
10  *
11  * Revision 1.6  1998/03/06 13:54:02  adam
12  * Fixed two nasty bugs in isc_merge.
13  *
14  * Revision 1.5  1997/09/17 12:19:13  adam
15  * Zebra version corresponds to YAZ version 1.4.
16  * Changed Zebra server so that it doesn't depend on global common_resource.
17  *
18  * Revision 1.4  1996/11/08 11:10:21  adam
19  * Buffers used during file match got bigger.
20  * Compressed ISAM support everywhere.
21  * Bug fixes regarding masking characters in queries.
22  * Redesigned Regexp-2 queries.
23  *
24  * Revision 1.3  1996/06/04 10:18:58  adam
25  * Minor changes - removed include of ctype.h.
26  *
27  * Revision 1.2  1996/05/22  08:25:56  adam
28  * Minor change.
29  *
30  * Revision 1.1  1996/05/14 14:04:34  adam
31  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
32  * is collected.
33  *
34  */
35 #include <stdio.h>
36 #include <assert.h>
37 #include <string.h>
38
39 #include "index.h"
40 #include "recindex.h"
41
42 struct inv_stat_info {
43     ISAM isam;
44     ISAMC isamc;
45     int no_isam_entries[8];
46     int no_dict_entries;
47     int no_dict_bytes;
48     int isam_bounds[20];
49     int isam_occurrences[20];
50     char tmp[128];
51 };
52
53 static int inv_stat_handle (char *name, const char *info, int pos,
54                             void *client)
55 {
56     int occur = 0;
57     int i = 0;
58     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
59     ISAM_P isam_p;
60
61     stat_info->no_dict_entries++;
62     stat_info->no_dict_bytes += strlen(name);
63
64     assert (*info == sizeof(ISAM_P));
65     memcpy (&isam_p, info+1, sizeof(ISAM_P));
66
67     if (stat_info->isam)
68     {
69         ISPT ispt;
70
71         ispt = is_position (stat_info->isam, isam_p);
72         occur = is_numkeys (ispt);
73         is_pt_free (ispt);
74     }
75     if (stat_info->isamc)
76     {
77         ISAMC_PP pp;
78         int occurx = 0;
79         char buf[128];
80
81         pp = isc_pp_open (stat_info->isamc, isam_p);
82         occur = isc_pp_num (pp);
83         while (isc_pp_read(pp, buf))
84             occurx++;
85         assert (occurx == occur);
86         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
87         isc_pp_close (pp);
88     }
89
90     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
91         i++;
92     ++(stat_info->isam_occurrences[i]);
93     return 0;
94 }
95
96 void inv_prstat (BFiles bfs)
97 {
98     Dict dict;
99     ISAM isam = NULL;
100     ISAMC isamc = NULL;
101     Records records;
102     int i, prev;
103     int before = 0;
104     int after = 1000000000;
105     struct inv_stat_info stat_info;
106     char term_dict[2*IT_MAX_WORD+2];
107
108     term_dict[0] = 1;
109     term_dict[1] = 0;
110
111     dict = dict_open (bfs, FNAME_DICT, 100, 0);
112     if (!dict)
113     {
114         logf (LOG_FATAL, "dict_open fail");
115         exit (1);
116     }
117     if (!res_get_match (common_resource, "isam", "i", NULL))
118     {
119         isamc = isc_open (bfs, FNAME_ISAMC, 0, key_isamc_m (common_resource));
120         if (!isamc)
121         {
122             logf (LOG_FATAL, "isc_open fail");
123             exit (1);
124         }
125     }
126     else
127     {
128         isam = is_open (bfs, FNAME_ISAM, key_compare, 0,
129                         sizeof(struct it_key), common_resource);
130         if (!isam)
131         {
132             logf (LOG_FATAL, "is_open fail");
133             exit (1);
134         }
135     }
136     records = rec_open (bfs, 0);
137
138     for (i = 0; i<8; i++)
139         stat_info.no_isam_entries[i] = 0;
140     stat_info.no_dict_entries = 0;
141     stat_info.no_dict_bytes = 0;
142     stat_info.isam = isam;
143     stat_info.isamc = isamc;
144     stat_info.isam_bounds[0] = 1;
145     stat_info.isam_bounds[1] = 2;
146     stat_info.isam_bounds[2] = 3;
147     stat_info.isam_bounds[3] = 6;
148     stat_info.isam_bounds[4] = 10;
149     stat_info.isam_bounds[5] = 20;
150     stat_info.isam_bounds[6] = 30;
151     stat_info.isam_bounds[7] = 50;
152     stat_info.isam_bounds[8] = 100;
153     stat_info.isam_bounds[9] = 200;
154     stat_info.isam_bounds[10] = 5000;
155     stat_info.isam_bounds[11] = 10000;
156     stat_info.isam_bounds[12] = 20000;
157     stat_info.isam_bounds[13] = 50000;
158     stat_info.isam_bounds[14] = 100000;
159     stat_info.isam_bounds[15] = 200000;
160     stat_info.isam_bounds[16] = 500000;
161     stat_info.isam_bounds[17] = 1000000;
162     stat_info.isam_bounds[18] = 0;
163
164     for (i = 0; i<20; i++)
165         stat_info.isam_occurrences[i] = 0;
166
167     dict_scan (dict, term_dict, &before, &after, &stat_info, inv_stat_handle);
168
169     if (isamc)
170     {
171         fprintf (stderr, "   Blocks    Occur  Size KB   Bytes/Entry\n");
172         for (i = 0; isc_block_used (isamc, i) >= 0; i++)
173         {
174             fprintf (stderr, " %8d %8d", isc_block_used (isamc, i),
175                      stat_info.no_isam_entries[i]);
176
177             if (stat_info.no_isam_entries[i])
178                 fprintf (stderr, " %8d   %f",
179                          (int) ((1023.0 + (double) isc_block_used(isamc, i) *
180                                  isc_block_size(isamc,i))/1024),
181                          ((double) isc_block_used(isamc, i) *
182                           isc_block_size(isamc,i))/
183                          stat_info.no_isam_entries[i]);
184             fprintf (stderr, "\n");
185         }
186     }
187         
188     fprintf (stderr, "\n%d words using %d bytes\n",
189              stat_info.no_dict_entries, stat_info.no_dict_bytes);
190     fprintf (stderr, "    Occurrences     Words\n");
191     prev = 1;
192     for (i = 0; stat_info.isam_bounds[i]; i++)
193     {
194         int here = stat_info.isam_bounds[i];
195         fprintf (stderr, "%7d-%-7d %7d\n",
196                  prev, here, stat_info.isam_occurrences[i]);
197         prev = here+1;
198     }
199     fprintf (stderr, "%7d-        %7d\n",
200              prev, stat_info.isam_occurrences[i]);
201
202     rec_close (&records);
203     dict_close (dict);
204
205     if (isam)
206         is_close (isam);
207     if (isamc)
208         isc_close (isamc);
209     
210 }