96 lines
2.2 KiB
C++
96 lines
2.2 KiB
C++
#pragma once
|
|
|
|
|
|
#include <iostream>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include <map>
|
|
|
|
enum class IndexedDataType {
|
|
integer,
|
|
string
|
|
};
|
|
|
|
|
|
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, int rowid) {
|
|
// std::cout << "inserting key: " << key << " val: " << rowid << std::endl;
|
|
|
|
// TODO handle uniqueness
|
|
auto search = m_index.find(key);
|
|
if (search != m_index.end()) {
|
|
search->second.push_back(rowid);
|
|
} else {
|
|
std::vector<int> rowids{rowid};
|
|
m_index[key] = rowids;
|
|
}
|
|
}
|
|
|
|
void update(K old_key, K new_key, int rowid) {
|
|
// std::cout << "updating key: " << old_key << " to: " << new_key << " val: " << rowid << std::endl;
|
|
|
|
// TODO handle uniqueness
|
|
}
|
|
|
|
void remove(K key, int rowid) {
|
|
// std::cout << "removing key: " << key << " val: " << rowid << std::endl;
|
|
|
|
auto search = m_index.find(key);
|
|
if (search != m_index.end()) {
|
|
search->second.erase(find(search->second.begin(), search->second.end(), rowid));
|
|
}
|
|
}
|
|
|
|
std::vector<int> search(K key) {
|
|
// std::cout << "returning rowids for key: " << key << std::endl;
|
|
|
|
auto search = m_index.find(key);
|
|
if (search != m_index.end()) {
|
|
return search->second;
|
|
} else {
|
|
return std::vector<int>{};
|
|
}
|
|
}
|
|
|
|
void truncate() {
|
|
// std::cout << "truncating" << std::endl;
|
|
m_index.clear();
|
|
}
|
|
|
|
void dump() {
|
|
std::for_each(m_index.begin(), m_index.end(),
|
|
[](std::pair<K, std::vector<int>> element){
|
|
K key = element.first;
|
|
std::vector<int> 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;
|
|
|
|
// DEBUG for debug it is public
|
|
public:
|
|
std::map<K, std::vector<int> > m_index;
|
|
}; |