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