Skip to content

Commit 84d9209

Browse files
authored
feat: implement extendible htable bucket page helpers (#612)
Signed-off-by: Yuchen Liang <[email protected]>
1 parent 0f31396 commit 84d9209

File tree

3 files changed

+176
-9
lines changed

3 files changed

+176
-9
lines changed

src/include/storage/page/extendible_htable_bucket_page.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
#pragma once
2525

26+
#include <optional>
2627
#include <utility>
2728
#include <vector>
2829

@@ -55,25 +56,33 @@ class ExtendibleHTableBucketPage {
5556
* method to set default values
5657
* @param max_size Max size of the bucket array
5758
*/
58-
void Init(int max_size = HTableBucketArraySize(sizeof(MappingType)));
59+
void Init(uint32_t max_size = HTableBucketArraySize(sizeof(MappingType)));
5960

61+
/**
62+
* Lookup a key
63+
*
64+
* @param key key to lookup
65+
* @param[out] value value to set
66+
* @param cmp the comparator
67+
* @return true if the key and value are present, false if not found.
68+
*/
6069
auto Lookup(const KeyType &key, ValueType &value, const KeyComparator &cmp) const -> bool;
6170

6271
/**
6372
* Attempts to insert a key and value in the bucket.
6473
*
6574
* @param key key to insert
6675
* @param value value to insert
67-
* @return true if inserted, false if duplicate KV pair or bucket is full
76+
* @return true if inserted, false if bucket is full or the same key is already present
6877
*/
69-
auto Insert(KeyType key, ValueType value, const KeyComparator &cmp) -> bool;
78+
auto Insert(const KeyType &key, const ValueType &value, const KeyComparator &cmp) -> bool;
7079

7180
/**
7281
* Removes a key and value.
7382
*
7483
* @return true if removed, false if not found
7584
*/
76-
auto Remove(KeyType key, ValueType value, const KeyComparator &cmp) -> bool;
85+
auto Remove(const KeyType &key, const KeyComparator &cmp) -> bool;
7786

7887
/**
7988
* Gets the key at an index in the bucket.
@@ -91,11 +100,6 @@ class ExtendibleHTableBucketPage {
91100
*/
92101
auto ValueAt(uint32_t bucket_idx) const -> ValueType;
93102

94-
/**
95-
* Remove the KV pair at bucket_idx
96-
*/
97-
void RemoveAt(uint32_t bucket_idx);
98-
99103
/**
100104
* @return whether the bucket is full
101105
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// BusTub
4+
//
5+
// extendible_htable_bucket_page.cpp
6+
//
7+
// Identification: src/storage/page/extendible_htable_bucket_page.cpp
8+
//
9+
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "storage/page/extendible_htable_bucket_page.h"
14+
#include <optional>
15+
16+
namespace bustub {
17+
18+
template <typename KeyType, typename ValueType, typename KeyComparator>
19+
void ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::Init(uint32_t max_size) {
20+
throw NotImplementedException("ExtendibleHTableBucketPage is not implemented");
21+
}
22+
23+
template <typename KeyType, typename ValueType, typename KeyComparator>
24+
auto ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::Lookup(const KeyType &key, ValueType &value,
25+
const KeyComparator &cmp) const -> bool {
26+
return false;
27+
}
28+
29+
template <typename KeyType, typename ValueType, typename KeyComparator>
30+
auto ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::Insert(const KeyType &key, const ValueType &value,
31+
const KeyComparator &cmp) -> bool {
32+
return false;
33+
}
34+
35+
template <typename KeyType, typename ValueType, typename KeyComparator>
36+
auto ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::Remove(const KeyType &key, const KeyComparator &cmp)
37+
-> bool {
38+
return false;
39+
}
40+
41+
template <typename KeyType, typename ValueType, typename KeyComparator>
42+
auto ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::KeyAt(uint32_t bucket_idx) const -> KeyType {
43+
return {};
44+
}
45+
46+
template <typename KeyType, typename ValueType, typename KeyComparator>
47+
auto ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::ValueAt(uint32_t bucket_idx) const -> ValueType {
48+
return {};
49+
}
50+
51+
template <typename KeyType, typename ValueType, typename KeyComparator>
52+
auto ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::IsFull() -> bool {
53+
return false;
54+
}
55+
56+
template <typename KeyType, typename ValueType, typename KeyComparator>
57+
auto ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::IsEmpty() -> bool {
58+
return false;
59+
}
60+
61+
template <typename KeyType, typename ValueType, typename KeyComparator>
62+
void ExtendibleHTableBucketPage<KeyType, ValueType, KeyComparator>::PrintBucket() {
63+
throw NotImplementedException("PrintBucket is not implemented");
64+
}
65+
66+
template class ExtendibleHTableBucketPage<GenericKey<4>, RID, GenericComparator<4>>;
67+
68+
template class ExtendibleHTableBucketPage<GenericKey<8>, RID, GenericComparator<8>>;
69+
70+
template class ExtendibleHTableBucketPage<GenericKey<16>, RID, GenericComparator<16>>;
71+
72+
template class ExtendibleHTableBucketPage<GenericKey<32>, RID, GenericComparator<32>>;
73+
74+
template class ExtendibleHTableBucketPage<GenericKey<64>, RID, GenericComparator<64>>;
75+
76+
} // namespace bustub
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// BusTub
4+
//
5+
// extendible_htable_bucket_page_test.cpp
6+
//
7+
// Identification: test/storage/extendible_htable_bucket_page_test.cpp
8+
//
9+
// Copyright (c) 2015-2023, Carnegie Mellon University Database Group
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include <memory>
14+
#include <thread> // NOLINT
15+
#include <vector>
16+
17+
#include "buffer/buffer_pool_manager.h"
18+
#include "common/logger.h"
19+
#include "gtest/gtest.h"
20+
#include "storage/disk/disk_manager_memory.h"
21+
#include "storage/index/generic_key.h"
22+
#include "storage/index/int_comparator.h"
23+
#include "storage/page/extendible_htable_bucket_page.h"
24+
#include "storage/page/page_guard.h"
25+
#include "test_util.h" // NOLINT
26+
27+
namespace bustub {
28+
29+
// NOLINTNEXTLINE
30+
TEST(ExtendibleHTableTest, DISABLED_BucketPageSampleTest) {
31+
auto disk_mgr = std::make_unique<DiskManagerUnlimitedMemory>();
32+
auto bpm = std::make_unique<BufferPoolManager>(5, disk_mgr.get());
33+
34+
page_id_t bucket_page_id = INVALID_PAGE_ID;
35+
{
36+
BasicPageGuard guard = bpm->NewPageGuarded(&bucket_page_id);
37+
auto bucket_page = guard.AsMut<ExtendibleHTableBucketPage<GenericKey<8>, RID, GenericComparator<8>>>();
38+
bucket_page->Init(10);
39+
40+
auto key_schema = ParseCreateStatement("a bigint");
41+
GenericComparator<8> comparator(key_schema.get());
42+
GenericKey<8> index_key;
43+
RID rid;
44+
45+
// insert a few (key, value) pairs
46+
for (int64_t i = 0; i < 10; i++) {
47+
index_key.SetFromInteger(i);
48+
rid.Set(i, i);
49+
ASSERT_TRUE(bucket_page->Insert(index_key, rid, comparator));
50+
}
51+
52+
index_key.SetFromInteger(11);
53+
rid.Set(11, 11);
54+
ASSERT_TRUE(bucket_page->IsFull());
55+
ASSERT_FALSE(bucket_page->Insert(index_key, rid, comparator));
56+
57+
// check for the inserted pairs
58+
for (unsigned i = 0; i < 10; i++) {
59+
index_key.SetFromInteger(i);
60+
ASSERT_TRUE(bucket_page->Lookup(index_key, rid, comparator));
61+
ASSERT_EQ(rid, RID(i, i));
62+
}
63+
64+
// remove a few pairs
65+
for (unsigned i = 0; i < 10; i++) {
66+
if (i % 2 == 1) {
67+
index_key.SetFromInteger(i);
68+
ASSERT_TRUE(bucket_page->Remove(index_key, comparator));
69+
}
70+
}
71+
72+
for (unsigned i = 0; i < 10; i++) {
73+
if (i % 2 == 1) {
74+
// remove the same pairs again
75+
index_key.SetFromInteger(i);
76+
ASSERT_FALSE(bucket_page->Remove(index_key, comparator));
77+
} else {
78+
index_key.SetFromInteger(i);
79+
ASSERT_TRUE(bucket_page->Remove(index_key, comparator));
80+
}
81+
}
82+
83+
ASSERT_TRUE(bucket_page->IsEmpty());
84+
} // page guard dropped
85+
}
86+
87+
} // namespace bustub

0 commit comments

Comments
 (0)