Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<char*>(&size), sizeof(size_t));

// std::vector<Field> 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();
Expand Down Expand Up @@ -40,6 +58,9 @@ void execute_sql(Operator op, std::stringstream& ss) {

std::vector<Row> 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;
}
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions src/include/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ typedef enum {
SCHEMA,
INSERT,
SELECT,
SERIALIZE,
DESERIALIZE,
LOAD,
} Operator;

const std::unordered_map<std::string, Operator> str_to_operator = {
{"create", CREATE},
{"schema", SCHEMA},
{"insert", INSERT},
{"select", SELECT},
{"serialize", SERIALIZE},
{"deserialize", DESERIALIZE},
{"load", LOAD},
};

std::unordered_map<std::string, Table*> tables;
Expand Down
4 changes: 2 additions & 2 deletions src/include/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Field> f) : fields{f}, size{f.size()} {}

Expand Down Expand Up @@ -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);
};
3 changes: 3 additions & 0 deletions src/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
46 changes: 29 additions & 17 deletions src/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,27 @@ 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<const char*>(&size), sizeof(size_t));

// Serialize every field
for (const auto &f : fields) {
f.serialize(file);
}

file.close();
}

Row Row::deserialize(std::ifstream& file) {
size_t size;
file.read(reinterpret_cast<char*>(&size), sizeof(size_t));

std::cout << size << "\n";

std::vector<Field> fields;
for (unsigned int i = 0; i < size; ++i) {
fields.push_back(Field::deserialize(file));
Expand Down Expand Up @@ -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<char*>(&data_length), sizeof(size_t));
// // Serialize size of row vector
// size_t data_length = data.size();
// file.write(reinterpret_cast<char*>(&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
Expand All @@ -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<char*>(&num_rows), sizeof(size_t));

// Deserialize all the rows
// Deserialize rows until no more
std::vector<Row> 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;
}