Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(p2): move helper functions to new file #523

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/storage/index/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_library(
OBJECT
b_plus_tree_index.cpp
b_plus_tree.cpp
b_plus_tree_helper.cpp
extendible_hash_table_index.cpp
index_iterator.cpp
linear_probe_hash_table_index.cpp)
Expand Down
223 changes: 0 additions & 223 deletions src/storage/index/b_plus_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,229 +111,6 @@ auto BPLUSTREE_TYPE::End() -> INDEXITERATOR_TYPE { return INDEXITERATOR_TYPE();
INDEX_TEMPLATE_ARGUMENTS
auto BPLUSTREE_TYPE::GetRootPageId() -> page_id_t { return 0; }

/*****************************************************************************
* UTILITIES AND DEBUG
*****************************************************************************/

/*
* This method is used for test only
* Read data from file and insert one by one
*/
INDEX_TEMPLATE_ARGUMENTS
void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *txn) {
int64_t key;
std::ifstream input(file_name);
while (input) {
input >> key;

KeyType index_key;
index_key.SetFromInteger(key);
RID rid(key);
Insert(index_key, rid, txn);
}
}
/*
* This method is used for test only
* Read data from file and remove one by one
*/
INDEX_TEMPLATE_ARGUMENTS
void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *txn) {
int64_t key;
std::ifstream input(file_name);
while (input) {
input >> key;
KeyType index_key;
index_key.SetFromInteger(key);
Remove(index_key, txn);
}
}

INDEX_TEMPLATE_ARGUMENTS
void BPLUSTREE_TYPE::Print(BufferPoolManager *bpm) {
auto root_page_id = GetRootPageId();
auto guard = bpm->FetchPageBasic(root_page_id);
PrintTree(guard.PageId(), guard.template As<BPlusTreePage>());
}

INDEX_TEMPLATE_ARGUMENTS
void BPLUSTREE_TYPE::PrintTree(page_id_t page_id, const BPlusTreePage *page) {
if (page->IsLeafPage()) {
auto *leaf = reinterpret_cast<const LeafPage *>(page);
std::cout << "Leaf Page: " << page_id << "\tNext: " << leaf->GetNextPageId() << std::endl;

// Print the contents of the leaf page.
std::cout << "Contents: ";
for (int i = 0; i < leaf->GetSize(); i++) {
std::cout << leaf->KeyAt(i);
if ((i + 1) < leaf->GetSize()) {
std::cout << ", ";
}
}
std::cout << std::endl;
std::cout << std::endl;

} else {
auto *internal = reinterpret_cast<const InternalPage *>(page);
std::cout << "Internal Page: " << page_id << std::endl;

// Print the contents of the internal page.
std::cout << "Contents: ";
for (int i = 0; i < internal->GetSize(); i++) {
std::cout << internal->KeyAt(i) << ": " << internal->ValueAt(i);
if ((i + 1) < internal->GetSize()) {
std::cout << ", ";
}
}
std::cout << std::endl;
std::cout << std::endl;
for (int i = 0; i < internal->GetSize(); i++) {
auto guard = bpm_->FetchPageBasic(internal->ValueAt(i));
PrintTree(guard.PageId(), guard.template As<BPlusTreePage>());
}
}
}

/**
* This method is used for debug only, You don't need to modify
*/
INDEX_TEMPLATE_ARGUMENTS
void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::string &outf) {
if (IsEmpty()) {
LOG_WARN("Drawing an empty tree");
return;
}

std::ofstream out(outf);
out << "digraph G {" << std::endl;
auto root_page_id = GetRootPageId();
auto guard = bpm->FetchPageBasic(root_page_id);
ToGraph(guard.PageId(), guard.template As<BPlusTreePage>(), out);
out << "}" << std::endl;
out.close();
}

/**
* This method is used for debug only, You don't need to modify
*/
INDEX_TEMPLATE_ARGUMENTS
void BPLUSTREE_TYPE::ToGraph(page_id_t page_id, const BPlusTreePage *page, std::ofstream &out) {
std::string leaf_prefix("LEAF_");
std::string internal_prefix("INT_");
if (page->IsLeafPage()) {
auto *leaf = reinterpret_cast<const LeafPage *>(page);
// Print node name
out << leaf_prefix << page_id;
// Print node properties
out << "[shape=plain color=green ";
// Print data of the node
out << "label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\">\n";
// Print data
out << "<TR><TD COLSPAN=\"" << leaf->GetSize() << "\">P=" << page_id << "</TD></TR>\n";
out << "<TR><TD COLSPAN=\"" << leaf->GetSize() << "\">"
<< "max_size=" << leaf->GetMaxSize() << ",min_size=" << leaf->GetMinSize() << ",size=" << leaf->GetSize()
<< "</TD></TR>\n";
out << "<TR>";
for (int i = 0; i < leaf->GetSize(); i++) {
out << "<TD>" << leaf->KeyAt(i) << "</TD>\n";
}
out << "</TR>";
// Print table end
out << "</TABLE>>];\n";
// Print Leaf node link if there is a next page
if (leaf->GetNextPageId() != INVALID_PAGE_ID) {
out << leaf_prefix << page_id << " -> " << leaf_prefix << leaf->GetNextPageId() << ";\n";
out << "{rank=same " << leaf_prefix << page_id << " " << leaf_prefix << leaf->GetNextPageId() << "};\n";
}
} else {
auto *inner = reinterpret_cast<const InternalPage *>(page);
// Print node name
out << internal_prefix << page_id;
// Print node properties
out << "[shape=plain color=pink "; // why not?
// Print data of the node
out << "label=<<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\">\n";
// Print data
out << "<TR><TD COLSPAN=\"" << inner->GetSize() << "\">P=" << page_id << "</TD></TR>\n";
out << "<TR><TD COLSPAN=\"" << inner->GetSize() << "\">"
<< "max_size=" << inner->GetMaxSize() << ",min_size=" << inner->GetMinSize() << ",size=" << inner->GetSize()
<< "</TD></TR>\n";
out << "<TR>";
for (int i = 0; i < inner->GetSize(); i++) {
out << "<TD PORT=\"p" << inner->ValueAt(i) << "\">";
if (i > 0) {
out << inner->KeyAt(i);
} else {
out << " ";
}
out << "</TD>\n";
}
out << "</TR>";
// Print table end
out << "</TABLE>>];\n";
// Print leaves
for (int i = 0; i < inner->GetSize(); i++) {
auto child_guard = bpm_->FetchPageBasic(inner->ValueAt(i));
auto child_page = child_guard.template As<BPlusTreePage>();
ToGraph(child_guard.PageId(), child_page, out);
if (i > 0) {
auto sibling_guard = bpm_->FetchPageBasic(inner->ValueAt(i - 1));
auto sibling_page = sibling_guard.template As<BPlusTreePage>();
if (!sibling_page->IsLeafPage() && !child_page->IsLeafPage()) {
out << "{rank=same " << internal_prefix << sibling_guard.PageId() << " " << internal_prefix
<< child_guard.PageId() << "};\n";
}
}
out << internal_prefix << page_id << ":p" << child_guard.PageId() << " -> ";
if (child_page->IsLeafPage()) {
out << leaf_prefix << child_guard.PageId() << ";\n";
} else {
out << internal_prefix << child_guard.PageId() << ";\n";
}
}
}
}

INDEX_TEMPLATE_ARGUMENTS
auto BPLUSTREE_TYPE::DrawBPlusTree() -> std::string {
if (IsEmpty()) {
return "()";
}

PrintableBPlusTree p_root = ToPrintableBPlusTree(GetRootPageId());
std::ostringstream out_buf;
p_root.Print(out_buf);

return out_buf.str();
}

INDEX_TEMPLATE_ARGUMENTS
auto BPLUSTREE_TYPE::ToPrintableBPlusTree(page_id_t root_id) -> PrintableBPlusTree {
auto root_page_guard = bpm_->FetchPageBasic(root_id);
auto root_page = root_page_guard.template As<BPlusTreePage>();
PrintableBPlusTree proot;

if (root_page->IsLeafPage()) {
auto leaf_page = root_page_guard.template As<LeafPage>();
proot.keys_ = leaf_page->ToString();
proot.size_ = proot.keys_.size() + 4; // 4 more spaces for indent

return proot;
}

// draw internal page
auto internal_page = root_page_guard.template As<InternalPage>();
proot.keys_ = internal_page->ToString();
proot.size_ = 0;
for (int i = 0; i < internal_page->GetSize(); i++) {
page_id_t child_id = internal_page->ValueAt(i);
PrintableBPlusTree child_node = ToPrintableBPlusTree(child_id);
proot.size_ += child_node.size_;
proot.children_.push_back(child_node);
}

return proot;
}

template class BPlusTree<GenericKey<4>, RID, GenericComparator<4>>;

template class BPlusTree<GenericKey<8>, RID, GenericComparator<8>>;
Expand Down
Loading