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