Esempio n. 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)
Esempio n. 2
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 = self.read_block(root)
        parent = -1

        while not block.is_leaf():
            parent = block.get_location()
            index_nodes = block.get_index_nodes()
            flag = 0
            for node in index_nodes:
                if key < node.get_key():
                    flag = 1
                    block_location = node.get_index_block_pointer()
                    break
            if flag == 0:
                block_location = block.get_dummy_node_pointer()
            if block_location == -1:
                return self.read_block(parent)
            if block_location == None:
                x = 1
            block = self.read_block(block_location)
        return block
Esempio n. 3
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
Esempio n. 4
0
    def split_block(self, block):
        index_nodes = block.get_index_nodes()
        median = int(len(index_nodes)/2 - 1)
        median_node = index_nodes[median]

        new_block = IndexBlock(self.get_free_block_pointer(), self._index_name, self._index_directory)
        new_block.create_dummy_node()
        i = 0
        while i < median:
            new_block.add_index_node(index_nodes[i])
            i += 1

        new_block.get_index_nodes()[-1].set_index_block_pointer(median_node.get_index_block_pointer())
        i = 0
        while i <= median:
            block.delete_index_node(index_nodes[0])
            i += 1

        new_block.update_child_blocks(self._fd)
        median_node.set_index_block_pointer(new_block.get_location())
        new_block.set_parent_block(block.get_parent_block())
        return new_block, median_node