From 40fa78dd2a383a7ac4f96fd35158d1c19eeaa1a1 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Sat, 18 Dec 2021 11:42:02 +0100 Subject: [PATCH] index refactoring --- .idea/.gitignore | 3 -- .idea/misc.xml | 4 --- .idea/modules.xml | 8 ----- .idea/msql.iml | 2 -- .idea/vcs.xml | 6 ---- index.h | 86 ++++++++++++++++++++++++++++++----------------- table.cpp | 16 ++------- 7 files changed, 58 insertions(+), 67 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/msql.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 79b3c94..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 015bace..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/msql.iml b/.idea/msql.iml deleted file mode 100644 index f08604b..0000000 --- a/.idea/msql.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/index.h b/index.h index 134962e..5b0c2df 100644 --- a/index.h +++ b/index.h @@ -2,6 +2,7 @@ #include "exception.h" #include "parser.h" +#include "row.h" #include #include @@ -12,7 +13,7 @@ namespace usql { -using IndexValue=std::variant; +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; @@ -27,6 +28,55 @@ public: throw Exception("creating index on unsupported type"); } + + std::vector search(ValueNode *key) { + return search(to_index_value(key)); + } + + void insert(ColValue *key, rowid_t rowid) { + return insert(to_index_value(key), rowid); + } + + void remove(ColValue *key, rowid_t rowid) { + return remove(to_index_value(key), rowid); + } + + void truncate() { + m_index.clear(); + } + + [[nodiscard]] const std::string &get_column_name() const { + return m_column_name; + } + + [[nodiscard]] const std::string &get_index_name() const { + return m_index_name; + } + + [[nodiscard]] ColumnType get_data_type() const { + return m_data_type; + } + +private: + + IndexValue to_index_value(ValueNode *key) { + if (m_data_type == ColumnType::integer_type) + return key->getIntegerValue(); + else if (m_data_type == ColumnType::varchar_type) + return key->getStringValue(); + else + throw Exception("using index on unsupported type"); + } + + IndexValue to_index_value(ColValue *key) { + if (m_data_type == ColumnType::integer_type) + return key->getIntValue(); + else if (m_data_type == ColumnType::varchar_type) + return key->getStringValue(); + else + throw Exception("using index on unsupported type"); + } + void insert(IndexValue key, rowid_t rowid) { auto search = m_index.find(key); if (search != m_index.end()) { @@ -60,20 +110,8 @@ public: } } - std::vector search(ValueNode * key) { - if (m_data_type == ColumnType::integer_type) - return search(key->getIntegerValue()); - else if (m_data_type == ColumnType::varchar_type) - return search(key->getStringValue()); - else - throw Exception("using index on unsupported type"); - } - void truncate() { - m_index.clear(); - } - // void dump() { // std::for_each(m_index.begin(), m_index.end(), // [](std::pair> element){ @@ -83,25 +121,13 @@ public: // }); // } - [[nodiscard]] const std::string &get_column_name() const { - return m_column_name; - } - - [[nodiscard]] const std::string &get_index_name() const { - return m_index_name; - } - - [[nodiscard]] ColumnType get_data_type() const { - return m_data_type; - } - private: - bool m_uniq; - std::string m_index_name; - std::string m_column_name; - ColumnType m_data_type; + bool m_uniq; + std::string m_index_name; + std::string m_column_name; + ColumnType m_data_type; std::map > m_index; }; -} // namespace \ No newline at end of file +} // namespace \ No newline at end of file diff --git a/table.cpp b/table.cpp index a8cae48..80bd1b2 100644 --- a/table.cpp +++ b/table.cpp @@ -282,23 +282,11 @@ 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) { - 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)"); - } + index.insert(reinterpret_cast(&row[col_def.order]), 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) { - index.remove(row[col_def.order].getStringValue(), rowid); - } else { - throw Exception("implement me! Table::index_row(const Row &row)"); - } + index.remove(reinterpret_cast(&row[col_def.order]), rowid); } void Table::reindex_row(Index &index, const ColDefNode &col_def, const Row &old_row, const Row &new_row, size_t rowid) {