Skip to content

Commit d9bdaa6

Browse files
authored
feat: declare extendible htable interface (#617)
* feat: declare extendible htable interface Signed-off-by: Yuchen Liang <[email protected]> * fix clang-tidy Signed-off-by: Yuchen Liang <[email protected]> * add identity hash, tests, improve docs Signed-off-by: Yuchen Liang <[email protected]> * chore: update p2 submission files (#619) Signed-off-by: Yuchen Liang <[email protected]> * move helper functions to utility files Signed-off-by: Yuchen Liang <[email protected]> * fix page guard comments Signed-off-by: Yuchen Liang <[email protected]> --------- Signed-off-by: Yuchen Liang <[email protected]>
1 parent e858360 commit d9bdaa6

21 files changed

+1121
-584
lines changed

CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,16 @@ add_custom_target(submit-p1
291291
)
292292

293293
set(P2_FILES
294-
"src/include/storage/page/b_plus_tree_page.h"
295-
"src/storage/page/b_plus_tree_page.cpp"
296-
"src/include/storage/page/b_plus_tree_internal_page.h"
297-
"src/storage/page/b_plus_tree_internal_page.cpp"
298-
"src/include/storage/page/b_plus_tree_leaf_page.h"
299-
"src/storage/page/b_plus_tree_leaf_page.cpp"
300-
"src/include/storage/index/index_iterator.h"
301-
"src/storage/index/index_iterator.cpp"
302-
"src/include/storage/index/b_plus_tree.h"
303-
"src/storage/index/b_plus_tree.cpp"
294+
"src/include/storage/page/page_guard.h"
295+
"src/storage/page/page_guard.cpp"
296+
"src/include/storage/page/extendible_htable_bucket_page.h"
297+
"src/storage/page/extendible_htable_bucket_page.cpp"
298+
"src/include/storage/page/extendible_htable_directory_page.h"
299+
"src/storage/page/extendible_htable_directory_page.cpp"
300+
"src/include/storage/page/extendible_htable_header_page.h"
301+
"src/storage/page/extendible_htable_header_page.cpp"
302+
"src/include/container/disk/hash/disk_extendible_hash_table.h"
303+
"src/container/disk/hash/disk_extendible_hash_table.cpp"
304304
${P1_FILES}
305305
)
306306
add_custom_target(check-clang-tidy-p2

src/container/disk/hash/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ add_library(
22
bustub_container_disk_hash
33
OBJECT
44
disk_extendible_hash_table.cpp
5-
linear_probe_hash_table.cpp)
5+
linear_probe_hash_table.cpp
6+
disk_extendible_hash_table_utils.cpp)
67

78
set(ALL_OBJECT_FILES
89
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:bustub_container_disk_hash>

src/container/disk/hash/disk_extendible_hash_table.cpp

Lines changed: 43 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
//
55
// disk_extendible_hash_table.cpp
66
//
7-
// Identification: src/container/hash/extendible_hash_table.cpp
7+
// Identification: src/container/disk/hash/disk_extendible_hash_table.cpp
88
//
9-
// Copyright (c) 2015-2021, Carnegie Mellon University Database Group
9+
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
1010
//
1111
//===----------------------------------------------------------------------===//
1212

@@ -15,124 +15,84 @@
1515
#include <utility>
1616
#include <vector>
1717

18+
#include "common/config.h"
1819
#include "common/exception.h"
1920
#include "common/logger.h"
21+
#include "common/macros.h"
2022
#include "common/rid.h"
23+
#include "common/util/hash_util.h"
2124
#include "container/disk/hash/disk_extendible_hash_table.h"
25+
#include "storage/index/hash_comparator.h"
26+
#include "storage/page/extendible_htable_bucket_page.h"
27+
#include "storage/page/extendible_htable_directory_page.h"
28+
#include "storage/page/extendible_htable_header_page.h"
29+
#include "storage/page/page_guard.h"
2230

2331
namespace bustub {
2432

25-
template <typename KeyType, typename ValueType, typename KeyComparator>
26-
HASH_TABLE_TYPE::DiskExtendibleHashTable(const std::string &name, BufferPoolManager *buffer_pool_manager,
27-
const KeyComparator &comparator, HashFunction<KeyType> hash_fn)
28-
: buffer_pool_manager_(buffer_pool_manager), comparator_(comparator), hash_fn_(std::move(hash_fn)) {
29-
// implement me!
30-
}
31-
32-
/*****************************************************************************
33-
* HELPERS
34-
*****************************************************************************/
35-
/**
36-
* Hash - simple helper to downcast MurmurHash's 64-bit hash to 32-bit
37-
* for extendible hashing.
38-
*
39-
* @param key the key to hash
40-
* @return the downcasted 32-bit hash
41-
*/
42-
template <typename KeyType, typename ValueType, typename KeyComparator>
43-
auto HASH_TABLE_TYPE::Hash(KeyType key) -> uint32_t {
44-
return static_cast<uint32_t>(hash_fn_.GetHash(key));
45-
}
46-
47-
template <typename KeyType, typename ValueType, typename KeyComparator>
48-
inline auto HASH_TABLE_TYPE::KeyToDirectoryIndex(KeyType key, HashTableDirectoryPage *dir_page) -> uint32_t {
49-
return 0;
50-
}
51-
52-
template <typename KeyType, typename ValueType, typename KeyComparator>
53-
inline auto HASH_TABLE_TYPE::KeyToPageId(KeyType key, HashTableDirectoryPage *dir_page) -> page_id_t {
54-
return 0;
55-
}
56-
57-
template <typename KeyType, typename ValueType, typename KeyComparator>
58-
auto HASH_TABLE_TYPE::FetchDirectoryPage() -> HashTableDirectoryPage * {
59-
return nullptr;
60-
}
61-
62-
template <typename KeyType, typename ValueType, typename KeyComparator>
63-
auto HASH_TABLE_TYPE::FetchBucketPage(page_id_t bucket_page_id) -> HASH_TABLE_BUCKET_TYPE * {
64-
return nullptr;
33+
template <typename K, typename V, typename KC>
34+
DiskExtendibleHashTable<K, V, KC>::DiskExtendibleHashTable(const std::string &name, BufferPoolManager *bpm,
35+
const KC &cmp, const HashFunction<K> &hash_fn,
36+
uint32_t header_max_depth, uint32_t directory_max_depth,
37+
uint32_t bucket_max_size)
38+
: bpm_(bpm),
39+
cmp_(cmp),
40+
hash_fn_(std::move(hash_fn)),
41+
header_max_depth_(header_max_depth),
42+
directory_max_depth_(directory_max_depth),
43+
bucket_max_size_(bucket_max_size) {
44+
throw NotImplementedException("DiskExtendibleHashTable is not implemented");
6545
}
6646

6747
/*****************************************************************************
6848
* SEARCH
6949
*****************************************************************************/
70-
template <typename KeyType, typename ValueType, typename KeyComparator>
71-
auto HASH_TABLE_TYPE::GetValue(Transaction *transaction, const KeyType &key, std::vector<ValueType> *result) -> bool {
50+
template <typename K, typename V, typename KC>
51+
auto DiskExtendibleHashTable<K, V, KC>::GetValue(const K &key, std::vector<V> *result, Transaction *transaction) const
52+
-> bool {
7253
return false;
7354
}
7455

7556
/*****************************************************************************
7657
* INSERTION
7758
*****************************************************************************/
78-
template <typename KeyType, typename ValueType, typename KeyComparator>
79-
auto HASH_TABLE_TYPE::Insert(Transaction *transaction, const KeyType &key, const ValueType &value) -> bool {
59+
60+
template <typename K, typename V, typename KC>
61+
auto DiskExtendibleHashTable<K, V, KC>::Insert(const K &key, const V &value, Transaction *transaction) -> bool {
8062
return false;
8163
}
8264

83-
template <typename KeyType, typename ValueType, typename KeyComparator>
84-
auto HASH_TABLE_TYPE::SplitInsert(Transaction *transaction, const KeyType &key, const ValueType &value) -> bool {
65+
template <typename K, typename V, typename KC>
66+
auto DiskExtendibleHashTable<K, V, KC>::InsertToNewDirectory(ExtendibleHTableHeaderPage *header, uint32_t directory_idx,
67+
uint32_t hash, const K &key, const V &value) -> bool {
8568
return false;
8669
}
8770

88-
/*****************************************************************************
89-
* REMOVE
90-
*****************************************************************************/
91-
template <typename KeyType, typename ValueType, typename KeyComparator>
92-
auto HASH_TABLE_TYPE::Remove(Transaction *transaction, const KeyType &key, const ValueType &value) -> bool {
71+
template <typename K, typename V, typename KC>
72+
auto DiskExtendibleHashTable<K, V, KC>::InsertToNewBucket(ExtendibleHTableDirectoryPage *directory, uint32_t bucket_idx,
73+
const K &key, const V &value) -> bool {
9374
return false;
9475
}
9576

96-
/*****************************************************************************
97-
* MERGE
98-
*****************************************************************************/
99-
template <typename KeyType, typename ValueType, typename KeyComparator>
100-
void HASH_TABLE_TYPE::Merge(Transaction *transaction, const KeyType &key, const ValueType &value) {}
101-
102-
/*****************************************************************************
103-
* GETGLOBALDEPTH - DO NOT TOUCH
104-
*****************************************************************************/
105-
template <typename KeyType, typename ValueType, typename KeyComparator>
106-
auto HASH_TABLE_TYPE::GetGlobalDepth() -> uint32_t {
107-
table_latch_.RLock();
108-
HashTableDirectoryPage *dir_page = FetchDirectoryPage();
109-
uint32_t global_depth = dir_page->GetGlobalDepth();
110-
assert(buffer_pool_manager_->UnpinPage(directory_page_id_, false));
111-
table_latch_.RUnlock();
112-
return global_depth;
77+
template <typename K, typename V, typename KC>
78+
void DiskExtendibleHashTable<K, V, KC>::UpdateDirectoryMapping(ExtendibleHTableDirectoryPage *directory,
79+
uint32_t new_bucket_idx, page_id_t new_bucket_page_id,
80+
uint32_t new_local_depth, uint32_t local_depth_mask) {
81+
throw NotImplementedException("DiskExtendibleHashTable is not implemented");
11382
}
11483

11584
/*****************************************************************************
116-
* VERIFY INTEGRITY - DO NOT TOUCH
85+
* REMOVE
11786
*****************************************************************************/
118-
template <typename KeyType, typename ValueType, typename KeyComparator>
119-
void HASH_TABLE_TYPE::VerifyIntegrity() {
120-
table_latch_.RLock();
121-
HashTableDirectoryPage *dir_page = FetchDirectoryPage();
122-
dir_page->VerifyIntegrity();
123-
assert(buffer_pool_manager_->UnpinPage(directory_page_id_, false));
124-
table_latch_.RUnlock();
87+
template <typename K, typename V, typename KC>
88+
auto DiskExtendibleHashTable<K, V, KC>::Remove(const K &key, Transaction *transaction) -> bool {
89+
return false;
12590
}
12691

127-
/*****************************************************************************
128-
* TEMPLATE DEFINITIONS - DO NOT TOUCH
129-
*****************************************************************************/
13092
template class DiskExtendibleHashTable<int, int, IntComparator>;
131-
13293
template class DiskExtendibleHashTable<GenericKey<4>, RID, GenericComparator<4>>;
13394
template class DiskExtendibleHashTable<GenericKey<8>, RID, GenericComparator<8>>;
13495
template class DiskExtendibleHashTable<GenericKey<16>, RID, GenericComparator<16>>;
13596
template class DiskExtendibleHashTable<GenericKey<32>, RID, GenericComparator<32>>;
13697
template class DiskExtendibleHashTable<GenericKey<64>, RID, GenericComparator<64>>;
137-
13898
} // namespace bustub
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// BusTub
4+
//
5+
// disk_extendible_hash_table_utils.cpp
6+
//
7+
// Identification: src/container/disk/hash/disk_extendible_hash_table_utils.cpp
8+
//
9+
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <iostream>
14+
15+
#include "container/disk/hash/disk_extendible_hash_table.h"
16+
17+
namespace bustub {
18+
19+
/**
20+
* Hash - simple helper to downcast MurmurHash's 64-bit hash to 32-bit
21+
* for extendible hashing.
22+
*
23+
* @param key the key to hash
24+
* @return the downcasted 32-bit hash
25+
*/
26+
template <typename K, typename V, typename KC>
27+
auto DiskExtendibleHashTable<K, V, KC>::Hash(K key) const -> uint32_t {
28+
return static_cast<uint32_t>(hash_fn_.GetHash(key));
29+
}
30+
31+
/**
32+
* @brief Identity Hash for testing purposes.
33+
*/
34+
template <>
35+
auto DiskExtendibleHashTable<int, int, IntComparator>::Hash(int key) const -> uint32_t {
36+
return static_cast<uint32_t>(key);
37+
}
38+
39+
template <typename K, typename V, typename KC>
40+
void DiskExtendibleHashTable<K, V, KC>::PrintHT() const {
41+
std::cout << "\n";
42+
std::cout << "==================== PRINT! ====================\n";
43+
BasicPageGuard header_guard = bpm_->FetchPageBasic(header_page_id_);
44+
auto *header = header_guard.As<ExtendibleHTableHeaderPage>();
45+
46+
header->PrintHeader();
47+
48+
for (uint32_t idx = 0; idx < header->MaxSize(); idx++) {
49+
page_id_t directory_page_id = header->GetDirectoryPageId(idx);
50+
if (directory_page_id == INVALID_PAGE_ID) {
51+
std::cout << "Directory " << idx << ", page id: " << directory_page_id << "\n";
52+
continue;
53+
}
54+
BasicPageGuard directory_guard = bpm_->FetchPageBasic(directory_page_id);
55+
auto *directory = directory_guard.As<ExtendibleHTableDirectoryPage>();
56+
57+
std::cout << "Directory " << idx << ", page id: " << directory_page_id << "\n";
58+
directory->PrintDirectory();
59+
60+
for (uint32_t idx2 = 0; idx2 < directory->Size(); idx2++) {
61+
page_id_t bucket_page_id = directory->GetBucketPageId(idx2);
62+
BasicPageGuard bucket_guard = bpm_->FetchPageBasic(bucket_page_id);
63+
auto *bucket = bucket_guard.As<ExtendibleHTableBucketPage<K, V, KC>>();
64+
65+
std::cout << "Bucket " << idx2 << ", page id: " << bucket_page_id << "\n";
66+
bucket->PrintBucket();
67+
}
68+
}
69+
std::cout << "==================== END OF PRINT! ====================\n";
70+
std::cout << "\n";
71+
}
72+
73+
/*****************************************************************************
74+
* Verification
75+
*****************************************************************************/
76+
77+
template <typename K, typename V, typename KC>
78+
void DiskExtendibleHashTable<K, V, KC>::VerifyIntegrity() const {
79+
BUSTUB_ASSERT(header_page_id_ != INVALID_PAGE_ID, "header page id is invalid");
80+
BasicPageGuard header_guard = bpm_->FetchPageBasic(header_page_id_);
81+
auto *header = header_guard.As<ExtendibleHTableHeaderPage>();
82+
83+
// for each of the directory pages, check their integrity using directory page VerifyIntegrity
84+
for (uint32_t idx = 0; idx < header->MaxSize(); idx++) {
85+
auto directory_page_id = header->GetDirectoryPageId(idx);
86+
if (static_cast<int>(directory_page_id) != INVALID_PAGE_ID) {
87+
BasicPageGuard directory_guard = bpm_->FetchPageBasic(directory_page_id);
88+
auto *directory = directory_guard.As<ExtendibleHTableDirectoryPage>();
89+
directory->VerifyIntegrity();
90+
}
91+
}
92+
}
93+
94+
template class DiskExtendibleHashTable<int, int, IntComparator>;
95+
template class DiskExtendibleHashTable<GenericKey<4>, RID, GenericComparator<4>>;
96+
template class DiskExtendibleHashTable<GenericKey<8>, RID, GenericComparator<8>>;
97+
template class DiskExtendibleHashTable<GenericKey<16>, RID, GenericComparator<16>>;
98+
template class DiskExtendibleHashTable<GenericKey<32>, RID, GenericComparator<32>>;
99+
template class DiskExtendibleHashTable<GenericKey<64>, RID, GenericComparator<64>>;
100+
101+
} // namespace bustub

0 commit comments

Comments
 (0)