-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsm_manager.cpp
203 lines (190 loc) · 6.04 KB
/
sm_manager.cpp
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
#include "sm/sm_manager.h"
#include "ix/ix.h"
#include "record_printer.h"
#include "rm/rm.h"
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>
DbMeta SmManager::db;
std::map<std::string, std::unique_ptr<RmFileHandle>> SmManager::fhs;
std::map<std::string, std::unique_ptr<IxIndexHandle>> SmManager::ihs;
bool SmManager::is_dir(const std::string &db_name) {
struct stat st;
return stat(db_name.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
}
void SmManager::create_db(const std::string &db_name) {
if (is_dir(db_name)) {
throw DatabaseExistsError(db_name);
}
// Create a subdirectory for the database
std::string cmd = "mkdir " + db_name;
if (system(cmd.c_str()) < 0) {
throw UnixError();
}
if (chdir(db_name.c_str()) < 0) {
throw UnixError();
}
// Create the system catalogs
DbMeta new_db;
new_db.name = db_name;
std::ofstream ofs(DB_META_NAME);
ofs << new_db;
// cd back to root dir
if (chdir("..") < 0) {
throw UnixError();
}
}
void SmManager::drop_db(const std::string &db_name) {
if (!is_dir(db_name)) {
throw DatabaseNotFoundError(db_name);
}
std::string cmd = "rm -r " + db_name;
if (system(cmd.c_str()) < 0) {
throw UnixError();
}
}
void SmManager::open_db(const std::string &db_name) {
if (!is_dir(db_name)) {
throw DatabaseNotFoundError(db_name);
}
// cd to database dir
if (chdir(db_name.c_str()) < 0) {
throw UnixError();
}
// Load meta
std::ifstream ifs(DB_META_NAME);
ifs >> db;
// Open all record files & index files
for (auto &entry : db.tabs) {
auto &tab = entry.second;
fhs[tab.name] = RmManager::open_file(tab.name);
for (size_t i = 0; i < tab.cols.size(); i++) {
auto &col = tab.cols[i];
if (col.index) {
auto index_name = IxManager::get_index_name(tab.name, i);
assert(ihs.count(index_name) == 0);
ihs[index_name] = IxManager::open_index(tab.name, i);
}
}
}
}
void SmManager::close_db() {
// Dump meta
std::ofstream ofs(DB_META_NAME);
ofs << db;
db.name.clear();
db.tabs.clear();
// Close all record files
for (auto &entry : fhs) {
RmManager::close_file(entry.second.get());
}
fhs.clear();
// Close all index files
for (auto &entry : ihs) {
IxManager::close_index(entry.second.get());
}
ihs.clear();
if (chdir("..") < 0) {
throw UnixError();
}
}
void SmManager::show_tables() {
RecordPrinter printer(1);
printer.print_separator();
printer.print_record({"Tables"});
printer.print_separator();
for (auto &entry : db.tabs) {
auto &tab = entry.second;
printer.print_record({tab.name});
}
printer.print_separator();
}
void SmManager::desc_table(const std::string &tab_name) {
TabMeta &tab = db.get_table(tab_name);
std::vector<std::string> captions = {"Field", "Type", "Index"};
RecordPrinter printer(captions.size());
// Print header
printer.print_separator();
printer.print_record(captions);
printer.print_separator();
// Print fields
for (auto &col : tab.cols) {
std::vector<std::string> field_info = {col.name, coltype2str(col.type), col.index ? "YES" : "NO"};
printer.print_record(field_info);
}
// Print footer
printer.print_separator();
}
void SmManager::create_table(const std::string &tab_name, const std::vector<ColDef> &col_defs) {
if (db.is_table(tab_name)) {
throw TableExistsError(tab_name);
}
// Create table meta
int curr_offset = 0;
TabMeta tab;
tab.name = tab_name;
for (auto &col_def : col_defs) {
ColMeta col(tab_name, col_def.name, col_def.type, col_def.len, curr_offset, false);
curr_offset += col_def.len;
tab.cols.push_back(col);
}
// Create & open record file
int record_size = curr_offset;
RmManager::create_file(tab_name, record_size);
db.tabs[tab_name] = tab;
fhs[tab_name] = RmManager::open_file(tab_name);
}
void SmManager::drop_table(const std::string &tab_name) {
// Find table index in db meta
TabMeta &tab = db.get_table(tab_name);
// Close & destroy record file
RmManager::close_file(fhs.at(tab_name).get());
RmManager::destroy_file(tab_name);
// Close & destroy index file
for (auto &col : tab.cols) {
if (col.index) {
SmManager::drop_index(tab_name, col.name);
}
}
db.tabs.erase(tab_name);
fhs.erase(tab_name);
}
void SmManager::create_index(const std::string &tab_name, const std::string &col_name) {
TabMeta &tab = db.get_table(tab_name);
auto col = tab.get_col(col_name);
if (col->index) {
throw IndexExistsError(tab_name, col_name);
}
// Create index file
int col_idx = col - tab.cols.begin();
IxManager::create_index(tab_name, col_idx, col->type, col->len);
// Open index file
auto ih = IxManager::open_index(tab_name, col_idx);
// Get record file handle
auto fh = fhs.at(tab_name).get();
// Index all records into index
for (RmScan rm_scan(fh); !rm_scan.is_end(); rm_scan.next()) {
auto rec = fh->get_record(rm_scan.rid());
const uint8_t *key = rec->data + col->offset;
ih->insert_entry(key, rm_scan.rid());
}
// Store index handle
auto index_name = IxManager::get_index_name(tab_name, col_idx);
assert(ihs.count(index_name) == 0);
ihs[index_name] = std::move(ih);
// Mark column index as created
col->index = true;
}
void SmManager::drop_index(const std::string &tab_name, const std::string &col_name) {
TabMeta &tab = db.tabs[tab_name];
auto col = tab.get_col(col_name);
if (!col->index) {
throw IndexNotFoundError(tab_name, col_name);
}
int col_idx = col - tab.cols.begin();
auto index_name = IxManager::get_index_name(tab_name, col_idx);
IxManager::close_index(ihs.at(index_name).get());
IxManager::destroy_index(tab_name, col_idx);
ihs.erase(index_name);
col->index = false;
}