From ab822882ba5cc70257ec9af4a3446a36c049a312 Mon Sep 17 00:00:00 2001 From: YuhangQ Date: Mon, 25 Oct 2021 23:01:28 +0800 Subject: [PATCH] btree insert finished! --- invodb/btree/btree_node.h | 16 ++++++++++++---- invodb/btree/btree_uuid.cpp | 10 +++++++--- invodb/main.cpp | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/invodb/btree/btree_node.h b/invodb/btree/btree_node.h index 7709add..5d9f397 100644 --- a/invodb/btree/btree_node.h +++ b/invodb/btree/btree_node.h @@ -47,19 +47,27 @@ public: void updateParent() { if(isLeaf()) return; for(int i=0; i<=size(); i++) { - + BTreeNodeUUID child = getNode(val[i])->parent; + child.parent = address; + child.save(); } } int size() { return n_size; } int insert(const std::string uuid) { + +// static int cnt = 0; +// if(!isLeaf() && size() >= 27) { +// if(cnt) throw "fuck"; +// cnt++; +// } int pos = 0; while(pos < n_size && uuid > key[pos]) pos++; val[n_size + 1] = val[n_size]; for(int i=n_size; i>pos; i--) { - key[i] = key[i - 1]; val[i] = val[i - 1]; + key[i] = key[i - 1]; } key[pos] = uuid; n_size++; @@ -95,8 +103,8 @@ public: page.save(); } static const int m = 28; - std::string key[m]; - int val[m+1]; + std::string key[m+100]; + int val[m+200]; int parent; private: static std::map map; diff --git a/invodb/btree/btree_uuid.cpp b/invodb/btree/btree_uuid.cpp index 86215a5..c47a65a 100644 --- a/invodb/btree/btree_uuid.cpp +++ b/invodb/btree/btree_uuid.cpp @@ -25,7 +25,7 @@ void BTreeUUID::insert(const char *uuid, int address) { break; } } - cur->parent = cur->getAddress(); + cur->parent = parent->getAddress(); } // insert directly @@ -77,11 +77,17 @@ void BTreeUUID::insertInternal(std::string uuid, BTreeNodeUUID *cur, BTreeNodeUU rLeaf->save(); return; } + BTreeNodeUUID* newLChild = BTreeNodeUUID::getNode(PageManager::Instance().allocate()); BTreeNodeUUID* newRChild = BTreeNodeUUID::getNode(PageManager::Instance().allocate()); newLChild->setLeaf(false); newRChild->setLeaf(false); + if(cur->size() != 27) { + printf("%d %d %d p:%d cur:%d\n", cur->size(), cur->isLeaf(), cur==root, cur->parent, cur->getAddress()); + exit(0); + } + int pos = cur->insert(uuid); cur->val[pos] = lLeaf->getAddress(); cur->val[pos+1] = rLeaf->getAddress(); @@ -106,8 +112,6 @@ void BTreeUUID::insertInternal(std::string uuid, BTreeNodeUUID *cur, BTreeNodeUU newRoot->save(); newLChild->save(); newRChild->save(); - - } else { insertInternal(cur->key[mid], BTreeNodeUUID::getNode(cur->parent), newLChild, newRChild); } diff --git a/invodb/main.cpp b/invodb/main.cpp index 52e4007..63ced14 100644 --- a/invodb/main.cpp +++ b/invodb/main.cpp @@ -27,7 +27,7 @@ int main() { BTreeUUID *btree = new BTreeUUID(PageManager::Instance().allocate()); char uuid[32]; - for(int i=0; i<1000; i++) { + for(int i=0; i<100000; i++) { generateUUID(uuid); btree->insert(uuid, PageManager::Instance().allocate()); }