def __update_index(self, by_attr, key, value): current_dir = os.path.dirname(os.path.abspath(__file__)) index_file_path = os.path.join(current_dir, 'indexes', '%s_%s.txt' % (self.table_name, by_attr)) with open(index_file_path, 'r+b') as index_file: btree = SBplusTree(index_file) btree.open() btree[key] = value
def __update_index(self, by_attr, key, value): current_dir = os.path.dirname(os.path.abspath(__file__)) index_file_path = os.path.join( current_dir, 'indexes', '%s_%s.txt' % (self.table_name, by_attr)) with open(index_file_path, 'r+b') as index_file: btree = SBplusTree(index_file) btree.open() btree[key] = value
def index(table_name, by_attr): current_dir = os.path.dirname(os.path.abspath(__file__)) table_file_path = os.path.join(current_dir, 'tables', '%s.txt' % table_name) index_file_path = os.path.join(current_dir, 'indexes', '%s_%s.txt' % (table_name, by_attr)) if not os.path.isfile(table_file_path): raise FileNotFoundError('No such table') with open(index_file_path, 'w+b') as index_file: btree = SBplusTree(index_file, 0, 20, 42) btree.startup() with open(table_file_path, 'rb') as table: # read metadata size table.seek(-Metadata.header_length, os.SEEK_END) meta_length = int(table.read(Metadata.header_length)) # read all metadata table.seek(-(meta_length + Metadata.header_length), os.SEEK_END) meta = table.read().split() table_schema = to_string_list(meta[0].split(Delimiters.attr)) attr_index = table_schema.index(by_attr) indicies = [] for index in meta[3:-1]: index_obj = index.split(Delimiters.offset) indicies.append( Metadata.Index(offset=int(index_obj[0]), limit=int(index_obj[1]), attr_limits=to_int_list( index_obj[2].split( Delimiters.attr)))) for index in indicies: table.seek(index.offset) for i, limit in enumerate(index.attr_limits): if i < attr_index: table.seek(limit, os.SEEK_CUR) else: attr = table.read(limit) btree[attr.decode(DBMS_ENCODING)] = index.as_dict() break
def index(table_name, by_attr): current_dir = os.path.dirname(os.path.abspath(__file__)) table_file_path = os.path.join(current_dir, 'tables', '%s.txt' % table_name) index_file_path = os.path.join(current_dir, 'indexes', '%s_%s.txt' % (table_name, by_attr)) if not os.path.isfile(table_file_path): raise FileNotFoundError('No such table') with open(index_file_path, 'w+b') as index_file: btree = SBplusTree(index_file, 0, 20, 42) btree.startup() with open(table_file_path, 'rb') as table: # read metadata size table.seek(-Metadata.header_length, os.SEEK_END) meta_length = int(table.read(Metadata.header_length)) # read all metadata table.seek(-(meta_length + Metadata.header_length), os.SEEK_END) meta = table.read().split() table_schema = to_string_list(meta[0].split(Delimiters.attr)) attr_index = table_schema.index(by_attr) indicies = [] for index in meta[3:-1]: index_obj = index.split(Delimiters.offset) indicies.append(Metadata.Index( offset=int(index_obj[0]), limit=int(index_obj[1]), attr_limits=to_int_list(index_obj[2].split(Delimiters.attr)) )) for index in indicies: table.seek(index.offset) for i, limit in enumerate(index.attr_limits): if i < attr_index: table.seek(limit, os.SEEK_CUR) else: attr = table.read(limit) btree[attr.decode(DBMS_ENCODING)] = index.as_dict() break