-
Notifications
You must be signed in to change notification settings - Fork 0
/
MTFS.cpp~
448 lines (429 loc) · 11.3 KB
/
MTFS.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "MTFS.h"
#include "master.h"
#include <algorithm>
bool operator==(const smallfile &s1, const smallfile &s2)
{
return s1.d == s2.d;
}
bool operator!=(const smallfile &s1, const smallfile &s2)
{
return !(s1.d == s2.d);
}
bool operator>(const smallfile &s1, const smallfile &s2)
{
return s1.d > s2.d;
}
bool operator<(const smallfile &s1, const smallfile &s2)
{
return s1.d < s2.d;
}
bool operator>=(const smallfile &s1, const smallfile &s2)
{
return s1.d >= s2.d;
}
bool operator<=(const smallfile &s1, const smallfile &s2)
{
return s1.d <= s2.d;
}
void MTFSop::dataInit(void *s)
{
Master* self = (Master*)s;
vector<tableinfo>::iterator im_vtableInfo, im_vtableInfo2;
im_vtableInfo = (self->m_vtableInfo.end()) - 1;
vector<mapnode>::iterator idlist;
cout << "in datainit!!!" << endl;
stringstream ss;
string strx;
vector<fileinfo>::iterator ivfileinfo;
vector<bigfile>::iterator ivbigfile;
list<smallfile>::iterator ilsmallfile;
int c= 0;
while(1)
{
if((*im_vtableInfo).dlist.size() == (*im_vtableInfo).dcnum.size())
{
ifstream infile, inconf;
string filepath = "../data/";
infile.open((filepath + (*im_vtableInfo).filename).c_str());
if(!infile)
{
cout << "cannot open file " << (*im_vtableInfo).filename << "!" << endl;
continue;
}
inconf.open((filepath + (*im_vtableInfo).confname).c_str());
if(!inconf)
{
cout << "cannot open fileconf " << (*im_vtableInfo).confname << "!" << endl;
continue;
}
int linenum, dnum, addrnum, i;
string sline, sdnum, saddrnum;
getline(inconf, sline);
getline(inconf, sdnum);
getline(inconf, saddrnum);
ss.clear();
ss << sline;
ss >> linenum;
ss.clear();
ss << sdnum;
ss >> dnum;
ss.clear();
ss << saddrnum;
ss >> addrnum;
string line;
line = "";
getline(inconf,line);
string linetmp;
line = "";
getline(infile,line);
vector<int> d; //数据的保存坐标信息
vector<int>::iterator ivd;
int dcount;
string key;
for(i = 0; i < linenum; i++)
{
line = "";
getline(infile,line);
int comapos1 = 0, comapos2 = 0;
int x = 0;
dcount = 0;
for(idlist = (*im_vtableInfo).dlist.begin(); idlist != (*im_vtableInfo).dlist.end(); idlist++) //对每行中的每个属性
{
comapos2 = line.find(',',comapos1);
linetmp = line.substr(comapos1, comapos2 - comapos1);
int type = (*idlist).maptype;
comapos1 = comapos2 + 1;
switch(type)
{
case 10:
case 11:
case 12:
{
int n, min, xi;
vector<pair<int, int> >::iterator ii1, ii2, ii3;
ss.clear();
ss << linetmp;
ss >> n;
if(comapos1 == comapos2)
{
n = 0;
}
ii1 = (*idlist).vii.begin();
ii2 = (*idlist).vii.begin() + 1;
while(1)
{
while(n > (*ii1).first && (*ii2).first == (*ii1).first && (ii2 + 1) != (*idlist).vii.end())
{
ii2 += 1;
}
if(n > (*ii2).first && (ii2 + 1) != (*idlist).vii.end())
{
ii1 = ii2;
ii2 += 1;
}
else
break;
}
min = (*ii1).second;
ii3 = ii1;
for(; ii1 != ii2; ii1++)
{
if(min > (*ii1).second)
{
min = (*ii1).second;
ii3 = ii1;
}
}
for(xi = 0, ii1 = (*idlist).vii.begin(); ii1 != ii3;xi++, ii1++)
{
}
xi++;
(*ii3).second += 1;
// cout << "count:" << (*ii3).second << endl;
x += xi;
d.push_back(xi);
break;
}
case 20:
{
float n;
int xi, min;
vector<pair<float, int> >::iterator fi1, fi2, fi3;
ss.clear();
ss << linetmp;
ss >> n;
if(comapos1 == comapos2)
{
n = 0;
}
fi1 = (*idlist).vfi.begin();
fi2 = (*idlist).vfi.begin() + 1;
while(1)
{
while(n > (*fi1).first && (*fi2).first == (*fi1).first && (fi2 + 1) != (*idlist).vfi.end())
{
fi2 += 1;
}
if(n > (*fi2).first && (fi2 + 1) != (*idlist).vfi.end())
{
fi1 = fi2;
fi2 += 1;
}
else
break;
}
min = (*fi1).second;
fi3 = fi1;
for(; fi1 != fi2; fi1++)
{
if(min > (*fi1).second)
{
min = (*fi1).second;
fi3 = fi1;
}
}
for(xi = 0, fi1 = (*idlist).vfi.begin(); fi1 != fi3;xi++, fi1++)
{
}
xi++;
(*fi3).second += 1;
x += xi;
d.push_back(xi);
break;
}
case 30:
{
string n = linetmp;
int xi, min;
vector<pair<string, int> >::iterator si1, si2, si3;
if(comapos1 == comapos2)
{
n = "";
}
// cout << "n:" << n << endl;
si1 = (*idlist).vsi.begin();
si2 = (*idlist).vsi.begin() + 1;
while(1)
{
if(n == "")
{
while((*si2).first == "")
si2++;
break;
}
while(n > (*si1).first && (*si2).first == (*si1).first && (si2 + 1) != (*idlist).vsi.end())
{
si2 += 1;
}
if(n >= (*si2).first && (si2 + 1) != (*idlist).vsi.end())
{
si1 = si2;
si2 += 1;
}
else
break;
}
min = (*si1).second;
si3 = si1;
for(; si1 != si2; si1++)
{
if(min > (*si1).second)
{
min = (*si1).second;
si3 = si1;
}
}
for(xi = 0, si1 = (*idlist).vsi.begin(); si1 != si3;xi++, si1++)
{
}
xi++;
(*si3).second += 1;
x += xi;
d.push_back(xi);
break;
}
default:
break;
}
}
// cout << x << endl;
string strpartnum;
int cmd, partnum;
cmd = x % self->activeSlaves.size() + 1;
partnum = x / self->activeSlaves.size();
// cout << "cmd:" << cmd << endl;
ss.clear();
ss << cmd;
ss >> strx;
ss.clear();
ss << x;
ss >> strpartnum;
string fname;
string fd,tmp; //fd为这一行数据的坐标,也是小文件坐标
string bigfileD;//大文件坐标,即小文件坐标的前几位
int count = 0;
for(ivd = d.begin(); ivd != d.end(); ivd++)
{
ss.clear();
ss << *ivd;
ss >> tmp;
if(count < addrnum)
{
bigfileD += tmp; //计算bigfile的坐标
count++;
if(count != addrnum)
bigfileD += ".";
}
fd += tmp;
if(ivd + 1 == d.end())
break;
fd += ".";
}
// cout << "bigD:" << bigfileD << endl;
// cout << "smalld" << fd << endl;
for(ivbigfile = vbigfile.begin(); ivbigfile != vbigfile.end(); ivbigfile++) //修改vbigfile
{
if((*ivbigfile).d == bigfileD) //找到相同的bigfile
{
/* for(ivfileinfo = (*ivbigfile).vfileinfo.begin(); ivfileinfo != (*ivbigfile).vfileinfo.end(); ivfileinfo++) //大文件中的小文件信息
{
if((*ivfileinfo).d == fd) //有相同小文件
{
(*ivfileinfo).size++;
break;
}
}
if(ivfileinfo == (*ivbigfile).vfileinfo.end()) //无相同小文件
{
fileinfo fi;
fi.d = fd;
fi.size = 1;
(*ivbigfile).vfileinfo.push_back(fi);
}
*/
(*ivbigfile).msmallfile[fd].push_back(line);
// smallfile sma;
// sma.d = fd;
// cout << "??" << endl;
// cout << "begin:" << (*(*ivbigfile).lsmallfile.begin()).d << endl;
// ilsmallfile = lower_bound((*ivbigfile).lsmallfile.begin(), (*ivbigfile).lsmallfile.end(), &sma);
// cout << (*ilsmallfile).d << endl;
/* if((*ilsmallfile).d == fd)
{
cout << "append" << endl;
(*ilsmallfile).content.push_back(line);
}
else if(ilsmallfile != (*ivbigfile).lsmallfile.end())
{
// ilsmallfile = lower_bound((*ivbigfile).lsmallfile.begin(), (*ivbigfile).lsmallfile.end(), sma);
smallfile sm;
sm.d = fd;
sm.content.push_back(line);
cout << "insert" << endl;
// (*ivbigfile).lsmallfile.insert(ilsmallfile, sm);
(*ivbigfile).lsmallfile.insert(ilsmallfile, sm);
}
else if(ilsmallfile == (*ivbigfile).lsmallfile.end())
{
// smallfile sm;
// sm.d = fd;
// sm.content.push_back(line);
// cout << "insert" << endl;
// (*ivbigfile).lsmallfile.insert(ilsmallfile, sm);
// (*ivbigfile).lsmallfile.push_back(sm);
list<string> content;
content.push_back(line);
(*ivbigfile).msmallfile.insert(map<string, list<string> >::value_type(fd, content));
}
*/
/* for(ilsmallfile = (*ivbigfile).lsmallfile.begin(); ilsmallfile != (*ivbigfile).lsmallfile.end(); ilsmallfile++) //大文件中的小文件列表
{
if((*ilsmallfile).d == fd)
{
// cout << "pushback" << endl;
// (*((*ivsmallfile).content)).push_back(line);
(*ilsmallfile).content.push_front(line);
break;
}
if((*ilsmallfile).d > fd) //按字典序插入
{
// cout << "insert" << endl;
smallfile sm;
sm.d = fd;
// vector<string> *content = new vector<string>;
// sm.content = content;
// (*(sm.content)).push_back(line);
// (*ivbigfile).vsmallfile.insert(ivsmallfile, sm);
sm.content.push_back(line);
(*ivbigfile).lsmallfile.insert(ilsmallfile, sm);
break;
}
}
if(ilsmallfile == (*ivbigfile).lsmallfile.end()) //在末尾插入
{
// cout << "push_back small" << endl;
smallfile sm;
sm.d = fd;
// vector<string> *content = new vector<string>;
// sm.content = content;
// (*(sm.content)).push_back(line);
sm.content.push_back(line);
(*ivbigfile).lsmallfile.push_back(sm);
}*/
break;
}
}
if(ivbigfile == vbigfile.end()) //将新的bigfile加入到bigfile
{
bigfile bf;
bf.d = bigfileD;
fileinfo fi;
fi.d = fd;
fi.size = 1;
smallfile sm;
sm.d = fd;
sm.content.push_back(line);
// vector<string> *content = new vector<string>;
// sm.content = content;
// (*(sm.content)).push_back(line);
// cout << "smcontent:" << (*(sm.content))[0] << endl;
// sm.content.push_back(line);
// bf.lsmallfile.push_back(sm);
// bf.vfileinfo.push_back(fi);
list<string> c;
c.push_back(line);
bf.msmallfile[fd] = c;
vbigfile.push_back(bf);
cout << "new big file:" << bigfileD << endl;
}
c++;
if(c % 5000 == 0)
cout << c << ":" << bigfileD << " " << fd << endl;
d.clear(); //清空坐标信息,迎接新的一行
}
string fname;
string fpath = "../data/tmp/";
im_vtableInfo2 = (self->m_vtableInfo.end()) - 1;
for(ivbigfile = vbigfile.begin(); ivbigfile != vbigfile.end(); ivbigfile++)
{
fname = fpath + (*im_vtableInfo2).tableName + "." + strx + "." + (*ivbigfile).d;
cout << fname << endl;
}
/* vector<tableinfo>::iterator im_vtableInfo2;
im_vtableInfo2 = (self->m_vtableInfo.end()) - 1;
vector<mapnode>::iterator idlist;
fname = fpath + (*im_vtableInfo2).tableName + "." + strx + "." + fd;
ofstream of;
of.open(fname.c_str(), ofstream::out | ofstream::app);
of << line << endl;
of.close();
*/
cout << "FINISH!" << endl;
for(idlist = (*im_vtableInfo).dlist.begin(); idlist != (*im_vtableInfo).dlist.end(); idlist++)
{
cout << (*idlist).dname << endl;
}
break;
}
}
}