增加数据库 console 用于体验数据库。

This commit is contained in:
YuhangQ 2021-11-08 20:34:03 +08:00
parent 6c538caaab
commit c49cb450ce
2 changed files with 77 additions and 30 deletions

View File

@ -6,9 +6,9 @@
std::vector<nlohmann::json> Collection::query(const nlohmann::json &json) {
std::vector<nlohmann::json> res;
innerQuery("", json);
for(auto& json : innerQuery("", json)) {
res.push_back(json);
}
return res;
}
@ -26,7 +26,7 @@ std::vector<nlohmann::json> Collection::query(const nlohmann::json &json) {
std::set<nlohmann::json> 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<nlohmann::json> set;

View File

@ -7,6 +7,52 @@
void testAndBenchmark(int n);
Collection *col;
void terminal() {
printf("--------INVODB TERMINAL--------\n");
printf("insert\t<JSON>\tinsert a one line json to database.\n");
printf("query\t<JSON>\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);