-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.cpp
340 lines (314 loc) · 8.88 KB
/
table.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "global.h"
/**
* @brief Construct a new Table:: Table object
*
*/
Table::Table()
{
logger.log("Table::Table");
}
/**
* @brief Construct a new Table:: Table object used in the case where the data
* file is available and LOAD command has been called. This command should be
* followed by calling the load function;
*
* @param tableName
*/
Table::Table(string tableName)
{
logger.log("Table::Table");
this->sourceFileName = "../data/" + tableName + ".csv";
this->tableName = tableName;
}
/**
* @brief Construct a new Table:: Table object used when an assignment command
* is encountered. To create the table object both the table name and the
* columns the table holds should be specified.
*
* @param tableName
* @param columns
*/
Table::Table(string tableName, vector<string> columns)
{
logger.log("Table::Table");
this->sourceFileName = "../data/temp/" + tableName + ".csv";
this->tableName = tableName;
this->columns = columns;
this->columnCount = columns.size();
this->maxRowsPerBlock = (uint)((BLOCK_SIZE) / (sizeof(int) * columnCount));
this->writeRow<string>(columns);
}
/**
* @brief The load function is used when the LOAD command is encountered. It
* reads data from the source file, splits it into blocks and updates table
* statistics.
*
* @return true if the table has been successfully loaded
* @return false if an error occurred
*/
bool Table::load()
{
logger.log("Table::load");
fstream fin(this->sourceFileName, ios::in);
string line;
if (getline(fin, line))
{
fin.close();
if (this->extractColumnNames(line))
if (this->blockify())
return true;
}
fin.close();
return false;
}
/**
* @brief Function extracts column names from the header line of the .csv data
* file.
*
* @param line
* @return true if column names successfully extracted (i.e. no column name
* repeats)
* @return false otherwise
*/
bool Table::extractColumnNames(string firstLine)
{
logger.log("Table::extractColumnNames");
unordered_set<string> columnNames;
string word;
stringstream s(firstLine);
while (getline(s, word, ','))
{
word.erase(std::remove_if(word.begin(), word.end(), ::isspace), word.end());
if (columnNames.count(word))
return false;
columnNames.insert(word);
this->columns.emplace_back(word);
}
this->columnCount = this->columns.size();
this->maxRowsPerBlock = (uint)((BLOCK_SIZE) / (sizeof(int) * this->columnCount));
return true;
}
/**
* @brief This function splits all the rows and stores them in multiple files of
* one block size.
*
* @return true if successfully blockified
* @return false otherwise
*/
bool Table::blockify()
{
logger.log("Table::blockify");
ifstream fin(this->sourceFileName, ios::in);
string line, word;
vector<int> row(this->columnCount, 0);
vector<vector<int>> rowsInPage(this->maxRowsPerBlock, row);
int pageCounter = 0;
unordered_set<int> dummy;
dummy.clear();
this->distinctValuesInColumns.assign(this->columnCount, dummy);
this->distinctValuesPerColumnCount.assign(this->columnCount, 0);
getline(fin, line);
while (getline(fin, line))
{
stringstream s(line);
for (int columnCounter = 0; columnCounter < this->columnCount; columnCounter++)
{
if (!getline(s, word, ','))
return false;
row[columnCounter] = stoi(word);
rowsInPage[pageCounter][columnCounter] = row[columnCounter];
}
pageCounter++;
this->updateStatistics(row);
if (pageCounter == this->maxRowsPerBlock)
{
bufferManager.writePage(this->tableName, this->blockCount, rowsInPage, pageCounter);
this->blockCount++;
this->rowsPerBlockCount.emplace_back(pageCounter);
pageCounter = 0;
BLOCK_ACCESS++;
}
}
if (pageCounter)
{
bufferManager.writePage(this->tableName, this->blockCount, rowsInPage, pageCounter);
this->blockCount++;
this->rowsPerBlockCount.emplace_back(pageCounter);
pageCounter = 0;
BLOCK_ACCESS++;
}
if (this->rowCount == 0)
return false;
this->distinctValuesInColumns.clear();
return true;
}
/**
* @brief Given a row of values, this function will update the statistics it
* stores i.e. it updates the number of rows that are present in the column and
* the number of distinct values present in each column. These statistics are to
* be used during optimisation.
*
* @param row
*/
void Table::updateStatistics(vector<int> row)
{
this->rowCount++;
for (int columnCounter = 0; columnCounter < this->columnCount; columnCounter++)
{
if (!this->distinctValuesInColumns[columnCounter].count(row[columnCounter]))
{
this->distinctValuesInColumns[columnCounter].insert(row[columnCounter]);
this->distinctValuesPerColumnCount[columnCounter]++;
}
}
}
/**
* @brief Checks if the given column is present in this table.
*
* @param columnName
* @return true
* @return false
*/
bool Table::isColumn(string columnName)
{
logger.log("Table::isColumn");
for (auto col : this->columns)
{
if (col == columnName)
{
return true;
}
}
return false;
}
/**
* @brief Renames the column indicated by fromColumnName to toColumnName. It is
* assumed that checks such as the existence of fromColumnName and the non prior
* existence of toColumnName are done.
*
* @param fromColumnName
* @param toColumnName
*/
void Table::renameColumn(string fromColumnName, string toColumnName)
{
logger.log("Table::renameColumn");
for (int columnCounter = 0; columnCounter < this->columnCount; columnCounter++)
{
if (columns[columnCounter] == fromColumnName)
{
columns[columnCounter] = toColumnName;
break;
}
}
return;
}
/**
* @brief Function prints the first few rows of the table. If the table contains
* more rows than PRINT_COUNT, exactly PRINT_COUNT rows are printed, else all
* the rows are printed.
*
*/
void Table::print()
{
logger.log("Table::print");
uint count = min((long long)PRINT_COUNT, this->rowCount);
//print headings
this->writeRow(this->columns, cout);
Cursor cursor(this->tableName, 0);
vector<int> row;
for (int rowCounter = 0; rowCounter < count; rowCounter++)
{
row = cursor.getNext();
this->writeRow(row, cout);
}
printRowCount(this->rowCount);
}
/**
* @brief This function returns one row of the table using the cursor object. It
* returns an empty row is all rows have been read.
*
* @param cursor
* @return vector<int>
*/
void Table::getNextPage(Cursor *cursor)
{
logger.log("Table::getNext");
if (cursor->pageIndex < this->blockCount - 1)
{
cursor->nextPage(cursor->pageIndex+1);
}
}
/**
* @brief called when EXPORT command is invoked to move source file to "data"
* folder.
*
*/
void Table::makePermanent()
{
logger.log("Table::makePermanent");
if(!this->isPermanent())
bufferManager.deleteFile(this->sourceFileName);
string newSourceFile = "../data/" + this->tableName + ".csv";
ofstream fout(newSourceFile, ios::out);
//print headings
this->writeRow(this->columns, fout);
Cursor cursor(this->tableName, 0);
vector<int> row;
for (int rowCounter = 0; rowCounter < this->rowCount; rowCounter++)
{
row = cursor.getNext();
this->writeRow(row, fout);
}
fout.close();
}
/**
* @brief Function to check if table is already exported
*
* @return true if exported
* @return false otherwise
*/
bool Table::isPermanent()
{
logger.log("Table::isPermanent");
if (this->sourceFileName == "../data/" + this->tableName + ".csv")
return true;
return false;
}
/**
* @brief The unload function removes the table from the database by deleting
* all temporary files created as part of this table
*
*/
void Table::unload(){
logger.log("Table::~unload");
for (int pageCounter = 0; pageCounter < this->blockCount; pageCounter++)
bufferManager.deleteFile(this->tableName, pageCounter);
if (!isPermanent())
bufferManager.deleteFile(this->sourceFileName);
}
/**
* @brief Function that returns a cursor that reads rows from this table
*
* @return Cursor
*/
Cursor Table::getCursor()
{
logger.log("Table::getCursor");
Cursor cursor(this->tableName, 0);
return cursor;
}
/**
* @brief Function that returns the index of column indicated by columnName
*
* @param columnName
* @return int
*/
int Table::getColumnIndex(string columnName)
{
logger.log("Table::getColumnIndex");
for (int columnCounter = 0; columnCounter < this->columnCount; columnCounter++)
{
if (this->columns[columnCounter] == columnName)
return columnCounter;
}
}