"""Functions associated with organizing the structure on a variable record page. Do not, not, not, add any global variables. You may add additional functions if you like. And again: don't add any global variables. You'll be tempted because it's a good way to store additional data. But the whole idea is to get all of the data stored on the page, not floating around in Python's innards somewhere. A lot of these functions refer to an 'rid.' That's a record id, which in this context is also known a slot id; it's the identifying integer for that record. It's the index of the slot array that points to that record. Sometimes, in some contexts or references, the rid is a pair that also includes the page number. In this case, since everything is constrained to within a page, we will define the rid as just containing the slot number. """ import array import struct PAGESIZE = 1024 class PageFullException(Exception): pass class BadRidException(Exception): pass def makePage(): """One page's worth of data in memory. DO NOT MODIFY THIS FUNCTION.""" return array.array('c','\0' * PAGESIZE) def setup(page): """Initializes values on the page.""" pass def getAvailableSpace(page): """Determines how much space, in bytes, is actually available on the page, which depends on whether or not a new slot in the slot array is needed. If a new spot in the slot array is needed, then the amount of available space has to take this into consideration. In other words, the space you need for the addition to the slot array shouldn't be included as part of the available space, because from the user's perspective, it isn't available for adding data.""" return 0 def dumpPage(page): """Dumps out to the screen the # of entries on the page, the location where the free space starts, the slot array in a readable dashion, and the actual contents of each record. (This method merely exists for debugging and testing purposes.)""" print 'Dumping page.' def insertRecord(page,record): """Inserts a new record onto the page. The parameter record is to be an array of chars. A copy of that data should be placed on the page. Return the rid of the new record. Raise a PageFullException if there is not enough room for the record on the page.""" return -1 def deleteRecord(page,rid): """Deletes the record with the given rid from the page, compacting the hole created. Compacting the hole, in turn, requires that all the offsets (in the slot array) of all records after the hole be adjusted by the size of the hole, because you are moving these records to 'fill' the hole. You should leave a 'hole' in the slot array for the slot which pointed to the deleted record, if necessary, to make sure that the rids of the remaining records do not change. The slot array itself should be compacted only if the record corresponding to the last slot is being deleted. Return True if successful, False if the rid is actually not found on the page.""" return False def firstRecord(page): """Returns RID of first record on page. Remember that some slots may be empty, so you should skip over these. Return None if the page is empty.""" return -1 def nextRecord(page,rid): """Returns RID of next record on the page, where 'next on the page' means 'next in the slot array after the rid passed in' Remember that some slots may be empty, so you should skip over these. Returns None if there are no more. Raises BadRidException if the given rid is invalid.""" return -1 def getRecord(page,rid): """Returns the record associated with an RID. Specifically, returns a char array containing a copy of the record. The array has precisely the length of the record (there is no padded space). Raises BadRidException if the slot id within curRid is invalid.""" return None def empty(page): """Whether or not the page is empty. Return True if the page is empty, False otherwise.""" return True