Indexer: Improve merging and performance

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-12-27 07:43:39 +01:00
parent fd53d30caa
commit 4ac0ef3556
3 changed files with 28 additions and 22 deletions

View file

@ -1,10 +1,14 @@
package entity
import (
"sync"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/pkg/rnd"
)
var photoMergeMutex = sync.Mutex{}
// ResolvePrimary ensures there is only one primary file for a photo.
func (m *Photo) ResolvePrimary() error {
var file File
@ -29,7 +33,7 @@ func (m *Photo) Identical(includeMeta, includeUuid bool) (identical Photos, err
"OR (uuid = ? AND photo_stack > -1)"+
"OR (photo_path = ? AND photo_name = ?)",
m.TakenAt, m.CellID, m.CameraSerial, m.CameraID, m.UUID, m.PhotoPath, m.PhotoName).
Order("id ASC").Find(&identical).Error; err != nil {
Order("photo_quality DESC, id ASC").Find(&identical).Error; err != nil {
return identical, err
}
case includeMeta && m.HasLocation() && m.HasLatLng() && m.TakenSrc == SrcMeta:
@ -37,20 +41,20 @@ func (m *Photo) Identical(includeMeta, includeUuid bool) (identical Photos, err
Where("(taken_at = ? AND taken_src = 'meta' AND photo_stack > -1 AND cell_id = ? AND camera_serial = ? AND camera_id = ?) "+
"OR (photo_path = ? AND photo_name = ?)",
m.TakenAt, m.CellID, m.CameraSerial, m.CameraID, m.PhotoPath, m.PhotoName).
Order("id ASC").Find(&identical).Error; err != nil {
Order("photo_quality DESC, id ASC").Find(&identical).Error; err != nil {
return identical, err
}
case includeUuid && rnd.IsUUID(m.UUID):
if err := Db().
Where("(uuid = ? AND photo_stack > -1) OR (photo_path = ? AND photo_name = ?)",
m.UUID, m.PhotoPath, m.PhotoName).
Order("id ASC").Find(&identical).Error; err != nil {
Order("photo_quality DESC, id ASC").Find(&identical).Error; err != nil {
return identical, err
}
default:
if err := Db().
Where("photo_path = ? AND photo_name = ?", m.PhotoPath, m.PhotoName).
Order("id ASC").Find(&identical).Error; err != nil {
Order("photo_quality DESC, id ASC").Find(&identical).Error; err != nil {
return identical, err
}
}
@ -60,6 +64,9 @@ func (m *Photo) Identical(includeMeta, includeUuid bool) (identical Photos, err
// Merge photo with identical ones.
func (m *Photo) Merge(mergeMeta, mergeUuid bool) (original Photo, merged Photos, err error) {
photoMergeMutex.Lock()
defer photoMergeMutex.Unlock()
identical, err := m.Identical(mergeMeta, mergeUuid)
if len(identical) < 2 || err != nil {
@ -110,11 +117,5 @@ func (m *Photo) Merge(mergeMeta, mergeUuid bool) (original Photo, merged Photos,
m.PhotoQuality = -1
}
original.PhotoQuality = original.QualityScore()
if err := original.Save(); err != nil {
log.Errorf("photo: %s in %s (merge)", err, original.PhotoName)
}
return original, merged, err
}

View file

@ -67,3 +67,14 @@ func (m *Photo) QualityScore() (score int) {
return score
}
// UpdateQuality updates the photo quality attribute.
func (m *Photo) UpdateQuality() error {
if m.DeletedAt != nil || m.PhotoQuality < 0 {
return nil
}
m.PhotoQuality = m.QualityScore()
return m.Update("PhotoQuality", m.PhotoQuality)
}

View file

@ -749,7 +749,7 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName string) (
photo.PhotoQuality = photo.QualityScore()
if err := photo.Save(); err != nil {
log.Errorf("index: %s in %s (update photo metadata)", err, logName)
log.Errorf("index: %s in %s (update metadata)", err, logName)
result.Status = IndexFailed
result.Err = err
return result
@ -762,17 +762,11 @@ func (ind *Index) MediaFile(m *MediaFile, o IndexOptions, originalName string) (
if err := photo.IndexKeywords(); err != nil {
log.Errorf("index: %s in %s (save keywords)", err, logName)
}
} else if photo.DeletedAt == nil {
if photo.PhotoQuality >= 0 {
photo.PhotoQuality = photo.QualityScore()
}
if err := photo.Save(); err != nil {
log.Errorf("index: %s in %s (update photo quality)", err, logName)
result.Status = IndexFailed
result.Err = err
return result
}
} else if err := photo.UpdateQuality(); err != nil {
log.Errorf("index: %s in %s (update quality)", err, logName)
result.Status = IndexFailed
result.Err = err
return result
}
result.Status = IndexUpdated