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

feat: Add support to PageGuard upgrade #581

Closed
wants to merge 15 commits into from
Closed
40 changes: 40 additions & 0 deletions src/include/storage/page/page_guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ class BasicPageGuard {
return reinterpret_cast<T *>(GetDataMut());
}

/** The following methods are mainly used for testing */

auto GetPage() const -> Page * { return page_; }

auto GetBPM() const -> BufferPoolManager * { return bpm_; }

void SetNull() noexcept {
page_ = nullptr;
bpm_ = nullptr;
}

private:
friend class ReadPageGuard;
friend class WritePageGuard;
Expand All @@ -118,6 +129,19 @@ class ReadPageGuard {
ReadPageGuard(const ReadPageGuard &) = delete;
auto operator=(const ReadPageGuard &) -> ReadPageGuard & = delete;

/** TODO(P2): Add implementation
*
* @brief Provide guard upgrade functionality, from ReadPageGuard to WritePageGuard
*
* You can call this function by:
* ReadPageGuard rg;
* WritePageGuard wg = rg.UpgradeWrite();
* Note: You must not use the original ReadPageGuard after calling this function.
*
* @return WritePageGuard
*/
auto UpgradeWrite() -> WritePageGuard;

/** TODO(P2): Add implementation
*
* @brief Move constructor for ReadPageGuard
Expand Down Expand Up @@ -166,6 +190,14 @@ class ReadPageGuard {
return guard_.As<T>();
}

/** The following methods are mainly used for testing */

auto GetPage() const -> Page * { return guard_.GetPage(); }

auto GetBPM() const -> BufferPoolManager * { return guard_.GetBPM(); }

void SetNull() noexcept { guard_.SetNull(); }

private:
// You may choose to get rid of this and add your own private variables.
BasicPageGuard guard_;
Expand Down Expand Up @@ -233,6 +265,14 @@ class WritePageGuard {
return guard_.AsMut<T>();
}

/** The following methods are mainly used for testing */

auto GetPage() const -> Page * { return guard_.GetPage(); }

auto GetBPM() const -> BufferPoolManager * { return guard_.GetBPM(); }

void SetNull() noexcept { guard_.SetNull(); }

private:
// You may choose to get rid of this and add your own private variables.
BasicPageGuard guard_;
Expand Down
15 changes: 15 additions & 0 deletions src/storage/page/page_guard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@

namespace bustub {

auto BasicPageGuard::UpgradeRead() -> ReadPageGuard {
// TODO(p2): Your ReadPageGuard upgrade logic here
return {nullptr, nullptr};
}

auto BasicPageGuard::UpgradeWrite() -> WritePageGuard {
// TODO(p2): Your WritePageGuard upgrade logic here
return {nullptr, nullptr};
}

auto ReadPageGuard::UpgradeWrite() -> WritePageGuard {
// TODO(p2): Your WritePageGuard upgrade logic here
return {nullptr, nullptr};
}

BasicPageGuard::BasicPageGuard(BasicPageGuard &&that) noexcept {}

void BasicPageGuard::Drop() {}
Expand Down
14 changes: 7 additions & 7 deletions test/storage/b_plus_tree_concurrent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void InsertHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, con
delete transaction;
}

// helper function to seperate insert
// helper function to separate 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;
Expand Down Expand Up @@ -88,7 +88,7 @@ void DeleteHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, con
delete transaction;
}

// helper function to seperate delete
// helper function to separate 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) {
Expand Down Expand Up @@ -353,24 +353,24 @@ TEST(BPlusTreeConcurrentTest, DISABLED_MixTest2) {
BPlusTree<GenericKey<8>, RID, GenericComparator<8>> tree("foo_pk", page_id, bpm, comparator);

// Add perserved_keys
std::vector<int64_t> perserved_keys;
std::vector<int64_t> preserved_keys;
std::vector<int64_t> dynamic_keys;
int64_t total_keys = 50;
int64_t sieve = 5;
for (int64_t i = 1; i <= total_keys; i++) {
if (i % sieve == 0) {
perserved_keys.push_back(i);
preserved_keys.push_back(i);
} else {
dynamic_keys.push_back(i);
}
}
InsertHelper(&tree, perserved_keys, 1);
InsertHelper(&tree, preserved_keys, 1);
// Check there are 1000 keys in there
size_t size;

auto insert_task = [&](int tid) { InsertHelper(&tree, dynamic_keys, tid); };
auto delete_task = [&](int tid) { DeleteHelper(&tree, dynamic_keys, tid); };
auto lookup_task = [&](int tid) { LookupHelper(&tree, perserved_keys, tid); };
auto lookup_task = [&](int tid) { LookupHelper(&tree, preserved_keys, tid); };

std::vector<std::thread> threads;
std::vector<std::function<void(int)>> tasks;
Expand All @@ -396,7 +396,7 @@ TEST(BPlusTreeConcurrentTest, DISABLED_MixTest2) {
}
}

ASSERT_EQ(size, perserved_keys.size());
ASSERT_EQ(size, preserved_keys.size());

bpm->UnpinPage(HEADER_PAGE_ID, true);
delete bpm;
Expand Down
Loading
Loading