-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.cpp
More file actions
251 lines (145 loc) · 6.33 KB
/
DataBase.cpp
File metadata and controls
251 lines (145 loc) · 6.33 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
#include "DataBase.hpp"
#include <iostream>
#include <arpa/inet.h>
DataBase::DataBase(const std::string& name) : dbname(name) {}
bool DataBase::createDatabase() {
if (fs::create_directory(dbname)) {
return true;
}
std::cerr << "Failed to create database: " << dbname << std::endl;
return false;
}
bool DataBase::tableExists(const std::string& tableName) {
std::filesystem::path tablePath = std::filesystem::path(dbname) / (tableName+".HAD");
return std::filesystem::exists(tablePath);
}
bool DataBase::createTable(const std::string& tableName, const std::map<std::string, std::string>& schema) {
if (tableExists(tableName)) {
std::cerr << "Table already exists: " << tableName << std::endl;
return false;
}
uint32_t size;
serializeSchema(schema,dbname,tableName,size,0);
Table* table= new Table(tableName);
table->schema = schema;
table->size = size;
return true;
}
bool DataBase::deleteTable(const std::string& tableName) {
const std::string dbDirectory = "dbname";
std::filesystem::path tablePath = std::filesystem::path(dbDirectory) / tableName;
if (!std::filesystem::exists(tablePath)) {
std::cerr << "Table does not exist: " << tableName << std::endl;
return false;
}
try {
if (std::filesystem::is_directory(tablePath) && std::filesystem::remove(tablePath)) {
std::cout << "Table directory deleted: " << tableName << std::endl;
return true;
} else {
std::cerr << "Failed to delete table directory. Ensure it is empty: " << tableName << std::endl;
return false;
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Error deleting table directory: " << e.what() << std::endl;
return false;
}
}
Table* DataBase::getTable(const std::string tableName ) {
if (tableExists(tableName)) {
Table* my = deserializeSchema(dbname,tableName);
for (const auto& [key, value] : my->schema) {
}
return my;
}
std::cerr << "Table not found: " << tableName << std::endl;
return nullptr;
}
bool DataBase::serializeSchema(const std::map<std::string, std::string>& schema, const std::string& dbName, const std::string& fileName, uint32_t& size, uint32_t page_count) {
try {
size = schema.size();
std::string filePath = dbName + "/" + fileName + ".HAD";
std::ofstream outFile(filePath, std::ios::binary);
if (!outFile.is_open()) {
throw std::ios_base::failure("Error: Could not open file for writing: " + filePath);
}
uint32_t size_network = htonl(size);
uint32_t page_count_network = htonl(page_count);
outFile.write(reinterpret_cast<const char*>(&size_network), sizeof(size_network));
if (outFile.fail()) {
throw std::ios_base::failure("Error: Failed to write schema size.");
}
outFile.write(reinterpret_cast<const char*>(&page_count_network), sizeof(page_count_network));
if (outFile.fail()) {
throw std::ios_base::failure("Error: Failed to write page count.");
}
for (const auto& pair : schema) {
uint32_t keySize = pair.first.size();
uint32_t valueSize = pair.second.size();
uint32_t keySize_network = htonl(keySize);
uint32_t valueSize_network = htonl(valueSize);
outFile.write(reinterpret_cast<const char*>(&keySize_network), sizeof(keySize_network));
outFile.write(reinterpret_cast<const char*>(&valueSize_network), sizeof(valueSize_network));
if (outFile.fail()) {
throw std::ios_base::failure("Error: Failed to write key/value sizes.");
}
outFile.write(pair.first.c_str(), keySize);
outFile.write(pair.second.c_str(), valueSize);
if (outFile.fail()) {
throw std::ios_base::failure("Error: Failed to write key or value.");
}
}
outFile.close();
return true;
} catch (const std::ios_base::failure& e) {
std::cerr << "Error serializing schema: " << e.what() << std::endl;
return false;
}
}
Table* DataBase::deserializeSchema(const std::string& dbName, const std::string& fileName) {
std::map<std::string, std::string> schema;
std::string filePath = dbName + "/" + fileName + ".HAD";
std::ifstream inFile(filePath, std::ios::binary);
if (!inFile.is_open()) {
throw std::ios_base::failure("Error: Could not open file for reading: " + filePath);
}
uint32_t size_network;
inFile.read(reinterpret_cast<char*>(&size_network), sizeof(size_network));
uint32_t size = ntohl(size_network);
if (inFile.fail()) {
throw std::ios_base::failure("Error: Failed to read schema size.");
}
uint32_t page_count_network;
inFile.read(reinterpret_cast<char*>(&page_count_network), sizeof(page_count_network));
uint32_t page_count = ntohl(page_count_network);
if (inFile.fail()) {
throw std::ios_base::failure("Error: Failed to read page count.");
}
for (uint32_t i = 0; i < size; ++i) {
uint32_t keySize_network, valueSize_network;
inFile.read(reinterpret_cast<char*>(&keySize_network), sizeof(keySize_network));
inFile.read(reinterpret_cast<char*>(&valueSize_network), sizeof(valueSize_network));
uint32_t keySize = ntohl(keySize_network);
uint32_t valueSize = ntohl(valueSize_network);
if (inFile.fail()) {
throw std::ios_base::failure("Error: Failed to read key/value sizes.");
}
if (inFile.eof()) {
throw std::ios_base::failure("Error: Premature end of file while reading key/value sizes.");
}
std::string key(keySize, '\0');
inFile.read(&key[0], keySize);
std::string value(valueSize, '\0');
inFile.read(&value[0], valueSize);
if (inFile.fail()) {
throw std::ios_base::failure("Error: Failed to read key or value.");
}
schema[key] = value;
}
Table* table = new Table(fileName);
table->page_count = page_count;
table->schema = schema;
table->size = size;
inFile.close();
return table;
}