InvoDB/invodb/btree/btree_node.cpp

107 lines
2.4 KiB
C++
Raw Normal View History

2021-10-24 20:21:04 +08:00
//
2021-10-25 22:07:18 +08:00
// Created by YuhangQ on 2021/10/25.
2021-10-24 20:21:04 +08:00
//
#include "btree_node.h"
2021-10-25 22:07:18 +08:00
2021-10-28 23:15:15 +08:00
std::map<int, NodeUUID*> NodeUUID::map;
2021-10-25 22:07:18 +08:00
2021-10-28 23:15:15 +08:00
NodeUUID *NodeUUID::getNode(const int &address) {
2021-10-29 09:55:24 +08:00
if(address == 0) {
throw "fuck";
}
2021-10-28 23:15:15 +08:00
if(map.count(address) == 0) {
delete map[address];
map[address] = new NodeUUID(address);
2021-10-25 22:07:18 +08:00
}
2021-10-28 23:44:50 +08:00
// if(map.count(address) == 0) {
// map[address] = new NodeUUID(address);
// }
2021-10-25 22:07:18 +08:00
return map[address];
}
2021-10-26 12:29:59 +08:00
2021-10-28 23:15:15 +08:00
NodeUUID::NodeUUID(const int& address):address(address) {
2021-10-26 12:29:59 +08:00
clear();
StoragePage page = PageManager::Instance().getPage(address);
int p = 0;
2021-10-28 23:15:15 +08:00
size = page.getIntStartFrom(p); p += 4;
parent = page.getIntStartFrom(p); p += 4;
left = page.getIntStartFrom(p); p += 4;
right = page.getIntStartFrom(p); p += 4;
leaf = !page.getIntStartFrom(p); p += 4;
2021-10-26 12:29:59 +08:00
for(int i=0; i<m; i++) {
for(int j=0; j<32; j++) {
2021-10-28 23:15:15 +08:00
key[i].push_back(page[p++]);
2021-10-26 12:29:59 +08:00
}
}
for(int i=0; i<m+1; i++) {
val[i] = page.getIntStartFrom(p);
p += 4;
}
}
2021-10-28 23:15:15 +08:00
int NodeUUID::insert(const std::string& uuid) {
2021-10-26 12:29:59 +08:00
int pos = 0;
while(pos < size && uuid > key[pos]) pos++;
val[size + 1] = val[size];
for(int i=size; i>pos; i--) {
val[i] = val[i - 1];
key[i] = key[i - 1];
}
key[pos] = uuid;
size++;
return pos;
}
2021-10-28 23:15:15 +08:00
void NodeUUID::print() {
2021-10-26 12:29:59 +08:00
printf("---------BTreeNode---------\n");
for(int i=0; i<size; i++) {
printf("%s %d\n", key[i].c_str(), val[i]);
}
}
2021-10-28 23:15:15 +08:00
void NodeUUID::clear() {
2021-10-26 12:29:59 +08:00
for(int i=0; i<m+1; i++) key[i].clear(), val[i] = 0;
size = 0;
leaf = false;
parent = 0;
}
2021-10-28 23:15:15 +08:00
void NodeUUID::save() {
2021-10-26 12:29:59 +08:00
StoragePage page(address);
int p = 0;
2021-10-28 23:15:15 +08:00
page.setIntStartFrom(p, size); p += 4;
page.setIntStartFrom(p, parent); p += 4;
page.setIntStartFrom(p, left); p += 4;
page.setIntStartFrom(p, right); p += 4;
page.setIntStartFrom(p, !leaf); p += 4;
2021-10-26 12:29:59 +08:00
for(int i=0; i<m; i++) {
for(int j=0; j<32; j++) {
page[p++] = key[i][j];
}
}
for(int i=0; i<m+1; i++) {
page.setIntStartFrom(p, val[i]);
p += 4;
}
page.save();
}
2021-10-28 23:15:15 +08:00
int NodeUUID::findPos(const std::string &uuid) {
int pos = std::lower_bound(key, key+size, uuid) - key;
if(pos == size || key[pos] != uuid) return -1;
return pos;
}
void NodeUUID::release() {
NodeUUID::release(this->address);
}
NodeUUID *NodeUUID::release(const int &address) {
return nullptr;
}
2021-10-29 09:55:24 +08:00