-
Notifications
You must be signed in to change notification settings - Fork 3
/
08-buzzdb.cpp
323 lines (261 loc) · 9.29 KB
/
08-buzzdb.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
#include <iostream>
#include <map>
#include <vector>
#include <fstream>
#include <iostream>
#include <chrono>
#include <iostream>
#include <map>
#include <string>
#include <memory>
enum FieldType { INT, FLOAT, STRING };
// Define a basic Field variant class that can hold different types
class Field {
public:
FieldType type;
std::unique_ptr<char[]> data;
size_t data_length;
public:
Field(int i) : type(INT) {
data_length = sizeof(int);
data = std::make_unique<char[]>(data_length);
std::memcpy(data.get(), &i, data_length);
}
Field(float f) : type(FLOAT) {
data_length = sizeof(float);
data = std::make_unique<char[]>(data_length);
std::memcpy(data.get(), &f, data_length);
}
Field(const std::string& s) : type(STRING) {
data_length = s.size() + 1; // include null-terminator
data = std::make_unique<char[]>(data_length);
std::memcpy(data.get(), s.c_str(), data_length);
}
Field& operator=(const Field& other) {
if (&other == this) {
return *this;
}
type = other.type;
data_length = other.data_length;
std::memcpy(data.get(), other.data.get(), data_length);
return *this;
}
Field(Field&& other){
type = other.type;
data_length = other.data_length;
std::memcpy(data.get(), other.data.get(), data_length);
}
FieldType getType() const { return type; }
int asInt() const {
return *reinterpret_cast<int*>(data.get());
}
float asFloat() const {
return *reinterpret_cast<float*>(data.get());
}
std::string asString() const {
return std::string(data.get());
}
void print() const{
switch(getType()){
case INT: std::cout << asInt(); break;
case FLOAT: std::cout << asFloat(); break;
case STRING: std::cout << asString(); break;
}
}
};
class Tuple {
public:
std::vector<std::unique_ptr<Field>> fields;
void addField(std::unique_ptr<Field> field) {
fields.push_back(std::move(field));
}
size_t getSize() const {
size_t size = 0;
for (const auto& field : fields) {
size += field->data_length;
}
return size;
}
void print() const {
for (const auto& field : fields) {
field->print();
std::cout << " ";
}
std::cout << "\n";
}
};
const int PAGE_SIZE = 4096;
// Page class
class Page {
public:
size_t used_size = 0;
std::vector<std::unique_ptr<Tuple>> tuples;
// Add a tuple, returns true if it fits, false otherwise.
bool addTuple(std::unique_ptr<Tuple> tuple) {
// Calculate the size of tuple
size_t tuple_size = 0;
for (const auto& field : tuple->fields) {
tuple_size += field->data_length;
}
if (used_size + tuple_size > PAGE_SIZE) {
// If not enough space, run garbage collection and compaction first
//garbageCollect();
}
// If there is still not enough space, reject the operation
if (used_size + tuple_size > PAGE_SIZE) {
std::cout << "Page is full. Cannot add more tuples. ";
std::cout << "Page contains: " << tuples.size() << " tuples. \n";
return false;
}
tuples.push_back(std::move(tuple));
used_size += tuple_size;
return true;
}
// Write this page to a file.
void write(const std::string& filename) const {
std::ofstream out(filename);
// First write the number of tuples.
size_t numTuples = tuples.size();
out.write(reinterpret_cast<const char*>(&numTuples), sizeof(numTuples));
// Then write each tuple.
for (const auto& tuple : tuples) {
// Write the number of fields in the tuple.
size_t numFields = tuple->fields.size();
out.write(reinterpret_cast<const char*>(&numFields), sizeof(numFields));
// Then write each field.
for (const auto& field : tuple->fields) {
// Write the type of the field.
out.write(reinterpret_cast<const char*>(&field->type), sizeof(field->type));
// Write the length of the field.
out.write(reinterpret_cast<const char*>(&field->data_length), sizeof(field->data_length));
// Then write the field data.
out.write(field->data.get(), field->data_length);
}
}
out.close();
}
// Read this page from a file.
void read(const std::string& filename) {
std::ifstream in(filename);
// First read the number of tuples.
size_t numTuples;
in.read(reinterpret_cast<char*>(&numTuples), sizeof(numTuples));
std::cout << "Num Tuples: " << numTuples << "\n";
// Then read each tuple.
for (size_t i = 0; i < numTuples; ++i) {
auto tuple = std::make_unique<Tuple>();
// Read the number of fields in the tuple.
size_t numFields;
in.read(reinterpret_cast<char*>(&numFields), sizeof(numFields));
// Then read each field.
for (size_t j = 0; j < numFields; ++j) {
// Read the type of the field.
FieldType type;
in.read(reinterpret_cast<char*>(&type), sizeof(type));
// Read the length of the field.
size_t data_length;
in.read(reinterpret_cast<char*>(&data_length), sizeof(data_length));
// Then read the field data.
std::unique_ptr<char[]> data(new char[data_length]);
in.read(data.get(), data_length);
// Add the field to the tuple.
switch(type){
case INT:
{
int val = *reinterpret_cast<int*>(data.get());
auto field = std::make_unique<Field>(val);
tuple->addField(std::move(field));
break;
}
case FLOAT:
{
float val = *reinterpret_cast<float*>(data.get());
auto field = std::make_unique<Field>(val);
tuple->addField(std::move(field));
break;
}
case STRING:
{
char* val = reinterpret_cast<char*>(data.get());
auto field = std::make_unique<Field>(std::string(val, data_length));
tuple->addField(std::move(field));
break;
}
}
}
std::cout << "Tuple " << (i+1) << " :: ";
tuple->print();
// Add the tuple to the page.
//addTuple(std::move(tuple));
}
in.close();
}
};
class BuzzDB {
private:
// a map is an ordered key-value container
std::map<int, std::vector<int>> index;
public:
size_t max_number_of_tuples = 500;
size_t currently_added_tuples = 0;
// a vector of Tuple unique pointers acting as a table
std::vector<std::unique_ptr<Tuple>> table;
Page page;
// insert function
void insert(int key, int value) {
if (currently_added_tuples == max_number_of_tuples)
return;
auto newTuple = std::make_unique<Tuple>();
auto key_field = std::make_unique<Field>(key);
auto value_field = std::make_unique<Field>(value);
float float_val = 132.04;
auto float_field = std::make_unique<Field>(float_val);
auto string_field = std::make_unique<Field>("buzzdb");
newTuple->addField(std::move(key_field));
newTuple->addField(std::move(value_field));
newTuple->addField(std::move(float_field));
newTuple->addField(std::move(string_field));
//newTuple->print();
page.addTuple(std::move(newTuple));
currently_added_tuples += 1;
//table.push_back(std::move(newTuple));
index[key].push_back(value);
}
// perform a SELECT ... GROUP BY ... SUM query
void selectGroupBySum() {
for (auto const& pair : index) { // for each unique key
int sum = 0;
for (auto const& value : pair.second) {
sum += value; // sum all values for the key
}
std::cout << "key: " << pair.first << ", sum: " << sum << '\n';
}
}
};
int main() {
// Get the start time
auto start = std::chrono::high_resolution_clock::now();
BuzzDB db;
std::ifstream inputFile("output.txt");
if (!inputFile) {
std::cerr << "Unable to open file" << std::endl;
return 1;
}
int field1, field2;
while (inputFile >> field1 >> field2) {
db.insert(field1, field2);
}
db.selectGroupBySum();
std::string filename = "page.dat";
// Serialize to disk
db.page.write(filename);
// Deserialize from disk
Page page2;
page2.read(filename);
// Get the end time
auto end = std::chrono::high_resolution_clock::now();
// Calculate and print the elapsed time
std::chrono::duration<double> elapsed = end - start;
std::cout << "Elapsed time: " << elapsed.count() << " seconds" << std::endl;
return 0;
}