Cleanup previous PR

Use a dedicated API for better performance when calculating author sorts
This commit is contained in:
Kovid Goyal 2026-01-14 20:04:00 +05:30
parent 99eaca251f
commit 244ed64865
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 21 additions and 5 deletions

View File

@ -1075,6 +1075,20 @@ class Cache:
return {aid:af.author_data(aid) for aid in af.table.id_map}
return {aid:af.author_data(aid) for aid in author_ids if aid in af.table.id_map}
@read_api
def author_sorts(self, author_ids=None) -> dict[int, str]:
'''
Return author sorts for specified authors.
If no authors with the specified ids are found an empty dictionary is
returned. If author_ids is None, data for all authors is returned.
'''
af = self.fields['authors']
m = af.table.asort_map
if author_ids is None:
return m.copy()
return {aid:m[aid] for aid in author_ids if aid in m}
@read_api
def format_hash(self, book_id, fmt):
''' Return the hash of the specified format for the specified book. The

View File

@ -789,6 +789,8 @@ class WritingTest(BaseTest):
'Setting the author link to the same value as before, incorrectly marked some books as dirty')
sdata = {aid:f'{aid}, changed' for aid in adata}
self.assertEqual({1,2,3}, cache.set_sort_for_authors(sdata))
self.assertEqual(sdata, cache.author_sorts())
self.assertEqual(sdata, cache.author_sorts(sdata))
for bid in (1, 2, 3):
self.assertIn(', changed', cache.field_for('author_sort', bid))
sdata = {aid:f'{aid*2 if aid == max(adata) else aid}, changed' for aid in adata}

View File

@ -805,15 +805,15 @@ def get_grouped_iterator(db: Cache, book_ids_iter: Iterable[int], field_name: st
return
case 'authors':
field_id_map = db.get_id_map('authors')
author_sort_map = db.author_data(field_id_map)
author_sort_map = db.author_sorts()
def gas(aid: int, au: str) -> str:
try:
return author_sort_map[aid]['sort']
except Exception:
return author_sort_map[aid]
except KeyError:
return au
sort_field_map = {au: gas(aid, au) for aid, au in field_id_map.items()}
sort_key = lambda x: sort_field_map[x] # noqa: E731
sort_key = {au: gas(aid, au) for aid, au in field_id_map.items()}.__getitem__
get_field_id_map = lambda: field_id_map # noqa: E731
del gas
case 'languages':
lm = lang_map()
formatter = lambda x: lm.get(x, x) # noqa: E731