'Splits full leaf in two separate ones, first half of records stays on old position,
second half is written as new leaf at the end of file.'

def _split_leaf(self, leaf_start, nr_of_records_to_rewrite, new_key, new_doc_id, new_start, new_size, new_status, create_new_root=False):

    half_size = (self.node_capacity / 2)
    if ((self.node_capacity % 2) == 0):
        old_leaf_size = half_size
        new_leaf_size = (half_size + 1)
    else:
        old_leaf_size = new_leaf_size = (half_size + 1)
    if create_new_root:
        new_data = [new_key, new_doc_id, new_start, new_size, new_status]
        self._create_new_root_from_leaf(leaf_start, nr_of_records_to_rewrite, new_leaf_size, old_leaf_size, half_size, new_data)
    else:
        blanks = (((self.node_capacity - new_leaf_size) * self.single_leaf_record_size) * '\x00')
        (prev_l, next_l) = self._read_leaf_neighbours(leaf_start)
        if (nr_of_records_to_rewrite > half_size):
            self.buckets.seek(self._calculate_key_position(leaf_start, (self.node_capacity - nr_of_records_to_rewrite), 'l'))
            data = self.buckets.read((nr_of_records_to_rewrite * self.single_leaf_record_size))
            records_to_rewrite = struct.unpack(('<' + (nr_of_records_to_rewrite * self.single_leaf_record_format)), data)
            if self._update_if_has_deleted(leaf_start, records_to_rewrite, (self.node_capacity - nr_of_records_to_rewrite), [new_key, new_doc_id, new_start, new_size, new_status]):
                return None
            key_moved_to_parent_node = records_to_rewrite[((- new_leaf_size) * 5)]
            self.buckets.seek(0, 2)
            new_leaf_start = self.buckets.tell()
            new_leaf = struct.pack(((('<' + self.elements_counter_format) + (2 * self.pointer_format)) + (self.single_leaf_record_format * new_leaf_size)), new_leaf_size, leaf_start, next_l, *records_to_rewrite[((- new_leaf_size) * 5):])
            new_leaf += blanks
            self.buckets.write(new_leaf)
            self._update_leaf_size_and_pointers(leaf_start, old_leaf_size, prev_l, new_leaf_start)
            self.buckets.seek(self._calculate_key_position(leaf_start, (self.node_capacity - nr_of_records_to_rewrite), 'l'))
            self.buckets.write(struct.pack(('<' + (self.single_leaf_record_format * ((nr_of_records_to_rewrite - new_leaf_size) + 1))), new_key, new_doc_id, new_start, new_size, 'o', *records_to_rewrite[:((- new_leaf_size) * 5)]))
            if next_l:
                self._update_leaf_prev_pointer(next_l, new_leaf_start)
            self._find_key_in_leaf.delete(leaf_start)
            return (new_leaf_start, key_moved_to_parent_node)
        else:
            self.buckets.seek(self._calculate_key_position(leaf_start, old_leaf_size, 'l'))
            data = self.buckets.read((self.single_leaf_record_size * (new_leaf_size - 1)))
            records_to_rewrite = struct.unpack(('<' + ((new_leaf_size - 1) * self.single_leaf_record_format)), data)
            if self._update_if_has_deleted(leaf_start, records_to_rewrite, old_leaf_size, [new_key, new_doc_id, new_start, new_size, new_status]):
                return None
            key_moved_to_parent_node = records_to_rewrite[((- (new_leaf_size - 1)) * 5)]
            if (key_moved_to_parent_node > new_key):
                key_moved_to_parent_node = new_key
            self.buckets.seek(0, 2)
            new_leaf_start = self.buckets.tell()
            index_of_records_split = (nr_of_records_to_rewrite * 5)
            if index_of_records_split:
                records_before = records_to_rewrite[:(- index_of_records_split)]
                records_after = records_to_rewrite[(- index_of_records_split):]
            else:
                records_before = records_to_rewrite
                records_after = []
            new_leaf = struct.pack(((('<' + self.elements_counter_format) + (2 * self.pointer_format)) + (self.single_leaf_record_format * ((new_leaf_size - nr_of_records_to_rewrite) - 1))), new_leaf_size, leaf_start, next_l, *records_before)
            new_leaf += struct.pack(('<' + (self.single_leaf_record_format * (nr_of_records_to_rewrite + 1))), new_key, new_doc_id, new_start, new_size, 'o', *records_after)
            new_leaf += blanks
            self.buckets.write(new_leaf)
            self._update_leaf_size_and_pointers(leaf_start, old_leaf_size, prev_l, new_leaf_start)
            if next_l:
                self._update_leaf_prev_pointer(next_l, new_leaf_start)
            self._find_key_in_leaf.delete(leaf_start)
            return (new_leaf_start, key_moved_to_parent_node)
