This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
516 lines (397 loc) · 14.6 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <time.h>
#include <chrono>
#include "BPTree.h"
using namespace std;
const int MAXN = 15;
#pragma pack(1) // might not work on x86
struct _key {
unsigned int key: 24;
// functional comparator, for BPTree
bool operator<(const _key &b) const {
return key < b.key;
}
// for assertion and other purposes
bool operator<=(const _key &b) const {
return key <= b.key;
}
bool operator>(const _key &b) const {
return key > b.key;
}
bool operator>=(const _key &b) const {
return key >= b.key;
}
bool operator==(const _key &b) const {
return key == b.key;
}
friend std::ostream &operator<<(std::ostream &os, const _key &b) {
os << b.key;
return os;
}
};
#pragma pack(0)
#pragma pack(1) // might not work on x86
struct _record {
unsigned int tConst: 24;
unsigned int rating: 8;
unsigned int numVotes: 24;
};
#pragma pack(0)
void printLinebreak() {
cout << "---------------------------------------------------------------------";
cout << endl;
}
template<int n>
void correctnessTest() {
using tree = BPTree<int, int, n>;
// selfCheck take a lot of time, OPERATIONS cannot be too large
const int OPERATIONS = 100000;
int range = rand();
tree tr;
vector<int> insertedVal; // to store insertion history
multiset<int> sett; // to simulate the BPTree
cout << OPERATIONS << " random operations (insert, delete, query) on BPTree<int, int, " << n << ">" << endl;
for (int i = 1; i <= OPERATIONS; i++) {
int op = rand() % 3;
if (op == 0) { // insert
int num = rand() % range;
tr.insert(num, num);
insertedVal.push_back(num);
sett.insert(num);
assert(tr.selfCheck());
} else if (op == 1) { // delete
if (insertedVal.size() == 0) {
i--;
continue;
}
int id = rand() % ((int) insertedVal.size());
bool deleted = tr.remove(insertedVal[id]);
bool _deleted = sett.count(insertedVal[id]);
if (_deleted)
sett.erase(sett.find(insertedVal[id]));
assert(deleted == _deleted);
assert(tr.selfCheck());
} else { // query
if (insertedVal.size() == 0) {
i--;
continue;
}
// two random value from insert history
int id1 = rand() % ((int) insertedVal.size());
int id2 = rand() % ((int) insertedVal.size());
int shiftRange = range / 10;
int offset = (rand() % shiftRange) - (shiftRange / 2);
int lo = min(insertedVal[id1] + offset, insertedVal[id2] + offset);
int hi = max(insertedVal[id1] + offset, insertedVal[id2] + offset);
auto tmp = tr.query(lo, hi); // [lo, hi]
vector<int> q1;
for (auto p: tmp) {
q1.push_back(*p);
}
auto it1 = sett.lower_bound(lo), it2 = sett.upper_bound(hi);
vector<int> q2(it1, it2);
assert(q1 == q2);
}
assert(tr.size() == sett.size());
}
cout << "pass" << endl;
printLinebreak();
correctnessTest<n - 1>();
}
template<>
void correctnessTest<1>() {
return;
}
template<int n>
void insertionEfficiencyTest(int &bestN, int &bestClk) {
using tree = BPTree<int, int, n>;
// around the number of data.tsv
const int OPERATIONS = 10000000;
int range = rand();
tree tr;
cout << OPERATIONS << " random insertions on BPTree<int, int, " << n << ">" << endl;
clock_t st = clock();
for (int i = 1; i <= OPERATIONS; i++) {
int num = rand() % range;
clock_t st = clock();
tr.insert(num, num);
}
clock_t duration = clock() - st;
if (duration < bestClk) {
bestClk = duration;
bestN = n;
}
cout << "runtime for " << n << " : " << (double) duration / CLOCKS_PER_SEC << " s" << endl;
printLinebreak();
insertionEfficiencyTest<n - 1>(bestN, bestClk);
}
template<>
void insertionEfficiencyTest<1>(int &bestN, int &bestClk) {
return;
}
void findBestN() {
int bestN = 0, bestClk = INT32_MAX;
insertionEfficiencyTest<MAXN>(bestN, bestClk);
cout << "best n is " << bestN << " with runtime " << (double) bestClk / CLOCKS_PER_SEC << " s" << endl;
}
const int N = 15;
using tree = BPTree<_key, _record, N>;
tree *constructTreeFromTsv(string filename) {
// definition of B+ tree
using tree = BPTree<_key, _record, N>;
using node = tree::node;
cout << "Start processing the tsv..."
<< "\n";
tree *trp = new tree();
ifstream fin(filename);
if (!fin) {
throw runtime_error("data.tsv not found");
}
string line;
getline(fin, line);
unsigned int max_numVotes = 0;
unsigned int max_tconst = 0;
int cnt = 0;
while (getline(fin, line)) {
cnt++;
istringstream is(line);
string tconst_str;
string rating_str;
unsigned int numVotes_full = 0;
getline(is, tconst_str, '\t');
getline(is, rating_str, '\t');
is >> numVotes_full;
unsigned int tconst_full = stoi(tconst_str.substr(2, tconst_str.size() - 2));
unsigned int rating_full = (rating_str[0] - '0') * 10 + (rating_str[2] - '0');
_key skey = {numVotes_full};
_record srecord = {tconst_full, rating_full, numVotes_full};
trp->insert(skey, srecord);
max_numVotes = max(max_numVotes, numVotes_full);
max_tconst = max(max_tconst, tconst_full);
}
fin.close();
cout << "Number of records read: " << cnt << endl;
cout << "Max of numVotes = " << max_numVotes << ", max of tconst = " << max_tconst << "\n";
return trp;
}
void experiment1(tree *tr) {
cout << "Start experiment 1: "
<< "\n";
cout << "1.1 Number of records: " << tr->size() << "\n";
cout << "1.2 Size of a record: " << sizeof(_record) << " bytes\n";
tr->getDisk();
cout << "1.3 Number of records stored in a block: " << sizeof(Block) / sizeof(_record) << "\n";
cout << "1.4 Number of blocks to storing data: " << tr->getDisk()->getAllocatedBlock() << "\n";
cout << "Completed experiment 1. "
<< "\n\n";
}
void experiment2(tree *tr) {
cout << "Start experiment 2: "
<< "\n";
cout << "2.1 Parameter N: " << N << "\n";
cout << "2.2 Number of nodes: " << tr->nodeSize() << "\n";
cout << "2.3 Number of levels: " << tr->height() << "\n";
cout << "2.4 Keys of the root node: ";
tr->printRootInfo();
cout << "\n";
cout << "Completed experiment 2. "
<< "\n\n";
}
void experiment3(tree *tr) {
cout << "Start experiment 3: " << "\n";
auto start = chrono::steady_clock::now();
int accessedCnt = 0;
vector<_record *> precords = tr->query(_key{500}, _key{500}, &accessedCnt);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Number of records that satisfies (numVotes = 500): " << precords.size() << "\n";
cout << "3.1 Number of accessed tree nodes: " << accessedCnt << "\n";
auto disk = tr->getDisk();
unordered_set<Block *> uniBlk;
for (auto pRecord: precords) {
Block *blk = disk->blkOf(pRecord);
uniBlk.insert(blk);
}
vector<_record> records;
for (auto pBlk: uniBlk) {
auto tmp = disk->getAllFromBlock(pBlk);
for (auto r: tmp) {
if (r.numVotes == 500) records.push_back(r);
}
}
cout << "3.2 Number of accessed data blocks: " << uniBlk.size() << "\n";
int sum = 0;
for (int i = 0; i < records.size(); i++) {
sum += records[i].rating;
}
double avg = double(sum) / 10.0 / records.size();
cout << "3.3 Average value of averageRating: " << avg << "\n";
cout << "3.4 Running time of retrieval process: " << chrono::duration<double, milli>(diff).count() << " ms \n";
start = chrono::steady_clock::now();
disk->innitializeScan();
vector<_record> blk_records = disk->linearScanNextBlk();
sum = 0;
int cnt = 0;
int numAccessedBlock = 0;
while (blk_records.size() != 0) {
numAccessedBlock++;
for (auto r: blk_records) {
if (r.numVotes == 500) {
sum += r.rating;
cnt += 1;
}
}
blk_records = disk->linearScanNextBlk();
}
avg = double(sum) / 10.0 / cnt;
end = chrono::steady_clock::now();
diff = end - start;
cout << "3.5.0 Calculated average value of averageRating from linear scan: " << avg << "\n";
cout << "3.5.1 Number of data blocks accessed in linear scan: " << numAccessedBlock << "\n";
cout << "3.5.2 Running time of linear scan: " << chrono::duration<double, milli>(diff).count() << " ms \n";
cout << "Completed experiment 3. " << "\n\n";
}
void experiment4(tree *tr) {
cout << "Start experiment 4: " << "\n";
auto start = chrono::steady_clock::now();
int accessedCnt = 0;
vector<_record *> precords = tr->query(_key{30000}, _key{40000}, &accessedCnt);
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "Number of records that satisfies (numVotes in [30000, 40000]): " << precords.size() << "\n";
cout << "4.1 Number of accessed tree nodes: " << accessedCnt << "\n";
auto disk = tr->getDisk();
unordered_set<Block *> uniBlk;
for (auto pRecord: precords) {
Block *blk = disk->blkOf(pRecord);
uniBlk.insert(blk);
}
vector<_record> records;
for (auto pBlk: uniBlk) {
auto tmp = disk->getAllFromBlock(pBlk);
for (auto r: tmp) {
if (r.numVotes >= 30000 && r.numVotes <= 40000) records.push_back(r);
}
}
cout << "4.2 Number of accessed data blocks: " << uniBlk.size() << "\n";
int sum = 0;
for (int i = 0; i < records.size(); i++) {
sum += records[i].rating;
}
double avg = double(sum) / 10.0 / records.size();
cout << "4.3 Average value of averageRating: " << avg << "\n";
cout << "4.4 Running time of retrieval process: " << chrono::duration<double, milli>(diff).count() << " ms \n";
start = chrono::steady_clock::now();
disk->innitializeScan();
vector<_record> blk_records = disk->linearScanNextBlk();
sum = 0;
int cnt = 0;
int numAccessedBlock = 0;
while (blk_records.size() != 0) {
numAccessedBlock++;
for (auto r: blk_records) {
if (r.numVotes >= 30000 && r.numVotes <= 40000) {
sum += r.rating;
cnt += 1;
}
}
blk_records = disk->linearScanNextBlk();
}
avg = double(sum) / 10.0 / cnt;
end = chrono::steady_clock::now();
diff = end - start;
cout << "4.5.0 Calculated average value of averageRating from linear scan: " << avg << "\n";
cout << "4.5.1 Number of data blocks accessed in linear scan: " << numAccessedBlock << "\n";
cout << "4.5.2 Running time of linear scan: " << chrono::duration<double, milli>(diff).count() << " ms \n";
cout << "Completed experiment 4. " << "\n\n";
}
int numOfKeyInDisk(Disk<_record> *disk, int key) {
disk->innitializeScan();
vector<_record> blk_records = disk->linearScanNextBlk();
int cnt = 0;
while (blk_records.size() != 0) {
for (auto r: blk_records) {
if (r.numVotes == key) {
cnt += 1;
}
}
blk_records = disk->linearScanNextBlk();
}
return cnt;
}
void experiment5(tree *tr) {
cout << "Start experiment 5: " << "\n";
Disk disk_copy = Disk(*(tr->getDisk()));
vector<_record *> precords = tr->query(_key{1000}, _key{1000});
int numOfKeyInTree = precords.size();
cout << "5.0 Number of records to delete (numVotes = 1000): " << numOfKeyInTree << "\n";
auto start = chrono::steady_clock::now();
tr->removeAll(_key{1000});
auto end = chrono::steady_clock::now();
auto diff = end - start;
cout << "5.1 Updated number of tree nodes: " << tr->nodeSize() << "\n";
cout << "5.2 Updated tree height: " << tr->height() << "\n";
cout << "5.3 Keys of the root node: ";
tr->printRootInfo();
cout << "\n";
cout << "5.4 Running time of deletion process: " << chrono::duration<double, milli>(diff).count() << " ms \n";
//get number of records that (numVoted = 1000) in disk_copy before delete
int numTargetBeforeD = numOfKeyInDisk(&disk_copy, 1000);
assert(numOfKeyInTree == numTargetBeforeD);
//delete the records
start = chrono::steady_clock::now();
disk_copy.innitializeScan();
vector<_record> blk_records = disk_copy.linearScanNextBlk();
int numAccessedBlock = 0;
while (blk_records.size() != 0) {
numAccessedBlock++;
unordered_set<int> vjs;
for (int i = 0; i < blk_records.size(); i++) {
_record r = blk_records[i];
if (r.numVotes == 1000) {
vjs.insert(i);
}
}
if (vjs.size() > 0) disk_copy.deleteFromLastScanedBlkForLinearScan(vjs);
blk_records = disk_copy.linearScanNextBlk();
}
end = chrono::steady_clock::now();
diff = end - start;
//get number of records that (numVoted = 1000) in disk_copy after delete
int numTargetAfterD = numOfKeyInDisk(&disk_copy, 1000);
cout << "5.5.0 Number of records (numVotes = 1000) decreases from " << numTargetBeforeD << " to " << numTargetAfterD
<< " after linear-scan deletion\n";
cout << "5.5.1 Number of data blocks accessed in linear scan: " << numAccessedBlock << "\n";
cout << "5.5.2 Running time of linear scan: " << chrono::duration<double, milli>(diff).count() << " ms \n";
cout << "Completed experiment 5. " << "\n\n";
}
void runExperiment() {
tree *tr = constructTreeFromTsv("data.tsv");
printLinebreak();
experiment1(tr);
printLinebreak();
experiment2(tr);
printLinebreak();
experiment3(tr);
printLinebreak();
experiment4(tr);
printLinebreak();
experiment5(tr);
}
int main() {
srand(43); // for consistent output
// correctnessTest<MAXN>(); // check correctness from n = 2 to n = 15
// findBestN(); // simulate insertions, and get best n, (it might output different optimal n each time, but we will choose one in our following experiment)
cout << "Size of struct key: " << sizeof(_key) << " bytes\n";
cout << "Size of struct record: " << sizeof(_record) << " bytes\n";
cout << "Set parameter N = " << N << ", so the size of tree node is " << sizeof(tree::node) << " bytes\n";
printLinebreak();
cout << "Experiment starts: " << endl;
runExperiment();
return 0;
}