class TestAdjustingDeletion(unittest.TestCase): def setUp(self): try: os.remove('spam') except FileNotFoundError: pass self.manager = IndexManager('spam', '<i') self.manager.Node.n = 4 def tearDown(self): try: os.remove('spam') except FileNotFoundError: pass del self.manager def test_delete_transfer(self): self.manager.Node.n = 4 self.manager.insert(2, 32) self.manager.insert(24, 67) self.manager.insert(7, 87) self.manager.insert(15, 45) self.manager.insert(11, 43) self.manager.delete(24) self.assertEqual(self.manager.root, 3) Node = self.manager.Node root_block = BufferManager().get_file_block(self.manager.index_file_path, self.manager.root) root_node = Node.frombytes(root_block.read()) self.assertEqual(root_node.keys, [(11,)]) left_block = BufferManager().get_file_block(self.manager.index_file_path, 1) left_node = Node.frombytes(left_block.read()) self.assertEqual(left_node.keys, _convert_to_tuple_list([2, 7])) self.assertEqual(left_node.children, [32, 87, 2]) right_block = BufferManager().get_file_block(self.manager.index_file_path, 2) right_node = Node.frombytes(right_block.read()) self.assertEqual(right_node.keys, _convert_to_tuple_list([11, 15])) self.assertEqual(right_node.children, [43, 45, 0]) def test_delete_fuse(self): self.manager.Node.n = 4 self.manager.insert(2, 32) self.manager.insert(24, 67) self.manager.insert(7, 87) self.manager.insert(15, 45) self.manager.insert(11, 43) self.manager.delete(24) self.manager.delete(11) self.assertEqual(self.manager.root, 1) Node = self.manager.Node root_block = BufferManager().get_file_block(self.manager.index_file_path, self.manager.root) root_node = Node.frombytes(root_block.read()) self.assertEqual(root_node.is_leaf, True) self.assertEqual(root_node.keys, _convert_to_tuple_list([2, 7, 15])) self.assertEqual(root_node.children, [32, 87, 45, 0])
def test_shrinking_delete(self): manager = IndexManager('spam', '<id') manager.insert([42, 7.6], 518) manager.insert([233, 66.6], 7) manager.delete([233, 66.6]) manager.delete([42, 7.6]) self.assertEqual(manager.root, 0) self.assertEqual(manager.first_deleted_block, 1) self.assertEqual(manager.total_blocks, 2)
def _delete_stupid_index(record, table_name): metadata = load_metadata() for index_name, index in metadata.tables[table_name].indexes.items(): key_pos = list(metadata.tables[table_name].columns.keys()).index( index.columns[0]) attribute_name = index.columns[0] key_list = list() key_list.append(record[key_pos]) file_path = RecordManager.file_dir + index_name + '.index' fmt = metadata.tables[table_name].columns[attribute_name].fmt in_manager = IndexManager(file_path, fmt) in_manager.delete(key_list)
def test_successful_delete(self): manager = IndexManager('spam', '<id') manager.insert([42, 7.6], 518) manager.insert([233, 66.6], 7) manager.delete([42, 7.6]) self.assertEqual(manager.root, 1) self.assertEqual(manager.first_deleted_block, 0) self.assertEqual(manager.total_blocks, 2) block = BufferManager().get_file_block('spam', 1) Node = manager.Node node = Node.frombytes(block.read()) self.assertEqual(node.is_leaf, True) self.assertEqual(node.keys, [(233, 66.6)]) self.assertEqual(node.children, [7, 0])
def drop_index(index_name): metadata = load_metadata() for table_name, table in metadata.tables.items(): if index_name in table.indexes: file_path = 'schema/tables/' + table_name + '/' + index_name + '.index' fmt = ''.join(metadata.tables[table_name].columns[column].fmt for column in table.indexes[index_name].columns) manager = IndexManager(file_path, fmt) metadata.drop_index(table_name, index_name) try: for i in manager.iter_leaves(): key_list = list() key_list.append(i[0][0]) manager.delete(key_list) except RuntimeError: pass metadata.dump()
def delete_record_conditionally(table_name, conditions): RecordManager.set_file_dir('schema/tables/' + table_name + '/') metadata = load_metadata() if len(conditions) == 1: MinisqlFacade._delete_single_condition(table_name, conditions[0]) elif len(conditions) == 3: if conditions[1] == 'and': records = MinisqlFacade.select_record_conditionally( table_name, conditions) for record in records: for index_name, index in metadata.tables[ table_name].indexes.items(): file_path = RecordManager.file_dir + index_name + '.index' fmt = ''.join( metadata.tables[table_name].columns[column].fmt for column in index.columns) manager = IndexManager(file_path, fmt) pos = list( metadata.tables[table_name].columns.keys()).index( index.columns[0]) key_list = list() key_list.append(record[pos]) itr = manager.find(key_list) trash, value = next(itr) manager.delete(key_list) manager.dump_header() RecordManager.delete(table_name, metadata.tables[table_name].fmt, with_index=1, record_offset=value) # each tuple has its PRIMARY KEY index # actually only support single attribute index elif conditions[1] == 'or': MinisqlFacade._delete_single_condition(table_name, conditions[0]) MinisqlFacade._delete_single_condition(table_name, conditions[2]) else: pass # link the records outside else: pass
def delete_record_all(table_name): RecordManager.set_file_dir('schema/tables/' + table_name + '/') metadata = load_metadata() RecordManager.delete(table_name, metadata.tables[table_name].fmt, with_index=False, conditions={}) for index_name, index in metadata.tables[table_name].indexes.items(): file_path = RecordManager.file_dir + index_name + '.index' fmt = ''.join(metadata.tables[table_name].columns[column].fmt for column in index.columns) manager = IndexManager(file_path, fmt) try: for i in manager.iter_leaves(): key_list = list() key_list.append(i[0][0]) manager.delete(key_list) except RuntimeError: pass
def test_unsuccessful_delete(self): manager = IndexManager('spam', '<id') manager.insert([42, 7.6], 518) manager.insert([233, 66.6], 7) with self.assertRaises(ValueError): manager.delete([2, 3.3])
def test_delete_from_empty(self): manager = IndexManager('spam', '<id') with self.assertRaises(ValueError): manager.delete([2, 3.3])