From 8fdec82448408337cddd006a6e6864fa624ef9f3 Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Tue, 26 Oct 2021 21:26:48 +0800 Subject: [PATCH] btree find --- invodb/btree/btree_uuid.cpp | 20 ++++++++++++++++++++ invodb/btree/btree_uuid.h | 2 ++ invodb/main.cpp | 8 +++++++- invodb/main.h | 1 + 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/invodb/btree/btree_uuid.cpp b/invodb/btree/btree_uuid.cpp index 357ae27..a761e5c 100644 --- a/invodb/btree/btree_uuid.cpp +++ b/invodb/btree/btree_uuid.cpp @@ -177,6 +177,26 @@ void BTreeUUID::innerPrint(BTreeNodeUUID *cur) { } + +int BTreeUUID::find(std::string uuid) { + BTreeNodeUUID* cur = BTreeNodeUUID::getNode(root); + while(!cur->leaf) { + for(int i=0; isize; i++) { + if(uuid < cur->key[i]) { + cur = BTreeNodeUUID::getNode(cur->val[i]); + break; + } + if(i == cur->size - 1) { + cur = BTreeNodeUUID::getNode(cur->val[i + 1]); + break; + } + } + } + for(int i=0; isize; i++) { + if(uuid == cur->key[i]) return cur->val[i]; + } + return -1; +} /* void BTreeUUID::innerInsert(BTreeNodeUUID* &p, BTreeNodeUUID* f, const char *uuid, int address) { if(p == nullptr) { diff --git a/invodb/btree/btree_uuid.h b/invodb/btree/btree_uuid.h index d489557..fc4ef6e 100644 --- a/invodb/btree/btree_uuid.h +++ b/invodb/btree/btree_uuid.h @@ -11,9 +11,11 @@ class BTreeUUID { public: BTreeUUID(const int& address); void insert(const char* uuid, int address); + int find(std::string uuid); void print(); private: void innerPrint(BTreeNodeUUID* cur); + void split(std::string uuid, int address, int parentAddr, int curAddr); void insertInternal(std::string uuid, int curAddr, int lLeafAddr, int rLeafAddr); int root; diff --git a/invodb/main.cpp b/invodb/main.cpp index 911e34b..7270a4f 100644 --- a/invodb/main.cpp +++ b/invodb/main.cpp @@ -27,12 +27,18 @@ int main() { BTreeUUID *btree = new BTreeUUID(PageManager::Instance().allocate()); char uuid[32]; - for(int i=0; i<1000000; i++) { + + std::vector v; + + for(int i=0; i<10000; i++) { generateUUID(uuid); + v.push_back(std::string(uuid, 32)); btree->insert(uuid, PageManager::Instance().allocate()); } btree->print(); + printf("%d\n", btree->find("123")); + return 0; } \ No newline at end of file diff --git a/invodb/main.h b/invodb/main.h index 38d6d77..b39729d 100644 --- a/invodb/main.h +++ b/invodb/main.h @@ -8,6 +8,7 @@ #include #include #include +#include #include