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.
This commit is contained in:
voussoir 2018-03-10 22:50:41 -08:00
parent 8fcaf15fbe
commit 94e518068f
2 changed files with 25 additions and 9 deletions

View file

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

View file

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