-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.cpp
More file actions
288 lines (249 loc) · 8.67 KB
/
Table.cpp
File metadata and controls
288 lines (249 loc) · 8.67 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "HeaderFiles/Table.h"
// =============================================
// TABLE
// =============================================
Table::Table(std::string tableName, const std::string& fileName){
try{
this->pager = std::make_unique<Pager<Page>>(fileName.c_str());
}
catch(...){
throw;
}
// TODO: Read first page to get metadata
// Metadata:-
// 1. Column Names
// 2. Column Types
// 3. Column Size
// 4. Empty Rows
this->tableOpen = true;
this->tableName = std::move(tableName);
this->numRows = 0;
this->rowSize = 0;
this->rowsPerPage = 0;
// this->rowStackPtr = 0;
// this->rowStackOffset = 0;
this->rowStack = nullptr;
this->stackSize = 0;
this->nextPKey = 1;
}
Table::~Table(){
this->close();
}
bool Table::close(){
if(tableOpen) return pager->close();
return false;
}
Cursor Table::start(){
Cursor cursor(this);
cursor.row = 0;
cursor.endOfTable = (numRows == 0);
return cursor;
}
Cursor Table::end(){
Cursor cursor(this);
cursor.row = numRows;
cursor.endOfTable = true;
return cursor;
}
void Table::createColumns(std::vector<std::string>&& columnNames_, std::vector<DataType>&& columnTypes_, std::vector<uint32_t>&& columnSizes_){
this->columnNames = std::move(columnNames_);
this->columnSizes = std::move(columnSizes_);
this->columnTypes = std::move(columnTypes_);
this->createColumnIndex();
}
void Table::createColumnIndex(){
int32_t count = columnNames.size();
for(int index = 0; index < count; ++index){
columnIndex[columnNames[index]] = index;
if(columnTypes[index] == DataType::String){
BPTNode<dbms::string>::pKeyOffset = P_KEY_OFFSET(columnSizes[index]);
BPTNode<dbms::string>::childOffset = CHILD_OFFSET(columnSizes[index]);
}
}
}
void Table::storeMetadata() {
Page* page = pager->header.get();
serailizeColumnMetadata(page->buffer.get());
page->hasUncommitedChanges = true;
pager->flush(0);
calculateRowInfo();
trees.reserve(columnNames.size());
for(int i = 0; i < columnNames.size(); ++i) trees.emplace_back(nullptr);
}
void Table::loadMetadata() {
Page* page = pager->header.get();
deSerailizeColumnMetadata(page->buffer.get());
this->createColumnIndex();
calculateRowInfo();
trees.reserve(columnNames.size());
for(int i = 0; i < columnNames.size(); ++i) trees.emplace_back(nullptr);
}
void Table::calculateRowInfo(){
this->rowSize = 0;
for(int32_t size: columnSizes){
this->rowSize += size;
}
this->rowSize += sizeof(pkey_t);
this->rowsPerPage = PAGE_SIZE/rowSize;
int32_t count = columnSizes.size();
this->indexed.assign(count, false);
this->stackPtr.assign(count, 0);
}
void Table::serailizeColumnMetadata(char* buffer){
int32_t size1 = columnNames.size();
int32_t size2 = columnTypes.size();
int32_t size3 = columnSizes.size();
if(!(size1 == size2 && size2 == size3)){
printf("Failed To Serialize Metadata.\nInconsistent Metadata\n");
return;
}
int32_t offset = 0;
// Write Number of Rows
this->numRows = 0;
memcpy(buffer + offset, &numRows, sizeof(row_t));
offset += sizeof(row_t);
// Write Next pKey
this->nextPKey = 1;
memcpy(buffer + offset, &this->nextPKey, sizeof(pkey_t));
offset += sizeof(pkey_t);
// Write Number of columns
memcpy(buffer + offset, &size1, sizeof(int32_t));
offset += sizeof(int32_t);
for(const std::string& str: columnNames){
strncpy(buffer + offset, str.c_str(), MAX_COLUMN_SIZE);
offset += MAX_COLUMN_SIZE;
}
for(uint32_t size: columnSizes){
memcpy(buffer + offset, &size, sizeof(int32_t));
offset += sizeof(int32_t);
}
for(const DataType& type: columnTypes){
memcpy(buffer + offset, &type, sizeof(DataType));
offset += sizeof(DataType);
}
// int32_t rowStackPtr = 0;
// memcpy(buffer + offset, &rowStackPtr, sizeof(int32_t));
stackSize = (PAGE_SIZE - offset)/sizeof(row_t);
rowStack = new(buffer + offset) row_t[stackSize];
rowStack[0] = 0;
// rowStackOffset = offset + sizeof(int32_t);
}
void Table::deSerailizeColumnMetadata(char* metadataBuffer) {
char colName[MAX_COLUMN_SIZE + 1] = {0};
int32_t colSize;
DataType colType;
int32_t offset = 0;
int32_t size;
memcpy(&this->numRows, metadataBuffer + offset, sizeof(row_t));
offset += sizeof(row_t);
memcpy(&this->nextPKey, metadataBuffer + offset, sizeof(pkey_t));
offset += sizeof(pkey_t);
memcpy(&size, metadataBuffer + offset, sizeof(int32_t));
offset += sizeof(int32_t);
columnNames.reserve(size);
columnSizes.reserve(size);
columnTypes.reserve(size);
for(int32_t i = 0; i < size; ++i){
strncpy(colName, (metadataBuffer + offset), MAX_COLUMN_SIZE);
columnNames.emplace_back(colName);
offset += MAX_COLUMN_SIZE;
}
for(int32_t i = 0; i < size; ++i){
memcpy(&colSize, (metadataBuffer + offset), sizeof(int32_t));
columnSizes.emplace_back(colSize);
offset += sizeof(int32_t);
}
for(int32_t i = 0; i < size; ++i){
memcpy(&colType, (metadataBuffer + offset), sizeof(DataType));
columnTypes.emplace_back(colType);
offset += sizeof(DataType);
}
// memcpy(&stackIndex, (metadataBuffer + offset), sizeof(int32_t));
// stackIndex = new(metadataBuffer + offset) int32_t;
// offset += sizeof(int32_t);
// int32_t rowStackOffset = offset + sizeof(int32_t);
stackSize = (PAGE_SIZE - offset)/sizeof(row_t);
rowStack = new(metadataBuffer + offset) row_t[stackSize];
}
row_t Table::nextFreeRowLocation(){
if(rowStack[0] == 0) return numRows;
row_t nextRow = rowStack[rowStack[0]];
rowStack[0]--;
pager->header->hasUncommitedChanges = true;
// char* buffer = page->buffer.get();
// int32_t offset = (stackIndex - 1) * sizeof(row_t) + rowStackOffset;
// memcpy(&nextRow, buffer + offset, sizeof(row_t));
// memcpy(buffer, &stackIndex, sizeof(int32_t));
return nextRow;
}
void Table::addFreeRowLocation(row_t location){
++rowStack[0];
if(rowStack[0] >= stackSize){
printf("Stack Overflow occurred in main table.\n");
throw std::runtime_error("STACK OVERFLOWS HEADER PAGE");
}
rowStack[rowStack[0]] = location;
pager->header->hasUncommitedChanges = true;
// Page* page = pager->header.get();
// char* buffer = page->buffer.get();
// int32_t offset = rowStackPtr * sizeof(row_t) + sizeof(int32_t);
// rowStackPtr++;
// memcpy(buffer + offset, &location, sizeof(row_t));
}
int32_t Table::getRowSize() const{
return this->rowSize;
}
void Table::increaseRowCount() {
this->numRows++;
this->nextPKey++;
Page* page = pager->header.get();
char* buffer = page->buffer.get();
memcpy(buffer, &numRows, sizeof(row_t));
memcpy(buffer + sizeof(row_t), &nextPKey, sizeof(pkey_t));
page->hasUncommitedChanges = true;
}
bool Table::createIndex(int index, const std::string& filename){
if(!indexed[index]) return true;
int32_t branchingFactor;
switch(columnTypes[index]){
case DataType::Int:
trees[index] = std::make_unique<BPTree<int>>(filename.c_str(), 2, columnSizes[index]);
break;
case DataType::Float:
trees[index] = std::make_unique<BPTree<float>>(filename.c_str(), floatBranchingFactor, columnSizes[index]);
break;
case DataType::Char:
trees[index] = std::make_unique<BPTree<char>>(filename.c_str(), charBranchingFactor, columnSizes[index]);
break;
case DataType::Bool:
trees[index] = std::make_unique<BPTree<bool>>(filename.c_str(), boolBranchingFactor, columnSizes[index]);
break;
case DataType::String:
branchingFactor = BRANCHING_FACTOR(columnSizes[index]);
trees[index] = std::make_unique<BPTree<dbms::string>>(filename.c_str(), branchingFactor, columnSizes[index]);
break;
}
anyIndex = index;
tableIsIndexed = true;
return true;
}
bool Table::insertBTree(std::vector<std::string>& data, row_t row){
for(int i = 0; i < indexed.size(); ++i){
if(!indexed[i]) continue;
bool res;
switch(columnTypes[i]){
BTREE_HANDLER(res, trees[i].get(), insert(data[i], nextPKey - 1, row));
}
if(!res) return false;
}
return true;
}
bool Table::deleteRow(row_t row){
this->numRows--;
Page* page = pager->header.get();
char* buffer = page->buffer.get();
memcpy(buffer, &numRows, sizeof(row_t));
page->hasUncommitedChanges = true;
addFreeRowLocation(row);
return true;
}