diff --git a/src/db.cc b/src/db.cc index 304c150..cb63105 100644 --- a/src/db.cc +++ b/src/db.cc @@ -2,6 +2,24 @@ #include "include/db.h" int main() { + // std::ifstream file("databases/empty.txt", std::ios::out); + + // size_t size; + // file.read(reinterpret_cast(&size), sizeof(size_t)); + + // std::vector fields; + // for (unsigned int i = 0; i < size; ++i) { + // fields.push_back(Field::deserialize(file)); + // } + + // for (const auto &f : fields) { + // std::cout << f; + // } + + // std::cout << "\n"; + + // return 0; + std::string input; while (true) { db_prompt(); @@ -40,6 +58,9 @@ void execute_sql(Operator op, std::stringstream& ss) { std::vector table_data; Table *table = new Table(table_name, Schema(field_defs), table_data); + + std::string file_path = "databases/" + table_name; + table->serialize(file_path); tables[table_name] = table; break; } @@ -83,19 +104,13 @@ void execute_sql(Operator op, std::stringstream& ss) { break; } - case SERIALIZE: - { - std::string table_name; ss >> table_name; - tables[table_name]->serialize("/Users/arist/Documents/code/projects/relational-database-cpp/databases/test.txt"); - - break; - } - case DESERIALIZE: + case LOAD: { std::string table_name; ss >> table_name; - Table test = Table::deserialize("/Users/arist/Documents/code/projects/relational-database-cpp/databases/test.txt"); - test.print_schema(); + std::string file_path = "databases/" + table_name; + Table* test = Table::deserialize(file_path); + tables[table_name] = test; break; } default: diff --git a/src/include/db.h b/src/include/db.h index bacb355..97dc2de 100644 --- a/src/include/db.h +++ b/src/include/db.h @@ -8,8 +8,7 @@ typedef enum { SCHEMA, INSERT, SELECT, - SERIALIZE, - DESERIALIZE, + LOAD, } Operator; const std::unordered_map str_to_operator = { @@ -17,8 +16,7 @@ const std::unordered_map str_to_operator = { {"schema", SCHEMA}, {"insert", INSERT}, {"select", SELECT}, - {"serialize", SERIALIZE}, - {"deserialize", DESERIALIZE}, + {"load", LOAD}, }; std::unordered_map tables; diff --git a/src/include/table.h b/src/include/table.h index 7ad5a79..80143e4 100644 --- a/src/include/table.h +++ b/src/include/table.h @@ -54,7 +54,7 @@ class Row { public: const size_t size; - void serialize(std::ofstream& file) const; + void serialize(const std::string& file_path) const; static Row deserialize(std::ifstream& file); Row(std::vector f) : fields{f}, size{f.size()} {} @@ -91,5 +91,5 @@ class Table { void print_schema(); void serialize(const std::string& filePath) const; - static Table deserialize(const std::string& filePath); + static Table* deserialize(const std::string& filePath); }; \ No newline at end of file diff --git a/src/table.cc b/src/table.cc index 764c2a1..060447f 100644 --- a/src/table.cc +++ b/src/table.cc @@ -28,6 +28,9 @@ void Table::print_schema() { void Table::insert_row(Row &r) { if (validate_row(r)) { + std::string file_path = "databases/" + Table::name; + r.serialize(file_path); + Table::data.push_back(r); } } diff --git a/src/utils.cc b/src/utils.cc index c87077b..7228ecd 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -106,7 +106,10 @@ Field Field::deserialize(std::ifstream &file) { } } -void Row::serialize(std::ofstream& file) const { +void Row::serialize(const std::string& file_path) const { + // We serialize row to an existing database + std::ofstream file(file_path, std::ios::out | std::ios::app); + // Serialize the size file.write(reinterpret_cast(&size), sizeof(size_t)); @@ -114,12 +117,16 @@ void Row::serialize(std::ofstream& file) const { for (const auto &f : fields) { f.serialize(file); } + + file.close(); } Row Row::deserialize(std::ifstream& file) { size_t size; file.read(reinterpret_cast(&size), sizeof(size_t)); + std::cout << size << "\n"; + std::vector fields; for (unsigned int i = 0; i < size; ++i) { fields.push_back(Field::deserialize(file)); @@ -163,17 +170,19 @@ void Table::serialize(const std::string& file_path) const { // Serialize the schema schema.serialize(file); - // Serialize size of row vector - size_t data_length = data.size(); - file.write(reinterpret_cast(&data_length), sizeof(size_t)); + // // Serialize size of row vector + // size_t data_length = data.size(); + // file.write(reinterpret_cast(&data_length), sizeof(size_t)); - // Serialize all rows - for (const auto &r : data) { - r.serialize(file); - } + // // Serialize all rows + // for (const auto &r : data) { + // r.serialize(file); + // } + + file.close(); } -Table Table::deserialize(const std::string& file_path) { +Table* Table::deserialize(const std::string& file_path) { std::ifstream file(file_path, std::ios::out); // Deserialize length of name @@ -188,15 +197,18 @@ Table Table::deserialize(const std::string& file_path) { // Deserialize the schema Schema schema = Schema::deserialize(file); - // Deserialize the size of row vector - size_t num_rows; - file.read(reinterpret_cast(&num_rows), sizeof(size_t)); - - // Deserialize all the rows + // Deserialize rows until no more std::vector data; - for (unsigned int i = 0; i < num_rows; ++i) { - data.push_back(Row::deserialize(file)); + while (!file.eof()) { + Row r = Row::deserialize(file); + + if (r.size <= 0) { + break; + } + + data.push_back(r); } - return Table(name, schema, data); + Table* t = new Table(name, schema, data); + return t; }