Add index testing program.
[SXSI/TextCollection.git] / createIndex.cpp
1 // Test driver for text collection
2 #include <iostream>
3 #include <cstdlib>
4 #include <string>
5 using std::cout;
6 using std::endl;
7 using std::cin;
8 using std::string;
9 #include <sys/time.h>
10 #include <time.h>
11 #include <stdio.h>
12 #include <unistd.h>
13
14 static struct timeval t1;
15 static struct timeval t2;
16
17
18 #define STARTTIMER() (gettimeofday(&t1,NULL))
19 #define STOPTIMER()  (gettimeofday(&t2,NULL))
20 #define GETTIME()    (((t2.tv_sec  - t1.tv_sec) * 1000000.0 + (t2.tv_usec  - t1.tv_usec))/1000.0)
21
22 void *  last_brk = NULL;
23
24 long int get_mem(){
25   void * current_brk = sbrk(0);
26   long int mem = ((long int) current_brk ) - ((long int) last_brk);
27   //last_brk = current_brk;
28   return (mem/1024/1024);
29 }
30
31 #include "TextCollectionBuilder.h"
32 using SXSI::TextCollection;
33 using SXSI::TextCollectionBuilder;
34
35
36
37 int main(int argc, char**argv)
38 {
39   string str = string("");
40   unsigned int text_size = 0;
41   unsigned int max_str = 0;
42   unsigned int num_str = 0;
43   double time;
44   FILE* file;
45   TextCollection * tc;
46
47   TextCollectionBuilder *tcb =
48     TextCollectionBuilder::create(64, TextCollectionBuilder::index_type_swcsa);
49
50   STARTTIMER();
51   last_brk= sbrk(0);
52
53   std::cerr << "Filling collection\n";
54
55   while (not(cin.eof()) ){
56     std::getline(cin, str); // Read line by line.
57     if (str.compare("------") != 0 ){
58       if (!str.empty())
59         tcb->InsertText((unsigned char*) str.c_str());
60
61
62       if (num_str % 10000 == 0){
63         STOPTIMER();
64         time = GETTIME();
65         std::cerr << "Added " << num_str << " strings in "
66                   << time  << " ms, max_mem=" << get_mem() << "\n";
67         std::cerr.flush();
68         //STARTTIMER();
69       };
70
71       num_str++;
72       if (max_str < str.size())
73         max_str = str.size();
74       text_size += str.size();
75       str.clear();
76     }
77     else
78       str.clear();
79   };
80
81   std::cerr << "Number of bytes inserted : " << text_size << "\n";
82
83   std::cerr << "Calling InitTextCollection() for sf=64: ";
84   STARTTIMER();
85   tc = tcb->InitTextCollection();
86   STOPTIMER();
87   time = GETTIME();
88   std::cerr << time << "ms, max_mem = " << get_mem() << "\n" ;
89   delete tcb;
90   tcb = NULL;
91
92   file = fopen("index_64.tc","w+");
93   std::cerr << "Saving to index_64.tc ";
94   STARTTIMER();
95   tc->Save(file,"index_64.tc");
96   STOPTIMER();
97   time = GETTIME();
98   std::cerr << time << "ms, max_mem = " << get_mem() << "\n" ;
99   fclose(file);
100   delete tc;
101   tc = NULL;
102   std::cerr << "Freeing memory : max_mem = " << get_mem() << "\n";
103   std::cerr << "Loading TextCollection from saved file ... ";
104   file = fopen("index_64.tc","r");
105   tc = TextCollection::Load(file, "index_64.tc", TextCollection::index_mode_default);
106   std::cerr << "ok\n";
107   delete tc;
108
109   return 0;
110 }