fix btree.h

This commit is contained in:
YuhangQ 2021-10-31 14:51:09 +08:00
parent 7eb07c0ca4
commit 8ed2c907ab
11 changed files with 99 additions and 51 deletions

View File

@ -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)

View File

@ -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<int M_SIZE, typename KT, int K_SIZE>
BTree<M_SIZE, KT, K_SIZE>::BTree(const int& address) {
root = address;
n_size = 0;
}
template<int M_SIZE, typename KT, int K_SIZE>
void BTree<M_SIZE, KT, K_SIZE>::insert(const KT &key, const int &value) {
if(exists(key)) {
throw "keySet already exists.";
}
@ -275,6 +279,8 @@ void BTree<M_SIZE, KT, K_SIZE>::redistribute(int curAdd, int sibAdd) {
template<int M_SIZE, typename KT, int K_SIZE>
int BTree<M_SIZE, KT, K_SIZE>::findNode(const KT &key) {
auto cur = BTreeNode<M_SIZE, KT, K_SIZE>::getNode(root);
while(!cur->leaf) {
for(int i=0; i<cur->size; i++) {
@ -433,11 +439,7 @@ int BTree<M_SIZE, KT, K_SIZE>::getNodeSize() {
template<int M_SIZE, typename KT, int K_SIZE>
bool BTree<M_SIZE, KT, K_SIZE>::exists(const KT &key) {
auto cur = BTreeNode<M_SIZE, KT, K_SIZE>::getNode(findNode(key));
for(int i=0; i<cur->size; i++) {
if(key == cur->keySet[i]) return true;
}
return false;
return find(key) != -1;
}

View File

@ -83,6 +83,10 @@ BTreeNode<M_SIZE, KT, K_SIZE>::BTreeNode(const int& address): address(address) {
template<int M_SIZE, typename KT, int K_SIZE>
BTreeNode<M_SIZE, KT, K_SIZE> *BTreeNode<M_SIZE, KT, K_SIZE>::getNode(const int &address) {
std::cout << address << std::endl;
if(address < 4) {
throw "invalid address!";
}

View File

@ -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;
}

View File

@ -10,6 +10,7 @@
#include <map>
#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<int, StoragePage> map;
std::fstream stream;

View File

@ -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]; }

View File

@ -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);

View File

@ -6,16 +6,25 @@
Collection::Collection(const std::string &name, const int &firstPage) {
Logger::info<std::string, std::string>("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<std::string, std::string>("INSERT ", json.dump());
}
int add = PageManager::Instance().saveJSONToFile(json);
std::string id = json["__INVO_ID__"].get<std::string>();
tree->insert(id, add);
auto tjson = PageManager::Instance().readJSONFromFile(add);
Logger::info<std::string, std::string>("INSERT ", tjson.dump());
}
std::map<std::string, Collection*> Collection::map;
std::set<int> 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));

View File

@ -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 <map>
#include <set>
#include <algorithm>
@ -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<std::string, Collection*> map;
static std::set<int> free;
BTree<27, std::string, 32> *tree;
BTree<3, std::string, 32> *tree;
Collection(const std::string& name,const int& firstPage);
Collection() {}

View File

@ -1,5 +0,0 @@
//
// Created by YuhangQ on 2021/10/9.
//
#include "json.h"

View File

@ -1,19 +0,0 @@
//
// Created by YuhangQ on 2021/10/9.
//
#ifndef INVODB_JSON_H
#define INVODB_JSON_H
#include <string>
#include "json/json.hpp"
class JSON : public nlohmann::json {
public:
JSON() {}
JSON(std::string j): nlohmann::json(j) {}
private:
};
#endif //INVODB_JSON_H