def delete_game_for_update_at(self,idx): start_offset = self.entries[idx].pgn_offset # if the last game is to be deleted, first get # the offset at the very end of the file stop_offset = None if(idx == len(self.entries) -1): stop_offset = self.get_end_offset() print("stop offset: "+str(stop_offset)) else: # just take the start of the next game as end stop_offset = self.entries[idx+1].pgn_offset length = stop_offset - start_offset pgn = open(self.filename, 'r+') m=mm.mmap(pgn.fileno(),0) size = len(m) new_size = size - length m.move(start_offset,stop_offset,size-stop_offset) m.flush() m.close() pgn.truncate(new_size) pgn.close() return length
def delete_game_at(self,idx): start_offset = self.entries[idx].pgn_offset # if the last game is to be deleted, first get # the offset at the very end of the file stop_offset = None if(idx == len(self.entries) -1): stop_offset = self.get_end_offset() else: # just take the start of the next game as end stop_offset = self.entries[idx+1].pgn_offset length = stop_offset - start_offset # we can't mmap an empty file # the file is however empty, the # game to be deleted was the only one in the database # just delete it current_filesize = os.stat(self.filename).st_size print("current fs: "+str(current_filesize)) if current_filesize == 0: print("file is empty") pgn = open(self.filename, 'r+') pgn.close() else: pgn = open(self.filename, 'r+') m=mm.mmap(pgn.fileno(),0) size = len(m) new_size = size - length m.move(start_offset,stop_offset,size-stop_offset) m.flush() m.close() pgn.truncate(new_size) pgn.close() self.checksum = crc32_from_file(self.filename) for i in range(idx, len(self.entries)): self.entries[i].pgn_offset -= length del(self.entries[idx]) # if idx was the current open game, set current_idx to None if(idx == self.index_current_game): self.index_current_game = None
def delete_game_at(self, idx): start_offset = self.entries[idx].pgn_offset # if the last game is to be deleted, first get # the offset at the very end of the file stop_offset = None if (idx == len(self.entries) - 1): stop_offset = self.get_end_offset() else: # just take the start of the next game as end stop_offset = self.entries[idx + 1].pgn_offset length = stop_offset - start_offset # we can't mmap an empty file # the file is however empty, the # game to be deleted was the only one in the database # just delete it current_filesize = os.stat(self.filename).st_size if current_filesize == 0: pgn = open(self.filename, 'r+') pgn.close() else: pgn = open(self.filename, 'r+') m = mm.mmap(pgn.fileno(), 0) size = len(m) new_size = size - length m.move(start_offset, stop_offset, size - stop_offset) m.flush() m.close() pgn.truncate(new_size) pgn.close() self.checksum = crc32_from_file(self.filename) for i in range(idx, len(self.entries)): self.entries[i].pgn_offset -= length del (self.entries[idx]) # if idx was the current open game, set current_idx to None if (idx == self.index_current_game): self.index_current_game = None
def delete_game_for_update_at(self, idx): start_offset = self.entries[idx].pgn_offset # if the last game is to be deleted, first get # the offset at the very end of the file stop_offset = None if (idx == len(self.entries) - 1): stop_offset = self.get_end_offset() else: # just take the start of the next game as end stop_offset = self.entries[idx + 1].pgn_offset length = stop_offset - start_offset pgn = open(self.filename, 'r+') m = mm.mmap(pgn.fileno(), 0) size = len(m) new_size = size - length m.move(start_offset, stop_offset, size - stop_offset) m.flush() m.close() pgn.truncate(new_size) pgn.close() return length