From eac8447e478b7e1fad8d3378ec7077c2c163a2b8 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Fri, 3 Dec 2010 13:09:20 +0000 Subject: [PATCH] Speed up manage_authors by orders of magnitude. In the process, make database_changed notification sane, doing the operation once even if many emits were done. --- src/calibre/gui2/tag_view.py | 10 ++++++++-- src/calibre/library/database2.py | 7 ++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index 972a1eeba3..768b699ca9 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -106,10 +106,13 @@ class TagsView(QTreeView): # {{{ self.refresh_required.connect(self.recount, type=Qt.QueuedConnection) self.sort_by.currentIndexChanged.connect(self.sort_changed) self.made_connections = True + self.refresh_signal_processed = True db.add_listener(self.database_changed) def database_changed(self, event, ids): - self.refresh_required.emit() + if self.refresh_signal_processed: + self.refresh_signal_processed = False + self.refresh_required.emit() @property def match_all(self): @@ -295,6 +298,7 @@ class TagsView(QTreeView): # {{{ return self.isExpanded(idx) def recount(self, *args): + self.refresh_signal_processed = True ci = self.currentIndex() if not ci.isValid(): ci = self.indexAt(QPoint(10, 10)) @@ -937,7 +941,9 @@ class TagBrowserMixin(object): # {{{ if old_author != new_author: # The id might change if the new author already exists id = db.rename_author(id, new_author) - db.set_sort_field_for_author(id, unicode(new_sort)) + db.set_sort_field_for_author(id, unicode(new_sort), + commit=False, notify=False) + db.commit() self.library_view.model().refresh() self.tags_view.recount() diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index a07e46577e..4e05aa3a95 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1639,15 +1639,16 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): return [] return result - def set_sort_field_for_author(self, old_id, new_sort): + def set_sort_field_for_author(self, old_id, new_sort, commit=True, notify=False): self.conn.execute('UPDATE authors SET sort=? WHERE id=?', \ (new_sort.strip(), old_id)) - self.conn.commit() + if commit: + self.conn.commit() # Now change all the author_sort fields in books by this author bks = self.conn.get('SELECT book from books_authors_link WHERE author=?', (old_id,)) for (book_id,) in bks: ss = self.author_sort_from_book(book_id, index_is_id=True) - self.set_author_sort(book_id, ss) + self.set_author_sort(book_id, ss, notify=notify, commit=commit) def rename_author(self, old_id, new_name): # Make sure that any commas in new_name are changed to '|'!