From 97398bfa4894110e31640cfb4ed836fd080f9dca Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Thu, 25 Apr 2024 13:53:52 +0000 Subject: [PATCH] on half way of setting up bpt --- bpt/CMakeLists.txt | 2 +- bpt/include/bpt/bpt.hpp | 92 +++++++++++++++++++++ bpt/include/bpt/bpt_page.hpp | 17 ++++ bpt/include/bpt/config.h | 2 + bpt/include/bpt/disk_map.hpp | 0 bpt/include/bpt/disk_multimap.hpp | 0 bpt/include/bpt/single_value_storage.hpp | 0 bpt/include/bpt/skip_list_value_storage.hpp | 0 bpt/src/bpt.cpp | 2 + test/CMakeLists.txt | 4 +- test/bpt_basic_test.cpp | 40 +++++++++ 11 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 bpt/include/bpt/bpt.hpp create mode 100644 bpt/include/bpt/bpt_page.hpp create mode 100644 bpt/include/bpt/disk_map.hpp create mode 100644 bpt/include/bpt/disk_multimap.hpp create mode 100644 bpt/include/bpt/single_value_storage.hpp create mode 100644 bpt/include/bpt/skip_list_value_storage.hpp create mode 100644 bpt/src/bpt.cpp create mode 100644 test/bpt_basic_test.cpp diff --git a/bpt/CMakeLists.txt b/bpt/CMakeLists.txt index fae0699..b93c9f9 100644 --- a/bpt/CMakeLists.txt +++ b/bpt/CMakeLists.txt @@ -1 +1 @@ -add_library(bpt STATIC src/disk_manager.cpp src/replacer.cpp src/buffer_pool_manager.cpp) \ No newline at end of file +add_library(bpt STATIC src/disk_manager.cpp src/replacer.cpp src/buffer_pool_manager.cpp src/bpt.cpp) \ No newline at end of file diff --git a/bpt/include/bpt/bpt.hpp b/bpt/include/bpt/bpt.hpp new file mode 100644 index 0000000..168473c --- /dev/null +++ b/bpt/include/bpt/bpt.hpp @@ -0,0 +1,92 @@ +#ifndef BPT_HPP +#define BPT_HPP +#include +#include "bpt/bpt_page.hpp" +#include "bpt/buffer_pool_manager.h" +#include "bpt/config.h" +template +class BPlusTreeIndexer { + private: + // TODO : insert ? + public: + typedef std::pair value_type; + class iterator { + BPlusTreeIndexer *domain; + size_t internal_offset; + bool is_end; + WritePageGuard guard; + const KeyType &GetKey() const { + // TODO + } + const b_plus_tree_value_index_t &GetValue() const { + // TODO + } + }; + class const_iterator { + BPlusTreeIndexer *domain; + size_t internal_offset; + bool is_end; + ReadPageGuard guard; + const KeyType &GetKey() const { + // TODO + } + const b_plus_tree_value_index_t &GetValue() const { + // TODO + } + }; + BPlusTreeIndexer(const BPlusTreeIndexer &) = delete; + BPlusTreeIndexer(BPlusTreeIndexer &&) = delete; + BPlusTreeIndexer &operator=(const BPlusTreeIndexer &) = delete; + BPlusTreeIndexer &operator=(BPlusTreeIndexer &&) = delete; + iterator end() { + // TODO + } + iterator lower_bound(const KeyType &key) { + std::shared_lock guard(latch); + // TODO + } + const_iterator lower_bound_const(const KeyType &key) { + std::shared_lock guard(latch); + // TODO + } + bool Set(const iterator &iter, b_plus_tree_value_index_t value) { + std::unique_lock guard(latch); + // TODO + } + bool Erase(const iterator &iter) { + std::unique_lock guard(latch); + // TODO + } + b_plus_tree_value_index_t Get(const KeyType &key) { + auto it = lower_bound_const(key); + if (it == end()) return kInvalidValueIndex; + if (key_cmp(key, it.GetKey())) return kInvalidValueIndex; + return it->second; + } + bool Put(const KeyType &key, b_plus_tree_value_index_t value) { + auto it = lower_bound(key); + if (it != end() && !key_cmp(key, it.GetKey())) { + Set(it, value); + return false; + } + // TODO Insert it + return true; + } + bool Remove(const KeyType &key) { + auto it = lower_bound(key); + if (it == end()) return false; + if (key_cmp(key, it.GetKey())) return false; + Erase(it); + return true; + } + size_t Size() { return siz; } + + private: + page_id_t root_page_id; + size_t siz; + static KeyComparator key_cmp; + std::shared_mutex latch; +}; +template +KeyComparator BPlusTreeIndexer::key_cmp = KeyComparator(); +#endif // BPT_HPP \ No newline at end of file diff --git a/bpt/include/bpt/bpt_page.hpp b/bpt/include/bpt/bpt_page.hpp new file mode 100644 index 0000000..8f072c9 --- /dev/null +++ b/bpt/include/bpt/bpt_page.hpp @@ -0,0 +1,17 @@ +#ifndef BPT_PAGE_HPP +#define BPT_PAGE_HPP +#include +#include "bpt/config.h" +#pragma pack(push, 1) +template +class BPlusTreePage { + typedef std::pair value_type; + page_id_t p_n; + page_id_t p_parent; + unsigned char is_leaf; + const static size_t kMaxKeyCount = (kPageSize - sizeof(page_id_t) * 2 - sizeof(unsigned char)) / sizeof(value_type); + value_type p_data[kMaxKeyCount]; + char filler[kPageSize - sizeof(page_id_t) * 2 - sizeof(unsigned char) - sizeof(value_type) * kMaxKeyCount]; +}; +#pragma pack(pop) +#endif // BPT_PAGE_H \ No newline at end of file diff --git a/bpt/include/bpt/config.h b/bpt/include/bpt/config.h index bd95108..dbbd96c 100644 --- a/bpt/include/bpt/config.h +++ b/bpt/include/bpt/config.h @@ -6,4 +6,6 @@ typedef unsigned int default_numeric_index_t; typedef default_numeric_index_t page_id_t; typedef default_numeric_index_t block_id_t; typedef default_numeric_index_t frame_id_t; +typedef default_numeric_index_t b_plus_tree_value_index_t; +extern const b_plus_tree_value_index_t kInvalidValueIndex; #endif \ No newline at end of file diff --git a/bpt/include/bpt/disk_map.hpp b/bpt/include/bpt/disk_map.hpp new file mode 100644 index 0000000..e69de29 diff --git a/bpt/include/bpt/disk_multimap.hpp b/bpt/include/bpt/disk_multimap.hpp new file mode 100644 index 0000000..e69de29 diff --git a/bpt/include/bpt/single_value_storage.hpp b/bpt/include/bpt/single_value_storage.hpp new file mode 100644 index 0000000..e69de29 diff --git a/bpt/include/bpt/skip_list_value_storage.hpp b/bpt/include/bpt/skip_list_value_storage.hpp new file mode 100644 index 0000000..e69de29 diff --git a/bpt/src/bpt.cpp b/bpt/src/bpt.cpp new file mode 100644 index 0000000..ed77b79 --- /dev/null +++ b/bpt/src/bpt.cpp @@ -0,0 +1,2 @@ +#include "bpt/config.h" +const b_plus_tree_value_index_t kInvalidValueIndex = -1; \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index afab123..8defc7f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,4 +8,6 @@ target_link_libraries(replacer_test bpt GTest::gtest_main) add_executable(buffer_pool_manager_test buffer_pool_manager_test.cpp) target_link_libraries(buffer_pool_manager_test bpt GTest::gtest_main) add_executable(page_guard_test page_guard_test.cpp) -target_link_libraries(page_guard_test bpt GTest::gtest_main) \ No newline at end of file +target_link_libraries(page_guard_test bpt GTest::gtest_main) +add_executable(bpt_basic_test bpt_basic_test.cpp) +target_link_libraries(bpt_basic_test bpt GTest::gtest_main) \ No newline at end of file diff --git a/test/bpt_basic_test.cpp b/test/bpt_basic_test.cpp new file mode 100644 index 0000000..fa777e9 --- /dev/null +++ b/test/bpt_basic_test.cpp @@ -0,0 +1,40 @@ +#include +#include "bpt/bpt.hpp" +#include "bpt/config.h" + +namespace bpt_basic_test { +template +class FixLengthString { + public: + char data[length]; +}; +} // namespace bpt_basic_test +TEST(BasicTest, Compile) { + // test for long long, int, char, long double + BPlusTreePage page_long_long; + static_assert(sizeof(page_long_long) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage page_int; + static_assert(sizeof(page_int) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage page_char; + static_assert(sizeof(page_char) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage page_long_double; + static_assert(sizeof(page_long_double) == 4096, "BPlusTreePage size mismatch"); + + // test for FixLengthString with size = 5, 10, 15, 20, 25, 30, 35, 40; + BPlusTreePage> page_5; + static_assert(sizeof(page_5) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage> page_10; + static_assert(sizeof(page_10) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage> page_15; + static_assert(sizeof(page_15) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage> page_20; + static_assert(sizeof(page_20) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage> page_25; + static_assert(sizeof(page_25) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage> page_30; + static_assert(sizeof(page_30) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage> page_35; + static_assert(sizeof(page_35) == 4096, "BPlusTreePage size mismatch"); + BPlusTreePage> page_40; + static_assert(sizeof(page_40) == 4096, "BPlusTreePage size mismatch"); +} \ No newline at end of file