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