"""Simulate having a file of data simply as a big list of byte-arrays in memory.""" class PageNotPinnedException(Exception): pass class PagePinnedException(Exception): pass class Frame: def __init__(self): self.fileName = None self.pageNum = None self.pinCount = 0 self.dirty = False self.page = None class BufferManager: def __init__(self, poolSize): """Creates a buffer manager with the specified size. Feel free to add other instance variables if you find them useful.""" self.__bufferPool = [None]*poolSize def poolSize(self): """Returns the maximum capacity of the buffer pool, i.e. the total number of frames that it can hold.""" return 0 def pinPage(self, pinPageId, diskfile): """Checks if this page is in buffer pool. If it is, returns a pointer to it. Otherwise, it finds an available frame for this page, reads a copy of the page into it, and pins it. Writes out the old page, if it is dirty, before reading. pinPageId is the page id for the page to be pinned. diskfile is a pointer to the array of data that is simulating the disk file. Return a reference to the page in the buffer pool. If the buffer pool is full, return None.""" return None def unpinPage(self, unpinPageId, diskfile, dirty): """If the pin count for this page is greater than 0, it is decremented. If the pin count becomes zero, it is appropriately included in a group of replacement candidates. unpinPageId the page id for the page to be unpinned. diskfile is a pointer to the array of data that is simulating the disk file. dirty indicates whether or not the page is dirty, i.e., has been modified since read from disk. Raises PageNotPinnedException if the page is not pinned, or if the page id is invalid in some other way.""" pass def newPage(self, numPages,diskfile): """Requests a run of pages from the underlying database, then finds a frame in the buffer pool for the first page and pins it. If the buffer pool is full, no new pages are allocated from the database. numPages is the number of pages in the run to be allocated. diskfile is the name of the file. Return a tuple consisting of the first page id of the run, and a reference to the page which has been pinned in the buffer pool. Return None if there is not enough space in the buffer pool for the first page.""" return None def freePage(self,pageId, diskfile): """Deallocates a page from the underlying database (sets its pointer to None). Verifies that page is not pinned. pageId is the page id to be deallocated. diskfile is the name of the file. Raise PagePinnedException if the page is pinned.""" pass def flushPage(self, pageId, diskfile): """Writes page from the buffer pool to the underlying database if the page is dirty. If page is not dirty, nothing is written to the database. If the page is not in the buffer pool, do nothing. pageId is the page id to be flushed. diskfile is the name of the file.""" pass def flushAllPages(self): """Flushes all pages from the buffer pool to the underlying databases.""" pass def findFrame(self, pageId, diskfile): """Returns buffer pool location for a particular pageId. This method is just used for testing purposes: it probably doesn't serve a real purpose in an actual database system. pageId the page id to be looked up. diskfile is the name of the file. Return the frame location for the page of interest. Return -1 if the page is not in the pool.""" return -1