-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.cxx
72 lines (69 loc) · 2.09 KB
/
Table.cxx
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
#include <iostream>
#include "header.hpp"
namespace fs = std::filesystem;
Table::Table() {}
Row& Table::row(std::string index) {
Row* ret = new Row(scheme, location + "/" + name + "/" + index);
ret->index = index;
return *ret;
}
Row* Table::operator[](std::string index) {
for (const auto& entry :
fs::directory_iterator(location + "/" + name + "/")) {
if (fs::is_regular_file(entry)) {
// std::cout << "File: " << entry.path().filename() << std::endl;
if (entry.path().filename().string() == index) {
Row* ret = new Row(
scheme,
location + "/" + name + "/" +
index); // Row(*scheme, entry.path().filename().string());
ret->index = index;
ret->load();
return ret;
}
}
}
return nullptr;
}
bool Table::load(std::string location) {
this->location = location;
// if (scheme == nullptr) //idk if this will cause problem
scheme = new Schema();
scheme->load(location + "/" + name + "/Schema");
return true;
}
bool Table::create(std::string location, Schema* schema) {
this->location = location;
// this->name = "yay";
this->scheme = schema;
if (!std::filesystem::exists(location + "/" + name)) {
try {
if (std::filesystem::create_directory(location + "/" + name)) {
std::cout << "Table created successfully: " << location + "/" + name
<< std::endl;
} else {
std::cerr << "Failed to create table: " << location << std::endl;
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
if (scheme == 0) scheme = new Schema();
scheme->load(location + "/" + name + "/Schema");
return true;
}
bool Table::exists(std::string str) {
if (fs::exists(location + "/" + name + "/" + str)) return true;
return false;
}
bool Table::remove(std::string str) {
if (exists(str)) {
std::filesystem::remove(location + "/" + name + "/" + str);
}
return true;
}
/* What is a table
* table finds a row in the filesystem
* it only finds does not open
* index must be string for now
*/