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