btree insert finished!

This commit is contained in:
YuhangQ 2021-10-25 23:01:28 +08:00
parent 612005483a
commit ab822882ba
3 changed files with 20 additions and 8 deletions

View File

@ -47,19 +47,27 @@ public:
void updateParent() { void updateParent() {
if(isLeaf()) return; if(isLeaf()) return;
for(int i=0; i<=size(); i++) { for(int i=0; i<=size(); i++) {
BTreeNodeUUID child = getNode(val[i])->parent;
child.parent = address;
child.save();
} }
} }
int size() { int size() {
return n_size; return n_size;
} }
int insert(const std::string uuid) { int insert(const std::string uuid) {
// static int cnt = 0;
// if(!isLeaf() && size() >= 27) {
// if(cnt) throw "fuck";
// cnt++;
// }
int pos = 0; int pos = 0;
while(pos < n_size && uuid > key[pos]) pos++; while(pos < n_size && uuid > key[pos]) pos++;
val[n_size + 1] = val[n_size]; val[n_size + 1] = val[n_size];
for(int i=n_size; i>pos; i--) { for(int i=n_size; i>pos; i--) {
key[i] = key[i - 1];
val[i] = val[i - 1]; val[i] = val[i - 1];
key[i] = key[i - 1];
} }
key[pos] = uuid; key[pos] = uuid;
n_size++; n_size++;
@ -95,8 +103,8 @@ public:
page.save(); page.save();
} }
static const int m = 28; static const int m = 28;
std::string key[m]; std::string key[m+100];
int val[m+1]; int val[m+200];
int parent; int parent;
private: private:
static std::map<int, BTreeNodeUUID*> map; static std::map<int, BTreeNodeUUID*> map;

View File

@ -25,7 +25,7 @@ void BTreeUUID::insert(const char *uuid, int address) {
break; break;
} }
} }
cur->parent = cur->getAddress(); cur->parent = parent->getAddress();
} }
// insert directly // insert directly
@ -77,11 +77,17 @@ void BTreeUUID::insertInternal(std::string uuid, BTreeNodeUUID *cur, BTreeNodeUU
rLeaf->save(); rLeaf->save();
return; return;
} }
BTreeNodeUUID* newLChild = BTreeNodeUUID::getNode(PageManager::Instance().allocate()); BTreeNodeUUID* newLChild = BTreeNodeUUID::getNode(PageManager::Instance().allocate());
BTreeNodeUUID* newRChild = BTreeNodeUUID::getNode(PageManager::Instance().allocate()); BTreeNodeUUID* newRChild = BTreeNodeUUID::getNode(PageManager::Instance().allocate());
newLChild->setLeaf(false); newLChild->setLeaf(false);
newRChild->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); int pos = cur->insert(uuid);
cur->val[pos] = lLeaf->getAddress(); cur->val[pos] = lLeaf->getAddress();
cur->val[pos+1] = rLeaf->getAddress(); cur->val[pos+1] = rLeaf->getAddress();
@ -106,8 +112,6 @@ void BTreeUUID::insertInternal(std::string uuid, BTreeNodeUUID *cur, BTreeNodeUU
newRoot->save(); newRoot->save();
newLChild->save(); newLChild->save();
newRChild->save(); newRChild->save();
} else { } else {
insertInternal(cur->key[mid], BTreeNodeUUID::getNode(cur->parent), newLChild, newRChild); insertInternal(cur->key[mid], BTreeNodeUUID::getNode(cur->parent), newLChild, newRChild);
} }

View File

@ -27,7 +27,7 @@ int main() {
BTreeUUID *btree = new BTreeUUID(PageManager::Instance().allocate()); BTreeUUID *btree = new BTreeUUID(PageManager::Instance().allocate());
char uuid[32]; char uuid[32];
for(int i=0; i<1000; i++) { for(int i=0; i<100000; i++) {
generateUUID(uuid); generateUUID(uuid);
btree->insert(uuid, PageManager::Instance().allocate()); btree->insert(uuid, PageManager::Instance().allocate());
} }