Skip to content

Commit

Permalink
refactor(p2): remove transaction from b plus tree
Browse files Browse the repository at this point in the history
  • Loading branch information
unw9527 committed Sep 22, 2024
1 parent 80b4544 commit e5cf0d3
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 68 deletions.
6 changes: 3 additions & 3 deletions src/storage/index/b_plus_tree_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ auto BPLUSTREE_INDEX_TYPE::InsertEntry(const Tuple &key, RID rid, Transaction *t
KeyType index_key;
index_key.SetFromKey(key);

return container_->Insert(index_key, rid, transaction);
return container_->Insert(index_key, rid);
}

INDEX_TEMPLATE_ARGUMENTS
Expand All @@ -38,7 +38,7 @@ void BPLUSTREE_INDEX_TYPE::DeleteEntry(const Tuple &key, RID rid, Transaction *t
KeyType index_key;
index_key.SetFromKey(key);

container_->Remove(index_key, transaction);
container_->Remove(index_key);
}

INDEX_TEMPLATE_ARGUMENTS
Expand All @@ -47,7 +47,7 @@ void BPLUSTREE_INDEX_TYPE::ScanKey(const Tuple &key, std::vector<RID> *result, T
KeyType index_key;
index_key.SetFromKey(key);

container_->GetValue(index_key, result, transaction);
container_->GetValue(index_key, result);
}

INDEX_TEMPLATE_ARGUMENTS
Expand Down
26 changes: 6 additions & 20 deletions test/storage/b_plus_tree_concurrent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,80 +46,66 @@ void InsertHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, con
__attribute__((unused)) uint64_t thread_itr = 0) {
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);
for (auto key : keys) {
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
tree->Insert(index_key, rid, transaction);
tree->Insert(index_key, rid);
}
delete transaction;
}

// helper function to seperate insert
void InsertHelperSplit(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, const std::vector<int64_t> &keys,
int total_threads, __attribute__((unused)) uint64_t thread_itr) {
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);
for (auto key : keys) {
if (static_cast<uint64_t>(key) % total_threads == thread_itr) {
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
tree->Insert(index_key, rid, transaction);
tree->Insert(index_key, rid);
}
}
delete transaction;
}

// helper function to delete
void DeleteHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, const std::vector<int64_t> &remove_keys,
__attribute__((unused)) uint64_t thread_itr = 0) {
GenericKey<8> index_key;
// create transaction
auto *transaction = new Transaction(0);
for (auto key : remove_keys) {
index_key.SetFromInteger(key);
tree->Remove(index_key, transaction);
tree->Remove(index_key);
}
delete transaction;
}

// helper function to seperate delete
void DeleteHelperSplit(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree,
const std::vector<int64_t> &remove_keys, int total_threads,
__attribute__((unused)) uint64_t thread_itr) {
GenericKey<8> index_key;
// create transaction
auto *transaction = new Transaction(0);
for (auto key : remove_keys) {
if (static_cast<uint64_t>(key) % total_threads == thread_itr) {
index_key.SetFromInteger(key);
tree->Remove(index_key, transaction);
tree->Remove(index_key);
}
}
delete transaction;
}

void LookupHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, const std::vector<int64_t> &keys,
uint64_t tid, __attribute__((unused)) uint64_t thread_itr = 0) {
auto *transaction = new Transaction(static_cast<txn_id_t>(tid));
GenericKey<8> index_key;
RID rid;
for (auto key : keys) {
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
std::vector<RID> result;
bool res = tree->GetValue(index_key, &result, transaction);
bool res = tree->GetValue(index_key, &result);
ASSERT_EQ(res, true);
ASSERT_EQ(result.size(), 1);
ASSERT_EQ(result[0], rid);
}
delete transaction;
}

TEST(BPlusTreeConcurrentTest, DISABLED_InsertTest1) {
Expand Down Expand Up @@ -368,7 +354,7 @@ TEST(BPlusTreeConcurrentTest, DISABLED_MixTest2) {

size_t num_threads = 6;
for (size_t i = 0; i < num_threads; i++) {
threads.emplace_back(std::thread{tasks[i % tasks.size()], i});
threads.emplace_back(tasks[i % tasks.size()], i);
}
for (size_t i = 0; i < num_threads; i++) {
threads[i].join();
Expand Down
4 changes: 1 addition & 3 deletions test/storage/b_plus_tree_contention_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ bool BPlusTreeLockBenchmarkCall(size_t num_threads, int leaf_node_size, bool wit
auto func = [&tree, &mtx, i, keys_per_thread, with_global_mutex]() {
GenericKey<8> index_key;
RID rid;
auto *transaction = new Transaction(static_cast<txn_id_t>(i + 1));
const auto end_key = keys_stride * i + keys_per_thread;
for (auto key = i * keys_stride; key < end_key; key++) {
int64_t value = key & 0xFFFFFFFF;
Expand All @@ -52,12 +51,11 @@ bool BPlusTreeLockBenchmarkCall(size_t num_threads, int leaf_node_size, bool wit
if (with_global_mutex) {
mtx.lock();
}
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);
if (with_global_mutex) {
mtx.unlock();
}
}
delete transaction;
};
auto t = std::thread(std::move(func));
threads.emplace_back(std::move(t));
Expand Down
18 changes: 4 additions & 14 deletions test/storage/b_plus_tree_delete_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ TEST(BPlusTreeTests, DISABLED_DeleteTest1) {
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", page_id, bpm, comparator);
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);

std::vector<int64_t> keys = {1, 2, 3, 4, 5};
for (auto key : keys) {
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);
}

std::vector<RID> rids;
Expand All @@ -61,7 +59,7 @@ TEST(BPlusTreeTests, DISABLED_DeleteTest1) {
std::vector<int64_t> remove_keys = {1, 5};
for (auto key : remove_keys) {
index_key.SetFromInteger(key);
tree.Remove(index_key, transaction);
tree.Remove(index_key);
}

int64_t size = 0;
Expand All @@ -81,10 +79,7 @@ TEST(BPlusTreeTests, DISABLED_DeleteTest1) {
size = size + 1;
}
}

EXPECT_EQ(size, 3);

delete transaction;
delete bpm;
}

Expand All @@ -101,15 +96,13 @@ TEST(BPlusTreeTests, DISABLED_DeleteTest2) {
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", page_id, bpm, comparator);
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);

std::vector<int64_t> keys = {1, 2, 3, 4, 5};
for (auto key : keys) {
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);
}

std::vector<RID> rids;
Expand All @@ -126,7 +119,7 @@ TEST(BPlusTreeTests, DISABLED_DeleteTest2) {
std::vector<int64_t> remove_keys = {1, 5, 3, 4};
for (auto key : remove_keys) {
index_key.SetFromInteger(key);
tree.Remove(index_key, transaction);
tree.Remove(index_key);
}

int64_t size = 0;
Expand All @@ -146,10 +139,7 @@ TEST(BPlusTreeTests, DISABLED_DeleteTest2) {
size = size + 1;
}
}

EXPECT_EQ(size, 1);

delete transaction;
delete bpm;
}
} // namespace bustub
18 changes: 3 additions & 15 deletions test/storage/b_plus_tree_insert_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ TEST(BPlusTreeTests, DISABLED_InsertTest1) {
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", page_id, bpm, comparator, 2, 3);
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);

int64_t key = 42;
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key), value);
index_key.SetFromInteger(key);
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);

auto root_page_id = tree.GetRootPageId();
auto root_page_guard = bpm->ReadPage(root_page_id);
Expand All @@ -55,7 +53,6 @@ TEST(BPlusTreeTests, DISABLED_InsertTest1) {
ASSERT_EQ(root_as_leaf->GetSize(), 1);
ASSERT_EQ(comparator(root_as_leaf->KeyAt(0), index_key), 0);

delete transaction;
delete bpm;
}

Expand All @@ -72,15 +69,13 @@ TEST(BPlusTreeTests, DISABLED_InsertTest2) {
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", page_id, bpm, comparator, 2, 3);
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);

std::vector<int64_t> keys = {1, 2, 3, 4, 5};
for (auto key : keys) {
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);
}

std::vector<RID> rids;
Expand Down Expand Up @@ -108,10 +103,7 @@ TEST(BPlusTreeTests, DISABLED_InsertTest2) {
EXPECT_EQ(rids[0].GetSlotNum(), key);
size = size + 1;
}

EXPECT_EQ(size, keys.size());

delete transaction;
delete bpm;
}

Expand All @@ -128,15 +120,13 @@ TEST(BPlusTreeTests, DISABLED_InsertTest3) {
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", page_id, bpm, comparator);
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);

std::vector<int64_t> keys = {5, 4, 3, 2, 1};
for (auto key : keys) {
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);
}

std::vector<RID> rids;
Expand Down Expand Up @@ -171,8 +161,6 @@ TEST(BPlusTreeTests, DISABLED_InsertTest3) {
EXPECT_EQ(location.GetSlotNum(), current_key);
current_key = current_key + 1;
}

delete transaction;
delete bpm;
}
} // namespace bustub
5 changes: 1 addition & 4 deletions test/storage/b_plus_tree_sequential_scale_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ TEST(BPlusTreeTests, DISABLED_ScaleTest) { // NOLINT
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", page_id, bpm, comparator, 2, 3);
GenericKey<8> index_key;
RID rid;
// create transaction
auto *transaction = new Transaction(0);

int64_t scale = 5000;
std::vector<int64_t> keys;
Expand All @@ -58,7 +56,7 @@ TEST(BPlusTreeTests, DISABLED_ScaleTest) { // NOLINT
int64_t value = key & 0xFFFFFFFF;
rid.Set(static_cast<int32_t>(key >> 32), value);
index_key.SetFromInteger(key);
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);
}
std::vector<RID> rids;
for (auto key : keys) {
Expand All @@ -71,7 +69,6 @@ TEST(BPlusTreeTests, DISABLED_ScaleTest) { // NOLINT
ASSERT_EQ(rids[0].GetSlotNum(), value);
}

delete transaction;
delete bpm;
}
} // namespace bustub
16 changes: 7 additions & 9 deletions tools/b_plus_tree_printer/b_plus_tree_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ using bustub::GenericKey;
using bustub::page_id_t;
using bustub::ParseCreateStatement;
using bustub::RID;
using bustub::Transaction;

auto UsageMessage() -> std::string {
std::string message =
Expand Down Expand Up @@ -80,8 +79,7 @@ auto main(int argc, char **argv) -> int {
// create b+ tree
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", root_pid, bpm, comparator, leaf_max_size,
internal_max_size);
// create transaction
auto *transaction = new Transaction(0);

while (!quit) {
std::cout << "> ";
std::cin >> instruction;
Expand All @@ -91,26 +89,26 @@ auto main(int argc, char **argv) -> int {
switch (instruction) {
case 'c':
std::cin >> filename;
tree.RemoveFromFile(filename, transaction);
tree.RemoveFromFile(filename);
break;
case 'x':
std::cin >> filename;
tree.BatchOpsFromFile(filename, transaction);
tree.BatchOpsFromFile(filename);
break;
case 'd':
std::cin >> key;
index_key.SetFromInteger(key);
tree.Remove(index_key, transaction);
tree.Remove(index_key);
break;
case 'i':
std::cin >> key;
rid.Set(static_cast<int32_t>(key >> 32), static_cast<int>(key & 0xFFFFFFFF));
index_key.SetFromInteger(key);
tree.Insert(index_key, rid, transaction);
tree.Insert(index_key, rid);
break;
case 'f':
std::cin >> filename;
tree.InsertFromFile(filename, transaction);
tree.InsertFromFile(filename);
break;
case 'q':
quit = true;
Expand All @@ -136,7 +134,7 @@ auto main(int argc, char **argv) -> int {
BUSTUB_ASSERT(bpm->DeletePage(root_pid), "Unable to delete root page for some reason");

delete bpm;
delete transaction;

delete disk_manager;
remove("test.bustub");
remove("test.log");
Expand Down

0 comments on commit e5cf0d3

Please sign in to comment.