-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmydb.cpp
More file actions
269 lines (237 loc) · 8.16 KB
/
mydb.cpp
File metadata and controls
269 lines (237 loc) · 8.16 KB
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
// A simple, minimalistic DB system built in C++
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <vector>
#include <string>
#include <unordered_map>
#include <iomanip>
#include <stdexcept>
using namespace std;
struct Column {
string name;
string type;
};
vector<Column> schema;
bool loadSchema(const string &filename) {
ifstream file(filename);
if (!file.is_open()) return false;
string line;
getline(file, line);
file.close();
stringstream ss(line);
string field;
while (getline(ss, field, ',')) {
auto pos = field.find(':');
if (pos == string::npos) continue;
schema.push_back({field.substr(0, pos), field.substr(pos + 1)});
}
return true;
}
vector<char> serializeRow(const unordered_map<string, string> &data) {
vector<char> buffer;
for (const auto &col : schema) {
if (!data.count(col.name)) throw runtime_error("Missing value for column: " + col.name);
string val = data.at(col.name);
if (col.type == "int") {
try {
int v = stoi(val);
buffer.insert(buffer.end(), reinterpret_cast<char*>(&v), reinterpret_cast<char*>(&v) + sizeof(int));
} catch (...) {
throw runtime_error("Expected integer for column: " + col.name);
}
} else if (col.type == "string" || col.type == "char") {
int len = val.size();
buffer.insert(buffer.end(), reinterpret_cast<char*>(&len), reinterpret_cast<char*>(&len) + sizeof(int));
buffer.insert(buffer.end(), val.begin(), val.end());
}
}
return buffer;
}
unordered_map<string, string> deserializeRow(const vector<char> &buffer) {
unordered_map<string, string> result;
size_t offset = 0;
for (const auto &col : schema) {
if (col.type == "int") {
int val;
memcpy(&val, &buffer[offset], sizeof(int));
offset += sizeof(int);
result[col.name] = to_string(val);
} else if (col.type == "string" || col.type == "char") {
int len;
memcpy(&len, &buffer[offset], sizeof(int));
offset += sizeof(int);
string str(buffer.begin() + offset, buffer.begin() + offset + len);
offset += len;
result[col.name] = str;
}
}
return result;
}
void selectAll() {
ifstream db("dbfile.db", ios::binary);
if (!db.is_open()) {
cerr << "Could not open database file." << endl;
return;
}
// Header
cout << "+";
for (const auto &col : schema)
cout << setw(15) << setfill('-') << "" << "+";
cout << setfill(' ') << endl;
cout << "|";
for (const auto &col : schema)
cout << setw(15) << col.name << "|";
cout << endl;
cout << "+";
for (const auto &col : schema)
cout << setw(15) << setfill('-') << "" << "+";
cout << setfill(' ') << endl;
while (db.peek() != EOF) {
int rowSize;
db.read(reinterpret_cast<char*>(&rowSize), sizeof(int));
vector<char> row(rowSize);
db.read(row.data(), rowSize);
auto values = deserializeRow(row);
if (!values.count("__deleted__")) {
cout << "|";
for (const auto &col : schema)
cout << setw(15) << values[col.name] << "|";
cout << endl;
}
}
db.close();
}
void exportCSV(const string &filename) {
ifstream db("dbfile.db", ios::binary);
ofstream csv(filename);
for (size_t i = 0; i < schema.size(); ++i) {
csv << schema[i].name;
if (i + 1 < schema.size()) csv << ",";
}
csv << "\n";
while (db.peek() != EOF) {
int rowSize;
db.read(reinterpret_cast<char*>(&rowSize), sizeof(int));
vector<char> row(rowSize);
db.read(row.data(), rowSize);
auto values = deserializeRow(row);
if (!values.count("__deleted__")) {
for (size_t i = 0; i < schema.size(); ++i) {
csv << values[schema[i].name];
if (i + 1 < schema.size()) csv << ",";
}
csv << "\n";
}
}
db.close();
csv.close();
cout << "Exported data to " << filename << endl;
}
void insertRow(const unordered_map<string, string> &data) {
auto bytes = serializeRow(data);
ofstream db("dbfile.db", ios::app | ios::binary);
int size = bytes.size();
db.write(reinterpret_cast<char*>(&size), sizeof(int));
db.write(bytes.data(), size);
db.close();
}
void updateRow(const string &key, const string &valToMatch, unordered_map<string, string> updates) {
fstream db("dbfile.db", ios::in | ios::out | ios::binary);
vector<vector<char>> rows;
while (db.peek() != EOF) {
int rowSize;
db.read(reinterpret_cast<char*>(&rowSize), sizeof(int));
vector<char> row(rowSize);
db.read(row.data(), rowSize);
auto rowMap = deserializeRow(row);
if (rowMap[key] == valToMatch && !rowMap.count("__deleted__")) {
for (const auto &entry : updates)
rowMap[entry.first] = entry.second;
row = serializeRow(rowMap);
}
rows.push_back(row);
}
db.close();
ofstream out("dbfile.db", ios::binary | ios::trunc);
for (auto &r : rows) {
int s = r.size();
out.write(reinterpret_cast<char*>(&s), sizeof(int));
out.write(r.data(), s);
}
out.close();
}
void deleteRow(const string &key, const string &valToMatch) {
fstream db("dbfile.db", ios::in | ios::out | ios::binary);
vector<vector<char>> rows;
while (db.peek() != EOF) {
int rowSize;
db.read(reinterpret_cast<char*>(&rowSize), sizeof(int));
vector<char> row(rowSize);
db.read(row.data(), rowSize);
auto rowMap = deserializeRow(row);
if (rowMap[key] == valToMatch && !rowMap.count("__deleted__")) {
rowMap["__deleted__"] = "1";
row = serializeRow(rowMap);
}
rows.push_back(row);
}
db.close();
ofstream out("dbfile.db", ios::binary | ios::trunc);
for (auto &r : rows) {
int s = r.size();
out.write(reinterpret_cast<char*>(&s), sizeof(int));
out.write(r.data(), s);
}
out.close();
}
int main(int argc, char* argv[]) {
if (!loadSchema("schema.txt")) {
cerr << "Schema file missing or unreadable." << endl;
return 1;
}
string cmd = argc > 1 ? argv[1] : "";
try {
if (cmd == "insert") {
unordered_map<string, string> row;
for (int i = 2; i < argc; i++) {
string arg = argv[i];
size_t pos = arg.find('=');
if (pos == string::npos) throw runtime_error("Bad insert argument: " + arg);
row[arg.substr(0, pos)] = arg.substr(pos + 1);
}
insertRow(row);
} else if (cmd == "select") {
selectAll();
} else if (cmd == "export") {
if (argc < 3) throw runtime_error("Filename missing for export");
exportCSV(argv[2]);
} else if (cmd == "update") {
if (argc < 5) throw runtime_error("Usage: update column value key=val...");
string matchCol = argv[2], matchVal = argv[3];
unordered_map<string, string> updates;
for (int i = 4; i < argc; i++) {
string arg = argv[i];
size_t pos = arg.find('=');
if (pos == string::npos) throw runtime_error("Bad update argument: " + arg);
updates[arg.substr(0, pos)] = arg.substr(pos + 1);
}
updateRow(matchCol, matchVal, updates);
} else if (cmd == "delete") {
if (argc < 4) throw runtime_error("Usage: delete column value");
deleteRow(argv[2], argv[3]);
} else {
cout << "Available commands:\n";
cout << " insert key=val ...\n";
cout << " select\n";
cout << " update column value key=val ...\n";
cout << " delete column value\n";
cout << " export filename.csv\n";
}
} catch (const exception &e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}