index refactoring
This commit is contained in:
3
.idea/.gitignore
generated
vendored
3
.idea/.gitignore
generated
vendored
@@ -1,3 +0,0 @@
|
|||||||
# Default ignored files
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
4
.idea/misc.xml
generated
4
.idea/misc.xml
generated
@@ -1,4 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
|
||||||
</project>
|
|
||||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="ProjectModuleManager">
|
|
||||||
<modules>
|
|
||||||
<module fileurl="file://$PROJECT_DIR$/.idea/msql.iml" filepath="$PROJECT_DIR$/.idea/msql.iml" />
|
|
||||||
</modules>
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
2
.idea/msql.iml
generated
2
.idea/msql.iml
generated
@@ -1,2 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
|
||||||
6
.idea/vcs.xml
generated
6
.idea/vcs.xml
generated
@@ -1,6 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
86
index.h
86
index.h
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "exception.h"
|
#include "exception.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "row.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -12,7 +13,7 @@
|
|||||||
|
|
||||||
namespace usql {
|
namespace usql {
|
||||||
|
|
||||||
using IndexValue=std::variant<long, std::string>;
|
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;
|
||||||
@@ -27,6 +28,55 @@ public:
|
|||||||
throw Exception("creating index on unsupported type");
|
throw Exception("creating index on unsupported type");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::vector<rowid_t> search(ValueNode *key) {
|
||||||
|
return search(to_index_value(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert(ColValue *key, rowid_t rowid) {
|
||||||
|
return insert(to_index_value(key), rowid);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] ColumnType get_data_type() const {
|
||||||
|
return m_data_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
IndexValue to_index_value(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(ColValue *key) {
|
||||||
|
if (m_data_type == ColumnType::integer_type)
|
||||||
|
return key->getIntValue();
|
||||||
|
else if (m_data_type == ColumnType::varchar_type)
|
||||||
|
return key->getStringValue();
|
||||||
|
else
|
||||||
|
throw Exception("using index on unsupported type");
|
||||||
|
}
|
||||||
|
|
||||||
void insert(IndexValue 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()) {
|
||||||
@@ -60,20 +110,8 @@ 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// void dump() {
|
// void dump() {
|
||||||
// std::for_each(m_index.begin(), m_index.end(),
|
// std::for_each(m_index.begin(), m_index.end(),
|
||||||
// [](std::pair<IndexValue, std::vector<rowid_t>> element){
|
// [](std::pair<IndexValue, std::vector<rowid_t>> element){
|
||||||
@@ -83,25 +121,13 @@ public:
|
|||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
[[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]] ColumnType get_data_type() const {
|
|
||||||
return m_data_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_uniq;
|
bool m_uniq;
|
||||||
std::string m_index_name;
|
std::string m_index_name;
|
||||||
std::string m_column_name;
|
std::string m_column_name;
|
||||||
ColumnType m_data_type;
|
ColumnType m_data_type;
|
||||||
|
|
||||||
std::map<IndexValue, std::vector<rowid_t> > m_index;
|
std::map<IndexValue, std::vector<rowid_t> > m_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
16
table.cpp
16
table.cpp
@@ -282,23 +282,11 @@ bool Table::drop_index(const std::string &index_name) {
|
|||||||
|
|
||||||
|
|
||||||
void Table::index_row(Index &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) {
|
index.insert(reinterpret_cast<ColValue *>(&row[col_def.order]), rowid);
|
||||||
index.insert(row[col_def.order].getIntValue(), rowid);
|
|
||||||
} else if (col_def.type==ColumnType::varchar_type) {
|
|
||||||
index.insert(row[col_def.order].getStringValue(), rowid);
|
|
||||||
} else {
|
|
||||||
throw Exception("implement me! Table::index_row(const Row &row)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Table::unindex_row(Index &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) {
|
index.remove(reinterpret_cast<ColValue *>(&row[col_def.order]), rowid);
|
||||||
index.remove(row[col_def.order].getIntValue(), rowid);
|
|
||||||
} else if (col_def.type==ColumnType::varchar_type) {
|
|
||||||
index.remove(row[col_def.order].getStringValue(), rowid);
|
|
||||||
} else {
|
|
||||||
throw Exception("implement me! Table::index_row(const Row &row)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Table::reindex_row(Index &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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user