def _has_index(attribute_name, table_name): metadata = load_metadata() for index_name, index in metadata.tables[table_name].indexes.items(): if attribute_name in index.columns: file_path = RecordManager.file_dir + index_name + '.index' fmt = metadata.tables[table_name].columns[attribute_name].fmt manager = IndexManager(file_path, fmt) try: manager.iter_leaves() return index_name except RuntimeError: pass else: pass return None
def _select_single_condition(table_name, condition): metadata = load_metadata() index_name = MinisqlFacade._has_index(condition[0], table_name) if index_name: print('select with index on ', condition[0]) attribute_name = condition[0] operator = condition[1] key_list = list() key_list.append(condition[2]) file_path = RecordManager.file_dir + index_name + '.index' # fmt = metadata.tables[table_name].columns[attribute_name].fmt manager = IndexManager(file_path, fmt) records = list() if operator == '=': try: itr = manager.find(key_list) it_key, value = next(itr) if it_key[0] == key_list[0]: records.append( RecordManager.select( table_name, metadata.tables[table_name].fmt, with_index=True, record_offset=value)) except StopIteration: pass elif operator == '>': for i in manager.find(key_list): if i[0][0] > key_list[0]: value = i[1] records.append( RecordManager.select( table_name, metadata.tables[table_name].fmt, with_index=True, record_offset=value)) elif operator == '<': for i in manager.iter_leaves(): if i[0][0] < key_list[0]: value = i[1] records.append( RecordManager.select( table_name, metadata.tables[table_name].fmt, with_index=True, record_offset=value)) else: break else: pass else: records = MinisqlFacade.select_record_conditionally_without_index( table_name, condition) return records
def drop_index(index_name): metadata = load_metadata() for table_name, table in metadata.tables.items(): if index_name in table.indexes: file_path = 'schema/tables/' + table_name + '/' + index_name + '.index' fmt = ''.join(metadata.tables[table_name].columns[column].fmt for column in table.indexes[index_name].columns) manager = IndexManager(file_path, fmt) metadata.drop_index(table_name, index_name) try: for i in manager.iter_leaves(): key_list = list() key_list.append(i[0][0]) manager.delete(key_list) except RuntimeError: pass metadata.dump()
def delete_record_all(table_name): RecordManager.set_file_dir('schema/tables/' + table_name + '/') metadata = load_metadata() RecordManager.delete(table_name, metadata.tables[table_name].fmt, with_index=False, conditions={}) for index_name, index in metadata.tables[table_name].indexes.items(): file_path = RecordManager.file_dir + index_name + '.index' fmt = ''.join(metadata.tables[table_name].columns[column].fmt for column in index.columns) manager = IndexManager(file_path, fmt) try: for i in manager.iter_leaves(): key_list = list() key_list.append(i[0][0]) manager.delete(key_list) except RuntimeError: pass