Skip to content

Commit decbe02

Browse files
authored
feat: implement extendible htable header and directory page helpers (#613)
Signed-off-by: Yuchen Liang <[email protected]>
1 parent 84d9209 commit decbe02

File tree

4 files changed

+159
-50
lines changed

4 files changed

+159
-50
lines changed

src/include/storage/page/extendible_htable_directory_page.h

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* Directory page format:
1515
* --------------------------------------------------------------------------------------
16-
* | MaxSize (4) | GlobalDepth (4) | LocalDepths (512) | BucketPageIds(2048) | Free(1528)
16+
* | MaxDepth (4) | GlobalDepth (4) | LocalDepths (512) | BucketPageIds(2048) | Free(1528)
1717
* --------------------------------------------------------------------------------------
1818
*/
1919

@@ -39,7 +39,8 @@ static constexpr uint64_t HTABLE_DIRECTORY_PAGE_METADATA_SIZE = sizeof(uint32_t)
3939
* Extending the directory implementation to span multiple pages would be a meaningful improvement to the
4040
* implementation.
4141
*/
42-
static constexpr uint64_t HTABLE_DIRECTORY_ARRAY_SIZE = 512;
42+
static constexpr uint64_t HTABLE_DIRECTORY_MAX_DEPTH = 9;
43+
static constexpr uint64_t HTABLE_DIRECTORY_ARRAY_SIZE = 1 << HTABLE_DIRECTORY_MAX_DEPTH;
4344

4445
/**
4546
* Directory Page for extendible hash table.
@@ -53,17 +54,17 @@ class ExtendibleHTableDirectoryPage {
5354
/**
5455
* After creating a new directory page from buffer pool, must call initialize
5556
* method to set default values
56-
* @param max_size Max size of the array in the directory page
57+
* @param max_depth Max depth in the directory page
5758
*/
58-
void Init(int max_size = HTABLE_DIRECTORY_ARRAY_SIZE);
59+
void Init(uint32_t max_depth = HTABLE_DIRECTORY_MAX_DEPTH);
5960

6061
/**
61-
* Get the bucket page id that the key is hashed to
62+
* Get the bucket index that the key is hashed to
6263
*
6364
* @param hash the hash of the key
64-
* @return bucket page_id current key is hashed to
65+
* @return bucket index current key is hashed to
6566
*/
66-
auto HashToBucketPageId(uint32_t hash) -> page_id_t;
67+
auto HashToBucketIndex(uint32_t hash) -> uint32_t;
6768

6869
/**
6970
* Lookup a bucket page using a directory index
@@ -89,31 +90,6 @@ class ExtendibleHTableDirectoryPage {
8990
**/
9091
auto GetSplitImageIndex(uint32_t bucket_idx) -> uint32_t;
9192

92-
/**
93-
* GetGlobalDepthMask - returns a mask of global_depth 1's and the rest 0's.
94-
*
95-
* In Extendible Hashing we map a key to a directory index
96-
* using the following hash + mask function.
97-
*
98-
* DirectoryIndex = Hash(key) & GLOBAL_DEPTH_MASK
99-
*
100-
* where GLOBAL_DEPTH_MASK is a mask with exactly GLOBAL_DEPTH 1's from LSB
101-
* upwards. For example, global depth 3 corresponds to 0x00000007 in a 32-bit
102-
* representation.
103-
*
104-
* @return mask of global_depth 1's and the rest 0's (with 1's from LSB upwards)
105-
*/
106-
auto GetGlobalDepthMask() -> uint32_t;
107-
108-
/**
109-
* GetLocalDepthMask - same as global depth mask, except it
110-
* uses the local depth of the bucket located at bucket_idx
111-
*
112-
* @param bucket_idx the index to use for looking up local depth
113-
* @return mask of local 1's and the rest 0's (with 1's from LSB upwards)
114-
*/
115-
auto GetLocalDepthMask(uint32_t bucket_idx) -> uint32_t;
116-
11793
/**
11894
* Get the global depth of the hash table directory
11995
*
@@ -169,16 +145,6 @@ class ExtendibleHTableDirectoryPage {
169145
*/
170146
void DecrLocalDepth(uint32_t bucket_idx);
171147

172-
/**
173-
* Gets the high bit corresponding to the bucket's local depth.
174-
* This is not the same as the bucket index itself. This method
175-
* is helpful for finding the pair, or "split image", of a bucket.
176-
*
177-
* @param bucket_idx bucket index to lookup
178-
* @return the high bit corresponding to the bucket's local depth
179-
*/
180-
auto GetLocalHighBit(uint32_t bucket_idx) -> uint32_t;
181-
182148
/**
183149
* VerifyIntegrity
184150
*
@@ -195,7 +161,7 @@ class ExtendibleHTableDirectoryPage {
195161
void PrintDirectory();
196162

197163
private:
198-
uint32_t max_size_;
164+
uint32_t max_depth_ __attribute__((__unused__));
199165
uint32_t global_depth_;
200166
uint8_t local_depths_[HTABLE_DIRECTORY_ARRAY_SIZE];
201167
page_id_t bucket_page_ids_[HTABLE_DIRECTORY_ARRAY_SIZE];

src/include/storage/page/extendible_htable_header_page.h

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* Header page format:
1515
* ---------------------------------------------------
16-
* | DirectoryPageIds(2048) | MaxSize (4) | Free(2044)
16+
* | DirectoryPageIds(2048) | MaxDepth (4) | Free(2044)
1717
* ---------------------------------------------------
1818
*/
1919

@@ -27,7 +27,8 @@
2727
namespace bustub {
2828

2929
static constexpr uint64_t HTABLE_HEADER_PAGE_METADATA_SIZE = sizeof(uint32_t);
30-
static constexpr uint64_t HTABLE_HEADER_ARRAY_SIZE = 512;
30+
static constexpr uint64_t HTABLE_HEADER_MAX_DEPTH = 9;
31+
static constexpr uint64_t HTABLE_HEADER_ARRAY_SIZE = 1 << HTABLE_HEADER_MAX_DEPTH;
3132

3233
class ExtendibleHTableHeaderPage {
3334
public:
@@ -38,20 +39,42 @@ class ExtendibleHTableHeaderPage {
3839
/**
3940
* After creating a new header page from buffer pool, must call initialize
4041
* method to set default values
41-
* @param max_size Max size of the array in the header page
42+
* @param max_depth Max depth in the header page
4243
*/
43-
void Init(int max_size = HTABLE_HEADER_ARRAY_SIZE);
44+
void Init(uint32_t max_depth = HTABLE_HEADER_MAX_DEPTH);
4445

45-
auto HashToDirectoryPageId(uint32_t hash) -> page_id_t;
46+
/**
47+
* Get the directory index that the key is hashed to
48+
*
49+
* @param hash the hash of the key
50+
* @return directory index the key is hashed to
51+
*/
52+
auto HashToDirectoryIndex(uint32_t hash) const -> uint32_t;
53+
54+
/**
55+
* Get the directory page id at an index
56+
*
57+
* @param directory_idx index in the directory page id array
58+
* @return directory page_id at index
59+
*/
60+
auto GetDirectoryPageId(uint32_t directory_idx) const -> uint32_t;
61+
62+
/**
63+
* @brief Set the directory page id at an index
64+
*
65+
* @param directory_idx index in the directory page id array
66+
* @param directory_page_id page id of the directory
67+
*/
68+
void SetDirectoryPageId(uint32_t directory_idx, page_id_t directory_page_id);
4669

4770
/**
4871
* Prints the header's occupancy information
4972
*/
50-
void PrintHeader();
73+
void PrintHeader() const;
5174

5275
private:
5376
page_id_t directory_page_ids_[HTABLE_HEADER_ARRAY_SIZE];
54-
uint32_t max_size_;
77+
uint32_t max_depth_;
5578
};
5679

5780
static_assert(sizeof(page_id_t) == 4);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// BusTub
4+
//
5+
// extendible_htable_directory_page.cpp
6+
//
7+
// Identification: src/storage/page/extendible_htable_directory_page.cpp
8+
//
9+
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "storage/page/extendible_htable_directory_page.h"
14+
15+
#include <algorithm>
16+
#include <unordered_map>
17+
18+
#include "common/config.h"
19+
#include "common/logger.h"
20+
21+
namespace bustub {
22+
23+
void ExtendibleHTableDirectoryPage::Init(uint32_t max_depth) {
24+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
25+
}
26+
27+
auto ExtendibleHTableDirectoryPage::HashToBucketIndex(uint32_t hash) -> uint32_t { return 0; }
28+
29+
auto ExtendibleHTableDirectoryPage::GetBucketPageId(uint32_t bucket_idx) -> page_id_t { return INVALID_PAGE_ID; }
30+
31+
void ExtendibleHTableDirectoryPage::SetBucketPageId(uint32_t bucket_idx, page_id_t bucket_page_id) {
32+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
33+
}
34+
35+
auto ExtendibleHTableDirectoryPage::GetSplitImageIndex(uint32_t bucket_idx) -> uint32_t { return 0; }
36+
37+
auto ExtendibleHTableDirectoryPage::GetGlobalDepth() -> uint32_t { return 0; }
38+
39+
void ExtendibleHTableDirectoryPage::IncrGlobalDepth() {
40+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
41+
}
42+
43+
void ExtendibleHTableDirectoryPage::DecrGlobalDepth() {
44+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
45+
}
46+
47+
auto ExtendibleHTableDirectoryPage::CanShrink() -> bool { return false; }
48+
49+
auto ExtendibleHTableDirectoryPage::Size() -> uint32_t { return 0; }
50+
51+
auto ExtendibleHTableDirectoryPage::GetLocalDepth(uint32_t bucket_idx) -> uint32_t { return 0; }
52+
53+
void ExtendibleHTableDirectoryPage::SetLocalDepth(uint32_t bucket_idx, uint8_t local_depth) {
54+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
55+
}
56+
57+
void ExtendibleHTableDirectoryPage::IncrLocalDepth(uint32_t bucket_idx) {
58+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
59+
}
60+
61+
void ExtendibleHTableDirectoryPage::DecrLocalDepth(uint32_t bucket_idx) {
62+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
63+
}
64+
65+
void ExtendibleHTableDirectoryPage::VerifyIntegrity() {
66+
// write your own verification function.
67+
throw NotImplementedException("ExtendibleHTableDirectoryPage is not implemented");
68+
}
69+
70+
void ExtendibleHTableDirectoryPage::PrintDirectory() {
71+
LOG_DEBUG("======== DIRECTORY (global_depth_: %u) ========", global_depth_);
72+
LOG_DEBUG("| bucket_idx | page_id | local_depth |");
73+
for (uint32_t idx = 0; idx < static_cast<uint32_t>(0x1 << global_depth_); idx++) {
74+
LOG_DEBUG("| %u | %u | %u |", idx, bucket_page_ids_[idx], local_depths_[idx]);
75+
}
76+
LOG_DEBUG("================ END DIRECTORY ================");
77+
}
78+
79+
} // namespace bustub
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// BusTub
4+
//
5+
// extendible_htable_header_page.cpp
6+
//
7+
// Identification: src/storage/page/extendible_htable_header_page.cpp
8+
//
9+
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "storage/page/extendible_htable_header_page.h"
14+
15+
#include "common/exception.h"
16+
#include "common/logger.h"
17+
18+
namespace bustub {
19+
20+
void ExtendibleHTableHeaderPage::Init(uint32_t max_depth) {
21+
throw NotImplementedException("ExtendibleHTableHeaderPage is not implemented");
22+
}
23+
24+
auto ExtendibleHTableHeaderPage::HashToDirectoryIndex(uint32_t hash) const -> uint32_t { return 0; }
25+
26+
auto ExtendibleHTableHeaderPage::GetDirectoryPageId(uint32_t directory_idx) const -> uint32_t { return 0; }
27+
28+
void ExtendibleHTableHeaderPage::SetDirectoryPageId(uint32_t directory_idx, page_id_t directory_page_id) {
29+
throw NotImplementedException("ExtendibleHTableHeaderPage is not implemented");
30+
}
31+
32+
void ExtendibleHTableHeaderPage::PrintHeader() const {
33+
LOG_DEBUG("======== HEADER (max_depth_: %u) ========", max_depth_);
34+
LOG_DEBUG("| directory_idx | page_id |");
35+
for (uint32_t idx = 0; idx < static_cast<uint32_t>(1 << max_depth_); idx++) {
36+
LOG_DEBUG("| %u | %u |", idx, directory_page_ids_[idx]);
37+
}
38+
LOG_DEBUG("======== END HEADER ========");
39+
}
40+
41+
} // namespace bustub

0 commit comments

Comments
 (0)