cmu15-445 Project #1-Buffer Pool

TASK #1 - CLOCK REPLACEMENT POLICY

实现src/buffer/clock_replacer.cpp中的函数:
  • Victim(): 使用时钟算法淘汰一个frame,从时钟的指针指向的地方开始,找出第一个ref=0的frame。如果所指向的frame的ref=1,那么将其ref置为0。然后更新clock hand
  • Pin():
    在一个页面被pin的时候调用,将对应的frame直接移出clock_replacer
  • Unpin():
    在page的pin_count_为0的时候调用,将frame插入clock_replacer。
  • Size()
    返回clock_replacer中frame的数量。

TASK #2 - BUFFER POOL MANAGER

Task2为实现db中的BufferPoolManager,BufferPoolManager负责将Disk中的页面抓取到内存中。另外BufferPoolManager还负责将脏页写回disk,这种情况由主动调用还有淘汰一个页面时触发。
内存中的页面都使用Page对象来表示,每一个Page都存储了对应 disk位置中一个block的内容。需要注意的时在BufferPoolManager中会重用Page对象,同一个Page可能是多个disk区域的映射,Page使用page_id来记录该Page包含哪一个物理页面,如果一个Page对象中不包含物理页面,那么page_id应被设为INVALID_PAGE_ID。
每一个Page对象都会维护一个计数器来记录有多少个线程pin住该页。BufferPoolManager不允许一个Pinned Page被释放掉。Page对象中还记录了对应的页面是否为脏页,在实验中需要实现一个页面变为Unpinned之前该页是否被修改过。

实现src/buffer/buffer_pool_manager.cpp中的函数:
  • FetchPageImpl(page_id)
    将page_id对应的页面加载到内存,如果page_table_中已经存在对应的页面,那么证明该页面已经加载到内存,将Page的pin_count_增加1后直接返回。如果page_table_中没有相关的item,那么要从free_list_或者replacer中找出一个页来作为对应页面的缓存。free_list中存储的时frame_id_t,可以通过pages_[frame_id]来访问对应的Page。查看free_list是否有可用页面,如果有则将队首的页面pop出来。删除page_table_中的过期的映射关系,如果获取的页面为脏页,那么需要将frame中的内容写回硬盘。然后更新Page的metadata,包括将is_dirty设为false,将page_id设为新的page_id,将pin_count_设为1。最后将page_id从硬盘加载到Page上。

  • NewPageImpl(page_id)
    在buffer pool中创建一个新页,page_id通过disk_manager->AllocatePage获取,为全新的page_id。类似于FechPageImpl,从free_list_或者replacer中获取可用的frame,删除page_table_中的过期的映射关系,如果frame对应的Page为脏页需要将Page的data写回硬盘。在page_table_中建立page_id和frame_id_t的映射,更新Page的metadata,其中包括将is_dirty设为false,将page_id设为新的page_id,将pin_count_设为1,将memory reset掉。

  • UnpinPageImpl(page_id, is_dirty)
    upin一个page意味着有一个用户结束了对page_id对应Page的使用,如果pin_count_ <= 0,直接返回false。否则pin_count减1,减1后如果pin_count_为0,那么将对应frame放入replacer中,最后返回true。

  • FlushPageImpl(page_id)
    将page_id对应的Page中的data写回硬盘

  • DeletePageImpl(page_id)
    从buffer pool中回收一个页面到free_list_,如果page_table_中不存在对应的映射,直接返回true。如果page_table中存在对应的页面,且该页面的pin_count_不为0,返回false。如果pin_count_为0,删除page_table_中对应的item,将对应的frame放回free_list_。

  • FlushAllPagesImpl()
    将buffer pool中所有的页面都写回硬盘

### CMU 15-445 Buffer Pool Implementation and Concepts #### Overview of Buffer Pools in Database Systems A buffer pool serves as a cache between the database system&#39;s memory and disk storage. This mechanism aims to reduce expensive I/O operations by keeping frequently accessed data pages in memory. The management of these pages within the buffer pool involves various strategies for eviction, replacement policies like Least Recently Used (LRU), and pinning mechanisms which prevent certain pages from being evicted. #### Key Components of Buffer Pool Management The core functionalities include page allocation, deallocation, fetching pages into the buffer when requested, writing dirty pages back to disk, ensuring consistency through logging transactions, handling concurrency control issues during multi-threaded access scenarios, and optimizing performance via prefetching techniques[^1]. #### Implementation Details Specific to Course Materials In the context of CMU’s course material specifically related to 15-445, students are expected not only to understand theoretical aspects but also gain hands-on experience implementing key features such as: - **Page Replacement Policies**: Implement different algorithms including LRU, Clock Algorithm. - **Concurrency Control Mechanisms**: Develop solutions using locks or semaphores to manage simultaneous accesses safely without causing deadlocks or race conditions. - **Logging & Recovery Protocols**: Design methods for maintaining transaction logs so that changes can be undone if necessary due to failures; implement checkpoint procedures efficiently. Below is an example code snippet demonstrating how one might start coding up parts of this functionality in C++ based on principles taught in class: ```cpp class BufferPoolManager { private: std::vector<Page*> pages_; // Array holding all managed frames/pages size_t num_pages_; public: Page* FetchPage(page_id_t page_id); }; ``` This structure allows managing multiple pages while providing functions needed for interacting with them according to specified requirements set forth throughout lectures and assignments associated with CMU 15-445 coursework.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值