diff --git a/index.h b/index.h index d54a5ca..134962e 100644 --- a/index.h +++ b/index.h @@ -6,16 +6,18 @@ #include #include #include +#include #include namespace usql { +using IndexValue=std::variant; using rowid_t = size_t; // int is now enough but size_t is correct static const int k_default_rowids_size = 16; -template + class Index { public: Index(std::string index_name, std::string col_name, ColumnType type) : @@ -25,7 +27,7 @@ public: throw Exception("creating index on unsupported type"); } - void insert(K key, rowid_t rowid) { + void insert(IndexValue key, rowid_t rowid) { auto search = m_index.find(key); if (search != m_index.end()) { if (m_uniq) @@ -40,7 +42,7 @@ public: } } - void remove(K key, rowid_t rowid) { + void remove(IndexValue key, rowid_t rowid) { auto search = m_index.find(key); if (search != m_index.end()) { search->second.erase(find(search->second.begin(), search->second.end(), rowid)); @@ -49,7 +51,7 @@ public: } } - std::vector search(K key) { + std::vector search(IndexValue key) { auto search = m_index.find(key); if (search != m_index.end()) { return search->second; @@ -74,8 +76,8 @@ public: // void dump() { // std::for_each(m_index.begin(), m_index.end(), -// [](std::pair> element){ -// K key = element.first; +// [](std::pair> element){ +// IndexValue key = element.first; // std::vector rowids = element.second; // std::cout << "key: " << key << ", rowids count:" << rowids.size() << std::endl; // }); @@ -99,7 +101,7 @@ private: std::string m_column_name; ColumnType m_data_type; - std::map > m_index; + std::map > m_index; }; } // namespace \ No newline at end of file diff --git a/table.cpp b/table.cpp index 45586d0..a8cae48 100644 --- a/table.cpp +++ b/table.cpp @@ -263,13 +263,13 @@ void Table::validate_row(Row &row) { row.set_visible(); } -void Table::create_index(const Index& index) { +void Table::create_index(const Index& index) { m_indexes.push_back(index); } bool Table::drop_index(const std::string &index_name) { auto it = std::find_if(m_indexes.begin(), m_indexes.end(), - [&index_name](const Index &idx) { + [&index_name](const Index &idx) { return idx.get_index_name() == index_name; }); @@ -281,7 +281,7 @@ bool Table::drop_index(const std::string &index_name) { } -void Table::index_row(Index &index, const ColDefNode &col_def, const Row &row, const size_t rowid) { +void Table::index_row(Index &index, const ColDefNode &col_def, const Row &row, const size_t rowid) { if (col_def.type==ColumnType::integer_type) { index.insert(row[col_def.order].getIntValue(), rowid); } else if (col_def.type==ColumnType::varchar_type) { @@ -291,7 +291,7 @@ void Table::index_row(Index &index, const ColDefNode &col_def, const } } -void Table::unindex_row(Index &index, const ColDefNode &col_def, const Row &row, const size_t rowid) { +void Table::unindex_row(Index &index, const ColDefNode &col_def, const Row &row, const size_t rowid) { if (col_def.type==ColumnType::integer_type) { index.remove(row[col_def.order].getIntValue(), rowid); } else if (col_def.type==ColumnType::varchar_type) { @@ -301,7 +301,7 @@ void Table::unindex_row(Index &index, const ColDefNode &col_def, con } } -void Table::reindex_row(Index &index, const ColDefNode &col_def, const Row &old_row, const Row &new_row, size_t rowid) { +void Table::reindex_row(Index &index, const ColDefNode &col_def, const Row &old_row, const Row &new_row, size_t rowid) { unindex_row(index, col_def, old_row, rowid); index_row(index, col_def, new_row, rowid); } @@ -350,18 +350,18 @@ void Table::index_rows(const std::string &index_name) { } -Index * Table::get_index(const std::string &index_name) { +Index * Table::get_index(const std::string &index_name) { auto it = std::find_if(m_indexes.begin(), m_indexes.end(), - [&index_name](const Index &idx) { + [&index_name](const Index &idx) { return idx.get_index_name() == index_name; }); return (it != m_indexes.end()) ? &(*it) : nullptr; } -Index * Table::get_index_for_column(const std::string &col_name) { +Index * 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 &idx) { + [&col_name](const Index &idx) { return idx.get_column_name() == col_name; }); diff --git a/table.h b/table.h index 45f17e0..730277f 100644 --- a/table.h +++ b/table.h @@ -43,22 +43,22 @@ struct Table { void print(); - std::string m_name; + std::string m_name; std::vector m_col_defs; - std::vector m_rows; - std::vector> m_indexes; + std::vector m_rows; + std::vector m_indexes; static long string_to_long(const std::string &s); static double string_to_double(const std::string &s); void create_row_from_vector(const std::vector &colDefs, const std::vector &csv_line); - void create_index(const Index& index); + void create_index(const Index& index); bool drop_index(const std::string &index_name); - static void index_row(Index &index, const ColDefNode &col_def, const Row &row, size_t rowid); - static void unindex_row(Index &index, const ColDefNode &col_def, const Row &row, size_t rowid); - static void reindex_row(Index &index, const ColDefNode &col_def, const Row &old_row, const Row &new_row, size_t rowid); + static void index_row(Index &index, const ColDefNode &col_def, const Row &row, size_t rowid); + static void unindex_row(Index &index, const ColDefNode &col_def, const Row &row, size_t rowid); + static void reindex_row(Index &index, const ColDefNode &col_def, const Row &old_row, const Row &new_row, size_t rowid); void index_row(const Row &row); void unindex_row(const Row &row); @@ -66,8 +66,8 @@ struct Table { void index_rows(const std::string &index_name); - Index * get_index(const std::string &index_name); - Index * get_index_for_column(const std::string &col_name); + Index * get_index(const std::string &index_name); + Index * get_index_for_column(const std::string &col_name); struct rows_scanner { diff --git a/usql_dml.cpp b/usql_dml.cpp index c8d060d..9102f49 100644 --- a/usql_dml.cpp +++ b/usql_dml.cpp @@ -29,7 +29,7 @@ std::pair> USql::look_for_usable_index(const Node *wh ) { auto col_name = ((DatabaseValueNode *)ron->left.get())->col_name; - Index * used_index = table->get_index_for_column(col_name); + Index * used_index = table->get_index_for_column(col_name); if (used_index != nullptr) { std::vector rowids = used_index->search((ValueNode *)ron->right.get()); #ifndef NDEBUG