From f54a618f0d5431d62985ebefbca29ae3ce7bf1da Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Thu, 25 Nov 2021 00:24:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/test.js | 63 ++++++++++++++++++++++++++++------------ demo/test2.js | 47 ++++++++++++++++++++++++++++++ index.js | 3 +- src/collection/query.cpp | 22 +++++++------- 4 files changed, 104 insertions(+), 31 deletions(-) create mode 100644 demo/test2.js diff --git a/demo/test.js b/demo/test.js index f673bbe..ef538fc 100644 --- a/demo/test.js +++ b/demo/test.js @@ -1,26 +1,51 @@ - -const fs = require('fs') - const invodb = require('..') -invodb.database('zzz.invodb') +invodb.database('hr.invodb') -let col = invodb.collection('blog') -if(!col.exist()) col.create(); +let person = invodb.collection('persons') +if(!person.exist()) person.create(); -for(let json of col.query({})) col.remove(json) +person.insert({ + name: "张三", + age: 22, + salary: 3000, + kpi: 0.6 +}) -let list = fs.readFileSync(__dirname + "/list.txt").toString().split("\n") -for(let json of list) { - col.insert(JSON.parse(json)) -} +person.insert({ + name: "李四", + age: 35, + salary: 6000, + kpi: 0.8 +}) -let result = col.query( - { - "title": { - "$gte": "数据库设计(1)", - "$lte": "数据库设计(5)", +person.insert({ + name: "王五", + age: 37, + salary: 20000, + kpi: 0.95 +}) + +person.insert({ + name: "李雷", + age: 32, + salary: 15000, + kpi: 0.35 +}) + + +console.log("优化互联网人力资源结构专项名单:") +let hr = person.query({ + $or: [ + { + age: { $gte: 35 }, + salary: { $lt: 10000 } + }, + { + kpi: { $lt: 0.4 }, + salary: { $gte: 10000 } } - } -) -console.log(result) \ No newline at end of file + ] +}) +console.log(hr) + diff --git a/demo/test2.js b/demo/test2.js new file mode 100644 index 0000000..36be7f3 --- /dev/null +++ b/demo/test2.js @@ -0,0 +1,47 @@ +const invodb = require('..') + +invodb.database('db.invodb') + +let person = invodb.collection('persons') +if(!person.exist()) person.create(); + +person.insert({ + name: "张三", + age: 19, + courses: ["数据库", "计组", "编译原理", "操作系统"] +}) + +person.insert({ + name: "李四", + age: 18, + courses: ["数据库", "线代", "数学分析", "操作系统", "数电"] +}) + +person.insert({ + name: "王五", + age: 21, + courses: ["数据库", "嵌入式", "微机", "计网", "STP"] +}) + +person.insert({ + name: "李雷", + age: 22, + courses: ["吃", "喝", "玩", "乐"] +}) + + +console.log("看看谁学习 数据库 和 操作系统:") +let good = person.query({ + courses: ["数据库", "操作系统"] +}) +console.log(good) + + +console.log("看看谁姓李:") +let li = person.query({ + name: { + $gte: "李", + $lt: "杏" + } +}) +console.log(li) \ No newline at end of file diff --git a/index.js b/index.js index c330152..bdfb45b 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,8 @@ function collection(collectionName) { create: create, insert: insert, remove: remove, - query: query + query: query, + update: insert } } diff --git a/src/collection/query.cpp b/src/collection/query.cpp index 24828ae..b21d4fc 100644 --- a/src/collection/query.cpp +++ b/src/collection/query.cpp @@ -160,14 +160,14 @@ std::set Collection::queryRange(const std::string &prefix, nlohm else set = tmp, init = true; } - printf(">> %d queryRange prefix: %s query: %s\n", init, prefix.c_str(), json.dump().c_str()); + //printf(">> %d queryRange prefix: %s query: %s\n", init, prefix.c_str(), json.dump().c_str()); if(!init) set = queryAllByField(prefix); - printf("result: \n"); - for(auto it=set.begin(); it!=set.end(); it++) { - printf(" - %s\n", it->dump().c_str()); - } + // printf("result: \n"); + // for(auto it=set.begin(); it!=set.end(); it++) { + // printf(" - %s\n", it->dump().c_str()); + // } if(json.contains("$ne")) { if(json["$ne"].is_null()) { @@ -312,11 +312,11 @@ std::set Collection::innerQuery(const std::string &prefix, const } 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()); - } + // 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()); + // } if (init) return queryAllByField(prefix == "" ? prefix : prefix.substr(0, prefix.size()-1)); @@ -484,7 +484,7 @@ 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 %d\n", prefix.c_str(), minValue, maxValue, mod); + //printf(">>>> %s %f %f %d\n", prefix.c_str(), minValue, maxValue, mod); if(!index->exists(treeName)) { return res;