From 94e518068fb1469cd46c0525da32276970d103ba Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 10 Mar 2018 22:50:41 -0800 Subject: [PATCH] Add Album.add_photos for batch adding with less savepoint waste. Because each call to add_photo opens a savepoint, it is wasteful to use it for a large number of photos that belong together. --- etiquette/objects.py | 31 ++++++++++++++++++++++++------- etiquette/photodb.py | 3 +-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/etiquette/objects.py b/etiquette/objects.py index aebd119..3740d2d 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -282,6 +282,15 @@ class Album(ObjectBase, GroupableMixin): self.photodb.log.debug('Committing - add associated directory') self.photodb.commit() + def _add_photo(self, photo): + self.photodb.log.debug('Adding photo %s to %s', photo, self) + data = { + 'albumid': self.id, + 'photoid': photo.id, + } + self.photodb.sql_insert(table='album_photo_rel', data=data) + self._uncache_sums() + @decorators.required_feature('album.edit') @decorators.transaction def add_photo(self, photo, *, commit=True): @@ -290,18 +299,26 @@ class Album(ObjectBase, GroupableMixin): if self.has_photo(photo): return - self.photodb.log.debug('Adding photo %s to %s', photo, self) - data = { - 'albumid': self.id, - 'photoid': photo.id, - } - self.photodb.sql_insert(table='album_photo_rel', data=data) + self._add_photo(photo) - self._uncache_sums() if commit: self.photodb.log.debug('Committing - add photo to album') self.photodb.commit() + @decorators.required_feature('album.edit') + @decorators.transaction + def add_photos(self, photos, *, commit=True): + existing_photos = set(self.get_photos()) + photos = set(photos) + photos = photos.difference(existing_photos) + + for photo in photos: + self._add_photo(photo) + + if commit: + self.photodb.log.debug('Committing - add photos to album') + self.photodb.commit() + # Photo.add_tag already has @required_feature @decorators.transaction def add_tag_to_all(self, tag, *, nested_children=True, commit=True): diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 88e4b98..b70a9ef 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -1294,8 +1294,7 @@ class PhotoDB( current_album = create_or_fetch_current_album(albums_by_path, current_directory) orphan_join_parent_album(albums_by_path, current_album, current_directory) - for photo in photos: - current_album.add_photo(photo, commit=False) + current_album.add_photos(photos, commit=False) if commit: self.log.debug('Committing - digest_directory')