mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Same optimization for set_books_in_library
This commit is contained in:
parent
8769c07451
commit
10d5d64590
@ -1399,6 +1399,12 @@ class DeviceMixin(object): # {{{
|
|||||||
it sets the application_id for matched books. Book_on_device uses that
|
it sets the application_id for matched books. Book_on_device uses that
|
||||||
to both speed up matching and to count matches.
|
to both speed up matching and to count matches.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
string_pat = re.compile('(?u)\W|[_]')
|
||||||
|
def clean_string(x):
|
||||||
|
x = x.lower() if x else ''
|
||||||
|
return string_pat.sub('', x)
|
||||||
|
|
||||||
# Force a reset if the caches are not initialized
|
# Force a reset if the caches are not initialized
|
||||||
if reset or not hasattr(self, 'db_book_title_cache'):
|
if reset or not hasattr(self, 'db_book_title_cache'):
|
||||||
# It might be possible to get here without having initialized the
|
# It might be possible to get here without having initialized the
|
||||||
@ -1412,17 +1418,15 @@ class DeviceMixin(object): # {{{
|
|||||||
self.db_book_uuid_cache = {}
|
self.db_book_uuid_cache = {}
|
||||||
for id in db.data.iterallids():
|
for id in db.data.iterallids():
|
||||||
mi = db.get_metadata(id, index_is_id=True)
|
mi = db.get_metadata(id, index_is_id=True)
|
||||||
title = re.sub('(?u)\W|[_]', '', mi.title.lower())
|
title = clean_string(mi.title)
|
||||||
if title not in self.db_book_title_cache:
|
if title not in self.db_book_title_cache:
|
||||||
self.db_book_title_cache[title] = \
|
self.db_book_title_cache[title] = \
|
||||||
{'authors':{}, 'author_sort':{}, 'db_ids':{}}
|
{'authors':{}, 'author_sort':{}, 'db_ids':{}}
|
||||||
if mi.authors:
|
if mi.authors:
|
||||||
authors = authors_to_string(mi.authors).lower()
|
authors = clean_string(authors_to_string(mi.authors))
|
||||||
authors = re.sub('(?u)\W|[_]', '', authors)
|
|
||||||
self.db_book_title_cache[title]['authors'][authors] = mi
|
self.db_book_title_cache[title]['authors'][authors] = mi
|
||||||
if mi.author_sort:
|
if mi.author_sort:
|
||||||
aus = mi.author_sort.lower()
|
aus = clean_string(mi.author_sort)
|
||||||
aus = re.sub('(?u)\W|[_]', '', aus)
|
|
||||||
self.db_book_title_cache[title]['author_sort'][aus] = mi
|
self.db_book_title_cache[title]['author_sort'][aus] = mi
|
||||||
self.db_book_title_cache[title]['db_ids'][mi.application_id] = mi
|
self.db_book_title_cache[title]['db_ids'][mi.application_id] = mi
|
||||||
self.db_book_uuid_cache[mi.uuid] = mi
|
self.db_book_uuid_cache[mi.uuid] = mi
|
||||||
@ -1448,8 +1452,7 @@ class DeviceMixin(object): # {{{
|
|||||||
self.db_book_uuid_cache[book.uuid].application_id
|
self.db_book_uuid_cache[book.uuid].application_id
|
||||||
continue
|
continue
|
||||||
|
|
||||||
book_title = book.title.lower() if book.title else ''
|
book_title = clean_string(book.title)
|
||||||
book_title = re.sub('(?u)\W|[_]', '', book_title)
|
|
||||||
book.in_library = None
|
book.in_library = None
|
||||||
d = self.db_book_title_cache.get(book_title, None)
|
d = self.db_book_title_cache.get(book_title, None)
|
||||||
if d is not None:
|
if d is not None:
|
||||||
@ -1471,8 +1474,7 @@ class DeviceMixin(object): # {{{
|
|||||||
if book.authors:
|
if book.authors:
|
||||||
# Compare against both author and author sort, because
|
# Compare against both author and author sort, because
|
||||||
# either can appear as the author
|
# either can appear as the author
|
||||||
book_authors = authors_to_string(book.authors).lower()
|
book_authors = clean_string(authors_to_string(book.authors))
|
||||||
book_authors = re.sub('(?u)\W|[_]', '', book_authors)
|
|
||||||
if book_authors in d['authors']:
|
if book_authors in d['authors']:
|
||||||
book.in_library = True
|
book.in_library = True
|
||||||
book.application_id = \
|
book.application_id = \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user