Пример #1
0
    def add_index(self, index_node, block=None):
        key = index_node.get_key()
        root_block_location = self.get_root_block()

        if block is None:
            block = self.find_leaf_block(key, root_block_location)

        parent = block.get_parent_block()
        block.add_index_node(index_node)

        if block.get_size() < self._block_size:
            block.write_block(self._fd)
            if root_block_location == -1:
                self.set_root_block(block.get_location())
            return

        new_block, median_node = self.split_block(block)
        if parent != -1:
            parent = IndexBlock(parent, self._index_name, self._index_directory)
            parent.read_block(self._fd)
            block.write_block(self._fd)
            new_block.write_block(self._fd)
            self.add_index(median_node, parent)
        else:
            root_block = IndexBlock(self.get_free_block_pointer(), self._index_name, self._index_directory)
            root_block.create_dummy_node()
            root_block.get_index_nodes()[-1].set_index_block_pointer(block.get_location())
            root_block.add_index_node(median_node)
            block.set_parent_block(root_block.get_location())
            new_block.set_parent_block(root_block.get_location())
            self.set_root_block(root_block.get_location())
            root_block.write_block(self._fd)
            new_block.write_block(self._fd)
            block.write_block(self._fd)
Пример #2
0
    def find_index_block(self, index_node):
        block_location = self.get_root_block()
        while block_location != -1:
            block = IndexBlock(block_location, self._index_name, self._index_directory)
            block.read_block(self._fd)

            for node in block.get_index_nodes():
                if index_node.get_key() < node.get_key():
                    block_location = node.get_index_block_pointer()
                    break
                elif index_node.get_key() == node.get_key():
                    return block, node
        return None, None
Пример #3
0
    def delete_index_node(self, index_node):
        block, index_node = self.find_index_block(index_node)
        if block is None:
            print("Fail")
            return

        successor_block, successor_node = self.find_successor(block, index_node)

        if block == successor_block:
            successor_node.set_index_block_pointer(index_node.get_index_block_pointer())
            block.delete_index_node(index_node)
            if len(block.get_index_nodes()) == 1:
                if block.get_location() != self.get_root_block():
                    parent_location = block.get_parent_block()
                    if parent_location != -1:
                        parent = IndexBlock(parent_location, self._index_name, self._index_directory)
                        parent.read_block(self._fd)
                        for node in parent.get_index_nodes():
                            if node.get_index_block_pointer() == block.get_location():
                                node.set_index_block_pointer(block.get_index_nodes()[0].get_index_block_pointer())

                        parent.update_child_blocks(self._fd)
                        parent.write_block(self._fd)
                        block.delete_block(self._fd, self.get_free_block_pointer())
                else:
                    block.write_block(self._fd)
            else:
                block.write_block(self._fd)
        else :
            successor_block.delete_index_node(successor_node)
            block.delete_index_node(index_node)
            successor_node.set_index_block_pointer(index_node.get_index_block_pointer())
            block.add_index_node(successor_node)
            block.write_block(self._fd)
            if len(successor_block.get_index_nodes()) == 1:
                if successor_block.get_location() != self.get_root_block():
                    parent_location = successor_block.get_parent_block()
                    if parent_location != -1:
                        parent = IndexBlock(parent_location, self._index_name, self._index_directory)
                        parent.read_block(self._fd)
                        for node in parent.get_index_nodes():
                            if node.get_index_block_pointer() == successor_block.get_location():
                                node.set_index_block_pointer(successor_block.get_index_nodes()[0].get_index_block_pointer())
                        parent.update_child_blocks(self._fd)
                        parent.write_block(self._fd)
                    successor_block.delete_block(self._fd, self.get_free_block_pointer())
                else:
                    successor_block.write_block(self._fd)
            else:
                successor_block.write_block(self._fd)
Пример #4
0
    def find_index_node(self, key):
        block_location = self.get_root_block()
        while block_location != -1:
            block = IndexBlock(block_location, self._index_name, self._index_directory)
            block.read_block(self._fd)
            flag = 0
            for node in block.get_index_nodes():
                if key < node.get_key():
                    flag = 1
                    block_location = node.get_index_block_pointer()
                    break
                elif key == node.get_key():
                    return node.get_key(), node.get_value()

            if flag == 0:
                return key, None
        return key,
Пример #5
0
    def find_leaf_block(self, key, root):
        if root == -1:
            block = IndexBlock(self.get_free_block_pointer(), self._index_name, self._index_directory)
            block.create_dummy_node()
            return block
        block = IndexBlock(root, self._index_name, self._index_directory)
        parent = -1
        block.read_block(self._fd)

        while not block.is_leaf():
            parent = block.get_location()
            index_nodes = block.get_index_nodes()
            for node in index_nodes:
                if key < node.get_key():
                    block = IndexBlock(node.get_index_block_pointer(), self._index_name, self._index_directory)
                    break
            if block.get_location() == -1:
                return parent
            block.read_block(self._fd)
        return block
Пример #6
0
    def find_successor(self, block, index_node):
        index_nodes = block.get_index_nodes()

        for i in range(len(index_nodes)):
            if index_nodes[i].get_key() == index_node.get_key():
                break
        i += 1

        if block.get_index_nodes()[i].get_index_block_pointer() == -1:
            return block, index_nodes[i]

        block_location = block.get_index_nodes()[i].get_index_block_pointer()
        block = IndexBlock(block_location, self._index_name, self._index_directory)
        block.read_block(self._fd)
        while block.get_index_nodes()[0].get_index_block_pointer() != -1:
            block_location = block.get_index_nodes()[0].get_index_block_pointer()
            block = IndexBlock(block_location, self._index_name, self._index_directory)
            block.read_block(self._fd)

        return block, block.get_index_nodes()[0]