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() {
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<int, BTreeNodeUUID*> map;

View File

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

View File

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