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() < (0.80*self._block_size): self.write_block(block) 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 = self.read_block(parent) self.write_block(block) self.write_block(new_block) 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.set_dummy_node_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()) self.write_block(root_block) self.write_block(new_block) self.write_block(block)
def split_block(self, block): index_nodes = block.get_index_nodes() median = int(len(index_nodes)/2) 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.set_dummy_node_pointer(median_node.get_index_block_pointer()) i = 0 while i <= median: block.delete_index_node(index_nodes[0]) i += 1 self.update_child_blocks(new_block) median_node.set_index_block_pointer(new_block.get_location()) new_block.set_parent_block(block.get_parent_block()) return new_block, median_node