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