diff --git a/.gitignore b/.gitignore index 13b5060..f55c0b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ -test.invodb -/cmake-build-debug/ -.idea \ No newline at end of file +package-lock.json +*.invodb +.vscode +node_modules +build \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 96275ad..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -InvoDB \ No newline at end of file diff --git a/.idea/InvoDB.iml b/.idea/InvoDB.iml deleted file mode 100644 index f08604b..0000000 --- a/.idea/InvoDB.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 79b3c94..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index f4b947f..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 7575c27..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -cmake_minimum_required(VERSION 3.16.3) -project(InvoDB) - -set(CMAKE_CXX_STANDARD 17) - -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0") - -include_directories(.) -include_directories(./invodb) - -add_executable(InvoDB - invodb/main.cpp - 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/binding.gyp b/binding.gyp new file mode 100644 index 0000000..43fd267 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,27 @@ +{ + "targets": [ + { + "target_name": "core", + "sources": [ + "./src/collection/collection.cpp", + "./src/collection/index.cpp", + "./src/collection/query.cpp", + "./src/file/page_manager.cpp", + "./src/file/storage_page.cpp", + "./src/main.cpp", + ], + "include_dirs": [ + " #include #include -#include "file/page_manager.h" -#include "invodb/utils/cache.h" +#include "../file/page_manager.h" +#include "../utils/cache.h" template class BTreeNode { diff --git a/invodb/collection/collection.cpp b/src/collection/collection.cpp similarity index 93% rename from invodb/collection/collection.cpp rename to src/collection/collection.cpp index 76d9f36..d967b03 100644 --- a/invodb/collection/collection.cpp +++ b/src/collection/collection.cpp @@ -27,6 +27,10 @@ Collection& Collection::createCollection(const std::string &name) { return *col; } +bool Collection::existsCollection(const std::string& name) { + return map.count(name) != 0; +} + Collection &Collection::getCollection(const std::string &name) { if(map.count(name) == 0) { throw "no such collection"; @@ -55,7 +59,7 @@ void Collection::insert(nlohmann::json &json) { int add = PageManager::Instance().saveJSONToFile(json); uuid->insert(id, add); - //Logger::info("INSERT ", json.dump()); + Logger::info("INSERT ", json.dump()); // add index indexJSON("", json, add); diff --git a/invodb/collection/collection.h b/src/collection/collection.h similarity index 91% rename from invodb/collection/collection.h rename to src/collection/collection.h index a7bf000..1599000 100644 --- a/invodb/collection/collection.h +++ b/src/collection/collection.h @@ -5,16 +5,16 @@ #ifndef INVODB_COLLECTION_H #define INVODB_COLLECTION_H -#include "file/page_manager.h" -#include "utils/logger.h" -#include "btree/btree.h" -#include "json/json.hpp" +#include "../file/page_manager.h" +#include "../utils/logger.h" +#include "../btree/btree.h" +#include "../json/json.hpp" #include #include #include #include -#include "utils/uuid.h" -#include "btree/list.h" +#include "../utils/uuid.h" +#include "../btree/list.h" class Collection { public: @@ -23,6 +23,7 @@ public: std::vector query(const nlohmann::json &json); static void loadCollections(); static Collection& getCollection(const std::string& name); + static bool existsCollection(const std::string& name); static Collection& createCollection(const std::string& name); void test(); diff --git a/invodb/collection/index.cpp b/src/collection/index.cpp similarity index 100% rename from invodb/collection/index.cpp rename to src/collection/index.cpp diff --git a/invodb/collection/query.cpp b/src/collection/query.cpp similarity index 97% rename from invodb/collection/query.cpp rename to src/collection/query.cpp index 6c41c00..4c8a9ff 100644 --- a/invodb/collection/query.cpp +++ b/src/collection/query.cpp @@ -152,11 +152,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()); + // } return res; } @@ -267,7 +267,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\n", prefix.c_str(), minValue, maxValue); + //printf(">>>> %s %f %f\n", prefix.c_str(), minValue, maxValue); if(!index->exists(treeName)) { return res; diff --git a/invodb/file/page_manager.cpp b/src/file/page_manager.cpp similarity index 96% rename from invodb/file/page_manager.cpp rename to src/file/page_manager.cpp index 013ea2e..45f0ddd 100644 --- a/invodb/file/page_manager.cpp +++ b/src/file/page_manager.cpp @@ -3,10 +3,10 @@ // #include "page_manager.h" -#include "btree/list.h" - -int PageManager::loadDatabase(const char *filename) { +int PageManager::loadDatabase(const char *filename) { + std::ofstream file(filename, std::fstream::out); + file.close(); Instance().stream.open(filename); Instance().stream.seekp(0, std::ios::end); int index = Instance().stream.tellp() / 1024; diff --git a/invodb/file/page_manager.h b/src/file/page_manager.h similarity index 91% rename from invodb/file/page_manager.h rename to src/file/page_manager.h index 9de400a..913ebf1 100644 --- a/invodb/file/page_manager.h +++ b/src/file/page_manager.h @@ -9,12 +9,9 @@ #include #include +#include "../json/json.hpp" +#include "../utils/cache.h" #include "storage_page.h" -#include "json/json.hpp" -#include "invodb/utils/cache.h" - -template -class List; class PageManager { public: diff --git a/invodb/file/storage_page.cpp b/src/file/storage_page.cpp similarity index 98% rename from invodb/file/storage_page.cpp rename to src/file/storage_page.cpp index fd9f85b..4f4d783 100644 --- a/invodb/file/storage_page.cpp +++ b/src/file/storage_page.cpp @@ -62,6 +62,8 @@ int StoragePage::getAddress() { } StoragePage::StoragePage() { + + printf("aha!\n"); memset(page, 0, sizeof(page)); } diff --git a/invodb/file/storage_page.h b/src/file/storage_page.h similarity index 100% rename from invodb/file/storage_page.h rename to src/file/storage_page.h diff --git a/json/json.hpp b/src/json/json.hpp similarity index 100% rename from json/json.hpp rename to src/json/json.hpp diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..7190597 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,69 @@ +// +// Created by YuhangQ on 2021/9/24. +// + +#include "main.h" + +#include + +using namespace Napi; + + +void database(const CallbackInfo& info) { + std::string filename = info[0].As(); + PageManager::loadDatabase(filename.c_str()); + Collection::loadCollections(); +} + +Boolean exists(const CallbackInfo& info) { + std::string colName = info[0].As(); + return Boolean::New(info.Env(), Collection::existsCollection(colName)); +} + +void create(const CallbackInfo& info) { + std::string colName = info[0].As(); + Collection::createCollection(colName); +} + +void insert(const CallbackInfo& info) { + std::string colName = info[0].As(); + std::string json = info[1].As(); + + auto j = nlohmann::json::parse(json); + Collection::getCollection(colName).insert(j); +} + +Array query(const CallbackInfo& info) { + std::string colName = info[0].As(); + std::string json = info[1].As(); + + auto j = nlohmann::json::parse(json); + auto result = Collection::getCollection(colName).query(j); + + auto res = Array::New(info.Env(), result.size()); + for(int i=0; i(); + std::string json = info[1].As(); + + auto j = nlohmann::json::parse(json); + Collection::getCollection(colName).remove(j); +} + +Object Init(Env env, Object exports) { + srand(time(0)); + exports.Set("database", Function::New(env, database)); + exports.Set("exists", Function::New(env, exists)); + exports.Set("create", Function::New(env, create)); + exports.Set("insert", Function::New(env, insert)); + exports.Set("query", Function::New(env, query)); + exports.Set("remove", Function::New(env, _remove)); + return exports; +} +NODE_API_MODULE(core, Init) diff --git a/invodb/main.h b/src/main.h similarity index 100% rename from invodb/main.h rename to src/main.h diff --git a/invodb/main.cpp b/src/test/test.h similarity index 64% rename from invodb/main.cpp rename to src/test/test.h index e074885..493d1cb 100644 --- a/invodb/main.cpp +++ b/src/test/test.h @@ -1,11 +1,9 @@ -// -// Created by YuhangQ on 2021/9/24. -// - -#include "main.h" - - -void testAndBenchmark(int n); +#include +#include +#include +#include +#include "../collection/collection.h" +#include "../btree/list.h" Collection *col; @@ -53,84 +51,6 @@ void terminal() { } } -int main() { - int t = time(0); - srand(1635418590); - //srand(t); - printf("seed: %d\n", t); - - system("rm -rf test.invodb && touch test.invodb"); - - PageManager::loadDatabase("test.invodb"); - - Collection::loadCollections(); - - PageManager& manager = PageManager::Instance(); - - try { - col = &Collection::getCollection("hello"); - } catch(const char *error) { - Collection::createCollection("hello"); - 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 -//} -// )"); -// -// col->insert(j); -// -// col->query(nlohmann::json::parse(R"( -//{ -// "likes": {"$gt":50}, -// "$or": [ -// {"by": "菜鸟教程"}, -// {"title": "MongoDB 教程"} -// ] -//} -// )")); - - terminal(); - - -// 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(); - - - - - //testAndBenchmark(20000); - - return 0; -} void testAndBenchmark(int n) { diff --git a/invodb/utils/cache.h b/src/utils/cache.h similarity index 97% rename from invodb/utils/cache.h rename to src/utils/cache.h index 4f86db8..09d33ee 100644 --- a/invodb/utils/cache.h +++ b/src/utils/cache.h @@ -7,7 +7,7 @@ #include #include -#include "invodb/file/storage_page.h" +#include "../file/storage_page.h" template class LRUCache { diff --git a/invodb/utils/logger.h b/src/utils/logger.h similarity index 100% rename from invodb/utils/logger.h rename to src/utils/logger.h diff --git a/invodb/utils/uuid.h b/src/utils/uuid.h similarity index 100% rename from invodb/utils/uuid.h rename to src/utils/uuid.h