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

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 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

View File

View File

View File