From 114e5cf3a771c64421108ae65e839e6c6b65b5f0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 24 Jul 2013 18:55:39 +0530 Subject: [PATCH] Ensure that removing books does not cause errors when updating search caches --- src/calibre/db/cache.py | 8 +++++--- src/calibre/db/search.py | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 23e4498e72..ef4c8854b4 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -154,7 +154,7 @@ class Cache(object): self._search_api.update_or_clear(self, book_ids) @write_api - def clear_caches(self, book_ids=None, template_cache=True): + def clear_caches(self, book_ids=None, template_cache=True, search_cache=True): if template_cache: self._initialize_template_cache() # Clear the formatter template cache for field in self.fields.itervalues(): @@ -165,7 +165,8 @@ class Cache(object): self.format_metadata_cache.pop(book_id, None) else: self.format_metadata_cache.clear() - self._clear_search_caches(book_ids) + if search_cache: + self._clear_search_caches(book_ids) @write_api def reload_from_db(self, clear_caches=True): @@ -1303,7 +1304,8 @@ class Cache(object): continue # Some fields like ondevice do not have tables else: table.remove_books(book_ids, self.backend) - self._clear_caches(book_ids=book_ids, template_cache=False) + self._search_api.discard_books(book_ids) + self._clear_caches(book_ids=book_ids, template_cache=False, search_cache=False) @read_api def author_sort_strings_for_books(self, book_ids): diff --git a/src/calibre/db/search.py b/src/calibre/db/search.py index 3b441e6b2f..759b76fdb8 100644 --- a/src/calibre/db/search.py +++ b/src/calibre/db/search.py @@ -808,6 +808,11 @@ class Search(object): finally: sqp.dbcache = sqp.lookup_saved_search = None + def discard_books(self, book_ids): + book_ids = set(book_ids) + for query, result in self.cache: + result.difference_update(book_ids) + def _update_caches(self, sqp, book_ids): book_ids = sqp.all_book_ids = set(book_ids) remove = set()