-
Notifications
You must be signed in to change notification settings - Fork 3
/
10-buzzdb.cpp
321 lines (263 loc) · 8.73 KB
/
10-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
#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 serialize(std::ofstream& out) {
out << type << ' ' << data_length << ' ';
if (type == STRING) {
out << data.get() << ' ';
} else if (type == INT) {
out << *reinterpret_cast<int*>(data.get()) << ' ';
} else if (type == FLOAT) {
out << *reinterpret_cast<float*>(data.get()) << ' ';
}
}
static std::unique_ptr<Field> deserialize(std::ifstream& in) {
int type; in >> type;
size_t length; in >> length;
if (type == STRING) {
std::string val; in >> val;
return std::make_unique<Field>(val);
} else if (type == INT) {
int val; in >> val;
return std::make_unique<Field>(val);
} else if (type == FLOAT) {
float val; in >> val;
return std::make_unique<Field>(val);
}
return nullptr;
}
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 serialize(std::ofstream& out) {
out << fields.size() << ' ';
for (auto& field : fields) {
field->serialize(out);
}
}
static std::unique_ptr<Tuple> deserialize(std::ifstream& in) {
auto tuple = std::make_unique<Tuple>();
size_t fieldCount; in >> fieldCount;
for (size_t i = 0; i < fieldCount; ++i) {
tuple->addField(Field::deserialize(in));
}
return tuple;
}
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 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;
}
void deleteTuple(size_t index) {
if (index >= tuples.size()) {
std::cout << "Tuple index out of range. " << index << " not within " << tuples.size() << " tuples. \n";
return;
}
// Update used_size
auto& tuple = tuples[index];
used_size -= tuple->getSize();
// Remove tuple from page
auto it = tuples.begin();
tuples.erase(it + index);
}
// Write this page to a file.
void write(const std::string& filename) const {
std::ofstream out(filename);
// First write the number of tuples.
out << tuples.size() << '\n';
// Then write each tuple.
for (auto& tuple : tuples) {
tuple->serialize(out);
out << '\n';
}
out.close();
}
// Read this page from a file.
static std::unique_ptr<Page> deserialize(const std::string& filename) {
std::ifstream in(filename);
auto page = std::make_unique<Page>();
// First read the number of tuples.
size_t tupleCount; in >> tupleCount;
std::cout << "Num Tuples: " << tupleCount << "\n";
// Then read each tuple.
for (size_t i = 0; i < tupleCount; ++i) {
auto loadedTuple = Tuple::deserialize(in);
std::cout << "Tuple " << (i+1) << " :: ";
loadedTuple->print();
page->addTuple(std::move(loadedTuple));
}
in.close();
return page;
}
};
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 tuple_insertion_attempt_counter = 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) {
tuple_insertion_attempt_counter += 1;
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));
// Skip deleting tuples only once every hundred tuples
if (tuple_insertion_attempt_counter % 100 != 0){
page.deleteTuple(0);
}
//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
auto loadedPage = Page::deserialize(filename);
// PROBLEM: Deletion only in memory, not on disk
loadedPage->deleteTuple(0);
// Deserialize again from disk -- page unchanged
auto loadedPage2 = Page::deserialize(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;
}