do not use template for index

This commit is contained in:
2021-12-18 11:10:18 +01:00
parent 7e8d750f63
commit 7a6a9e209a
4 changed files with 28 additions and 26 deletions

16
index.h
View File

@@ -6,16 +6,18 @@
#include <iostream> #include <iostream>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <variant>
#include <map> #include <map>
namespace usql { namespace usql {
using IndexValue=std::variant<long, std::string>;
using rowid_t = size_t; // int is now enough but size_t is correct using rowid_t = size_t; // int is now enough but size_t is correct
static const int k_default_rowids_size = 16; static const int k_default_rowids_size = 16;
template<typename K>
class Index { class Index {
public: public:
Index(std::string index_name, std::string col_name, ColumnType type) : Index(std::string index_name, std::string col_name, ColumnType type) :
@@ -25,7 +27,7 @@ public:
throw Exception("creating index on unsupported type"); 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); auto search = m_index.find(key);
if (search != m_index.end()) { if (search != m_index.end()) {
if (m_uniq) 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); auto search = m_index.find(key);
if (search != m_index.end()) { if (search != m_index.end()) {
search->second.erase(find(search->second.begin(), search->second.end(), rowid)); search->second.erase(find(search->second.begin(), search->second.end(), rowid));
@@ -49,7 +51,7 @@ public:
} }
} }
std::vector<rowid_t> search(K key) { std::vector<rowid_t> search(IndexValue key) {
auto search = m_index.find(key); auto search = m_index.find(key);
if (search != m_index.end()) { if (search != m_index.end()) {
return search->second; return search->second;
@@ -74,8 +76,8 @@ public:
// void dump() { // void dump() {
// std::for_each(m_index.begin(), m_index.end(), // std::for_each(m_index.begin(), m_index.end(),
// [](std::pair<K, std::vector<rowid_t>> element){ // [](std::pair<IndexValue, std::vector<rowid_t>> element){
// K key = element.first; // IndexValue key = element.first;
// std::vector<rowid_t> rowids = element.second; // std::vector<rowid_t> rowids = element.second;
// std::cout << "key: " << key << ", rowids count:" << rowids.size() << std::endl; // std::cout << "key: " << key << ", rowids count:" << rowids.size() << std::endl;
// }); // });
@@ -99,7 +101,7 @@ private:
std::string m_column_name; std::string m_column_name;
ColumnType m_data_type; ColumnType m_data_type;
std::map<K, std::vector<rowid_t> > m_index; std::map<IndexValue, std::vector<rowid_t> > m_index;
}; };
} // namespace } // namespace

View File

@@ -263,13 +263,13 @@ void Table::validate_row(Row &row) {
row.set_visible(); row.set_visible();
} }
void Table::create_index(const Index<IndexValue>& index) { void Table::create_index(const Index& index) {
m_indexes.push_back(index); m_indexes.push_back(index);
} }
bool Table::drop_index(const std::string &index_name) { bool Table::drop_index(const std::string &index_name) {
auto it = std::find_if(m_indexes.begin(), m_indexes.end(), auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
[&index_name](const Index<IndexValue> &idx) { [&index_name](const Index &idx) {
return idx.get_index_name() == index_name; 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<IndexValue> &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) { if (col_def.type==ColumnType::integer_type) {
index.insert(row[col_def.order].getIntValue(), rowid); index.insert(row[col_def.order].getIntValue(), rowid);
} else if (col_def.type==ColumnType::varchar_type) { } else if (col_def.type==ColumnType::varchar_type) {
@@ -291,7 +291,7 @@ void Table::index_row(Index<IndexValue> &index, const ColDefNode &col_def, const
} }
} }
void Table::unindex_row(Index<IndexValue> &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) { if (col_def.type==ColumnType::integer_type) {
index.remove(row[col_def.order].getIntValue(), rowid); index.remove(row[col_def.order].getIntValue(), rowid);
} else if (col_def.type==ColumnType::varchar_type) { } else if (col_def.type==ColumnType::varchar_type) {
@@ -301,7 +301,7 @@ void Table::unindex_row(Index<IndexValue> &index, const ColDefNode &col_def, con
} }
} }
void Table::reindex_row(Index<IndexValue> &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); unindex_row(index, col_def, old_row, rowid);
index_row(index, col_def, new_row, rowid); index_row(index, col_def, new_row, rowid);
} }
@@ -350,18 +350,18 @@ void Table::index_rows(const std::string &index_name) {
} }
Index<IndexValue> * 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(), auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
[&index_name](const Index<IndexValue> &idx) { [&index_name](const Index &idx) {
return idx.get_index_name() == index_name; return idx.get_index_name() == index_name;
}); });
return (it != m_indexes.end()) ? &(*it) : nullptr; return (it != m_indexes.end()) ? &(*it) : nullptr;
} }
Index<IndexValue> * 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(), auto it = std::find_if(m_indexes.begin(), m_indexes.end(),
[&col_name](const Index<IndexValue> &idx) { [&col_name](const Index &idx) {
return idx.get_column_name() == col_name; return idx.get_column_name() == col_name;
}); });

14
table.h
View File

@@ -46,19 +46,19 @@ struct Table {
std::string m_name; std::string m_name;
std::vector<ColDefNode> m_col_defs; std::vector<ColDefNode> m_col_defs;
std::vector<Row> m_rows; std::vector<Row> m_rows;
std::vector<Index<IndexValue>> m_indexes; std::vector<Index> m_indexes;
static long string_to_long(const std::string &s); static long string_to_long(const std::string &s);
static double string_to_double(const std::string &s); static double string_to_double(const std::string &s);
void create_row_from_vector(const std::vector<ColDefNode> &colDefs, const std::vector<std::string> &csv_line); void create_row_from_vector(const std::vector<ColDefNode> &colDefs, const std::vector<std::string> &csv_line);
void create_index(const Index<IndexValue>& index); void create_index(const Index& index);
bool drop_index(const std::string &index_name); bool drop_index(const std::string &index_name);
static void index_row(Index<IndexValue> &index, const ColDefNode &col_def, const Row &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<IndexValue> &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<IndexValue> &index, const ColDefNode &col_def, const Row &old_row, const Row &new_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 index_row(const Row &row);
void unindex_row(const Row &row); void unindex_row(const Row &row);
@@ -66,8 +66,8 @@ struct Table {
void index_rows(const std::string &index_name); void index_rows(const std::string &index_name);
Index<IndexValue> * get_index(const std::string &index_name); Index * get_index(const std::string &index_name);
Index<IndexValue> * get_index_for_column(const std::string &col_name); Index * get_index_for_column(const std::string &col_name);
struct rows_scanner { struct rows_scanner {

View File

@@ -29,7 +29,7 @@ std::pair<bool, std::vector<rowid_t>> USql::look_for_usable_index(const Node *wh
) { ) {
auto col_name = ((DatabaseValueNode *)ron->left.get())->col_name; auto col_name = ((DatabaseValueNode *)ron->left.get())->col_name;
Index<IndexValue> * used_index = table->get_index_for_column(col_name); Index * used_index = table->get_index_for_column(col_name);
if (used_index != nullptr) { if (used_index != nullptr) {
std::vector<rowid_t> rowids = used_index->search((ValueNode *)ron->right.get()); std::vector<rowid_t> rowids = used_index->search((ValueNode *)ron->right.get());
#ifndef NDEBUG #ifndef NDEBUG