on half way of setting up bpt

This commit is contained in:
2024-04-25 13:53:52 +00:00
parent 898cc1fa0e
commit 97398bfa48
11 changed files with 157 additions and 2 deletions

View File

@ -1 +1 @@
add_library(bpt STATIC src/disk_manager.cpp src/replacer.cpp src/buffer_pool_manager.cpp) add_library(bpt STATIC src/disk_manager.cpp src/replacer.cpp src/buffer_pool_manager.cpp src/bpt.cpp)

92
bpt/include/bpt/bpt.hpp Normal file
View File

@ -0,0 +1,92 @@
#ifndef BPT_HPP
#define BPT_HPP
#include <shared_mutex>
#include "bpt/bpt_page.hpp"
#include "bpt/buffer_pool_manager.h"
#include "bpt/config.h"
template <typename KeyType, typename KeyComparator>
class BPlusTreeIndexer {
private:
// TODO : insert ?
public:
typedef std::pair<KeyType, b_plus_tree_value_index_t> 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<std::shared_mutex> guard(latch);
// TODO
}
const_iterator lower_bound_const(const KeyType &key) {
std::shared_lock<std::shared_mutex> guard(latch);
// TODO
}
bool Set(const iterator &iter, b_plus_tree_value_index_t value) {
std::unique_lock<std::shared_mutex> guard(latch);
// TODO
}
bool Erase(const iterator &iter) {
std::unique_lock<std::shared_mutex> 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 <typename KeyType, typename KeyComparator>
KeyComparator BPlusTreeIndexer<KeyType, KeyComparator>::key_cmp = KeyComparator();
#endif // BPT_HPP

View File

@ -0,0 +1,17 @@
#ifndef BPT_PAGE_HPP
#define BPT_PAGE_HPP
#include <utility>
#include "bpt/config.h"
#pragma pack(push, 1)
template <typename KeyType, size_t kPageSize = 4096>
class BPlusTreePage {
typedef std::pair<KeyType, b_plus_tree_value_index_t> 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

View File

@ -6,4 +6,6 @@ typedef unsigned int default_numeric_index_t;
typedef default_numeric_index_t page_id_t; typedef default_numeric_index_t page_id_t;
typedef default_numeric_index_t block_id_t; typedef default_numeric_index_t block_id_t;
typedef default_numeric_index_t frame_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 #endif

View File

View File

View File

2
bpt/src/bpt.cpp Normal file
View File

@ -0,0 +1,2 @@
#include "bpt/config.h"
const b_plus_tree_value_index_t kInvalidValueIndex = -1;

View File

@ -9,3 +9,5 @@ add_executable(buffer_pool_manager_test buffer_pool_manager_test.cpp)
target_link_libraries(buffer_pool_manager_test bpt GTest::gtest_main) target_link_libraries(buffer_pool_manager_test bpt GTest::gtest_main)
add_executable(page_guard_test page_guard_test.cpp) add_executable(page_guard_test page_guard_test.cpp)
target_link_libraries(page_guard_test bpt GTest::gtest_main) 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)

40
test/bpt_basic_test.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <gtest/gtest.h>
#include "bpt/bpt.hpp"
#include "bpt/config.h"
namespace bpt_basic_test {
template <size_t length>
class FixLengthString {
public:
char data[length];
};
} // namespace bpt_basic_test
TEST(BasicTest, Compile) {
// test for long long, int, char, long double
BPlusTreePage<long long> page_long_long;
static_assert(sizeof(page_long_long) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<int> page_int;
static_assert(sizeof(page_int) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<char> page_char;
static_assert(sizeof(page_char) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<long double> 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<bpt_basic_test::FixLengthString<5>> page_5;
static_assert(sizeof(page_5) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<bpt_basic_test::FixLengthString<10>> page_10;
static_assert(sizeof(page_10) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<bpt_basic_test::FixLengthString<15>> page_15;
static_assert(sizeof(page_15) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<bpt_basic_test::FixLengthString<20>> page_20;
static_assert(sizeof(page_20) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<bpt_basic_test::FixLengthString<25>> page_25;
static_assert(sizeof(page_25) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<bpt_basic_test::FixLengthString<30>> page_30;
static_assert(sizeof(page_30) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<bpt_basic_test::FixLengthString<35>> page_35;
static_assert(sizeof(page_35) == 4096, "BPlusTreePage size mismatch");
BPlusTreePage<bpt_basic_test::FixLengthString<40>> page_40;
static_assert(sizeof(page_40) == 4096, "BPlusTreePage size mismatch");
}