Fixed two nasty bugs in isc_merge.
[idzebra-moved-to-github.git] / index / invstat.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: invstat.c,v $
7  * Revision 1.6  1998-03-06 13:54:02  adam
8  * Fixed two nasty bugs in isc_merge.
9  *
10  * Revision 1.5  1997/09/17 12:19:13  adam
11  * Zebra version corresponds to YAZ version 1.4.
12  * Changed Zebra server so that it doesn't depend on global common_resource.
13  *
14  * Revision 1.4  1996/11/08 11:10:21  adam
15  * Buffers used during file match got bigger.
16  * Compressed ISAM support everywhere.
17  * Bug fixes regarding masking characters in queries.
18  * Redesigned Regexp-2 queries.
19  *
20  * Revision 1.3  1996/06/04 10:18:58  adam
21  * Minor changes - removed include of ctype.h.
22  *
23  * Revision 1.2  1996/05/22  08:25:56  adam
24  * Minor change.
25  *
26  * Revision 1.1  1996/05/14 14:04:34  adam
27  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
28  * is collected.
29  *
30  */
31 #include <stdio.h>
32 #include <assert.h>
33 #include <string.h>
34
35 #include "index.h"
36 #include "recindex.h"
37
38 struct inv_stat_info {
39     ISAM isam;
40     ISAMC isamc;
41     int no_dict_entries;
42     int no_dict_bytes;
43     int isam_bounds[20];
44     int isam_occurrences[20];
45     char tmp[128];
46 };
47
48 static int inv_stat_handle (char *name, const char *info, int pos,
49                             void *client)
50 {
51     int occur;
52     int i = 0;
53     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
54     ISAM_P isam_p;
55
56     stat_info->no_dict_entries++;
57     stat_info->no_dict_bytes += strlen(name);
58
59     assert (*info == sizeof(ISAM_P));
60     memcpy (&isam_p, info+1, sizeof(ISAM_P));
61
62     if (stat_info->isam)
63     {
64         ISPT ispt;
65
66         ispt = is_position (stat_info->isam, isam_p);
67         occur = is_numkeys (ispt);
68         is_pt_free (ispt);
69     }
70     if (stat_info->isamc)
71     {
72         ISAMC_PP pp;
73         int occurx = 0;
74         char buf[128];
75
76         pp = isc_pp_open (stat_info->isamc, isam_p);
77         occur = isc_pp_num (pp);
78 #if 1
79         while (isc_pp_read(pp, buf))
80             occurx++;
81         assert (occurx == occur);
82 #endif
83         isc_pp_close (pp);
84     }
85
86     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
87         i++;
88     ++(stat_info->isam_occurrences[i]);
89
90     return 0;
91 }
92
93 void inv_prstat (BFiles bfs)
94 {
95     Dict dict;
96     ISAM isam = NULL;
97     ISAMC isamc = NULL;
98     Records records;
99     int i, prev;
100     int before = 0;
101     int after = 1000000000;
102     struct inv_stat_info stat_info;
103     char term_dict[2*IT_MAX_WORD+2];
104
105     term_dict[0] = 1;
106     term_dict[1] = 0;
107
108     dict = dict_open (bfs, FNAME_DICT, 100, 0);
109     if (!dict)
110     {
111         logf (LOG_FATAL, "dict_open fail");
112         exit (1);
113     }
114     if (!res_get_match (common_resource, "isam", "i", NULL))
115     {
116         isamc = isc_open (bfs, FNAME_ISAMC, 0, key_isamc_m (common_resource));
117         if (!isamc)
118         {
119             logf (LOG_FATAL, "isc_open fail");
120             exit (1);
121         }
122     }
123     else
124     {
125         isam = is_open (bfs, FNAME_ISAM, key_compare, 0,
126                         sizeof(struct it_key), common_resource);
127         if (!isam)
128         {
129             logf (LOG_FATAL, "is_open fail");
130             exit (1);
131         }
132     }
133     records = rec_open (bfs, 0);
134
135     stat_info.no_dict_entries = 0;
136     stat_info.no_dict_bytes = 0;
137     stat_info.isam = isam;
138     stat_info.isamc = isamc;
139     stat_info.isam_bounds[0] = 1;
140     stat_info.isam_bounds[1] = 2;
141     stat_info.isam_bounds[2] = 3;
142     stat_info.isam_bounds[3] = 5;
143     stat_info.isam_bounds[4] = 10;
144     stat_info.isam_bounds[5] = 20;
145     stat_info.isam_bounds[6] = 30;
146     stat_info.isam_bounds[7] = 50;
147     stat_info.isam_bounds[8] = 100;
148     stat_info.isam_bounds[9] = 200;
149     stat_info.isam_bounds[10] = 5000;
150     stat_info.isam_bounds[11] = 10000;
151     stat_info.isam_bounds[12] = 20000;
152     stat_info.isam_bounds[13] = 50000;
153     stat_info.isam_bounds[14] = 100000;
154     stat_info.isam_bounds[15] = 200000;
155     stat_info.isam_bounds[16] = 500000;
156     stat_info.isam_bounds[17] = 1000000;
157     stat_info.isam_bounds[18] = 0;
158
159     for (i = 0; i<20; i++)
160         stat_info.isam_occurrences[i] = 0;
161
162     dict_scan (dict, term_dict, &before, &after, &stat_info, inv_stat_handle);
163
164     rec_close (&records);
165     dict_close (dict);
166     if (isam)
167         is_close (isam);
168     if (isamc)
169         isc_close (isamc);
170
171     fprintf (stderr, "%d dictionary entries. %d bytes for strings\n",
172              stat_info.no_dict_entries, stat_info.no_dict_bytes);
173     fprintf (stderr, " size   occurrences\n");
174     prev = 1;
175     for (i = 0; stat_info.isam_bounds[i]; i++)
176     {
177         int here = stat_info.isam_bounds[i];
178         fprintf (stderr, "%7d-%-7d %7d\n",
179                  prev, here, stat_info.isam_occurrences[i]);
180         prev = here+1;
181     }
182     fprintf (stderr, "%7d-        %7d\n",
183              prev, stat_info.isam_occurrences[i]);
184 }