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