Add caching of tag synonyms.

This commit is contained in:
voussoir 2018-05-06 21:04:19 -07:00
parent c6b71f1a0d
commit c736c33165

View file

@ -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()