ready to write delete

This commit is contained in:
2024-04-28 15:37:51 +00:00
parent 81d96aadaf
commit 1632870656
9 changed files with 150 additions and 42 deletions

View File

@ -88,8 +88,8 @@ class BPlusTreeIndexer {
.second;
}
// Then, use memmove to move the key_point pairs
fprintf(stderr, "parent_page_guard.template As<PageType>()->data.key_count = %d\n",
(int)parent_page_guard.template As<PageType>()->data.key_count);
// fprintf(stderr, "parent_page_guard.template As<PageType>()->data.key_count = %d\n",
// (int)parent_page_guard.template As<PageType>()->data.key_count);
if (pos.path[pos.path.size() - 2].second < parent_page_guard.template As<PageType>()->data.key_count) {
memmove(parent_page_guard.template AsMut<PageType>()->data.p_data + pos.path[pos.path.size() - 2].second + 1,
parent_page_guard.template As<PageType>()->data.p_data + pos.path[pos.path.size() - 2].second,
@ -178,10 +178,10 @@ class BPlusTreeIndexer {
new_page_guard.template AsMut<PageType>()->data.key_count = _ActualDataType::kMinNumberOfKeysForLeaf;
page_guard.template AsMut<PageType>()->data.key_count -= _ActualDataType::kMinNumberOfKeysForLeaf - 1;
}
fprintf(stderr, "Evicting page %d\n", (int)pos.path.back().first.PageId());
fprintf(stderr, "page id of page_guard = %d\n", (int)page_guard.PageId());
// fprintf(stderr, "Evicting page %d\n", (int)pos.path.back().first.PageId());
// fprintf(stderr, "page id of page_guard = %d\n", (int)page_guard.PageId());
pos.path.pop_back();
fprintf(stderr, "the page id of the res page in pos %d\n", (int)pos.path.back().first.PageId());
// fprintf(stderr, "the page id of the res page in pos %d\n", (int)pos.path.back().first.PageId());
if (pos.path.size() == 1) {
// we have split the root page, update and quit
page_guard.template AsMut<PageType>()->data.page_status &= ~PageStatusType::ROOT;
@ -199,7 +199,7 @@ class BPlusTreeIndexer {
}
void InsertEntryAt(PositionSignType &pos, const KeyType &key, b_plus_tree_value_index_t value,
bool is_fixing_up_recursive = false) {
fprintf(stderr, "_ActualDataType::kMaxKeyCount = %d\n", (int)_ActualDataType::kMaxKeyCount);
// fprintf(stderr, "_ActualDataType::kMaxKeyCount = %d\n", (int)_ActualDataType::kMaxKeyCount);
if (siz == 0) {
// special case for the first entry
BasicPageGuard new_page_guard = bpm->NewPageGuarded(&root_page_id);
@ -218,8 +218,8 @@ class BPlusTreeIndexer {
(page_guard.template As<PageType>()->data.key_count - pos.path.back().second) * sizeof(key_index_pair_t));
page_guard.template AsMut<PageType>()->data.p_data[pos.path.back().second] = std::make_pair(key, value);
page_guard.template AsMut<PageType>()->data.key_count++;
fprintf(stderr, "page_guard.template As<PageType>()->data.key_count = %d\n",
(int)page_guard.template As<PageType>()->data.key_count);
// fprintf(stderr, "page_guard.template As<PageType>()->data.key_count = %d\n",
// (int)page_guard.template As<PageType>()->data.key_count);
if (!is_fixing_up_recursive) ++siz;
return;
}
@ -279,8 +279,8 @@ class BPlusTreeIndexer {
page_guard.PageId());
new_root_page_guard.AsMut<PageType>()->data.p_data[1] = std::make_pair(KeyType(), new_page_id);
if (!is_fixing_up_recursive) ++siz;
fprintf(stderr, "new_page_guard.AsMut<PageType>()->data.key_count = %d\n",
(int)new_page_guard.AsMut<PageType>()->data.key_count);
// fprintf(stderr, "new_page_guard.AsMut<PageType>()->data.key_count = %d\n",
// (int)new_page_guard.AsMut<PageType>()->data.key_count);
return;
}
assert(pos.path.size() >= 2);
@ -302,7 +302,7 @@ class BPlusTreeIndexer {
page_guard.template As<PageType>()->data.p_data[page_guard.template As<PageType>()->data.key_count - 1].first;
pos.path[pos.path.size() - 2].second++;
pos.path.pop_back();
fprintf(stderr, "begin processing recursively\n");
// fprintf(stderr, "begin processing recursively\n");
InsertEntryAt(pos,
new_page_guard.template As<PageType>()
->data.p_data[new_page_guard.template As<PageType>()->data.key_count - 1]
@ -346,11 +346,15 @@ class BPlusTreeIndexer {
public:
const KeyType &GetKey() const {
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_lock<std::shared_mutex> lock_guard(domain->latch);
#endif
return guard.As<PageType>()->data.p_data[internal_offset].first;
}
const b_plus_tree_value_index_t &GetValue() {
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_lock<std::shared_mutex> lock_guard(domain->latch);
#endif
return guard.As<PageType>()->data.p_data[internal_offset].second;
}
bool operator==(iterator &that) {
@ -358,7 +362,9 @@ class BPlusTreeIndexer {
(is_end || (guard.PageId() == that.guard.PageId() && internal_offset == that.internal_offset));
}
void SetValue(b_plus_tree_value_index_t new_value) {
#ifdef ENABLE_ADVANCED_FEATURE
std::unique_lock<std::shared_mutex> lock_guard(domain->latch);
#endif
guard.AsMut<PageType>()->data.p_data[internal_offset].second = new_value;
}
// only support ++it
@ -386,11 +392,15 @@ class BPlusTreeIndexer {
public:
const KeyType &GetKey() {
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_lock<std::shared_mutex> lock_guard(domain->latch);
#endif
return guard.As<PageType>()->data.p_data[internal_offset].first;
}
const b_plus_tree_value_index_t &GetValue() {
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_lock<std::shared_mutex> lock_guard(domain->latch);
#endif
return guard.As<PageType>()->data.p_data[internal_offset].second;
}
bool operator==(const_iterator &that) {
@ -438,7 +448,9 @@ class BPlusTreeIndexer {
return res;
}
iterator lower_bound(const KeyType &key) { // Finish Design
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_lock<std::shared_mutex> guard(latch);
#endif
PositionSignType pos(std::move(FindPosition(key)));
iterator res;
res.domain = this;
@ -449,7 +461,9 @@ class BPlusTreeIndexer {
return res;
}
const_iterator lower_bound_const(const KeyType &key) { // Finish Design
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_lock<std::shared_mutex> guard(latch);
#endif
PositionSignType pos(std::move(FindPosition(key)));
const_iterator res;
res.domain = this;
@ -460,14 +474,18 @@ class BPlusTreeIndexer {
return res;
}
b_plus_tree_value_index_t Get(const KeyType &key) { // Finish Design
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_lock<std::shared_mutex> guard(latch);
#endif
auto it = lower_bound_const(key);
if (it == end_const()) return kInvalidValueIndex;
if (key_cmp(key, it.GetKey())) return kInvalidValueIndex;
return it.GetValue();
}
bool Put(const KeyType &key, b_plus_tree_value_index_t value) { // Finish Design
#ifdef ENABLE_ADVANCED_FEATURE
std::unique_lock<std::shared_mutex> guard(latch);
#endif
PositionSignType pos(std::move(FindPosition(key)));
if (!pos.is_end &&
!key_cmp(key, pos.path.back().first.template As<PageType>()->data.p_data[pos.path.back().second].first)) {
@ -478,7 +496,9 @@ class BPlusTreeIndexer {
return true;
}
bool Remove(const KeyType &key) { // Finish Design
#ifdef ENABLE_ADVANCED_FEATURE
std::unique_lock<std::shared_mutex> guard(latch);
#endif
PositionSignType pos(std::move(FindPosition(key)));
if (pos.is_end) return false;
if (key_cmp(key, pos.path.back().first.template As<PageType>()->data.p_data[pos.path.back().second].first))
@ -488,7 +508,9 @@ class BPlusTreeIndexer {
}
size_t Size() { return siz; } // Finish Design
void Flush() { // Finish Design
#ifdef ENABLE_ADVANCED_FEATURE
std::unique_lock<std::shared_mutex> guard(latch);
#endif
memcpy(raw_data_memory, &root_page_id, sizeof(page_id_t));
memcpy(raw_data_memory + sizeof(page_id_t), &siz, sizeof(bpt_size_t));
bpm->FlushAllPages();
@ -500,7 +522,9 @@ class BPlusTreeIndexer {
bpt_size_t siz; // stored in the next 8 (4-11) bytes of RawDatMemory, this directly operates on the buf
// maintained by DiskManager, BufferPoolManager only passes the pointer to it
static KeyComparator key_cmp;
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_mutex latch;
#endif
BufferPoolManager *bpm;
char *raw_data_memory;
};

View File

@ -18,6 +18,7 @@ class Page {
void ResetMemory();
char *GetData();
page_id_t GetPageId();
#ifdef ENABLE_ADVANCED_FEATURE
/** Acquire the page write latch. */
inline void WLatch() { rwlatch_.lock(); }
@ -29,11 +30,14 @@ class Page {
/** Release the page read latch. */
inline void RUnlatch() { rwlatch_.unlock_shared(); }
#endif
inline size_t GetPinCount() { return pin_count_; }
private:
#ifdef ENABLE_ADVANCED_FEATURE
std::shared_mutex rwlatch_;
#endif
char *mem;
bool is_dirty_;
size_t pin_count_;
@ -387,7 +391,9 @@ class BufferPoolManager {
const size_t replacer_k;
LRUKReplacer replacer;
DiskManager *disk_manager;
#ifdef ENABLE_ADVANCED_FEATURE
std::mutex latch;
#endif
Page *pages_;
std::unordered_map<page_id_t, frame_id_t> page_table_;
std::list<frame_id_t> free_list_;

View File

@ -69,7 +69,9 @@ class LRUKReplacer {
size_t current_evitable_count_{0};
size_t max_frame_count;
size_t k_value;
#ifdef ENABLE_ADVANCED_FEATURE
std::mutex latch;
#endif
LRUKRecord *hash_for_record;
};
#endif

View File

@ -48,17 +48,21 @@ auto ReadPageGuard::operator=(ReadPageGuard &&that) noexcept -> ReadPageGuard &
if (this == &that) {
return *this;
}
#ifdef ENABLE_ADVANCED_FEATURE
if (guard_.page_ != nullptr) {
guard_.page_->RUnlatch();
}
#endif
guard_ = std::move(that.guard_);
return *this;
}
void ReadPageGuard::Drop() {
#ifdef ENABLE_ADVANCED_FEATURE
if (guard_.page_ != nullptr) {
guard_.page_->RUnlatch();
}
#endif
guard_.Drop();
}
@ -70,17 +74,21 @@ auto WritePageGuard::operator=(WritePageGuard &&that) noexcept -> WritePageGuard
if (this == &that) {
return *this;
}
#ifdef ENABLE_ADVANCED_FEATURE
if (guard_.page_ != nullptr) {
guard_.page_->WUnlatch();
}
#endif
guard_ = std::move(that.guard_);
return *this;
}
void WritePageGuard::Drop() {
#ifdef ENABLE_ADVANCED_FEATURE
if (guard_.page_ != nullptr) {
guard_.page_->WUnlatch();
}
#endif
guard_.Drop();
}
@ -112,7 +120,9 @@ void BufferPoolManager::DeallocatePage(page_id_t page_id) { disk_manager->Deallo
size_t BufferPoolManager::GetPoolSize() { return pool_size; }
Page *BufferPoolManager::GetPages() { return pages_; }
auto BufferPoolManager::NewPage(page_id_t *page_id) -> Page * {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
if (!free_list_.empty()) {
int internal_page_object_offset = free_list_.front();
free_list_.pop_front();
@ -148,7 +158,9 @@ auto BufferPoolManager::NewPage(page_id_t *page_id) -> Page * {
}
auto BufferPoolManager::FetchPage(page_id_t page_id) -> Page * {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
if (page_table_.find(page_id) != page_table_.end()) {
frame_id_t frame_id = page_table_[page_id];
Page *page = &pages_[frame_id];
@ -190,7 +202,9 @@ auto BufferPoolManager::FetchPage(page_id_t page_id) -> Page * {
}
auto BufferPoolManager::UnpinPage(page_id_t page_id, bool is_dirty) -> bool {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
if (page_table_.find(page_id) == page_table_.end()) {
return false;
}
@ -210,7 +224,9 @@ auto BufferPoolManager::UnpinPage(page_id_t page_id, bool is_dirty) -> bool {
}
auto BufferPoolManager::FlushPage(page_id_t page_id) -> bool {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
frame_id_t frame_id = page_table_[page_id];
if (page_table_.find(page_id) == page_table_.end()) {
return false;
@ -229,7 +245,9 @@ void BufferPoolManager::FlushAllPages() {
}
auto BufferPoolManager::DeletePage(page_id_t page_id) -> bool {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
if (page_table_.find(page_id) == page_table_.end()) {
return true;
}
@ -258,18 +276,22 @@ auto BufferPoolManager::FetchPageBasic(page_id_t page_id) -> BasicPageGuard {
auto BufferPoolManager::FetchPageRead(page_id_t page_id) -> ReadPageGuard {
Page *page = FetchPage(page_id);
if (page == nullptr) throw std::runtime_error("Buffer Pool is full!");
#ifdef ENABLE_ADVANCED_FEATURE
if (page != nullptr) {
page->RLatch();
}
#endif
return {this, page};
}
auto BufferPoolManager::FetchPageWrite(page_id_t page_id) -> WritePageGuard {
Page *page = FetchPage(page_id);
if (page == nullptr) throw std::runtime_error("Buffer Pool is full!");
#ifdef ENABLE_ADVANCED_FEATURE
if (page != nullptr) {
page->WLatch();
}
#endif
return {this, page};
}

View File

@ -37,7 +37,9 @@ LRUKReplacer::~LRUKReplacer() {
}
void LRUKReplacer::SetEvictable(frame_id_t frame_id, bool evitable) {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
if (!hash_for_record[frame_id].active) {
return;
}
@ -72,7 +74,9 @@ LRUKReplacer::MainChainNodeType *LRUKReplacer::AddRecordToMainChain(frame_id_t f
}
bool LRUKReplacer::TryEvictExactFrame(frame_id_t frame_id) {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
if (!hash_for_record[frame_id].active) {
return false;
}
@ -90,16 +94,22 @@ bool LRUKReplacer::TryEvictExactFrame(frame_id_t frame_id) {
}
bool LRUKReplacer::TryEvictLeastImportant(frame_id_t &frame_id) {
#ifdef ENABLE_ADVANCED_FEATURE
latch.lock();
#endif
if (current_evitable_count_ == 0) {
#ifdef ENABLE_ADVANCED_FEATURE
latch.unlock();
#endif
return false;
}
LRUChainNodeType *node = LRU_chain_head_guard->next;
while (node != LRU_chain_tail_guard) {
frame_id = node->frame_id;
if (hash_for_record[frame_id].evitable) {
#ifdef ENABLE_ADVANCED_FEATURE
latch.unlock();
#endif
return TryEvictExactFrame(frame_id);
}
node = node->next;
@ -108,17 +118,23 @@ bool LRUKReplacer::TryEvictLeastImportant(frame_id_t &frame_id) {
while (main_chain_node != LRUK_chain_tail_guard) {
frame_id = main_chain_node->frame_id;
if (hash_for_record[frame_id].evitable) {
#ifdef ENABLE_ADVANCED_FEATURE
latch.unlock();
#endif
return TryEvictExactFrame(frame_id);
}
main_chain_node = main_chain_node->next;
}
#ifdef ENABLE_ADVANCED_FEATURE
latch.unlock();
#endif
return false;
}
void LRUKReplacer::RecordAccess(frame_id_t frame_id) {
#ifdef ENABLE_ADVANCED_FEATURE
std::lock_guard<std::mutex> guard(latch);
#endif
current_timestamp_++;
if (!hash_for_record[frame_id].active) {
hash_for_record[frame_id].active = true;