btree find

This commit is contained in:
YuhangQ 2021-10-26 21:26:48 +08:00
parent 177cee9f91
commit 8fdec82448
4 changed files with 30 additions and 1 deletions

View File

@ -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; i<cur->size; 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; i<cur->size; 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) {

View File

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

View File

@ -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<std::string> 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;
}

View File

@ -8,6 +8,7 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <models/collection.h>