CMU 15-445/645 Buffer Pool实现思路图

缓冲池管理器

管理器的组成

按照这个思维图,就不难写出对应实现代码。

CMU 15-445/645 Buffer Pool实现思路图_第1张图片

在最新实现中,将Clock换成了LRU,但原理是相同的。

由三部分组成Buffer Pool 、Clock、 Free List。他们分别的作用:

Buffer Pool:用来存储Page

Free List:目前Buffer Pool中有那几个地方没有加载Page

Clock:在Free List为空时(Buffer Pool加载满时),提供可以移除的受害者。

 

FetchPage

FetchPage(获取page),若在Buffer Pool中,直接返回对象。若不在,则从磁盘中加载后返回对象。

CMU 15-445/645 Buffer Pool实现思路图_第2张图片

 

CMU 15-445/645 Buffer Pool实现思路图_第3张图片

CMU 15-445/645 Buffer Pool实现思路图_第4张图片

 

CMU 15-445/645 Buffer Pool实现思路图_第5张图片

以下均为实现思路,不一定正确,还需要进一步调试!

FetchPageImpl的实现,获取一个页表的实现

Page *BufferPoolManager::FetchPageImpl(page_id_t page_id) {
  // 1.     Search the page table for the requested page (P).
  // 1.1    If P exists, pin it and return it immediately.
  // 1.2    If P does not exist, find a replacement page (R) from either the free list or the replacer.
  //        Note that pages are always found from the free list first.
  // 2.     If R is dirty, write it back to the disk.
  // 3.     Delete R from the page table and insert P.
  // 4.     Update P's metadata, read in the page content from disk, and then return a pointer to P.
  std::scoped_lock bpm_slk{latch_};
  if(page_table_.find(page_id)!=page_table_.end()) {
    auto frame_id = page_table_[page_id];
    auto page = pages_ + frame_id;
    page->pin_count_++;   // pin it
    return page;
  }
  if(free_list_.empty()&&replacer_->Size()==0) {
    return nullptr;
  }
  frame_id_t frame_id = -1;
  if(!free_list_.empty()){
    frame_id = free_list_.front();
    free_list_.pop_back();
  }
  replacer_->Victim(&frame_id);
  auto page = pages_ + frame_id;
  if(page->is_dirty_){
    FlushPageImpl(page->page_id_);
  }
  page_table_.erase(page->page_id_);
  page_table_[page_id] = frame_id;
  page->page_id_ = page_id;
  page->ResetMemory();
  page->page_id_ = 1;
  page->is_dirty_ = false;
  return page;
}

将一页写到磁盘实现:

bool BufferPoolManager::FlushPageImpl(page_id_t page_id) {
  std::scoped_lock bpm_slk{latch_};
  // Make sure you call DiskManager::WritePage!
  if(page_table_.find(page_id)==page_table_.end()){
    return false;
  }
  auto frame_id = page_table_[page_id];
  auto page = pages_ + frame_id;
  disk_manager_->WritePage(page_id,page->GetData());
  page->is_dirty_ = false;
  return true;
}

 

新建一页的实现

Page *BufferPoolManager::NewPageImpl(page_id_t *page_id) {
  // 0.   Make sure you call DiskManager::AllocatePage!
  // 1.   If all the pages in the buffer pool are pinned, return nullptr.
  // 2.   Pick a victim page P from either the free list or the replacer. Always pick from the free list first.
  // 3.   Update P's metadata, zero out memory and add P to the page table.
  // 4.   Set the page ID output parameter. Return a pointer to P.
  std::scoped_lock bpm_slk{latch_};
  auto new_page_id = disk_manager_->AllocatePage();
  frame_id_t frame_id = -1;
  if(!free_list_.empty()){
    frame_id = free_list_.front();
    free_list_.pop_front();
  }else if(!replacer_->Victim(&frame_id)){
    return nullptr;
  }
  auto page = pages_ + frame_id;
  page->page_id_ = new_page_id;
  page->ResetMemory();
  page_table_[page->page_id_] = frame_id;
  *page_id = page->page_id_;
  page->pin_count_ = 1;            
  page->is_dirty_ = true;          
  return page;
}

删除页的实现

bool BufferPoolManager::DeletePageImpl(page_id_t page_id) {
  // 0.   Make sure you call DiskManager::DeallocatePage!
  // 1.   Search the page table for the requested page (P).
  // 1.   If P does not exist, return true.
  // 2.   If P exists, but has a non-zero pin-count, return false. Someone is using the page.
  // 3.   Otherwise, P can be deleted. Remove P from the page table, reset its metadata and return it to the free list.
  std::scoped_lock bpm_slk{latch_};
  if(page_table_.find(page_id)==page_table_.end()) { return true;}
  auto frame_id = page_table_[page_id];
  auto page = pages_ + frame_id;
  if(page->pin_count_>0){
    return false;
  }
  if(page->is_dirty_){
    FlushPageImpl(page_id);
  }
  page_table_.erase(page_id);
  page->ResetMemory();
  page->page_id_ = INVALID_PAGE_ID;
  page->pin_count_ = 0;
  free_list_.push_front(frame_id);
  disk_manager_->DeallocatePage(page_id);
  return true;
}

刷新所以页

void BufferPoolManager::FlushAllPagesImpl() {
  // You can do it!
  std::scoped_lock bpm_slk{latch_};
  for(size_t i=0;i

Unpin实现

bool BufferPoolManager::UnpinPageImpl(page_id_t page_id, bool is_dirty) {
  std::scoped_lock bpm_slk{latch_};
  if(page_table_.find(page_id)==page_table_.end()){return false;}
  auto frame_id = page_table_[page_id];
  auto page = pages_ + frame_id;
  if(page->pin_count_==0) {return false;}
  page->is_dirty_ |= is_dirty;
  page->pin_count_--;
  if(page->pin_count_!=0){
    replacer_->Unpin(frame_id);
  }
  return true;
}

 

你可能感兴趣的:(算法)