119 lines
2.8 KiB
C++
119 lines
2.8 KiB
C++
#pragma once
|
|
|
|
#include "exception.h"
|
|
#include "parser.h"
|
|
#include "row.h"
|
|
|
|
#include <iostream>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include <variant>
|
|
#include <map>
|
|
|
|
|
|
namespace usql {
|
|
|
|
using IndexValue = std::variant<long, std::string>;
|
|
using rowid_t = size_t; // int is now enough but size_t is correct
|
|
|
|
static const int k_default_rowids_size = 16;
|
|
|
|
|
|
class Index {
|
|
public:
|
|
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) {
|
|
if (type != ColumnType::integer_type && type != ColumnType::varchar_type)
|
|
throw Exception("creating index on unsupported type");
|
|
}
|
|
|
|
|
|
std::vector<rowid_t> search(const ValueNode *key) {
|
|
return search(to_index_value(key));
|
|
}
|
|
|
|
void insert(const ColValue *key, rowid_t rowid) {
|
|
return insert(to_index_value(key), rowid);
|
|
}
|
|
|
|
void remove(const 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;
|
|
}
|
|
|
|
|
|
private:
|
|
IndexValue to_index_value(const 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(const ColValue *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");
|
|
}
|
|
|
|
void insert(const IndexValue& key, rowid_t rowid) {
|
|
auto search = m_index.find(key);
|
|
if (search != m_index.end()) {
|
|
if (m_uniq)
|
|
throw Exception("Inserting duplicate value into unique index");
|
|
|
|
search->second.push_back(rowid);
|
|
} else {
|
|
std::vector<rowid_t> rowids{rowid};
|
|
if (!m_uniq)
|
|
rowids.reserve(k_default_rowids_size);
|
|
m_index[key] = rowids;
|
|
}
|
|
}
|
|
|
|
void remove(const 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));
|
|
if (search->second.empty())
|
|
m_index.erase(search);
|
|
}
|
|
}
|
|
|
|
std::vector<rowid_t> search(const IndexValue& key) {
|
|
auto search = m_index.find(key);
|
|
if (search != m_index.end()) {
|
|
return search->second;
|
|
} else {
|
|
return std::vector<rowid_t>{};
|
|
}
|
|
}
|
|
|
|
|
|
private:
|
|
std::string m_index_name;
|
|
std::string m_column_name;
|
|
ColumnType m_data_type;
|
|
bool m_uniq;
|
|
|
|
std::map<IndexValue, std::vector<rowid_t> > m_index;
|
|
};
|
|
|
|
} // namespace
|