From a7f8ee99511643064a9978d01eb5f2319b688414 Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Sat, 13 Nov 2021 23:42:00 +0800 Subject: [PATCH] e --- demo/list.txt | 4 +++- demo/test.js | 7 ++++++- src/collection/query.cpp | 26 ++++++++++++++------------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/demo/list.txt b/demo/list.txt index bf3d308..5c642b8 100644 --- a/demo/list.txt +++ b/demo/list.txt @@ -5,4 +5,6 @@ {"id": 5} {"id": 6} {"id": 7} -{"id": 8} \ No newline at end of file +{"id": 8} +{"id": null} +{"id": null} \ No newline at end of file diff --git a/demo/test.js b/demo/test.js index 61fe103..a0ca08c 100644 --- a/demo/test.js +++ b/demo/test.js @@ -17,7 +17,12 @@ for(let json of list) { let result = col.query({ id: { - $ne: 2 + $gte: 1, + $lte: 8, + $or: [ + { $ne: 2 }, + { $ne: 3 } + ] } }) diff --git a/src/collection/query.cpp b/src/collection/query.cpp index 5e574c7..02a9631 100644 --- a/src/collection/query.cpp +++ b/src/collection/query.cpp @@ -60,7 +60,7 @@ std::set Collection::queryRange(const std::string &prefix, nlohm } } - if(isNumber) { + if(nInit && isNumber) { if(json.contains("$gt") && json.contains("$gte")) { auto gt = json["$gt"].get(); auto gte = json["$gte"].get(); @@ -110,7 +110,8 @@ std::set Collection::queryRange(const std::string &prefix, nlohm if(init) set = setIntersection(set, tmp); else set = tmp, init = true; - } else { + } + if(nInit && !isNumber){ if(json.contains("$gt") && json.contains("$gte")) { auto gt = json["$gt"].get(); auto gte = json["$gte"].get(); @@ -159,9 +160,9 @@ std::set Collection::queryRange(const std::string &prefix, nlohm else set = tmp, init = true; } - if(!init) set = queryAllByField(prefix); + printf(">> %d queryRange prefix: %s query: %s\n", init, prefix.c_str(), json.dump().c_str()); - printf(">> queryRange prefix: %s query: %s\n", prefix.c_str(), json.dump().c_str()); + if(!init) set = queryAllByField(prefix); printf("result: \n"); for(auto it=set.begin(); it!=set.end(); it++) { @@ -170,7 +171,10 @@ std::set Collection::queryRange(const std::string &prefix, nlohm if(json.contains("$ne")) { if(json["$ne"].is_null()) { - // do nothing + List tree(index->find(prefix + "$null")); + for(auto& add : tree.all()) { + set.erase(PageManager::readJSONFromFile(add)); + } } else if(json["$ne"].is_string()) { List tree(index->find(prefix + "$null")); for(auto& add : tree.all()) { @@ -289,7 +293,7 @@ std::set Collection::innerQuery(const std::string &prefix, const printf(" - %s\n", it->dump().c_str()); } - if (init) return queryAllByField(prefix); + if (init) return queryAllByField(prefix == "" ? prefix : prefix.substr(0, prefix.size()-1)); return res; } @@ -305,11 +309,10 @@ std::set Collection::queryAllByField(const std::string &fieldNa res.insert(PageManager::readJSONFromFile(value)); } } else { - auto tfieldName = fieldName.substr(0, fieldName.size()-1); int treeID; - if((treeID = index->find(tfieldName + "$string")) != -1) { + if((treeID = index->find(fieldName + "$string")) != -1) { BTree tree(treeID); for(auto &[key, value] : tree.all()) { List list(value); @@ -319,7 +322,7 @@ std::set Collection::queryAllByField(const std::string &fieldNa } } - if((treeID = index->find(tfieldName + "$number")) != -1) { + if((treeID = index->find(fieldName + "$number")) != -1) { BTree tree(treeID); for(auto &[key, value] : tree.all()) { List list(value); @@ -329,7 +332,7 @@ std::set Collection::queryAllByField(const std::string &fieldNa } } - if((treeID = index->find(tfieldName + "$boolean")) != -1) { + if((treeID = index->find(fieldName + "$boolean")) != -1) { BTree tree(treeID); for(auto &[key, value] : tree.all()) { List list(value); @@ -339,7 +342,7 @@ std::set Collection::queryAllByField(const std::string &fieldNa } } - if((treeID = index->find(tfieldName + "$null")) != -1) { + if((treeID = index->find(fieldName + "$null")) != -1) { List list(treeID); for(auto& add : list.all()) { res.insert(PageManager::readJSONFromFile(add)); @@ -559,7 +562,6 @@ Collection::queryBool(const std::string &prefix, const bool &value) { return res; } - std::set Collection::queryNull(const std::string &prefix) { std::set res; auto treeName = prefix + "$null";