Stores frequently accessed database pages in memory, evicting pages according to either the LRU or the LRU-2 page replacement policy.
void setMaxNumPages(int maxNumPages)Discard unpinned pages until either the number of pages in the cache is less than or equal to maxNumPages or all the pages in the cache are pinned. If there are still too many pages after discarding all unpinned pages, pages will continue to be discarded after being unpinned in the unpinPage function.
Page *fetchPage(unsigned pageId, bool allocate)- If the page is already in the cache, return a pointer to the page.
- If the page is not already in the cache, use the
allocateparameter to determine how to proceed.- If
allocateis false, return a null pointer. - If
allocateis true, examine the number of pages in the cache.- If the number of pages in the cache is less than the maximum, allocate and return a pointer to a new page.
- If the number of pages in the cache is greater than or equal to the maximum, try to replace a page.
- If there is at least one unpinned page, return a pointer to an existing unpinned page as determined by the replacement policy.
- If all pages are pinned, return a null pointer.
- If
Increment numFetches_, and if the fetch was a hit, increment numHits_. If the fetch was a hit, this function executes in
void unpinPage(Page *page, bool discard)The page is unpinned regardless of the number of prior fetches, meaning it can be safely discarded.
- If
discardis true, then discard the page. - If
discardis false, examine the number of pages in the cache.- If the number of pages in the cache is greater than the maximum, discard the page.
- If the number of pages in the cache is less than or equal to the maximum, do not discard the page.
This function executes in
void changePageId(Page *page, unsigned newPageId)If a page with page ID newPageId is already in the cache, it is assumed that the page is unpinned, and the page is discarded.
void discardPages(unsigned pageIdLimit)If any of these pages are pinned, then they are implicitly unpinned, meaning they can be safely discarded.