some code refactoring

This commit is contained in:
2021-12-18 10:53:43 +01:00
parent d645471c15
commit 7e8d750f63
7 changed files with 76 additions and 83 deletions

31
index.h
View File

@@ -1,6 +1,7 @@
#pragma once
#include "exception.h"
#include "parser.h"
#include <iostream>
#include <utility>
@@ -10,19 +11,19 @@
namespace usql {
enum class IndexedDataType {
integer,
string
};
using rowid_t = size_t; // int is now enough but size_t is correct
static const int k_default_rowids_size = 16;
template<typename K>
class Index {
public:
Index(std::string index_name, std::string col_name, IndexedDataType type) :
Index(std::string index_name, std::string col_name, ColumnType type) :
m_index_name(std::move(index_name)), m_column_name(std::move(col_name)),
m_data_type(type), m_uniq(false) {}
m_data_type(type), m_uniq(false) {
if (type != ColumnType::integer_type && type != ColumnType::varchar_type)
throw Exception("creating index on unsupported type");
}
void insert(K key, rowid_t rowid) {
auto search = m_index.find(key);
@@ -34,7 +35,7 @@ public:
} else {
std::vector<rowid_t> rowids{rowid};
if (!m_uniq)
rowids.reserve(8);
rowids.reserve(k_default_rowids_size);
m_index[key] = rowids;
}
}
@@ -57,6 +58,16 @@ public:
}
}
std::vector<rowid_t> 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();
}
@@ -78,7 +89,7 @@ public:
return m_index_name;
}
[[nodiscard]] IndexedDataType get_data_type() const {
[[nodiscard]] ColumnType get_data_type() const {
return m_data_type;
}
@@ -86,7 +97,7 @@ private:
bool m_uniq;
std::string m_index_name;
std::string m_column_name;
IndexedDataType m_data_type;
ColumnType m_data_type;
std::map<K, std::vector<rowid_t> > m_index;
};