Skip to content

Commit

Permalink
Apply comment suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
lanlou1554 committed Sep 30, 2024
1 parent d15b30f commit 9aa2641
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/include/storage/index/b_plus_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class BPlusTree {

public:
explicit BPlusTree(std::string name, page_id_t header_page_id, BufferPoolManager *buffer_pool_manager,
const KeyComparator &comparator, int leaf_max_size = LEAF_PAGE_SIZE,
int internal_max_size = INTERNAL_PAGE_SIZE);
const KeyComparator &comparator, int leaf_max_size = LEAF_PAGE_SLOT_CNT,
int internal_max_size = INTERNAL_PAGE_SLOT_CNT);

// Returns true if this B+ tree has no keys and values.
auto IsEmpty() const -> bool;
Expand Down
14 changes: 7 additions & 7 deletions src/include/storage/page/b_plus_tree_internal_page.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace bustub {

#define B_PLUS_TREE_INTERNAL_PAGE_TYPE BPlusTreeInternalPage<KeyType, ValueType, KeyComparator>
#define INTERNAL_PAGE_HEADER_SIZE 12
#define INTERNAL_PAGE_SIZE \
#define INTERNAL_PAGE_SLOT_CNT \
((BUSTUB_PAGE_SIZE - INTERNAL_PAGE_HEADER_SIZE) / ((int)(sizeof(KeyType) + sizeof(ValueType)))) // NOLINT

/**
Expand All @@ -34,9 +34,9 @@ namespace bustub {
* ---------
* | HEADER |
* ---------
* ---------------------------------
* | KEY(1) | KEY(2) | ... | KEY(n) |
* ---------------------------------
* ------------------------------------------
* | KEY(1)(INVALID) | KEY(2) | ... | KEY(n) |
* ------------------------------------------
* ---------------------------------------------
* | PAGE_ID(1) | PAGE_ID(2) | ... | PAGE_ID(n) |
* ---------------------------------------------
Expand All @@ -53,7 +53,7 @@ class BPlusTreeInternalPage : public BPlusTreePage {
* the creation of a new page to make a valid `BPlusTreeInternalPage`
* @param max_size Maximal size of the page
*/
void Init(int max_size = INTERNAL_PAGE_SIZE);
void Init(int max_size = INTERNAL_PAGE_SLOT_CNT);

/**
* @param index The index of the key to get. Index must be non-zero.
Expand Down Expand Up @@ -107,8 +107,8 @@ class BPlusTreeInternalPage : public BPlusTreePage {

private:
// Array members for page data.
KeyType key_array_[INTERNAL_PAGE_SIZE];
ValueType value_array_[INTERNAL_PAGE_SIZE];
KeyType key_array_[INTERNAL_PAGE_SLOT_CNT];
ValueType value_array_[INTERNAL_PAGE_SLOT_CNT];
// (Fall 2024) Feel free to add more fields and helper functions below if needed
};

Expand Down
8 changes: 4 additions & 4 deletions src/include/storage/page/b_plus_tree_leaf_page.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace bustub {

#define B_PLUS_TREE_LEAF_PAGE_TYPE BPlusTreeLeafPage<KeyType, ValueType, KeyComparator>
#define LEAF_PAGE_HEADER_SIZE 16
#define LEAF_PAGE_SIZE ((BUSTUB_PAGE_SIZE - LEAF_PAGE_HEADER_SIZE) / (sizeof(KeyType) + sizeof(ValueType)))
#define LEAF_PAGE_SLOT_CNT ((BUSTUB_PAGE_SIZE - LEAF_PAGE_HEADER_SIZE) / (sizeof(KeyType) + sizeof(ValueType)))

/**
* Store indexed key and record id (record id = page id combined with slot id,
Expand Down Expand Up @@ -58,7 +58,7 @@ class BPlusTreeLeafPage : public BPlusTreePage {
* method to set default values
* @param max_size Max size of the leaf node
*/
void Init(int max_size = LEAF_PAGE_SIZE);
void Init(int max_size = LEAF_PAGE_SLOT_CNT);

// Helper methods
auto GetNextPageId() const -> page_id_t;
Expand Down Expand Up @@ -93,8 +93,8 @@ class BPlusTreeLeafPage : public BPlusTreePage {
private:
page_id_t next_page_id_;
// Array members for page data.
KeyType key_array_[LEAF_PAGE_SIZE];
ValueType value_array_[LEAF_PAGE_SIZE];
KeyType key_array_[LEAF_PAGE_SLOT_CNT];
ValueType value_array_[LEAF_PAGE_SLOT_CNT];
// (Fall 2024) Feel free to add more fields and helper functions below if needed
};

Expand Down
42 changes: 21 additions & 21 deletions test/storage/b_plus_tree_concurrent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ using bustub::DiskManagerUnlimitedMemory;

// helper function to launch multiple threads
template <typename... Args>
void LaunchParallelTest(uint64_t num_threads, uint64_t txn_id_start, Args &&...args) {
void LaunchParallelTest(uint64_t num_threads, Args &&...args) {
std::vector<std::thread> thread_group;

// Launch a group of threads
for (uint64_t thread_itr = 0; thread_itr < num_threads; ++thread_itr) {
thread_group.push_back(std::thread(args..., txn_id_start + thread_itr, thread_itr));
thread_group.push_back(std::thread(args..., thread_itr));
}

// Join the threads with the main thread
Expand All @@ -45,7 +45,7 @@ void LaunchParallelTest(uint64_t num_threads, uint64_t txn_id_start, Args &&...a

// helper function to insert
void InsertHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, const std::vector<int64_t> &keys,
uint64_t tid, __attribute__((unused)) uint64_t thread_itr = 0) {
__attribute__((unused)) uint64_t thread_itr = 0) {
GenericKey<8> index_key;
RID rid;

Expand All @@ -59,7 +59,7 @@ void InsertHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, con

// helper function to seperate insert
void InsertHelperSplit(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, const std::vector<int64_t> &keys,
int total_threads, uint64_t tid, __attribute__((unused)) uint64_t thread_itr) {
int total_threads, __attribute__((unused)) uint64_t thread_itr) {
GenericKey<8> index_key;
RID rid;

Expand All @@ -75,7 +75,7 @@ void InsertHelperSplit(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree

// helper function to delete
void DeleteHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, const std::vector<int64_t> &remove_keys,
uint64_t tid, __attribute__((unused)) uint64_t thread_itr = 0) {
__attribute__((unused)) uint64_t thread_itr = 0) {
GenericKey<8> index_key;

for (auto key : remove_keys) {
Expand All @@ -86,7 +86,7 @@ void DeleteHelper(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree, con

// helper function to seperate delete
void DeleteHelperSplit(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree,
const std::vector<int64_t> &remove_keys, int total_threads, uint64_t tid,
const std::vector<int64_t> &remove_keys, int total_threads,
__attribute__((unused)) uint64_t thread_itr) {
GenericKey<8> index_key;

Expand All @@ -99,7 +99,7 @@ void DeleteHelperSplit(BPlusTree<GenericKey<8>, RID, GenericComparator<8>> *tree
}

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) {
__attribute__((unused)) uint64_t thread_itr = 0) {
GenericKey<8> index_key;
RID rid;
for (auto key : keys) {
Expand Down Expand Up @@ -139,7 +139,7 @@ void InsertTest1Call() {
for (int64_t key = 1; key < scale_factor; key++) {
keys.push_back(key);
}
LaunchParallelTest(2, 0, InsertHelper, &tree, keys);
LaunchParallelTest(2, InsertHelper, &tree, keys);

std::vector<RID> rids;
GenericKey<8> index_key;
Expand Down Expand Up @@ -194,7 +194,7 @@ void InsertTest2Call() {
for (int64_t key = 1; key < scale_factor; key++) {
keys.push_back(key);
}
LaunchParallelTest(2, 0, InsertHelperSplit, &tree, keys, 2);
LaunchParallelTest(2, InsertHelperSplit, &tree, keys, 2);

std::vector<RID> rids;
GenericKey<8> index_key;
Expand Down Expand Up @@ -245,10 +245,10 @@ void DeleteTest1Call() {

// sequential insert
std::vector<int64_t> keys = {1, 2, 3, 4, 5};
InsertHelper(&tree, keys, 1);
InsertHelper(&tree, keys);

std::vector<int64_t> remove_keys = {1, 5, 3, 4};
LaunchParallelTest(2, 1, DeleteHelper, &tree, remove_keys);
LaunchParallelTest(2, DeleteHelper, &tree, remove_keys);

int64_t start_key = 2;
int64_t current_key = start_key;
Expand Down Expand Up @@ -289,10 +289,10 @@ void DeleteTest2Call() {

// sequential insert
std::vector<int64_t> keys = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
InsertHelper(&tree, keys, 1);
InsertHelper(&tree, keys);

std::vector<int64_t> remove_keys = {1, 4, 3, 2, 5, 6};
LaunchParallelTest(2, 1, DeleteHelperSplit, &tree, remove_keys, 2);
LaunchParallelTest(2, DeleteHelperSplit, &tree, remove_keys, 2);

int64_t start_key = 7;
int64_t current_key = start_key;
Expand Down Expand Up @@ -344,10 +344,10 @@ void MixTest1Call() {
}
}
// Insert all the keys to delete
InsertHelper(&tree, for_delete, 1);
InsertHelper(&tree, for_delete);

auto insert_task = [&](int tid) { InsertHelper(&tree, for_insert, tid); };
auto delete_task = [&](int tid) { DeleteHelper(&tree, for_delete, tid); };
auto insert_task = [&](int tid) { InsertHelper(&tree, for_insert); };
auto delete_task = [&](int tid) { DeleteHelper(&tree, for_delete); };
std::vector<std::function<void(int)>> tasks;
tasks.emplace_back(insert_task);
tasks.emplace_back(delete_task);
Expand Down Expand Up @@ -404,13 +404,13 @@ void MixTest2Call() {
dynamic_keys.push_back(i);
}
}
InsertHelper(&tree, perserved_keys, 1);
// Check there are 1000 keys in there
InsertHelper(&tree, perserved_keys);

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 insert_task = [&](int tid) { InsertHelper(&tree, dynamic_keys); };
auto delete_task = [&](int tid) { DeleteHelper(&tree, dynamic_keys); };
auto lookup_task = [&](int tid) { LookupHelper(&tree, perserved_keys); };

std::vector<std::thread> threads;
std::vector<std::function<void(int)>> tasks;
Expand Down

0 comments on commit 9aa2641

Please sign in to comment.