Optimize PDB.get_root_albums and get_root_tags.

Achieves in a single query what used to require a ton of
individual get_parent calls.
This commit is contained in:
voussoir 2018-05-01 20:32:45 -07:00
parent 8c356df6fd
commit 1626a6fa11

View file

@ -70,9 +70,10 @@ class PDBAlbumMixin:
return self.get_things_by_id('album', ids) return self.get_things_by_id('album', ids)
def get_root_albums(self): def get_root_albums(self):
for album in self.get_albums(): '''
if album.get_parent() is None: Yield Albums that have no parent.
yield album '''
yield from self.get_root_things('album')
@decorators.required_feature('album.new') @decorators.required_feature('album.new')
@decorators.transaction @decorators.transaction
@ -856,11 +857,9 @@ class PDBTagMixin:
def get_root_tags(self): def get_root_tags(self):
''' '''
Yield all Tags that have no parent. Yield Tags that have no parent.
''' '''
for tag in self.get_tags(): yield from self.get_root_things('tag')
if tag.get_parent() is None:
yield tag
@decorators.required_feature('tag.new') @decorators.required_feature('tag.new')
@decorators.transaction @decorators.transaction
@ -1417,6 +1416,26 @@ class PhotoDB(
self._cached_qualname_map = tag_export.qualified_names(self.get_tags()) self._cached_qualname_map = tag_export.qualified_names(self.get_tags())
return self._cached_qualname_map return self._cached_qualname_map
def get_root_things(self, thing_type):
thing_map = _THING_CLASSES[thing_type]
thing_class = thing_map['class']
table = thing_map['table']
group_table = thing_class.group_table
query = '''
SELECT * FROM {table}
WHERE NOT EXISTS (
SELECT 1 FROM {group_table}
WHERE memberid == {table}.id
)
'''.format(table=table, group_table=group_table)
rows = self.sql_select(query)
for row in rows:
thing = thing_class(self, row)
yield thing
def get_thing_by_id(self, thing_type, thing_id): def get_thing_by_id(self, thing_type, thing_id):
thing_map = _THING_CLASSES[thing_type] thing_map = _THING_CLASSES[thing_type]