Esempio n. 1
0
 def insert_record(table_name, attributes):
     RecordManager.set_file_dir('schema/tables/' + table_name + '/')
     metadata = load_metadata()
     position = RecordManager.insert(table_name,
                                     metadata.tables[table_name].fmt,
                                     tuple(attributes))
     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)
         key_pos = list(metadata.tables[table_name].columns.keys()).index(
             index.columns[0])
         key_list = list()
         key_list.append(attributes[key_pos])
         try:
             manager.insert(
                 key_list, position)  # index can be set on single attribute
             manager.dump_header()
         except ValueError:  # duplicated key, the inserted record should be deleted
             RecordManager.delete(table_name,
                                  metadata.tables[table_name].fmt,
                                  with_index=True,
                                  record_offset=position)
             raise
Esempio n. 2
0
 def test_find_all(self):
     manager = IndexManager('spam', '<id')
     manager.insert([42, 7.6], 518)
     manager.insert([42, 7.6], 212)
     manager.insert([233, 66.6], 7)
     results = manager.find([42, 7.6])
     self.assertEqual(sorted(results), [212, 518])
Esempio n. 3
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)
Esempio n. 4
0
    def test_initial_insert(self):
        manager = IndexManager('spam', '<id')
        manager.insert([42, 7.6], 518)

        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, [(42, 7.6)])
        self.assertEqual(node.children, [518, 0])
Esempio n. 5
0
    def test_multiple_delete(self):
        manager = IndexManager('spam', '<id')
        manager.insert([42, 7.6], 518)
        manager.insert([42, 7.6], 212)
        manager.insert([233, 66.6], 7)
        deleted_num = manager.delete([42, 7.6])

        self.assertEqual(deleted_num, 2)
        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])
Esempio n. 6
0
    def test_persistence(self):
        manager = IndexManager('spam', '<id')
        manager.insert([42, 7.6], 518)
        manager.insert([233, 66.6], 7)
        manager.delete([42, 7.6])
        manager.dump_header()
        manager._manager.flush_all()
        del manager

        manager = IndexManager('spam', '<id')
        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])
Esempio n. 7
0
 def create_index(table_name, index_name, column_name):
     RecordManager.set_file_dir('schema/tables/' + table_name + '/')
     offset = -1
     metadata = load_metadata()
     metadata.add_index(table_name, index_name, column_name)
     records = RecordManager.select(table_name,
                                    metadata.tables[table_name].fmt,
                                    with_index=False,
                                    conditions={})
     file_path = RecordManager.file_dir + index_name + '.index'
     table_target = metadata.tables[table_name]
     fmt = ''.join(table_target.columns[column].fmt
                   for column in table_target.indexes[index_name].columns)
     manager = IndexManager(file_path, fmt)
     key_pos = list(
         metadata.tables[table_name].columns.keys()).index(column_name)
     for record in records:
         key_list = list()
         key_list.append(record[key_pos])
         offset += 1
         manager.insert(key_list, offset)
     metadata.dump()
     manager.dump_header()
Esempio n. 8
0
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])
Esempio n. 9
0
 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])
Esempio n. 10
0
 def test_find_not_exists(self):
     manager = IndexManager('spam', '<id')
     manager.insert([42, 7.6], 518)
     manager.insert([233, 66.6], 7)
     result = manager.find([233, 7.6])
     self.assertEqual(next(result), ((233, 66.6), 7))
Esempio n. 11
0
 def test_insert_duplicate(self):
     manager = IndexManager('spam', '<id')
     manager.insert([42, 7.6], 518)
     with self.assertRaises(ValueError):
         manager.insert([42, 7.6], 233)