From c49cb450ce20b221e5f43c187a6af4ac8fb571f7 Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Mon, 8 Nov 2021 20:34:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=20console=20=E7=94=A8=E4=BA=8E=E4=BD=93=E9=AA=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- invodb/collection/query.cpp | 8 +-- invodb/main.cpp | 99 +++++++++++++++++++++++++++---------- 2 files changed, 77 insertions(+), 30 deletions(-) diff --git a/invodb/collection/query.cpp b/invodb/collection/query.cpp index 914511f..6c41c00 100644 --- a/invodb/collection/query.cpp +++ b/invodb/collection/query.cpp @@ -6,9 +6,9 @@ std::vector Collection::query(const nlohmann::json &json) { std::vector res; - - innerQuery("", json); - + for(auto& json : innerQuery("", json)) { + res.push_back(json); + } return res; } @@ -26,7 +26,7 @@ std::vector Collection::query(const nlohmann::json &json) { 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()); + //printf(">> queryRange prefix: %s query: %s\n", prefix.c_str(), json.dump().c_str()); std::set set; diff --git a/invodb/main.cpp b/invodb/main.cpp index 768f0bb..e074885 100644 --- a/invodb/main.cpp +++ b/invodb/main.cpp @@ -7,6 +7,52 @@ void testAndBenchmark(int n); +Collection *col; + +void terminal() { + printf("--------INVODB TERMINAL--------\n"); + printf("insert\t\tinsert a one line json to database.\n"); + printf("query\t\tquery all jsons satisfying the query json.\n"); + printf("-------------------------------\n"); + + while(true) { + std::string input; + printf("INVODB > "); + std::getline(std::cin, input); + if(input.size() == 0) continue; + if(input.find("insert ") == 0) { + input = input.substr(7, input.size()); + + nlohmann::json json; + try { + json = nlohmann::json::parse(input); + } catch(...) { + printf("ERROR: your insert input isn't a json.\n"); + continue; + } + col->insert(json); + printf("You insert json: %s\n", json.dump().c_str()); + + } else if(input.find("query ") == 0) { + input = input.substr(6, input.size()); + nlohmann::json json; + try { + json = nlohmann::json::parse(input); + } catch(...) { + printf("ERROR: your query input isn't a json.\n"); + continue; + } + + auto res = col->query(json); + + printf("query result: \n"); + for(auto& j : res) { + printf(" - %s\n", j.dump().c_str()); + } + } + } +} + int main() { int t = time(0); srand(1635418590); @@ -21,7 +67,6 @@ int main() { PageManager& manager = PageManager::Instance(); - Collection *col; try { col = &Collection::getCollection("hello"); } catch(const char *error) { @@ -29,32 +74,34 @@ 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 -} - )"); +// nlohmann::json j = nlohmann::json::parse(R"( +//{ +// "title" : "MongoDB 教程", +// "description" : "MongoDB 是一个 Nosql 数据库", +// "by" : "菜鸟教程", +// "url" : "http://www.runoob.com", +// "tags" : [ +// "mongodb", +// "database", +// "NoSQL" +// ], +// "likes" : 100 +//} +// )"); +// +// col->insert(j); +// +// col->query(nlohmann::json::parse(R"( +//{ +// "likes": {"$gt":50}, +// "$or": [ +// {"by": "菜鸟教程"}, +// {"title": "MongoDB 教程"} +// ] +//} +// )")); - col->insert(j); - - col->query(nlohmann::json::parse(R"( -{ - "likes": {"$gt":50}, - "$or": [ - {"by": "菜鸟教程"}, - {"title": "MongoDB 教程"} - ] -} - )")); + terminal(); // freopen("qq.txt", "r", stdin);