diff --git a/src/replacer/lru_replacer.cpp b/src/replacer/lru_replacer.cpp index 314de145676430c9a7b647ae23397e61726a0033..b21115135b4143f315b24ba22afbb1602e60ddf3 100644 --- a/src/replacer/lru_replacer.cpp +++ b/src/replacer/lru_replacer.cpp @@ -28,16 +28,15 @@ bool LRUReplacer::victim(frame_id_t* frame_id) { for(auto iter = LRUhash_.begin(); iter != LRUhash_.end(); iter++){ iter1 = iter++; if(iter1 == LRUhash_.end())break; - t1 = iter1->second;//iter->second是frame_id + t1 = iter1->second; t2 = iter->second; *frame_id = *t2; //frame_id应该是è¿ä»£å™¨t2的第一个值 int i1 = 0, i2 = 0; - //计算t1å’Œt2frame_idçš„åºå· for(t1 = LRUlist_.begin(); t1 != LRUlist_.end(); t1++){ if(*t1 == iter1->first)break; i1++; } - + //计算frame_idçš„åºå· for(t2 = LRUlist_.begin(); t2 != LRUlist_.end(); t2++){ if(*t2 == iter->first)break; i2++; diff --git a/src/storage/buffer_pool_manager.cpp b/src/storage/buffer_pool_manager.cpp index 8656d6172088e6064f03fb4d11314d8a3a87ae7a..4f8064ede8a359e2d432ad38eaaf9791cef96cc8 100644 --- a/src/storage/buffer_pool_manager.cpp +++ b/src/storage/buffer_pool_manager.cpp @@ -20,16 +20,8 @@ bool BufferPoolManager::find_victim_page(frame_id_t* frame_id) { // 1 使用BufferPoolManager::free_list_判æ–ç¼“å†²æ± æ˜¯å¦å·²æ»¡éœ€è¦æ·˜æ±°é¡µé¢ // 1.1 未满获得frame // 1.2 已满使用lru_replacerä¸çš„æ–¹æ³•é€‰æ‹©æ·˜æ±°é¡µé¢ - auto iter = this->free_list_.begin(); - if(iter == this->free_list_.end()){ - //ç¼“å†²æ± æ»¡ - if(this->replacer_->victim(frame_id))return true;//看BufferPoolManageråˆå§‹åŒ–结果,å¯èƒ½è°ƒç”¨LRU,å¯èƒ½è°ƒç”¨CLOCK? - else return false; - } - *frame_id = *iter; - this->free_list_.pop_front(); - return true; - //return false; + + return false; } /** @@ -41,18 +33,9 @@ bool BufferPoolManager::find_victim_page(frame_id_t* frame_id) { void BufferPoolManager::update_page(Page *page, PageId new_page_id, frame_id_t new_frame_id) { // Todo: // 1 如果是è„页,写回ç£ç›˜ï¼Œå¹¶ä¸”把dirty置为false - if(page->is_dirty_ == true){ - //写回ç£ç›˜ - disk_manager_->write_page(new_page_id.fd, new_page_id.page_no, page->data_, sizeof(page->data_)); - //把dirty置为false - page->is_dirty_ = false; - } // 2 æ›´æ–°page table - PageIdHash ph; - //size_t hashValue = ph(new_page_id); - page_table_.emplace(new_page_id, new_frame_id, ph); // 3 é‡ç½®pageçš„data,更新page id - page->id_ = new_page_id; + } /** @@ -65,35 +48,11 @@ void BufferPoolManager::update_page(Page *page, PageId new_page_id, frame_id_t n Page* BufferPoolManager::fetch_page(PageId page_id) { //Todo: // 1. 从page_table_ä¸æœå¯»ç›®æ ‡é¡µ - for(auto iter = this->page_table_.begin(); iter != this->page_table_.end(); iter++){ - if(iter->first == page_id){ - // 1.1 è‹¥ç›®æ ‡é¡µæœ‰è¢«page_table_记录,则将其所在frame固定(pin)ï¼Œå¹¶è¿”å›žç›®æ ‡é¡µã€‚ - for(int i = 0; i<pool_size_; i++){ - if(pages_[i].id_ == page_id){ - pages_[i].pin_count_++; - replacer_->pin(iter->second); - return &pages_[i]; - } - } - - } - } + // 1.1 è‹¥ç›®æ ‡é¡µæœ‰è¢«page_table_记录,则将其所在frame固定(pin)ï¼Œå¹¶è¿”å›žç›®æ ‡é¡µã€‚ // 1.2 å¦åˆ™ï¼Œå°è¯•è°ƒç”¨find_victim_page获得一个å¯ç”¨çš„frame,若失败则返回nullptr - frame_id_t* frame_id; - if(!find_victim_page(frame_id))return nullptr; // 2. 若获得的å¯ç”¨frameå˜å‚¨çš„为dirty page,则须调用updata_pageå°†page写回到ç£ç›˜ - for(int i = 0; i<pool_size_; i++){ - if(pages_[i].id_ == page_id){ - if(!pages_[i].is_dirty_)update_page(&pages_[i], page_id, *frame_id); - // 3. 调用disk_manager_çš„read_page读å–ç›®æ ‡é¡µåˆ°frame - disk_manager_->read_page(page_id.fd, page_id.page_no, pages_[i].data_, sizeof(pages_[i].data_)); - // 4. å›ºå®šç›®æ ‡é¡µï¼Œæ›´æ–°pin_count_ - replacer_->pin(*frame_id); - pages_[i].pin_count_++; - return &pages_[i]; - } - } - + // 3. 调用disk_manager_çš„read_page读å–ç›®æ ‡é¡µåˆ°frame + // 4. å›ºå®šç›®æ ‡é¡µï¼Œæ›´æ–°pin_count_ // 5. è¿”å›žç›®æ ‡é¡µ return nullptr; } @@ -107,32 +66,14 @@ Page* BufferPoolManager::fetch_page(PageId page_id) { bool BufferPoolManager::unpin_page(PageId page_id, bool is_dirty) { // Todo: // 0. lock latch - std::scoped_lock lock{latch_}; // 1. å°è¯•åœ¨page_table_ä¸æœå¯»page_id对应的页P - for(auto iter = this->page_table_.begin(); iter != this->page_table_.end(); iter++){ - if(iter->first.fd == page_id.fd){ - // 1.2 P在页表ä¸å˜åœ¨ï¼ŒèŽ·å–å…¶pin_count_ - for(int i = 0; i<pool_size_; i++){ - // 2.1 è‹¥pin_count_å·²ç»ç‰äºŽ0,则返回false - if(pages_[i].id_.fd == page_id.fd){ - if(pages_[i].pin_count_ == 0)return false; - // 2.2 è‹¥pin_count_大于0,则pin_count_自å‡ä¸€ - else if(pages_[i].pin_count_ > 1)pages_[2].pin_count_--; - // 2.2.1 若自å‡åŽç‰äºŽ0,则调用replacer_çš„Unpin - else if(pages_[i].pin_count_ == 1){ - pages_[2].pin_count_--; - replacer_->unpin(iter->second); - } - pages_[i].is_dirty_ = is_dirty; - break; - } - } - // 3 æ ¹æ®å‚æ•°is_dirty,更改Pçš„is_dirty_ - return true; - } - } // 1.1 P在页表ä¸ä¸å˜åœ¨ return false - return false; + // 1.2 P在页表ä¸å˜åœ¨ï¼ŒèŽ·å–å…¶pin_count_ + // 2.1 è‹¥pin_count_å·²ç»ç‰äºŽ0,则返回false + // 2.2 è‹¥pin_count_大于0,则pin_count_自å‡ä¸€ + // 2.2.1 若自å‡åŽç‰äºŽ0,则调用replacer_çš„Unpin + // 3 æ ¹æ®å‚æ•°is_dirty,更改Pçš„is_dirty_ + return true; } /** @@ -143,25 +84,12 @@ bool BufferPoolManager::unpin_page(PageId page_id, bool is_dirty) { bool BufferPoolManager::flush_page(PageId page_id) { // Todo: // 0. lock latch - std::scoped_lock lock{latch_}; // 1. 查找页表,å°è¯•èŽ·å–ç›®æ ‡é¡µP - for(auto iter = this->page_table_.begin(); iter != this->page_table_.end(); iter++){ - if(iter->first.fd == page_id.fd){ - // 2. æ— è®ºP是å¦ä¸ºè„都将其写回ç£ç›˜ã€‚ - for(int i = 0; i<pool_size_; i++){ - if(pages_[i].id_ == page_id){ - disk_manager_->write_page(page_id.fd, page_id.page_no, pages_[i].data_, sizeof(pages_[i].data_)); - // 3. æ›´æ–°Pçš„is_dirty_ - pages_[i].is_dirty_ = true; - break; - } - } - return true; - } - } // 1.1 ç›®æ ‡é¡µP没有被page_table_记录 ,返回false - return false; + // 2. æ— è®ºP是å¦ä¸ºè„都将其写回ç£ç›˜ã€‚ + // 3. æ›´æ–°Pçš„is_dirty_ + return true; } /** @@ -175,7 +103,6 @@ Page* BufferPoolManager::new_page(PageId* page_id) { // 3. å°†frameçš„æ•°æ®å†™å›žç£ç›˜ // 4. 固定frame,更新pin_count_ // 5. 返回获得的page - return nullptr; } @@ -188,29 +115,13 @@ bool BufferPoolManager::delete_page(PageId page_id) { // 1. 在page_table_ä¸æŸ¥æ‰¾ç›®æ ‡é¡µï¼Œè‹¥ä¸å˜åœ¨è¿”回true // 2. è‹¥ç›®æ ‡é¡µçš„pin_countä¸ä¸º0,则返回false // 3. å°†ç›®æ ‡é¡µæ•°æ®å†™å›žç£ç›˜ï¼Œä»Žé¡µè¡¨ä¸åˆ é™¤ç›®æ ‡é¡µï¼Œé‡ç½®å…¶å…ƒæ•°æ®ï¼Œå°†å…¶åŠ å…¥free_list_,返回true - for(auto iter = page_table_.begin(); iter != page_table_.end(); iter++){ - if(iter->first.fd == page_id.fd){ - for(int i = 0; i<pool_size_; i++){ - if(pages_[i].id_ == page_id){ - //ç›®æ ‡é¡µpin_count!=0 - if(pages_[i].pin_count_ == 0)return false; - //写回ç£ç›˜ - disk_manager_->write_page(page_id.fd, page_id.page_no, pages_[i].data_, sizeof(pages_[i].data_)); - //从页表ä¸åˆ é™¤ç›®æ ‡é¡µ - page_table_.erase(page_id); - //é‡ç½®å…ƒæ•°æ® - pages_[i].reset_memory(); - memset(&pages_[i], 0, sizeof(pages_[i])); - break; - } - } - //åŠ å…¥free_list_ - this->free_list_.push_back(iter->second); - return true; + if(iter->second == page_id.fd){ + //写回ç£ç›˜ + } } - return false; + return true; } /** @@ -218,9 +129,5 @@ bool BufferPoolManager::delete_page(PageId page_id) { * @param {int} fd 文件å¥æŸ„ */ void BufferPoolManager::flush_all_pages(int fd) { - for(int i = 0; i<pool_size_; i++){ - //ä¸æ˜¯æ¯ä¸ªé¡µé¢ä¸çš„PageID都有它的fdå—ï¼Ÿè¿™ä¸ªä¼ å‚çš„fd是? - disk_manager_->write_page(fd, pages_[i].id_.page_no, pages_[i].data_, sizeof(pages_[i].data_)); - } } \ No newline at end of file diff --git a/src/storage/disk_manager.cpp b/src/storage/disk_manager.cpp index 410f56d630dc141758a42d2a710588d87270b5eb..08b9cd1f99cd9b0d727b17ff15f0f89d1a8f661e 100644 --- a/src/storage/disk_manager.cpp +++ b/src/storage/disk_manager.cpp @@ -172,6 +172,7 @@ void DiskManager::destroy_file(const std::string &path) { std::cerr << "Failed to destroy file " << path << ".\n"; } } + }