示例#1
0
	def insert(self, value, record):
		start = timer()
		if self.workings:
			print("Search for key value first to ensure record does not already exist.")
		if not (self.utilSearch(value, False, False) is None):
			print("Record with that key already exists, cannot insert.")
			return	
		if self.workings:
			print("Begin insert.")
		
			
		#using the hash function
		bucket = self.getBucketPointer(value)
		
		if self.workings:
			print(str(value) + " maps to bucket " + str(bucket))
		
		#format the record to be inserted
		formattedRecord = Record.new(self.recordSize, self.fieldSize, False, value, record)
		#open the file as binary read and write
		with open(self.file, 'r+b', buffering=self.blockSize) as f:
			#navigate to the appropriate bucket
			f.seek(self.blockSize*(bucket))
			
			if self.workings:
				print("Navigate to bucket " + str(bucket))
			
			#check to see if data exits in this bucket
			theBlock = self.makeBlock(f.read(self.blockSize))
			space = theBlock.hasSpace()
			if space>=0:
				if self.workings:
					print("Space " + str(space) + " is available in this bucket.")
				# spot was open, move pointer back
				f.seek(self.blockSize*(bucket) + self.recordSize*space)
				#slot data in there boiiiiii
				f.write(formattedRecord.bytes)
				if self.workings:
					print("The record was inserted at record number " + str(space) + " in bucket " + str(bucket) + ".")
			
			else:
				if self.workings:
					print("The bucket is full.  Initiate a split.")
				# clear out bucket on disk
				f.seek(self.blockSize*(bucket))
				f.write(bytearray(self.blockSize))
				self.split(f, theBlock, formattedRecord, value)
		end = timer()		
		if self.times:
			print("Insert time: " + str((end-start)*1000) + "ms")
示例#2
0
	def update(self, value, data):
		start = timer()
        #format record
		formattedRecord = Record.new(self.recordSize, self.fieldSize, False, value, data)
		bucket = self.getBucketPointer(value)
		recordInfo = self.utilSearch(value, True, False)
		if recordInfo is None:
			print("Record not found.")
		else:
			# open the file as binary read and write
			with open(self.file, 'r+b', buffering=self.blockSize) as f:
				# navigate to the appropriate bucket
				# plus 2 is to account for the header
				f.seek(self.blockSize*(bucket))
				# load bucket into memory
				theBlock = self.makeBlock(f.read(self.blockSize))
				if theBlock.containsRecordWithValue(value):
					recLoc = theBlock.getRecordWithValueLoc(value)
					f.seek(self.blockSize*bucket + self.recordSize*recLoc)
					f.write(formattedRecord.bytes)
		end = timer()
		if self.times:
			print("Search time: " + str((end-start)*1000) + "ms")