diff --git a/CMakeLists.txt b/CMakeLists.txt index fd5e12e..771d8e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,12 +3,12 @@ project(InvoDB) set(CMAKE_CXX_STANDARD 14) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") +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/models/json.cpp invodb/models/json.h invodb/models/collection.cpp invodb/models/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/main.h invodb/file/page_manager.cpp invodb/file/page_manager.h invodb/models/collection.cpp invodb/models/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) diff --git a/invodb/btree/btree.h b/invodb/btree/btree.h index e15c2aa..eb4f9b3 100644 --- a/invodb/btree/btree.h +++ b/invodb/btree/btree.h @@ -19,6 +19,7 @@ public: int getNodeSize(); int find(const KT &key); int size(); + private: void removeEntry(int curAdd, const KT& key, const int& pointer); bool canCoalesce(int curAdd, int sibAdd); @@ -38,11 +39,14 @@ private: template BTree::BTree(const int& address) { root = address; + n_size = 0; } template void BTree::insert(const KT &key, const int &value) { + + if(exists(key)) { throw "keySet already exists."; } @@ -275,6 +279,8 @@ void BTree::redistribute(int curAdd, int sibAdd) { template int BTree::findNode(const KT &key) { + + auto cur = BTreeNode::getNode(root); while(!cur->leaf) { for(int i=0; isize; i++) { @@ -433,11 +439,7 @@ int BTree::getNodeSize() { template bool BTree::exists(const KT &key) { - auto cur = BTreeNode::getNode(findNode(key)); - for(int i=0; isize; i++) { - if(key == cur->keySet[i]) return true; - } - return false; + return find(key) != -1; } diff --git a/invodb/btree/node.h b/invodb/btree/node.h index 7431150..3b5d7e3 100644 --- a/invodb/btree/node.h +++ b/invodb/btree/node.h @@ -83,6 +83,10 @@ BTreeNode::BTreeNode(const int& address): address(address) { template BTreeNode *BTreeNode::getNode(const int &address) { + + std::cout << address << std::endl; + + if(address < 4) { throw "invalid address!"; } diff --git a/invodb/file/page_manager.cpp b/invodb/file/page_manager.cpp index 1f839bb..0b79ccb 100644 --- a/invodb/file/page_manager.cpp +++ b/invodb/file/page_manager.cpp @@ -31,3 +31,53 @@ int PageManager::allocate() { setPage(index, StoragePage(index)); return index; } + +void PageManager::release(const int &index) { + +} + +nlohmann::json PageManager::readJSONFromFile(const int &index) { + std::string content; + + StoragePage page = getPage(index); + while(true) { + for(int i=0; i<1016; i++) { + if(page[i] == '\0') break; + content.push_back(page[i]); + } + if(page.next() == 0) break; + page = getPage(page.next()); + } + + return nlohmann::json::parse(content); +} + +int PageManager::saveJSONToFile(const nlohmann::json& json) { + std::string content = json.dump(); + int size = content.size(); + + StoragePage page = getPage(allocate()); + int res = page.getAddress(); + int p = 0; + while(p < size) { + int len = std::min(size - p, 1016); + page.setStartFrom(0, &content.c_str()[p], len); + page.save(); + p += len; + if(p < size) { + int newPage = allocate(); + int lastPage = page.getAddress(); + page.setNext(newPage); + page.save(); + page = getPage(newPage); + page.setLast(lastPage); + page.save(); + } + } + + + + return res; +} + + diff --git a/invodb/file/page_manager.h b/invodb/file/page_manager.h index 859a3b1..0b3aa90 100644 --- a/invodb/file/page_manager.h +++ b/invodb/file/page_manager.h @@ -10,6 +10,7 @@ #include #include "storage_page.h" +#include "json/json.hpp" class PageManager { public: @@ -21,7 +22,9 @@ public: StoragePage getPage(const int &index); void setPage(const int &index, const StoragePage &page); int allocate(); - void free(const int &index); + void release(const int &index); + int saveJSONToFile(const nlohmann::json& json); + nlohmann::json readJSONFromFile(const int &index); private: std::map map; std::fstream stream; diff --git a/invodb/file/storage_page.h b/invodb/file/storage_page.h index b10bfed..3d563d7 100644 --- a/invodb/file/storage_page.h +++ b/invodb/file/storage_page.h @@ -28,7 +28,6 @@ public: double getDoubleStartFrom(const int &index); - int *intArray(); StoragePage(const int& id) { memset(page, 0, sizeof(page)); this->address = id; } char& operator[] (int index) { if(index>=1024 || index < 0) throw "overflow"; else return this->page[index]; } diff --git a/invodb/main.cpp b/invodb/main.cpp index 3d7dc8d..7db0050 100644 --- a/invodb/main.cpp +++ b/invodb/main.cpp @@ -13,7 +13,7 @@ int main() { srand(t); printf("seed: %d\n", t); - //system("rm -rf test.invodb && touch test.invodb"); + system("rm -rf test.invodb && touch test.invodb"); PageManager::loadDatabase("test.invodb"); Collection::loadCollections(); @@ -27,12 +27,17 @@ int main() { Collection::createCollection("hello"); } - JSON j; - j["hello"] = 1; + std::string test; + for(int i=0; i<100; i++) { + test += generateUUID(); + } + + nlohmann::json j; + j["hello"] = test; col->insert(j); - testAndBenchmark(100000); + //testAndBenchmark(100000); //btree->testAndBenchmark(100000); diff --git a/invodb/models/collection.cpp b/invodb/models/collection.cpp index 1f3149c..16f268f 100644 --- a/invodb/models/collection.cpp +++ b/invodb/models/collection.cpp @@ -6,16 +6,25 @@ Collection::Collection(const std::string &name, const int &firstPage) { Logger::info("load Collection: ", name); - tree = new BTree<27, std::string, 32>(firstPage); + tree = new BTree<3, std::string, 32>(firstPage); } -void Collection::insert(JSON &json) { - if(json["__Invo_ID__"].empty()) { - json["__Invo_ID__"] = generateUUID(); +void Collection::insert(nlohmann::json &json) { + + //printf("fuck:%d\n", tree); + if(json["__INVO_ID__"].empty()) { + json["__INVO_ID__"] = generateUUID(); } - Logger::info("INSERT ", json.dump()); -} + int add = PageManager::Instance().saveJSONToFile(json); + + std::string id = json["__INVO_ID__"].get(); + + tree->insert(id, add); + + auto tjson = PageManager::Instance().readJSONFromFile(add); + Logger::info("INSERT ", tjson.dump()); +} std::map Collection::map; std::set Collection::free; @@ -51,22 +60,21 @@ Collection& Collection::createCollection(const std::string &name) { int id = *free.begin(); free.erase(free.begin()); - printf("id: %d\n", id / 32); StoragePage page = PageManager::Instance().getPage(id / 32); id %= 32; - int collectionPage = PageManager::Instance().allocate(); + StoragePage collectionPage = PageManager::Instance().getPage(PageManager::Instance().allocate()); if(name.size() > 28) { throw "too long name of collection"; } page.setStringStartFrom(id*32, name.c_str()); - page.setIntStartFrom(id*32+28, collectionPage); + page.setIntStartFrom(id*32+28, collectionPage.getAddress()); page.print(); page.save(); - Collection *col = new Collection(name, collectionPage); + Collection *col = new Collection(name, collectionPage.getAddress()); map.insert(make_pair(name, col)); diff --git a/invodb/models/collection.h b/invodb/models/collection.h index 02e5999..8d5686c 100644 --- a/invodb/models/collection.h +++ b/invodb/models/collection.h @@ -8,7 +8,7 @@ #include "file/page_manager.h" #include "utils/logger.h" #include "btree/btree.h" -#include "models/json.h" +#include "json/json.hpp" #include #include #include @@ -17,7 +17,8 @@ class Collection { public: - static void insert(JSON &json); + void insert(nlohmann::json &json); + static void loadCollections(); static Collection& getCollection(const std::string& name); static Collection& createCollection(const std::string& name); @@ -25,7 +26,7 @@ private: static std::map map; static std::set free; - BTree<27, std::string, 32> *tree; + BTree<3, std::string, 32> *tree; Collection(const std::string& name,const int& firstPage); Collection() {} diff --git a/invodb/models/json.cpp b/invodb/models/json.cpp deleted file mode 100644 index b90722a..0000000 --- a/invodb/models/json.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by YuhangQ on 2021/10/9. -// - -#include "json.h" diff --git a/invodb/models/json.h b/invodb/models/json.h deleted file mode 100644 index b6e2c67..0000000 --- a/invodb/models/json.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// Created by YuhangQ on 2021/10/9. -// - -#ifndef INVODB_JSON_H -#define INVODB_JSON_H - -#include -#include "json/json.hpp" - -class JSON : public nlohmann::json { -public: - JSON() {} - JSON(std::string j): nlohmann::json(j) {} -private: -}; - - -#endif //INVODB_JSON_H