-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
307 lines (283 loc) · 6.73 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <unistd.h>
#include <map>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include "jobScheduler.hpp"
#define M 100
#define C 100
#define CHILDS_NUM 10
#define ThreadNum 10
using namespace std;
int main (int argc, char** argv) {
char *i_file;
char *q_file;
int ar;
while ((ar = getopt(argc, argv, ":i:q:")) != -1) {
switch(ar) {
case 'i':
i_file= optarg;
break;
case 'q':
q_file= optarg;
break;
case '?':
fprintf(stderr,"Unrecognized option: -%c\n", optopt);
}
}
Min_heap * min_heap = Create_min_heap();
int query_num=0;
int max=0;
string *out;
string * text_ptr;
char *adq_job = new char[15000];
adq_job[0] = 'F';
int adq_num = 0;
int current_version = 0;
int version_A = 0;
int version_D = 0;
//memory allocation
Trie * trie = new Trie();
int j=0, k=0;
trie->root->createLinearHash();
string * ngram = new string[150000];
Bloom * filter = Bloom_create(100000,5);
int capacity = 150000;
int word_count = 0;
string line,word;
char is_dynamic ;
ifstream myfile (i_file); //διαβασμα του αρχικου αρχειου για δημιουργια του δεντρου
int i=1;
int line_num=0;
if (myfile.is_open())
{
//trie->root->ht->print_hash();
while ( getline (myfile,line) )
{
//cout<<i<<" word= "<<line<<endl;
if(i==1){
if(line.compare("STATIC")==0){
is_dynamic = 'n';
i++;
continue;
}
else if(line.compare("DYNAMIC")==0){
is_dynamic = 'y';
i++;
continue;
}
else{
i++;
}
}
//cout << line << '\n';
istringstream is(line); // σπαει τη καθε γραμμη σε λεξεις
while(is >> word){
if(word_count >= capacity){
//cout<<"diplasiasmos"<<endl;
string * temp = new string[2*capacity];
for(int i=0;i<capacity;i++){
temp[i]=ngram[i];
}
capacity = 2*capacity;
ngram = temp;
ngram[word_count]=word;
}
else{
ngram[word_count] = word;
}
word_count++;
}
trie->InsertNgram(ngram,word_count);
i++;
word_count=0;
}
myfile.close();
}
else
cout << "Unable to open file";
JobScheduler *jobSch = new JobScheduler(ThreadNum, trie, is_dynamic);
ifstream myfile2 (q_file); //διαβασμα του αρχικου αρχειου για δημιουργια του δεντρου
if (myfile2.is_open()){
if(is_dynamic=='y'){
while ( getline (myfile2,line) ){
line_num++;
istringstream is(line);
//cout<<line<<endl;
is >> word;
if(word.compare("A")==0){
//cout<<line<<endl;
i=0;
while(is >> word){
ngram[i]=word;
i++;
}
adq_num++;
adq_job[adq_num] = 'A';
if (adq_job[adq_num -1 ] == 'A' || adq_job[adq_num -1 ] == 'D')
version_A = current_version;
else if (adq_job[adq_num -1 ] == 'Q')
{
current_version++;
version_A = current_version;
}
else if (adq_job[adq_num -1 ] == 'F')
current_version = 0;
trie->InsertNgram(ngram,i);
}
else if(word.compare("D")==0){
//cout<<line<<endl;
i=0;
while(is >> word){
ngram[i]=word;
i++;
}
adq_num++;
adq_job[adq_num] = 'D';
if (adq_job[adq_num -1 ] == 'A' || adq_job[adq_num -1 ] == 'D')
version_D = current_version;
else if (adq_job[adq_num -1 ] == 'Q')
{
current_version++;
version_D = current_version;
}
else if (adq_job[adq_num -1 ] == 'F')
current_version = 0;
trie->DeleteNgram(ngram,i);
}
else if(word.compare("F")==0){
if(query_num>1){
if(is >> word){
int k = atoi(word.c_str());
initializeMin_heap(min_heap,k);
string top_k = FindtopK(min_heap);
cout<<"Top: "+top_k.substr(1,top_k.size()-1)<<endl;
resetMin_heap(min_heap);
}
else{
resetHash(min_heap);
}
query_num=0;
adq_num = 0;
adq_job[0] = 'F';
}
}
else{
out=new string();
i=0;
adq_num++;
adq_job[adq_num] = 'Q';
if(word.compare("Q")==0){
query_num++;
is >> word;
ngram[i]=word;
i++;
}
while(is >> word){
ngram[i]=word;
i++;
}
for(int j=0;j<i;j++){
//if(line_num==36)
trie->SearchNgram(&ngram[j],i-j,out,filter,min_heap);
}
if(out->empty()){
//if(line_num==36)
cout<<-1<<endl;
}
else{
*out = out->substr(1, out->size()-2);
cout<<*out<<endl;
}
reset(filter);
delete out;
}
}
}
else{ //static
trie->Compress();
while ( getline (myfile2,line) ){
line_num++;
istringstream is(line);
is >> word;
if(word.compare("F")==0){
query_num++;
string* results = new string[query_num];
jobSch->results = results;
is >> word;
int k = atoi(word.c_str());
Job* j = new Job(query_num , 0, 0, 0, 'F', NULL,0,k, trie, min_heap);
jobSch->executeAllJobs();
jobSch->waitTasks(0);
//perform topk
if ( j->k > 0)
{
initializeMin_heap(j->min_heap, j->k);
string top_k = FindtopK(j->min_heap);
jobSch->results[j->number - 1] = "Top: "+top_k.substr(1,top_k.size()-1)+"\n";
resetMin_heap(j->min_heap);
}
else{
resetHash(j->min_heap);
}
//print results
for(int l=0; l<query_num; l++) //print stored results("\n" is already stored in string)
cout<<jobSch->results[l];
query_num = 0; // οταν βρεις F κανε τα query_num μηδεν
delete j;
delete[] results;
}
else{
string * ngram = new string[150000];
//string* ngram = new string;
//string wordN;
i=0;
if(word.compare("Q")==0){
query_num++;
is >> word;
ngram[i]=word;
//is >> wordN;
//wordN += " ";
//*ngram += word;
i++;
}
while(is >> word){
//while(is >> wordN){
//wordN += " ";
ngram[i]=word;
//*ngram += wordN;
i++;
}
Job* j = new Job(query_num, 0, 0, 0, 'Q', ngram,i, 0, trie, min_heap);
jobSch->submitJob(j);
delete[] ngram;
}
}
}
myfile2.close();
}
//trie->DeleteTrie(trie->root);
delete trie;
delete[] ngram;
Bloom_destroy(filter);
/*trie->root->ht->print_hash();
cout<<"aa "<<(trie->root->ht->b[121]->nodes[19].child_num)<<endl;
for(int i=0;i<(trie->root->ht->b[121]->nodes[19].child_num);i++){
cout<<i<<": "<<*(trie->root->ht->b[121]->nodes[19].children[i].word)<<" | ";
}
string* n = new string[2];
n[0]= "the";
n[1]= "work";
string a;
trie->SearchNgram(&n[0],2,&a);
cout<<"w: "<<a<<endl;*/
//trie->root->destroyLinearHash(trie->root->ht);
//free(trie->root);
//free(trie);
return 0;
}