From 1626a6fa11873af17dfdddcfc25f528fb0828958 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 1 May 2018 20:32:45 -0700 Subject: [PATCH] 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. --- etiquette/photodb.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 2f9b3ff..05c2d23 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -70,9 +70,10 @@ class PDBAlbumMixin: return self.get_things_by_id('album', ids) def get_root_albums(self): - for album in self.get_albums(): - if album.get_parent() is None: - yield album + ''' + Yield Albums that have no parent. + ''' + yield from self.get_root_things('album') @decorators.required_feature('album.new') @decorators.transaction @@ -856,11 +857,9 @@ class PDBTagMixin: def get_root_tags(self): ''' - Yield all Tags that have no parent. + Yield Tags that have no parent. ''' - for tag in self.get_tags(): - if tag.get_parent() is None: - yield tag + yield from self.get_root_things('tag') @decorators.required_feature('tag.new') @decorators.transaction @@ -1417,6 +1416,26 @@ class PhotoDB( self._cached_qualname_map = tag_export.qualified_names(self.get_tags()) 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): thing_map = _THING_CLASSES[thing_type]