From 6c538caaab562388f1012623eb15b8313f74e2de Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Mon, 8 Nov 2021 20:03:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- invodb/btree/btree.h | 14 +- invodb/btree/node.h | 18 +- invodb/collection/collection.cpp | 94 +++++ invodb/{models => collection}/collection.h | 12 +- .../collection.cpp => collection/index.cpp} | 96 +---- invodb/collection/query.cpp | 371 ++++++++++++++++++ invodb/file/page_manager.h | 2 +- invodb/index/index.cpp | 5 - invodb/index/index.h | 25 -- invodb/index/query.cpp | 9 - invodb/index/query.h | 16 - invodb/main.cpp | 72 ++-- invodb/main.h | 2 +- invodb/{models => utils}/cache.h | 0 json/json.hpp | 6 +- 16 files changed, 558 insertions(+), 186 deletions(-) create mode 100644 invodb/collection/collection.cpp rename invodb/{models => collection}/collection.h (67%) rename invodb/{models/collection.cpp => collection/index.cpp} (68%) create mode 100644 invodb/collection/query.cpp delete mode 100644 invodb/index/index.cpp delete mode 100644 invodb/index/index.h delete mode 100644 invodb/index/query.cpp delete mode 100644 invodb/index/query.h rename invodb/{models => utils}/cache.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d741eb3..7575c27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,4 +11,4 @@ include_directories(./invodb) add_executable(InvoDB invodb/main.cpp - invodb/main.h invodb/file/page_manager.cpp invodb/file/page_manager.h invodb/models/collection.cpp invodb/models/collection.h invodb/file/storage_page.cpp invodb/file/storage_page.h invodb/utils/logger.h invodb/utils/uuid.h invodb/btree/node.h invodb/btree/btree.h invodb/index/index.cpp invodb/index/index.h invodb/btree/list.h invodb/index/query.cpp invodb/index/query.h invodb/models/cache.h) + invodb/main.h invodb/file/page_manager.cpp invodb/file/page_manager.h invodb/collection/collection.cpp invodb/collection/collection.h invodb/file/storage_page.cpp invodb/file/storage_page.h invodb/utils/logger.h invodb/utils/uuid.h invodb/btree/node.h invodb/btree/btree.h invodb/btree/list.h invodb/collection/query.cpp invodb/utils/cache.h invodb/collection/index.cpp) diff --git a/invodb/btree/btree.h b/invodb/btree/btree.h index 443d38f..5e84deb 100644 --- a/invodb/btree/btree.h +++ b/invodb/btree/btree.h @@ -8,14 +8,18 @@ #include "btree/node.h" #include "utils/uuid.h" +#define M_SIZE 1000 / (K_SIZE + 4) + template class BTree { public: + + static std::shared_ptr> getNode(const int &index); + BTree(const int& address); void insert(const KT &key, const int &value); void update(const KT &key, const int &value); void remove(const KT &key); - bool exists(const KT &key); int getNodeSize(); int find(const KT &key); @@ -37,9 +41,15 @@ private: void insertInternal(const KT &key, int curAdd, int lLeafAdd, int rLeafAdd); int root; int n_size; - static const int M_SIZE = 1000 / (K_SIZE + 4); + }; + +template +std::shared_ptr> BTree::getNode(const int &index) { + return BTreeNode::getNode(index); +} + template BTree::BTree(const int& address) { root = address; diff --git a/invodb/btree/node.h b/invodb/btree/node.h index 5145183..83f5622 100644 --- a/invodb/btree/node.h +++ b/invodb/btree/node.h @@ -14,7 +14,7 @@ #include #include #include "file/page_manager.h" -#include "invodb/models/cache.h" +#include "invodb/utils/cache.h" template class BTreeNode { @@ -37,6 +37,7 @@ public: int save(); bool enough(); bool full(); + void print(); KT keySet[m + 1]; int linkSet[m + 1]; @@ -232,4 +233,19 @@ bool BTreeNode::full() { return size == maxCount; } +template +void BTreeNode::print() { + + printf("----Node: %d-----\n", address); + + for(int i=0; i Collection::colList(1); +std::map Collection::map; + +void Collection::loadCollections() { + int cnt = 0; + for(auto& key : colList.keySet()) { + map.insert(std::make_pair(key, new Collection(key, colList.find(key)))); + cnt++; + } + Logger::info("Successfully load Collections: ", cnt); +} + +Collection& Collection::createCollection(const std::string &name) { + // exist + if(map.count(name) != 0) { + return *map[name]; + } + colList.insert(name, PageManager::Instance().allocate()); + Collection *col = new Collection(name, colList.find(name)); + map.insert(make_pair(name, col)); + return *col; +} + +Collection &Collection::getCollection(const std::string &name) { + if(map.count(name) == 0) { + throw "no such collection"; + } + return *map[name]; +} + +Collection::Collection(const std::string &name, const int &firstPage) { + Logger::info("load Collection: ", name); + index = new BTree(firstPage); + if(!index->exists("__INVO_ID__")) { + index->insert("__INVO_ID__", PageManager::Instance().allocate()); + } + uuid = new BTree(index->find("__INVO_ID__")); +} + +void Collection::insert(nlohmann::json &json) { + + if(json["__INVO_ID__"].empty()) { + json["__INVO_ID__"] = generateUUID(); + } else { + remove(json); + } + + std::string id = json["__INVO_ID__"].get(); + int add = PageManager::Instance().saveJSONToFile(json); + uuid->insert(id, add); + + //Logger::info("INSERT ", json.dump()); + + // add index + indexJSON("", json, add); +} + +void Collection::remove(const nlohmann::json &json) { + if(json["__INVO_ID__"].empty()) { + throw "no invo_id"; + } + std::string id = json["__INVO_ID__"].get(); + + + int address = uuid->find(id); + uuid->remove(id); + + nlohmann::json jsonInDisk = PageManager::Instance().readJSONFromFile(address); + + clearIndex("", json, address); + + PageManager::Instance().release(address); +} + + +void Collection::test() { + index->print(); + auto qq = new BTree(7); + while(true) { + std::string q; + std::cin >> q; + List list(qq->find(q)); + //list.print(); + for(auto& add : list.all()) { + std::cout << ">> " << PageManager::Instance().readJSONFromFile(add).dump() << std::endl; + } + } +} diff --git a/invodb/models/collection.h b/invodb/collection/collection.h similarity index 67% rename from invodb/models/collection.h rename to invodb/collection/collection.h index 2459499..a7bf000 100644 --- a/invodb/models/collection.h +++ b/invodb/collection/collection.h @@ -20,7 +20,7 @@ class Collection { public: void insert(nlohmann::json &json); void remove(const nlohmann::json &json); - + std::vector query(const nlohmann::json &json); static void loadCollections(); static Collection& getCollection(const std::string& name); static Collection& createCollection(const std::string& name); @@ -28,7 +28,6 @@ public: void test(); private: - void indexJSON(const std::string prefix, const nlohmann::json &json, const int& address); void insertIndex(const std::string indexName, const std::string indexValue, const int& address); void insertIndex(const std::string indexName, double indexValue, const int& address); @@ -38,6 +37,15 @@ private: void removeIndex(const std::string indexName, double indexValue, const int& address); void removeIndex(const std::string indexName, bool indexValue, const int& address); + std::set setIntersection(const std::set &a, const std::set &b); + std::set setUnion(const std::set &a, const std::set &b); + + std::set innerQuery(const std::string &prefix, const nlohmann::json &json); + std::set queryString(const std::string &prefix, const std::string &minValue, const std::string &maxValue, const int &mod = 0); + std::set queryNumber(const std::string &prefix, const double &minValue, const double &maxValue, const int &mod = 0); + std::set queryBool(const std::string &prefix, const bool &value); + std::set queryRange(const std::string &prefix, const nlohmann::json &json); + static std::map map; static BTree colList; diff --git a/invodb/models/collection.cpp b/invodb/collection/index.cpp similarity index 68% rename from invodb/models/collection.cpp rename to invodb/collection/index.cpp index 590dc84..39d9342 100644 --- a/invodb/models/collection.cpp +++ b/invodb/collection/index.cpp @@ -1,83 +1,9 @@ // -// Created by YuhangQ on 2021/10/9. +// Created by YuhangQ on 2021/11/7. // #include "collection.h" -BTree Collection::colList(1); -std::map Collection::map; - -void Collection::loadCollections() { - int cnt = 0; - for(auto& key : colList.keySet()) { - map.insert(std::make_pair(key, new Collection(key, colList.find(key)))); - cnt++; - } - Logger::info("Successfully load Collections: ", cnt); -} - -Collection& Collection::createCollection(const std::string &name) { - // exist - if(map.count(name) != 0) { - return *map[name]; - } - colList.insert(name, PageManager::Instance().allocate()); - Collection *col = new Collection(name, colList.find(name)); - map.insert(make_pair(name, col)); - return *col; -} - -Collection &Collection::getCollection(const std::string &name) { - if(map.count(name) == 0) { - throw "no such collection"; - } - return *map[name]; -} - -Collection::Collection(const std::string &name, const int &firstPage) { - Logger::info("load Collection: ", name); - index = new BTree(firstPage); - if(!index->exists("__INVO_ID__")) { - index->insert("__INVO_ID__", PageManager::Instance().allocate()); - } - uuid = new BTree(index->find("__INVO_ID__")); -} - -void Collection::insert(nlohmann::json &json) { - - if(json["__INVO_ID__"].empty()) { - json["__INVO_ID__"] = generateUUID(); - } else { - remove(json); - } - - std::string id = json["__INVO_ID__"].get(); - int add = PageManager::Instance().saveJSONToFile(json); - uuid->insert(id, add); - - //Logger::info("INSERT ", json.dump()); - - // add index - indexJSON("", json, add); -} - -void Collection::remove(const nlohmann::json &json) { - if(json["__INVO_ID__"].empty()) { - throw "no invo_id"; - } - std::string id = json["__INVO_ID__"].get(); - - - int address = uuid->find(id); - uuid->remove(id); - - nlohmann::json jsonInDisk = PageManager::Instance().readJSONFromFile(address); - - clearIndex("", json, address); - - PageManager::Instance().release(address); -} - // age=1 age=“hello” void Collection::indexJSON(const std::string prefix, const nlohmann::json &json, const int& address) { // even easier with structured bindings (C++17) @@ -88,7 +14,7 @@ void Collection::indexJSON(const std::string prefix, const nlohmann::json &json, if(value.is_boolean()) insertIndex(prefix + key, value.get(), address); if(value.is_number()) insertIndex(prefix + key, value.get(), address); if(value.is_string()) insertIndex(prefix + key, value.get(), address); - //if(value.is_object()) indexJSON(prefix + key + ".", value.get(),address); + if(value.is_object()) indexJSON(prefix + key + ".", value.get(),address); if(value.is_array()) { for(auto& element : value.get()) { if(element.is_string()) insertIndex(prefix + key, element.get(), address); @@ -110,7 +36,7 @@ void Collection::insertIndex(const std::string indexName, const std::string inde index->insert(treeName, PageManager::Instance().allocate()); } - BTree indexTree(index->find(treeName)); + BTree indexTree(index->find(treeName)); if(!indexTree.exists(indexValue)) { indexTree.insert(indexValue, PageManager::Instance().allocate()); @@ -227,18 +153,4 @@ void Collection::removeIndex(const std::string indexName, bool indexValue, const List list(indexTree.find(indexValue)); list.remove(address); -} - -void Collection::test() { - index->print(); - auto qq = new BTree(7); - while(true) { - std::string q; - std::cin >> q; - List list(qq->find(q)); - //list.print(); - for(auto& add : list.all()) { - std::cout << ">> " << PageManager::Instance().readJSONFromFile(add).dump() << std::endl; - } - } -} +} \ No newline at end of file diff --git a/invodb/collection/query.cpp b/invodb/collection/query.cpp new file mode 100644 index 0000000..914511f --- /dev/null +++ b/invodb/collection/query.cpp @@ -0,0 +1,371 @@ +// +// Created by projector-user on 11/1/21. +// + +#include "collection.h" + +std::vector Collection::query(const nlohmann::json &json) { + std::vector res; + + innerQuery("", json); + + return res; +} + +/* + * range search mod explain + * mod = 0: minValue <= value <= maxValue + * mod = 1: minValue < value <= maxValue + * mod = 2: minValue <= value < maxValue + * mod = 3: minValue < value < maxValue + * mod = 4: value < maxValue + * mod = 5: value <= maxValue + * mod = 6: value > minValue + * mod = 7: value >= minValue + */ + +std::set Collection::queryRange(const std::string &prefix, const nlohmann::json &json) { + + printf(">> queryRange prefix: %s query: %s\n", prefix.c_str(), json.dump().c_str()); + + std::set set; + + for(auto& [key, value] : json.items()) { + if(value.is_number()) { + + if(json.contains("$ne")) { + if(!index->exists(prefix+"$number")) { + return set; + } + BTree tree(index->find(prefix+"$number")); + for(auto& key: tree.keySet()) { + if(key != json["$ne"].get()) continue; + List list(tree.find(key)); + for(auto& add : list.all()) { + set.insert(PageManager::Instance().readJSONFromFile(add)); + } + } + } + else if(json.contains("$gt")) { + if(json.contains("$lt")) { + set = queryNumber(prefix, json["$gt"].get(), json["$lt"].get(), 3); + } else if(json.contains("$lte")) { + set = queryNumber(prefix, json["$gt"].get(), json["$lte"].get(), 1); + } else { + + set = queryNumber(prefix, json["$gt"].get(), 0, 6); + + } + } else if(json.contains("$gte")) { + if(json.contains("$lt")) { + set = queryNumber(prefix, json["$gte"].get(), json["$lt"].get(), 2); + } else if(json.contains("$lte")) { + set = queryNumber(prefix, json["$gte"].get(), json["$lte"].get(), 0); + } else { + set = queryNumber(prefix, json["$gte"].get(), 0, 7); + } + } else if(json.contains("$lt")) { + set = queryNumber(prefix, 0, json["$lt"].get(), 4); + } else if(json.contains("$lte")) { + set = queryNumber(prefix, 0, json["$lte"].get(), 5); + } + return set; + } + else if(value.is_string()) { + + if(json.contains("$ne")) { + if(!index->exists(prefix+"$string")) { + return set; + } + BTree tree(index->find(prefix+"$string")); + for(auto& key: tree.keySet()) { + if(key != json["$ne"].get()) continue; + List list(tree.find(key)); + for(auto& add : list.all()) { + set.insert(PageManager::Instance().readJSONFromFile(add)); + } + } + } + else if(json.contains("$gt")) { + if(json.contains("$lt")) { + set = queryString(prefix, json["$gt"].get(), json["$lt"].get(), 3); + } else if(json.contains("$lte")) { + set = queryString(prefix, json["$gt"].get(), json["$lte"].get(), 1); + } else { + set = queryString(prefix, json["$gt"].get(), "", 6); + } + } else if(json.contains("$gte")) { + if(json.contains("$lt")) { + set = queryString(prefix, json["$gte"].get(), json["$lt"].get(), 2); + } else if(json.contains("$lte")) { + set = queryString(prefix, json["$gte"].get(), json["$lte"].get(), 0); + } else { + set = queryString(prefix, json["$gte"].get(), "", 7); + } + } else if(json.contains("$lt")) { + set = queryString(prefix, "", json["$lt"].get(), 4); + } else if(json.contains("$lte")) { + set = queryString(prefix, "", json["$lte"].get(), 5); + } + return set; + } + } + + return set; +} + +std::set Collection::innerQuery(const std::string &prefix, const nlohmann::json &json) { + + for(auto& [key, value] : json.items()) { + if(key == "$lt" || key == "$lte" || key == "$gt" || key == "$gte" || key == "$ne") { + return queryRange(prefix.substr(0, prefix.size()-1), json); + } + } + + std::set res; + bool init = true; + + for(auto& [key, value] : json.items()) { + + std::set tmp; + std::string tPrefix = prefix + key; + + if(key == "$or") { + nlohmann::json line = json[key].get(); + for(auto& obj : line) { + tmp = setUnion(tmp, innerQuery(prefix, obj.get())); + } + } else if(json[key].is_object()) { + tmp = innerQuery(prefix + key + ".", json[key].get()); + } else if(json[key].is_array()) { + + } else if(json[key].is_boolean()) { + tmp = queryBool(tPrefix, json[key].get()); + } else if(json[key].is_string()) { + tmp = queryString(tPrefix, json[key].get(), json[key].get()); + } else if(json[key].is_number()) { + tmp = queryNumber(tPrefix, json[key].get(), json[key].get()); + } + if(init) res = tmp, init = false; + else res = setIntersection(res, tmp); + } + + auto str = json.dump(); + + printf("query: %s prefix: %s\n", str.c_str(), prefix.c_str()); + printf("result: \n"); + for(auto it=res.begin(); it!=res.end(); it++) { + printf(" - %s\n", it->dump().c_str()); + } + + return res; +} + +std::set +Collection::setIntersection(const std::set &a, const std::set &b) { + std::set res; + std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::inserter(res, res.end())); + return res; +} + +std::set +Collection::setUnion(const std::set &a, const std::set &b) { + std::set res; + std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::inserter(res, res.end())); + return res; +} + +/* + * range search mod explain + * mod = 0: minValue <= value <= maxValue + * mod = 1: minValue < value <= maxValue + * mod = 2: minValue <= value < maxValue + * mod = 3: minValue < value < maxValue + */ + +std::set +Collection::queryString(const std::string &prefix, const std::string &minValue, const std::string &maxValue, const int &mod) { + std::set res; + auto treeName = prefix + "$string"; + //printf(">>>> %s %s %s\n", prefix.c_str(), minValue.c_str(), maxValue.c_str()); + if(!index->exists(treeName)) { + return res; + } + BTree tree(index->find(treeName)); + const int K_SIZE = 64; + std::shared_ptr> node; + + if(mod == 4 || mod == 5) { + node = tree.getNode(tree.findNode(maxValue)); + } else { + node = tree.getNode(tree.findNode(minValue)); + } + + while(true) { + bool flag = true; + for(int i=0; isize; i++) { + if(mod == 0) { + if(node->keySet[i] < minValue) continue; + if(node->keySet[i] > maxValue) { + flag = false; + break; + } + } else if(mod == 1) { + if(node->keySet[i] <= minValue) continue; + if(node->keySet[i] > maxValue) { + flag = false; + break; + } + } else if(mod == 2) { + if(node->keySet[i] < minValue) continue; + if(node->keySet[i] >= maxValue) { + flag = false; + break; + } + } else if(mod == 3) { + if(node->keySet[i] <= minValue) continue; + if(node->keySet[i] >= maxValue) { + flag = false; + break; + } + } else if(mod == 4) { + if(node->keySet[i] >= maxValue) { + flag = false; + break; + } + } else if(mod == 5) { + if(node->keySet[i] > maxValue) { + flag = false; + break; + } + } else if(mod == 6) { + if(node->keySet[i] <= minValue) continue; + } else if(mod == 7) { + if(node->keySet[i] < minValue) continue; + } + List list(node->linkSet[i]); + for(auto& add : list.all()) { + res.insert(PageManager::Instance().readJSONFromFile(add)); + } + } + if(!flag) break; + if(node->right == 0) break; + node = tree.getNode(node->right); + } + return res; +} + +/* + * range search mod explain + * mod = 0: minValue <= value <= maxValue + * mod = 1: minValue < value <= maxValue + * mod = 2: minValue <= value < maxValue + * mod = 3: minValue < value < maxValue + */ + +std::set +Collection::queryNumber(const std::string &prefix, const double &minValue, const double &maxValue, const int &mod) { + std::set res; + auto treeName = prefix + "$number"; + printf(">>>> %s %f %f\n", prefix.c_str(), minValue, maxValue); + + if(!index->exists(treeName)) { + return res; + } + + BTree tree(index->find(treeName)); + const int K_SIZE = 8; + std::shared_ptr> node; + + if(mod == 4 || mod == 5) { + node = tree.getNode(tree.findNode(maxValue)); + } else { + node = tree.getNode(tree.findNode(minValue)); + } + + while(true) { + bool flag = true; + for(int i=0; isize; i++) { + + if(mod == 0) { + if(node->keySet[i] < minValue) continue; + if(node->keySet[i] > maxValue) { + flag = false; + break; + } + } else if(mod == 1) { + if(node->keySet[i] <= minValue) continue; + if(node->keySet[i] > maxValue) { + flag = false; + break; + } + } else if(mod == 2) { + if(node->keySet[i] < minValue) continue; + if(node->keySet[i] >= maxValue) { + flag = false; + break; + } + } else if(mod == 3) { + if(node->keySet[i] <= minValue) continue; + if(node->keySet[i] >= maxValue) { + flag = false; + break; + } + } else if(mod == 4) { + if(node->keySet[i] >= maxValue) { + flag = false; + break; + } + } else if(mod == 5) { + if(node->keySet[i] > maxValue) { + flag = false; + break; + } + } else if(mod == 6) { + if(node->keySet[i] <= minValue) continue; + } else if(mod == 7) { + if(node->keySet[i] < minValue) continue; + } + + + List list(node->linkSet[i]); + for(auto& add : list.all()) { + res.insert(PageManager::Instance().readJSONFromFile(add)); + } + } + if(!flag) break; + if(node->right == 0) break; + node = tree.getNode(node->right); + } + return res; +} + +std::set +Collection::queryBool(const std::string &prefix, const bool &value) { + std::set res; + auto treeName = prefix + "$boolean"; + //printf(">>>> %s %s %s\n", prefix.c_str(), minValue.c_str(), maxValue.c_str()); + if(!index->exists(treeName)) { + return res; + } + BTree tree(index->find(treeName)); + auto node = tree.getNode(tree.findNode(value)); + while(true) { + bool flag = true; + for(int i=0; isize; i++) { + if(node->keySet[i] < value) continue; + if(node->keySet[i] > value) { + flag = false; + break; + } + List list(node->linkSet[i]); + for(auto& add : list.all()) { + res.insert(PageManager::Instance().readJSONFromFile(add)); + } + } + if(!flag) break; + if(node->right == 0) break; + node = tree.getNode(node->right); + } + return res; +} diff --git a/invodb/file/page_manager.h b/invodb/file/page_manager.h index 336c3e1..9de400a 100644 --- a/invodb/file/page_manager.h +++ b/invodb/file/page_manager.h @@ -11,7 +11,7 @@ #include "storage_page.h" #include "json/json.hpp" -#include "invodb/models/cache.h" +#include "invodb/utils/cache.h" template class List; diff --git a/invodb/index/index.cpp b/invodb/index/index.cpp deleted file mode 100644 index 2eb27af..0000000 --- a/invodb/index/index.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by YuhangQ on 2021/11/1. -// - -#include "index.h" diff --git a/invodb/index/index.h b/invodb/index/index.h deleted file mode 100644 index 1d88f44..0000000 --- a/invodb/index/index.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// Created by YuhangQ on 2021/11/1. -// - -#ifndef INVODB_INDEX_H -#define INVODB_INDEX_H - -#include "btree/btree.h" -#include "json/json.hpp" - -class Index { -public: - void insert(const nlohmann::json& json); -private: - template - void insertElement(T const& key, const int& add) { - BTree treeString; - BTree treeBool; - BTree treeDouble; - BTree treeInt; - } -}; - - -#endif //INVODB_INDEX_H diff --git a/invodb/index/query.cpp b/invodb/index/query.cpp deleted file mode 100644 index 1edac9e..0000000 --- a/invodb/index/query.cpp +++ /dev/null @@ -1,9 +0,0 @@ -// -// Created by projector-user on 11/1/21. -// - -#include "query.h" - -std::vector Query::find(const nlohmann::json &json) { - return std::vector(); -} diff --git a/invodb/index/query.h b/invodb/index/query.h deleted file mode 100644 index d3a54af..0000000 --- a/invodb/index/query.h +++ /dev/null @@ -1,16 +0,0 @@ -// -// Created by projector-user on 11/1/21. -// - -#ifndef INVODB_QUERY_H -#define INVODB_QUERY_H - -#include "json/json.hpp" - -class Query { - std::vector find(const nlohmann::json &json); - -}; - - -#endif //INVODB_QUERY_H diff --git a/invodb/main.cpp b/invodb/main.cpp index 0754425..768f0bb 100644 --- a/invodb/main.cpp +++ b/invodb/main.cpp @@ -29,42 +29,58 @@ int main() { col = &Collection::getCollection("hello"); } + nlohmann::json j = nlohmann::json::parse(R"( +{ + "title" : "MongoDB 教程", + "description" : "MongoDB 是一个 Nosql 数据库", + "by" : "菜鸟教程", + "url" : "http://www.runoob.com", + "tags" : [ + "mongodb", + "database", + "NoSQL" + ], + "likes" : 100 +} + )"); - freopen("qq.txt", "r", stdin); - const int n = 1000000; - char qq[100], phone[100]; + col->insert(j); - clock_t start = clock(); - for(int i=0; iinsert(json); + col->query(nlohmann::json::parse(R"( +{ + "likes": {"$gt":50}, + "$or": [ + {"by": "菜鸟教程"}, + {"title": "MongoDB 教程"} + ] +} + )")); - if(i % (n/1000) == 0) { - printf("[%d/%d] time=%fs!\n", i, n, (double)(clock() - start) / CLOCKS_PER_SEC); - start = clock(); - } - } + +// freopen("qq.txt", "r", stdin); +// const int n = 1000000; +// char qq[100], phone[100]; +// +// clock_t start = clock(); +// for(int i=0; iinsert(json); +// +// if(i % (n/1000) == 0) { +// printf("[%d/%d] time=%fs!\n", i, n, (double)(clock() - start) / CLOCKS_PER_SEC); +// start = clock(); +// } +// } //col->test(); -// nlohmann::json j = nlohmann::json::parse(R"( -//{ -// "string": "this is a string!", -// "double": 3.1415, -// "int": 25565, -// "bool": true, -// "child": { -// "id": 3 -// }, -// "array": ["1", "2", "3"] -//} -// )"); - testAndBenchmark(20000); + + //testAndBenchmark(20000); return 0; } diff --git a/invodb/main.h b/invodb/main.h index e51a415..9cf043f 100644 --- a/invodb/main.h +++ b/invodb/main.h @@ -9,7 +9,7 @@ #include #include #include -#include "models/collection.h" +#include "collection/collection.h" #include "btree/list.h" diff --git a/invodb/models/cache.h b/invodb/utils/cache.h similarity index 100% rename from invodb/models/cache.h rename to invodb/utils/cache.h diff --git a/json/json.hpp b/json/json.hpp index 87475ab..3fab136 100644 --- a/json/json.hpp +++ b/json/json.hpp @@ -11442,11 +11442,11 @@ namespace detail /* @brief an iterator for primitive JSON types -This class models an iterator for primitive JSON types (boolean, number, +This class collection an iterator for primitive JSON types (boolean, number, string). It's only purpose is to allow the iterator/const_iterator classes to "iterate" over primitive values. Internally, the iterator is modeled by -a `difference_type` variable. Value begin_value (`0`) models the begin, -end_value (`1`) models past the end. +a `difference_type` variable. Value begin_value (`0`) collection the begin, +end_value (`1`) collection past the end. */ class primitive_iterator_t {