-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
165 lines (134 loc) · 6.68 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <tuple>
#include <unordered_map>
#include <cstring>
#include <chrono>
#include <stdlib.h>
#include "pc_disk_storage.cpp"
#include "BPlusTree.cpp"
#include "pc_disk_storage.h"
using namespace std;
typedef unsigned int uint;
typedef unsigned char uchar;
int main(){
BPlusTree bplusTree = BPlusTree();
PCDiskStorage pcDiskStorage{100000000, sizeOfBlock};
ifstream dataFile("data.tsv");
bool flag=true;
vector<tuple<void *, uint>> data;
if(dataFile.is_open()) {
string line;
while (getline(dataFile, line)) {
if(flag) {
flag=false;
continue;
}
Record record;
string temp;
strcpy(record.tconst, line.substr(0, line.find('\t')).c_str());
stringstream linestream(line);
getline(linestream, temp,'\t');
linestream >> record.avgRating >> record.numVotes;
tuple<void *, uint> data_record = pcDiskStorage.allocateARecord(sizeof(record));
data.push_back(data_record);
void *rdAddr = (uchar *)get<0>(data_record) + get<1>(data_record);
memcpy(rdAddr, &record, sizeof(record));
bplusTree.insertv1(record.numVotes, (Record *) get<0>(data_record));
}
dataFile.close();
}
cout << "\n<------------------- Experiment 1 ------------------->\n";
cout << "Number of records: " << pcDiskStorage.getnumAllocatedRecords() << "\n";
cout << "Size of a record: " << sizeof(Record) << "\n";
cout << "Number of blocks for storing the data: " << pcDiskStorage.getnumAllocatedBlocks() << "\n";
cout << "Number of records stored in a data block: " << maxNumRecords << "\n\n";
cout << "\n<------------------- Experiment 2 ------------------->\n";
cout << "Parameter n of the B+ tree is: " << n << "\n";
int numNodes = 0;
numNodes = bplusTree.NumNodesTree(bplusTree.getRoot(), numNodes);
cout << "No. of nodes of the B+ tree: " << numNodes << "\n";
int level = 0;
level = bplusTree.getLevelOfTree();
cout << "No. of levels of the B+ tree: " << level << "\n";
bplusTree.showRootNodes(bplusTree.getRoot());
cout << "\n<------------------- Experiment 3 ------------------->\n";
int searchNode = 500;
recordResults * gottenResults = NULL;
auto start = std::chrono::high_resolution_clock::now();
auto [nodesAccessed, blocksAccessed] = bplusTree.findNumVotes(searchNode, &gottenResults);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
recordResults *tmpResults = gottenResults;
std::cout << "Number of index nodes the process accesses: " << nodesAccessed << std::endl;
std::cout << "Number of data blocks the process accesses: " << blocksAccessed << std::endl;
tmpResults = gottenResults;
int i = 0;
float avg = 0;
float totalAvg = 0;
while (tmpResults != NULL) {
totalAvg += tmpResults->record.avgRating;
i++;
tmpResults = tmpResults->nextRecord;
}
avg = totalAvg/i;
cout << "Average of averageRatings of the records that are returned: " << avg << "\n";
std::cout << "Running time of the retrieval process: " << duration.count() << " microseconds\n";
auto start3 = std::chrono::high_resolution_clock::now();
int datablocksAccessed = pcDiskStorage.searchNumVotes(searchNode);
auto end3 = std::chrono::high_resolution_clock::now();
auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>(end3 - start3);
cout << "Number of data blocks accessed by linear brute scan: " << datablocksAccessed << endl;
std::cout << "Running time of the linear brute scan: " << duration3.count() << " microseconds\n";
cout << "\n<------------------- Experiment 4 ------------------->\n";
int searchNodeLow = 30000;
int searchNodeHigh = 40000;
gottenResults = NULL;
auto start1 = std::chrono::high_resolution_clock::now();
auto [nodesAccessed1, blocksAccessed1] = bplusTree.findNumVotes(searchNodeLow, searchNodeHigh, &gottenResults);
auto end1 = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end1 - start1);
std::cout << "Number of index nodes the process accesses: " << nodesAccessed1 << std::endl;
std::cout << "Number of data blocks the process accesses: " << blocksAccessed1 << std::endl;
tmpResults = gottenResults;
float avg1 = 0;
float totalAvg1 = 0;
i = 0;
while (tmpResults != NULL) {
totalAvg1 += tmpResults->record.avgRating;
i++;
tmpResults = tmpResults->nextRecord;
}
avg1 = totalAvg1/i;
cout << "Average of averageRatings of the records that are returned: " << avg1 << "\n";
std::cout << "Running time of the retrieval process: " << duration1.count() << " microseconds\n";
auto start4 = std::chrono::high_resolution_clock::now();
int datablocksAccessed1 = pcDiskStorage.searchNumVotesBetween(searchNodeLow,searchNodeHigh);
auto end4 = std::chrono::high_resolution_clock::now();
auto duration4 = std::chrono::duration_cast<std::chrono::microseconds>(end4 - start4);
cout << "Number of data blocks accessed by linear brute scan: " << datablocksAccessed1 << endl;
std::cout << "Running time of the linear brute scan: " << duration4.count() << " microseconds\n";
cout << "\n<------------------- Experiment 5 ------------------->\n";
int deletethis = 1000;
auto start2 = std::chrono::high_resolution_clock::now();
bplusTree.deleteKey(deletethis);
auto end2 = std::chrono::high_resolution_clock::now();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start2);
int after = 0;
after = bplusTree.NumNodesTree(bplusTree.getRoot(),after);
cout << "No. of nodes in updated tree: " << after << "\n";
level = 0;
level = bplusTree.getLevelOfTree();
cout << "No. of levels in updated B+ tree: " << level << "\n";
bplusTree.showRootNodes(bplusTree.getRoot());
std::cout << "Running time of the retrieval process: " << duration2.count() << " microseconds\n";
auto start5 = std::chrono::high_resolution_clock::now();
int datablocksAccessed2 = pcDiskStorage.searchNumVotes(deletethis);
auto end5 = std::chrono::high_resolution_clock::now();
auto duration5 = std::chrono::duration_cast<std::chrono::microseconds>(end5 - start5);
cout << "Number of data blocks accessed by linear brute scan: " << datablocksAccessed2 << endl;
std::cout << "Running time of the linear brute scan: " << duration5.count() << " microseconds\n";
return 0;
}