From 303a5cca6afd2c4046dbb37677b64f575ba0ea6e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 23 Apr 2017 12:00:13 +0530 Subject: [PATCH] API to easily get books in a virtual library Uses the search cache for maximum performance --- src/calibre/db/cache.py | 8 ++++++++ src/calibre/db/tests/reading.py | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 2a273f5bef..d90dc96e1e 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -981,6 +981,14 @@ class Cache(object): ''' return self._search_api(self, query, restriction, virtual_fields=virtual_fields, book_ids=book_ids) + @read_api + def books_in_virtual_library(self, vl): + ' Return the set of books in the specified virtual library ' + vl = self._pref('virtual_libraries', {}).get(vl) if vl else None + if vl is None: + return self.all_book_ids() + return frozenset(self._search('', vl)) + @api def get_categories(self, sort='name', book_ids=None, already_fixed=None, first_letter_sort=False): diff --git a/src/calibre/db/tests/reading.py b/src/calibre/db/tests/reading.py index 0886680414..f1bcc4b220 100644 --- a/src/calibre/db/tests/reading.py +++ b/src/calibre/db/tests/reading.py @@ -483,13 +483,18 @@ class ReadingTest(BaseTest): def test_restrictions(self): # {{{ ' Test searching with and without restrictions ' cache = self.init_cache() - self.assertSetEqual(cache.all_book_ids(), cache.search('')) - self.assertSetEqual({1, 2}, cache.search('', 'not authors:=Unknown')) - self.assertSetEqual(set(), cache.search('authors:=Unknown', 'not authors:=Unknown')) - self.assertSetEqual({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown')) - self.assertSetEqual({2}, cache.search('not authors:"=Author Two"', book_ids={1, 2})) - self.assertSetEqual({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown', book_ids={1,2,3})) - self.assertSetEqual(set(), cache.search('authors:=Unknown', 'not authors:=Unknown', book_ids={1,2,3})) + se = self.assertSetEqual + se(cache.all_book_ids(), cache.search('')) + se({1, 2}, cache.search('', 'not authors:=Unknown')) + se(set(), cache.search('authors:=Unknown', 'not authors:=Unknown')) + se({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown')) + se({2}, cache.search('not authors:"=Author Two"', book_ids={1, 2})) + se({2}, cache.search('not authors:"=Author Two"', 'not authors:=Unknown', book_ids={1,2,3})) + se(set(), cache.search('authors:=Unknown', 'not authors:=Unknown', book_ids={1,2,3})) + se(cache.all_book_ids(), cache.books_in_virtual_library('')) + se(cache.all_book_ids(), cache.books_in_virtual_library('does not exist')) + cache.set_pref('virtual_libraries', {'1':'title:"=Title One"'}) + se({2}, cache.books_in_virtual_library('1')) # }}} def test_search_caching(self): # {{{