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