94 lines
2.0 KiB
C++
94 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include "exception.h"
|
|
|
|
#include <iostream>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include <map>
|
|
|
|
|
|
namespace usql {
|
|
|
|
enum class IndexedDataType {
|
|
integer,
|
|
string
|
|
};
|
|
|
|
using rowid_t = size_t; // int is now enough but size_t is correct
|
|
|
|
template<typename K>
|
|
class Index {
|
|
public:
|
|
Index(std::string index_name, std::string col_name, IndexedDataType type) :
|
|
m_index_name(std::move(index_name)), m_column_name(std::move(col_name)),
|
|
m_data_type(type), m_uniq(false) {}
|
|
|
|
void insert(K 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(8);
|
|
m_index[key] = rowids;
|
|
}
|
|
}
|
|
|
|
void remove(K 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(K key) {
|
|
auto search = m_index.find(key);
|
|
if (search != m_index.end()) {
|
|
return search->second;
|
|
} else {
|
|
return std::vector<rowid_t>{};
|
|
}
|
|
}
|
|
|
|
void truncate() {
|
|
m_index.clear();
|
|
}
|
|
|
|
// void dump() {
|
|
// std::for_each(m_index.begin(), m_index.end(),
|
|
// [](std::pair<K, std::vector<rowid_t>> element){
|
|
// K key = element.first;
|
|
// std::vector<rowid_t> rowids = element.second;
|
|
// std::cout << "key: " << key << ", rowids count:" << rowids.size() << std::endl;
|
|
// });
|
|
// }
|
|
|
|
[[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]] IndexedDataType get_data_type() const {
|
|
return m_data_type;
|
|
}
|
|
|
|
private:
|
|
bool m_uniq;
|
|
std::string m_index_name;
|
|
std::string m_column_name;
|
|
IndexedDataType m_data_type;
|
|
|
|
std::map<K, std::vector<rowid_t> > m_index;
|
|
};
|
|
|
|
} // namespace
|