Debug swcsa
[SXSI/TextCollection.git] / swcsa / buildIntIndex.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "buildFacade.h"
5
6 /* only for getTime() */
7 #include <sys/time.h>
8 #include <sys/resource.h>
9  
10 /* macro to detect and notify errors  */
11 #define IFERROR(error) {{if (error) { fprintf(stderr, "%s\n", error_index(error)); exit(1); }}}
12
13 int loadSEfile (char *basename, uint **v, uint *n);
14 void print_usage(char *);
15 double getTime(void);
16
17 int main(int argc, char *argv[]) {
18
19         char *basenamefile;
20         char *params = NULL;
21         void *index; uint index_len;
22         int error, i;
23         double start, end;
24         uint *se;
25         uint seSize;
26
27     if (argc < 2) print_usage(argv[0]);
28         if (argc > 2) { 
29                 int nchars, len;
30                 nchars = argc-2;
31                 for(i=1;i<argc;i++)
32                         nchars += strlen(argv[i]);
33                 params = (char *) malloc((nchars+1)*sizeof(char));
34                 params[nchars] = '\0';
35                 nchars = 0;
36                 for(i=2;i<argc;i++) {
37                         len = strlen(argv[i]);
38                         strncpy(params+nchars,argv[i],len);
39                         params[nchars+len] = ' ';
40                         nchars += len+1;
41                 }
42                 params[nchars-1] = '\0';
43         }
44
45         basenamefile = argv[1];
46
47         start = getTime();
48
49         error = loadSEfile (basenamefile, &se, &seSize);
50         IFERROR(error);
51                 
52     //fprintf(stderr,"\n parameters (stopwordsFilename): \"%s\"\n",params); fflush(stderr);
53     
54         error =  buildIntIndex(se,seSize, params,(void **)&index);                              
55         IFERROR(error);
56
57         if (index) {
58                 error = saveIntIndex(index, basenamefile);              
59         }       
60                 
61         IFERROR(error);
62         end = getTime();        
63         
64         error = sizeIntIndex(index, &index_len);
65         IFERROR(error);
66
67         printf("\n\n\t Freeing memory...");
68                 
69         error = freeIntIndex(index);
70         IFERROR(error);
71
72         free(se); 
73         free(params);
74         
75         fprintf(stdout,"\n\tBuilding time (**building self-index on ints: %.3f secs", end-start );
76         fprintf(stdout,"\n\t ## Input: %u bytes --> Output (pres_layer) %u bytes.", seSize*sizeof(uint), index_len);
77         fprintf(stdout,"\n\t ## Overall compression --> %.3f%% (%.3f bits per char).\n\n",
78                         (100.0*index_len)/(seSize*sizeof(uint)), (index_len*8.0)/(seSize*sizeof(uint)));
79
80         exit(0);
81 }
82
83
84 int loadSEfile (char *basename, uint **v, uint *n){
85         char filename[255];
86         int file; 
87         sprintf(filename,"%s.%s",basename,SE_FILE_EXT);
88         
89         uint sizeFile = fileSize(filename);
90         
91         if( sizeFile <= 0) {
92                 printf("Cannot read information from file %s\n", filename);     return -1;
93         }       
94         
95         if( (file = open(filename, O_RDONLY)) < 0) {
96                 printf("Cannot open file %s\n", filename);
97                 return -1;
98         }
99
100         uint *se = (uint *) malloc (sizeFile);
101         uint seSize = sizeFile / sizeof(uint);
102         read(file, se, sizeFile);               //the samples
103         close(file);
104         *v=se;
105         *n=seSize;      
106         return 0;       
107 }
108
109
110
111 double
112 getTime (void)
113 {
114
115         double usertime, systime;
116         struct rusage usage;
117
118         getrusage (RUSAGE_SELF, &usage);
119
120         usertime = (double) usage.ru_utime.tv_sec +
121                 (double) usage.ru_utime.tv_usec / 1000000.0;
122
123         systime = (double) usage.ru_stime.tv_sec +
124                 (double) usage.ru_stime.tv_usec / 1000000.0;
125
126         return (usertime + systime);
127
128 }
129
130 void print_usage(char * progname) {
131         fprintf(stderr, "Usage: %s <index file> [<parameters>]\n", progname);
132         fprintf(stderr, "\nIt builds the index on Integer for the sequence in <index file>.se,\n");
133         fprintf(stderr, "storing it in [<index file>.[*]]; Any additional <parameters> \n");
134         fprintf(stderr, "will be passed to the construction function.\n");
135         fprintf(stderr, "At the end, the program sends to the standard error \n");
136         fprintf(stderr, "performance measures on time to build it.\n\n");
137         exit(1);
138 }