def binary_search_table_interior(fd, cell_pointer, target_value, right_most_pointer): global table_interior_page_read table_interior_page_read += 1 n = len(cell_pointer) // 2 cell = cell_pointer[n] fd.seek(cell) left_pointer = util.read_to_int(fd, 4) row_id = util.decode_varint(fd)[0] if len(cell_pointer) == 1: if target_value <= row_id: return left_pointer else: return right_most_pointer if len(cell_pointer) == 2: if target_value <= row_id: cell_0 = cell_pointer[0] fd.seek(cell_0) left_pointer_0 = util.read_to_int(fd, 4) row_id_0 = util.decode_varint(fd)[0] if target_value <= row_id_0: return left_pointer_0 return left_pointer else: return right_most_pointer else: if target_value <= row_id: return binary_search_table_interior(fd, cell_pointer[:n + 1], target_value, right_most_pointer) else: return binary_search_table_interior(fd, cell_pointer[n + 1:], target_value, right_most_pointer)
def binary_search_table_leaf(fd, cell_pointer, target_value): global data_page_read data_page_read += 1 n = len(cell_pointer) // 2 cell = cell_pointer[n] fd.seek(cell) payload_byte = util.decode_varint(fd) row_id, row_id_size = util.decode_varint(fd) if n == 0: if target_value == row_id: header_size = util.decode_varint(fd)[0] fd.seek(cell + payload_byte[1] + row_id_size + header_size) emp_id = util.read_to_int(fd, 3) record = fd.read(payload_byte[0] - 3) employee = util.construct_record(emp_id, record) return employee else: print('row id does not exist in this page, something is wrong.') exit(1) else: if target_value < row_id: return binary_search_table_leaf(fd, cell_pointer[:n], target_value) else: return binary_search_table_leaf(fd, cell_pointer[n:], target_value)
def read_table_leaf_cell(fd, usable_size, cell): fd.seek(cell) payload_byte = util.decode_varint(fd) rowid_byte = util.decode_varint(fd) header_size = util.decode_varint(fd)[0] _, initial_payload_size = util.overflow(13, payload_byte[0], usable_size) fd.seek(cell + payload_byte[1] + rowid_byte[1] + header_size) emp_id = util.read_to_int(fd, 3) reset_of_record = fd.read(initial_payload_size - 3) # if overflow: # TODO # pass employee = util.construct_record(emp_id, reset_of_record) return employee
def search_index_leaf_range(fd, cell_pointer, lower_bound, upper_bound): global index_leaf_page_read index_leaf_page_read += 1 result = [] for cell in cell_pointer: fd.seek(cell) payload_byte = util.decode_varint(fd) header_size = util.decode_varint(fd)[0] emp_id_size = util.read_to_int(fd, 1) row_id_size = util.read_to_int(fd, 1) fd.seek(cell + payload_byte[1] + header_size) emp_id = util.read_to_int(fd, emp_id_size) row_id = util.read_to_int(fd, row_id_size) if emp_id > lower_bound and emp_id < upper_bound: result.append(row_id) return result
def binary_search_index_interior(fd, cell_pointer, target_value, right_most_pointer): global index_interior_page_read index_interior_page_read += 1 n = len(cell_pointer) // 2 cell = cell_pointer[n] fd.seek(cell) left_pointer = util.read_to_int(fd, 4) key_payload_byte = util.decode_varint(fd) key_header_size = util.decode_varint(fd)[0] emp_id_size = util.read_to_int(fd, 1) fd.seek(cell + 4 + key_payload_byte[1] + key_header_size) emp_id = util.read_to_int(fd, emp_id_size) if len(cell_pointer) == 1: if target_value < emp_id: return left_pointer else: return right_most_pointer elif len(cell_pointer) == 2: if target_value < emp_id: cell_0 = cell_pointer[0] fd.seek(cell_0) left_pointer_0 = util.read_to_int(fd, 4) key_payload_byte_0 = util.decode_varint(fd) key_header_size_0 = util.decode_varint(fd)[0] emp_id_size_0 = util.read_to_int(fd, 1) fd.seek(cell_0 + key_payload_byte_0[1] + key_header_size_0) emp_id_0 = util.read_to_int(fd, emp_id_size_0) if target_value < emp_id_0: return left_pointer_0 return left_pointer else: return right_most_pointer else: if target_value < emp_id: return binary_search_index_interior(fd, cell_pointer[:n + 1], target_value, right_most_pointer) else: return binary_search_index_interior(fd, cell_pointer[n + 1:], target_value, right_most_pointer)
def search_index_interior_range(fd, cell_pointer, lower_bound, upper_bound, right_most_pointer): global index_interior_page_read index_interior_page_read += 1 result = [] for cell in cell_pointer: fd.seek(cell) left_pointer = util.read_to_int(fd, 4) key_payload_byte = util.decode_varint(fd) key_header_size = util.decode_varint(fd)[0] emp_id_size = util.read_to_int(fd, 1) fd.seek(cell + 4 + key_payload_byte[1] + key_header_size) emp_id = util.read_to_int(fd, emp_id_size) if emp_id > lower_bound and emp_id < upper_bound: result.append(left_pointer) if emp_id > upper_bound: result.append(left_pointer) break if emp_id < upper_bound: result.append(right_most_pointer) return result
def binary_search_index_leaf(fd, cell_pointer, target_value): global index_leaf_page_read index_leaf_page_read += 1 n = len(cell_pointer) // 2 cell = cell_pointer[n] fd.seek(cell) payload_byte = util.decode_varint(fd) header_size = util.decode_varint(fd)[0] emp_id_size = util.read_to_int(fd, 1) row_id_size = util.read_to_int(fd, 1) fd.seek(cell + payload_byte[1] + header_size) emp_id = util.read_to_int(fd, emp_id_size) row_id = util.read_to_int(fd, row_id_size) if n == 0: if target_value == emp_id: return row_id else: return None else: if target_value < emp_id: return binary_search_index_leaf(fd, cell_pointer[:n], target_value) else: return binary_search_index_leaf(fd, cell_pointer[n:], target_value)