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