From 4a5dbd9f67a66d56e44c709035ab1ac308899136 Mon Sep 17 00:00:00 2001 From: Yuchen Liang Date: Mon, 6 Nov 2023 13:20:14 -0500 Subject: [PATCH 1/2] [DNM] Testing Extendible HT Wrapper for Fall 2023 Signed-off-by: Yuchen Liang --- .../disk/hash/disk_extendible_hash_table.cpp | 49 ++++++++++++------- .../disk/hash/disk_extendible_hash_table.h | 15 ++++-- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/container/disk/hash/disk_extendible_hash_table.cpp b/src/container/disk/hash/disk_extendible_hash_table.cpp index 460901b74..54f0d3443 100644 --- a/src/container/disk/hash/disk_extendible_hash_table.cpp +++ b/src/container/disk/hash/disk_extendible_hash_table.cpp @@ -11,22 +11,17 @@ //===----------------------------------------------------------------------===// #include -#include -#include -#include +#include // NOLINT #include "common/config.h" #include "common/exception.h" #include "common/logger.h" #include "common/macros.h" #include "common/rid.h" -#include "common/util/hash_util.h" #include "container/disk/hash/disk_extendible_hash_table.h" -#include "storage/index/hash_comparator.h" -#include "storage/page/extendible_htable_bucket_page.h" -#include "storage/page/extendible_htable_directory_page.h" -#include "storage/page/extendible_htable_header_page.h" -#include "storage/page/page_guard.h" +#include "storage/index/stl_comparator_wrapper.h" +#include "storage/index/stl_equal_wrapper.h" +#include "storage/index/stl_hasher_wrapper.h" namespace bustub { @@ -36,20 +31,26 @@ DiskExtendibleHashTable::DiskExtendibleHashTable(const std::string &na uint32_t header_max_depth, uint32_t directory_max_depth, uint32_t bucket_max_size) : bpm_(bpm), - cmp_(cmp), - hash_fn_(std::move(hash_fn)), header_max_depth_(header_max_depth), directory_max_depth_(directory_max_depth), - bucket_max_size_(bucket_max_size) { - throw NotImplementedException("DiskExtendibleHashTable is not implemented"); -} + bucket_max_size_(bucket_max_size), + hash_fn_(hash_fn), + cmp_(StlComparatorWrapper(cmp)), + eq_(StlEqualWrapper(cmp)), + data_(0, StlHasherWrapper(hash_fn_), eq_) {} /***************************************************************************** * SEARCH *****************************************************************************/ template -auto DiskExtendibleHashTable::GetValue(const K &key, std::vector *result, Transaction *transaction) const +auto DiskExtendibleHashTable::GetValue(const K &key, std::vector *result, Transaction *transaction) -> bool { + std::scoped_lock lock(mu_); + if (auto it = data_.find(key); it != data_.end()) { + *result = std::vector{it->second}; + return true; + } + *result = {}; return false; } @@ -59,26 +60,31 @@ auto DiskExtendibleHashTable::GetValue(const K &key, std::vector *r template auto DiskExtendibleHashTable::Insert(const K &key, const V &value, Transaction *transaction) -> bool { - return false; + std::scoped_lock lock(mu_); + if (data_.find(key) != data_.end()) { + return false; + } + data_.emplace(key, value); + return true; } template auto DiskExtendibleHashTable::InsertToNewDirectory(ExtendibleHTableHeaderPage *header, uint32_t directory_idx, uint32_t hash, const K &key, const V &value) -> bool { - return false; + UNREACHABLE("Not implemented"); } template auto DiskExtendibleHashTable::InsertToNewBucket(ExtendibleHTableDirectoryPage *directory, uint32_t bucket_idx, const K &key, const V &value) -> bool { - return false; + UNREACHABLE("Not implemented"); } template void DiskExtendibleHashTable::UpdateDirectoryMapping(ExtendibleHTableDirectoryPage *directory, uint32_t new_bucket_idx, page_id_t new_bucket_page_id, uint32_t new_local_depth, uint32_t local_depth_mask) { - throw NotImplementedException("DiskExtendibleHashTable is not implemented"); + UNREACHABLE("Not implemented"); } /***************************************************************************** @@ -86,6 +92,11 @@ void DiskExtendibleHashTable::UpdateDirectoryMapping(ExtendibleHTableD *****************************************************************************/ template auto DiskExtendibleHashTable::Remove(const K &key, Transaction *transaction) -> bool { + std::scoped_lock lock(mu_); + if (auto it = data_.find(key); it != data_.end()) { + data_.erase(it); + return true; + } return false; } diff --git a/src/include/container/disk/hash/disk_extendible_hash_table.h b/src/include/container/disk/hash/disk_extendible_hash_table.h index ba83ae76c..b2274bc8b 100644 --- a/src/include/container/disk/hash/disk_extendible_hash_table.h +++ b/src/include/container/disk/hash/disk_extendible_hash_table.h @@ -13,8 +13,10 @@ #pragma once #include +#include // NOLINT #include #include +#include #include #include @@ -22,6 +24,9 @@ #include "common/config.h" #include "concurrency/transaction.h" #include "container/hash/hash_function.h" +#include "storage/index/stl_comparator_wrapper.h" +#include "storage/index/stl_equal_wrapper.h" +#include "storage/index/stl_hasher_wrapper.h" #include "storage/page/extendible_htable_bucket_page.h" #include "storage/page/extendible_htable_directory_page.h" #include "storage/page/extendible_htable_header_page.h" @@ -83,7 +88,7 @@ class DiskExtendibleHashTable { * @param transaction the current transaction * @return the value(s) associated with the given key */ - auto GetValue(const K &key, std::vector *result, Transaction *transaction = nullptr) const -> bool; + auto GetValue(const K &key, std::vector *result, Transaction *transaction = nullptr) -> bool; /** * Helper function to verify the integrity of the extendible hash table's directory. @@ -126,12 +131,16 @@ class DiskExtendibleHashTable { // member variables std::string index_name_; BufferPoolManager *bpm_; - KC cmp_; - HashFunction hash_fn_; uint32_t header_max_depth_; uint32_t directory_max_depth_; uint32_t bucket_max_size_; page_id_t header_page_id_; + + HashFunction hash_fn_; + StlComparatorWrapper cmp_; + StlEqualWrapper eq_; + std::mutex mu_; + std::unordered_map, StlEqualWrapper> data_; }; } // namespace bustub From 93f2f732e450b9f8fcc5b468f1ad0fa058a15229 Mon Sep 17 00:00:00 2001 From: Lan Lou Date: Sun, 17 Mar 2024 12:33:01 -0400 Subject: [PATCH 2/2] fix test for p3 --- src/container/disk/hash/disk_extendible_hash_table.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/container/disk/hash/disk_extendible_hash_table.cpp b/src/container/disk/hash/disk_extendible_hash_table.cpp index 54f0d3443..901c1d889 100644 --- a/src/container/disk/hash/disk_extendible_hash_table.cpp +++ b/src/container/disk/hash/disk_extendible_hash_table.cpp @@ -47,7 +47,7 @@ auto DiskExtendibleHashTable::GetValue(const K &key, std::vector *r -> bool { std::scoped_lock lock(mu_); if (auto it = data_.find(key); it != data_.end()) { - *result = std::vector{it->second}; + result->push_back(it->second); return true; } *result = {};