2021-10-11 22:05:45 +08:00
|
|
|
//
|
2021-11-08 20:03:38 +08:00
|
|
|
// Created by YuhangQ on 2021/11/7.
|
2021-10-11 22:05:45 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "collection.h"
|
2021-10-23 16:28:57 +08:00
|
|
|
|
2021-11-01 10:35:16 +00:00
|
|
|
// age=1 age=“hello”
|
2021-11-01 15:11:33 +08:00
|
|
|
void Collection::indexJSON(const std::string prefix, const nlohmann::json &json, const int& address) {
|
|
|
|
// even easier with structured bindings (C++17)
|
|
|
|
for (auto& [key, value] : json.items()) {
|
2021-11-03 22:05:13 +08:00
|
|
|
//std::cout << prefix << key << " : " << value << "\n";
|
|
|
|
if(key == "__INVO_ID__") continue;
|
|
|
|
|
2021-11-01 15:11:33 +08:00
|
|
|
if(value.is_boolean()) insertIndex(prefix + key, value.get<bool>(), address);
|
2021-11-03 22:05:13 +08:00
|
|
|
if(value.is_number()) insertIndex(prefix + key, value.get<double>(), address);
|
|
|
|
if(value.is_string()) insertIndex(prefix + key, value.get<std::string>(), address);
|
2021-11-08 20:03:38 +08:00
|
|
|
if(value.is_object()) indexJSON(prefix + key + ".", value.get<nlohmann::json>(),address);
|
2021-11-01 15:11:33 +08:00
|
|
|
if(value.is_array()) {
|
|
|
|
for(auto& element : value.get<nlohmann::json>()) {
|
|
|
|
if(element.is_string()) insertIndex(prefix + key, element.get<std::string>(), address);
|
|
|
|
if(element.is_number()) insertIndex(prefix + key, element.get<double>(), address);
|
|
|
|
if(element.is_boolean()) insertIndex(prefix + key, element.get<bool>(), address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Collection::insertIndex(const std::string indexName, const std::string indexValue, const int &address) {
|
|
|
|
|
|
|
|
std::string treeName = indexName + "$string";
|
2021-11-03 22:05:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
//printf("INDEX TO %s: %s = \"%s\" add:(%d)\n", treeName.c_str(), indexName.c_str(), indexValue.c_str(), address);
|
|
|
|
|
2021-11-01 15:11:33 +08:00
|
|
|
if(!index->exists(treeName)) {
|
|
|
|
index->insert(treeName, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
2021-11-08 20:03:38 +08:00
|
|
|
BTree<std::string, 64> indexTree(index->find(treeName));
|
2021-11-01 15:11:33 +08:00
|
|
|
|
|
|
|
if(!indexTree.exists(indexValue)) {
|
|
|
|
indexTree.insert(indexValue, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int, 4> list(indexTree.find(indexValue));
|
|
|
|
list.insert(address);
|
2021-11-03 22:05:13 +08:00
|
|
|
//printf("INSERT %d INTO %d\n", address, indexTree.find(indexValue));
|
|
|
|
//list.print();
|
2021-11-01 15:11:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Collection::insertIndex(const std::string indexName, double indexValue, const int &address) {
|
2021-11-03 22:05:13 +08:00
|
|
|
//printf("INDEX: %s = %f add:(%d)\n", indexName.c_str(), indexValue, address);
|
2021-11-01 15:11:33 +08:00
|
|
|
|
|
|
|
std::string treeName = indexName + "$number";
|
|
|
|
if(!index->exists(treeName)) {
|
|
|
|
index->insert(treeName, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
BTree<double, 8> indexTree(index->find(treeName));
|
|
|
|
|
|
|
|
if(!indexTree.exists(indexValue)) {
|
|
|
|
indexTree.insert(indexValue, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int, 4> list(indexTree.find(indexValue));
|
|
|
|
list.insert(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Collection::insertIndex(const std::string indexName, bool indexValue, const int &address) {
|
2021-11-03 22:05:13 +08:00
|
|
|
//printf("INDEX: %s = %s add:(%d)\n", indexName.c_str(), indexValue ? "true" : "false", address);
|
2021-11-01 15:11:33 +08:00
|
|
|
|
|
|
|
std::string treeName = indexName + "$boolean";
|
|
|
|
if(!index->exists(treeName)) {
|
|
|
|
index->insert(treeName, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
BTree<bool, 1> indexTree(index->find(treeName));
|
|
|
|
|
|
|
|
if(!indexTree.exists(indexValue)) {
|
|
|
|
indexTree.insert(indexValue, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int, 4> list(indexTree.find(indexValue));
|
|
|
|
list.insert(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Collection::clearIndex(const std::string prefix, const nlohmann::json &json, const int &address) {
|
|
|
|
for (auto& [key, value] : json.items()) {
|
|
|
|
std::cout << prefix << key << " : " << value << "\n";
|
|
|
|
if(value.is_string()) removeIndex(prefix + key, value.get<std::string>(), address);
|
|
|
|
if(value.is_number()) removeIndex(prefix + key, value.get<double>(), address);
|
|
|
|
if(value.is_boolean()) removeIndex(prefix + key, value.get<bool>(), address);
|
|
|
|
if(value.is_object()) clearIndex(prefix + key + ".", value.get<nlohmann::json>(),address);
|
|
|
|
if(value.is_array()) {
|
|
|
|
for(auto& element : value.get<nlohmann::json>()) {
|
|
|
|
if(element.is_string()) removeIndex(prefix + key, element.get<std::string>(), address);
|
|
|
|
if(element.is_number()) removeIndex(prefix + key, element.get<double>(), address);
|
|
|
|
if(element.is_boolean()) removeIndex(prefix + key, element.get<bool>(), address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Collection::removeIndex(const std::string indexName, const std::string indexValue, const int &address) {
|
2021-11-03 22:05:13 +08:00
|
|
|
//printf("REMOVE: %s = \"%s\" add:(%d)\n", indexName.c_str(), indexValue.c_str(), address);
|
2021-11-01 15:11:33 +08:00
|
|
|
|
|
|
|
std::string treeName = indexName + "$string";
|
|
|
|
if(!index->exists(treeName)) {
|
|
|
|
index->insert(treeName, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
BTree<std::string, 128> indexTree(index->find(treeName));
|
|
|
|
|
|
|
|
if(!indexTree.exists(indexValue)) {
|
|
|
|
indexTree.insert(indexValue, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int, 4> list(indexTree.find(indexValue));
|
|
|
|
list.remove(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Collection::removeIndex(const std::string indexName, double indexValue, const int &address) {
|
2021-11-03 22:05:13 +08:00
|
|
|
//printf("REMOVE: %s = %f add:(%d)\n", indexName.c_str(), indexValue, address);
|
2021-11-01 15:11:33 +08:00
|
|
|
|
|
|
|
std::string treeName = indexName + "$number";
|
|
|
|
if(!index->exists(treeName)) {
|
|
|
|
index->insert(treeName, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
BTree<double, 8> indexTree(index->find(treeName));
|
|
|
|
|
|
|
|
if(!indexTree.exists(indexValue)) {
|
|
|
|
indexTree.insert(indexValue, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int, 4> list(indexTree.find(indexValue));
|
|
|
|
list.remove(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Collection::removeIndex(const std::string indexName, bool indexValue, const int &address) {
|
2021-11-03 22:05:13 +08:00
|
|
|
//printf("REMOVE: %s = %s add:(%d)\n", indexName.c_str(), indexValue ? "true" : "false", address);
|
2021-11-01 15:11:33 +08:00
|
|
|
|
|
|
|
std::string treeName = indexName + "$boolean";
|
|
|
|
if(!index->exists(treeName)) {
|
|
|
|
index->insert(treeName, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
BTree<bool, 1> indexTree(index->find(treeName));
|
|
|
|
|
|
|
|
if(!indexTree.exists(indexValue)) {
|
|
|
|
indexTree.insert(indexValue, PageManager::Instance().allocate());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<int, 4> list(indexTree.find(indexValue));
|
|
|
|
list.remove(address);
|
2021-11-08 20:03:38 +08:00
|
|
|
}
|