indexes WIP
This commit is contained in:
63
table.cpp
63
table.cpp
@@ -27,7 +27,7 @@ Table::Table(const Table &other) {
|
||||
ColDefNode Table::get_column_def(const std::string &col_name) {
|
||||
auto name_cmp = [col_name](const ColDefNode& cd) { return cd.name == col_name; };
|
||||
|
||||
auto col_def = std::find_if(begin(m_col_defs), end(m_col_defs), name_cmp);
|
||||
auto col_def = std::find_if(std::begin(m_col_defs), std::end(m_col_defs), name_cmp);
|
||||
if (col_def != std::end(m_col_defs)) {
|
||||
return *col_def;
|
||||
} else {
|
||||
@@ -39,7 +39,7 @@ ColDefNode Table::get_column_def(int col_index) {
|
||||
if (col_index >= 0 && col_index < columns_count()) {
|
||||
return m_col_defs[col_index];
|
||||
} else {
|
||||
throw Exception("column with this index does not exists (" + std::to_string(col_index) + ")");
|
||||
throw Exception("column with this m_index does not exists (" + std::to_string(col_index) + ")");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,4 +244,63 @@ void Table::validate_row(const Row &row) {
|
||||
}
|
||||
}
|
||||
|
||||
void Table::create_index(const Index<IndexValue>& index) {
|
||||
m_indexes.push_back(index);
|
||||
}
|
||||
|
||||
void Table::drop_index(const std::string &column) {
|
||||
throw Exception("implement me! Table::drop_index(const std::string &column)");
|
||||
}
|
||||
|
||||
void Table::index_row(Index<IndexValue> &index, const Row &row, const size_t rowid) {
|
||||
ColDefNode col_def = get_column_def(index.get_column_name());
|
||||
|
||||
if (col_def.type==ColumnType::integer_type) {
|
||||
index.insert(row[col_def.order].getIntValue(), rowid);
|
||||
} else if (col_def.type==ColumnType::varchar_type) {
|
||||
index.insert(row[col_def.order].getStringValue(), rowid);
|
||||
} else {
|
||||
throw Exception("implement me! Table::index_row(const Row &row)");
|
||||
}
|
||||
}
|
||||
|
||||
void Table::index_row(const Row &row, const size_t rowid) {
|
||||
for (auto &i : m_indexes) {
|
||||
index_row(i, row, rowid);
|
||||
}
|
||||
}
|
||||
|
||||
void Table::index_rows(const std::string &index_name) {
|
||||
auto index = get_index(index_name);
|
||||
// TODO handle null pointer
|
||||
size_t rowid = 0;
|
||||
for(const Row& r : m_rows) {
|
||||
index_row(*index, r, rowid);
|
||||
rowid++;
|
||||
}
|
||||
}
|
||||
|
||||
Index<IndexValue> * Table::get_index(const std::string &index_name) {
|
||||
auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
|
||||
[&index_name](const Index<IndexValue> &idx) {
|
||||
return idx.get_index_name() == index_name;
|
||||
});
|
||||
|
||||
if (it != m_indexes.end()) return &(*it);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Index<IndexValue> * Table::get_index_for_column(const std::string &col_name) {
|
||||
auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
|
||||
[&col_name](const Index<IndexValue> &idx) {
|
||||
return idx.get_column_name() == col_name;
|
||||
});
|
||||
|
||||
if (it != m_indexes.end()) return &(*it);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user