"""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 pool size.""" 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, makes a copy of the page (make sure you do this, it's easy in Python just to get a pointer copy) 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. If False, then the page does not actually need to be written back to 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 a pointer to the array of data that is simulating the disk 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 a pointer to the array of data that is simulating the disk file. Raise PagePinnedException if the page is pinned.""" pass def flushPage(self, pageId, diskfile): """Flushes page from the buffer pool to the underlying database if it is dirty. If page is not dirty, it is not flushed, especially since an undirty page may hang around even after the underlying database has been erased. If the page is not in the buffer pool, do nothing, since the page is effectively flushed already. pageId is the page id to be flushed. diskfile is a pointer to the array of data that is simulating the disk file.""" pass def flushAllPages(self): """Flushes all dirty pages from the buffer pool to the underlying databases. If page is not dirty, it is not flushed, especially since an undirty page may hang around even after the underlying database has been erased.""" 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 a pointer to the array of data that is simulating the disk file. Return the frame location for the page of interest. Return -1 if the page is not in the pool.""" return -1