Minor change.
[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.2  1996-05-22 08:25:56  adam
8  * Minor change.
9  *
10  * Revision 1.1  1996/05/14 14:04:34  adam
11  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
12  * is collected.
13  *
14  */
15 #include <stdio.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <ctype.h>
19
20 #include "index.h"
21 #include "recindex.h"
22
23 struct inv_stat_info {
24     ISAM isam;
25     int no_dict_entries;
26     int no_dict_bytes;
27     int isam_bounds[20];
28     int isam_occurrences[20];
29     char tmp[128];
30 };
31
32 static int inv_stat_handle (char *name, const char *info, int pos,
33                             void *client)
34 {
35     int occur;
36     int i = 0;
37     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
38     ISPT ispt;
39     ISAM_P isam_p;
40
41     stat_info->no_dict_entries++;
42     stat_info->no_dict_bytes += strlen(name);
43
44     assert (*info == sizeof(ISAM_P));
45     memcpy (&isam_p, info+1, sizeof(ISAM_P));
46
47     ispt = is_position (stat_info->isam, isam_p);
48     
49     occur = is_numkeys (ispt);
50
51     is_pt_free (ispt);
52
53     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
54         i++;
55     ++(stat_info->isam_occurrences[i]);
56
57     return 0;
58 }
59
60 void inv_prstat (const char *dict_fname, const char *isam_fname)
61 {
62     Dict dict;
63     ISAM isam;
64     Records records;
65     int i, prev;
66     int before = 0;
67     int after = 1000000000;
68     struct inv_stat_info stat_info;
69     char term_dict[2*IT_MAX_WORD+2];
70
71     term_dict[0] = 1;
72     term_dict[1] = 0;
73
74     dict = dict_open (dict_fname, 100, 0);
75     if (!dict)
76     {
77         logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
78         exit (1);
79     }
80     isam = is_open (isam_fname, key_compare, 0, sizeof(struct it_key));
81     if (!isam)
82     {
83         logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
84         exit (1);
85     }
86     records = rec_open (0);
87
88     stat_info.no_dict_entries = 0;
89     stat_info.no_dict_bytes = 0;
90     stat_info.isam = isam;
91     stat_info.isam_bounds[0] = 1;
92     stat_info.isam_bounds[1] = 2;
93     stat_info.isam_bounds[2] = 3;
94     stat_info.isam_bounds[3] = 5;
95     stat_info.isam_bounds[4] = 10;
96     stat_info.isam_bounds[5] = 20;
97     stat_info.isam_bounds[6] = 30;
98     stat_info.isam_bounds[7] = 50;
99     stat_info.isam_bounds[8] = 100;
100     stat_info.isam_bounds[9] = 200;
101     stat_info.isam_bounds[10] = 5000;
102     stat_info.isam_bounds[11] = 10000;
103     stat_info.isam_bounds[12] = 20000;
104     stat_info.isam_bounds[13] = 50000;
105     stat_info.isam_bounds[14] = 100000;
106     stat_info.isam_bounds[15] = 200000;
107     stat_info.isam_bounds[16] = 500000;
108     stat_info.isam_bounds[17] = 1000000;
109     stat_info.isam_bounds[18] = 0;
110
111     for (i = 0; i<20; i++)
112         stat_info.isam_occurrences[i] = 0;
113
114     dict_scan (dict, term_dict, &before, &after, &stat_info, inv_stat_handle);
115
116     rec_close (&records);
117     dict_close (dict);
118     is_close (isam);
119
120     fprintf (stderr, "%d dictionary entries. %d bytes for strings\n",
121              stat_info.no_dict_entries, stat_info.no_dict_bytes);
122     fprintf (stderr, " size   occurrences\n");
123     prev = 1;
124     for (i = 0; stat_info.isam_bounds[i]; i++)
125     {
126         int here = stat_info.isam_bounds[i];
127         fprintf (stderr, "%7d-%-7d %7d\n",
128                  prev, here, stat_info.isam_occurrences[i]);
129         prev = here+1;
130     }
131     fprintf (stderr, "%7d-        %7d\n",
132              prev, stat_info.isam_occurrences[i]);
133 }