From c736c33165bc0e460edf3dca48b71b622e620d80 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 6 May 2018 21:04:19 -0700 Subject: [PATCH] Add caching of tag synonyms. --- etiquette/objects.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/etiquette/objects.py b/etiquette/objects.py index 6941b48..36a4f50 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -1157,6 +1157,7 @@ class Tag(ObjectBase, GroupableMixin): self.group_getter = self.photodb.get_tag self.group_getter_many = self.photodb.get_tags_by_id + self._cached_synonyms = None def __eq__(self, other): return self.name == other or ObjectBase.__eq__(self, other) @@ -1232,6 +1233,9 @@ class Tag(ObjectBase, GroupableMixin): } self.photodb.sql_insert(table='tag_synonyms', data=data) + if self._cached_synonyms is not None: + self._cached_synonyms.add(synname) + if commit: self.photodb.log.debug('Committing - add synonym') self.photodb.commit() @@ -1255,10 +1259,13 @@ class Tag(ObjectBase, GroupableMixin): # Migrate the old tag's synonyms to the new one # UPDATE is safe for this operation because there is no chance of duplicates. + my_synonyms = self.get_synonyms() data = { 'mastername': (self.name, mastertag.name), } self.photodb.sql_update(table='tag_synonyms', pairs=data, where_key='mastername') + if mastertag._cached_synonyms is not None: + mastertag._cached_synonyms.update(my_synonyms) # Because these were two separate tags, perhaps in separate trees, it # is possible for a photo to have both at the moment. @@ -1340,13 +1347,16 @@ class Tag(ObjectBase, GroupableMixin): self.photodb.commit() def get_synonyms(self): + if self._cached_synonyms is not None: + return self._cached_synonyms.copy() + syn_rows = self.photodb.sql_select( 'SELECT name FROM tag_synonyms WHERE mastername == ?', [self.name] ) - syn_names = [row[0] for row in syn_rows] - syn_names.sort() - return syn_names + synonyms = set(row[0] for row in syn_rows) + self._cached_synonyms = synonyms.copy() + return synonyms @decorators.required_feature('tag.edit') # GroupableMixin.join_group already has @transaction. @@ -1413,6 +1423,9 @@ class Tag(ObjectBase, GroupableMixin): self.photodb._cached_frozen_children = None self.photodb.sql_delete(table='tag_synonyms', pairs={'name': synname}) + if self._cached_synonyms is not None: + self._cached_synonyms.remove(synname) + if commit: self.photodb.log.debug('Committing - remove synonym') self.photodb.commit()