From 49c82ededa935326f84b9164ed7b5a0795eb6d0c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 28 Jul 2013 11:30:41 +0530 Subject: [PATCH] Add has_id() to db.data in the new backend Also speed up has_id() and all_book_ids() --- src/calibre/db/cache.py | 6 +++++- src/calibre/db/legacy.py | 4 ++-- src/calibre/db/view.py | 9 ++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index d5e6bb25aa..0be9f9f9ef 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -401,7 +401,7 @@ class Cache(object): ''' Frozen set of all known book ids. ''' - return type(self.fields['uuid']) + return type(self.fields['uuid'].table.book_col_map) @read_api def all_field_ids(self, name): @@ -1236,6 +1236,10 @@ class Cache(object): return True return False + @read_api + def has_id(self, book_id): + return book_id in self.fields['title'].table.book_col_map + @write_api def create_book_entry(self, mi, cover=None, add_duplicates=True, force_id=None, apply_import_tags=True, preserve_uuid=False): if mi.tags: diff --git a/src/calibre/db/legacy.py b/src/calibre/db/legacy.py index 3da725fe7e..bd595fb36f 100644 --- a/src/calibre/db/legacy.py +++ b/src/calibre/db/legacy.py @@ -143,7 +143,7 @@ class LibraryDatabase(object): self.data.cache.initialize_template_cache() def all_ids(self): - for book_id in self.data.cache.all_book_ids(): + for book_id in self.new_api.all_book_ids(): yield book_id def is_empty(self): @@ -510,7 +510,7 @@ class LibraryDatabase(object): self.new_api._remove_items('tags', tag_ids) def has_id(self, book_id): - return book_id in self.new_api.all_book_ids() + return self.new_api.has_id(book_id) def format(self, index, fmt, index_is_id=False, as_file=False, mode='r+b', as_path=False, preserve_filename=False): book_id = index if index_is_id else self.id(index) diff --git a/src/calibre/db/view.py b/src/calibre/db/view.py index f679fc2416..7468ec11b2 100644 --- a/src/calibre/db/view.py +++ b/src/calibre/db/view.py @@ -135,10 +135,13 @@ class View(object): return self.cache.field_metadata def _get_id(self, idx, index_is_id=True): - if index_is_id and idx not in self.cache.all_book_ids(): + if index_is_id and not self.cache.has_id(idx): raise IndexError('No book with id %s present'%idx) return idx if index_is_id else self.index_to_id(idx) + def has_id(self, book_id): + return self.cache.has_id(book_id) + def __getitem__(self, row): return TableRow(self._map_filtered[row], self) @@ -177,7 +180,7 @@ class View(object): def _get(self, field, idx, index_is_id=True, default_value=None, fmt=lambda x:x): id_ = idx if index_is_id else self.index_to_id(idx) - if index_is_id and id_ not in self.cache.all_book_ids(): + if index_is_id and not self.cache.has_id(id_): raise IndexError('No book with id %s present'%idx) return fmt(self.cache.field_for(field, id_, default_value=default_value)) @@ -323,7 +326,7 @@ class View(object): self.cache.clear_search_caches(old_marked_ids | set(self.marked_ids)) def refresh(self, field=None, ascending=True, clear_caches=True): - self._map = tuple(self.cache.all_book_ids()) + self._map = tuple(sorted(self.cache.all_book_ids())) self._map_filtered = tuple(self._map) if clear_caches: self.cache.clear_caches()