From 1f386533cfbbab67e7c4c48c293535ef4e954599 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Thu, 17 Dec 2020 10:23:17 +0100 Subject: [PATCH] Indexer: Use lookup table to prioritize metadata sources --- internal/entity/const.go | 13 ----------- internal/entity/photo.go | 6 +++--- internal/entity/photo_fixtures.go | 4 ++-- internal/entity/photo_test.go | 36 +++++++++++++++---------------- internal/entity/src.go | 33 ++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 36 deletions(-) create mode 100644 internal/entity/src.go diff --git a/internal/entity/const.go b/internal/entity/const.go index 187fbb977..4461f68e7 100644 --- a/internal/entity/const.go +++ b/internal/entity/const.go @@ -1,19 +1,6 @@ package entity -import "github.com/photoprism/photoprism/internal/classify" - const ( - // Data sources. - SrcAuto = "" - SrcManual = "manual" - SrcEstimate = "estimate" - SrcName = "name" - SrcMeta = "meta" - SrcXmp = "xmp" - SrcYaml = "yaml" - SrcLocation = classify.SrcLocation - SrcImage = classify.SrcImage - // Sort orders. SortOrderAdded = "added" SortOrderNewest = "newest" diff --git a/internal/entity/photo.go b/internal/entity/photo.go index 199493b63..7257204c6 100644 --- a/internal/entity/photo.go +++ b/internal/entity/photo.go @@ -812,7 +812,7 @@ func (m *Photo) SetTitle(title, source string) { return } - if m.TitleSrc != SrcAuto && m.TitleSrc != source && source != SrcManual && m.HasTitle() { + if (SrcPriority[source] < SrcPriority[m.TitleSrc]) && m.HasTitle() { return } @@ -828,7 +828,7 @@ func (m *Photo) SetDescription(desc, source string) { return } - if m.DescriptionSrc != SrcAuto && m.DescriptionSrc != source && source != SrcManual && m.PhotoDescription != "" { + if (SrcPriority[source] < SrcPriority[m.DescriptionSrc]) && m.HasDescription() { return } @@ -842,7 +842,7 @@ func (m *Photo) SetTakenAt(taken, local time.Time, zone, source string) { return } - if m.TakenSrc != SrcAuto && m.TakenSrc != source && source != SrcManual { + if SrcPriority[source] < SrcPriority[m.TakenSrc] && !m.TakenAt.IsZero() { return } diff --git a/internal/entity/photo_fixtures.go b/internal/entity/photo_fixtures.go index 69e7f9ec4..5678b51d6 100644 --- a/internal/entity/photo_fixtures.go +++ b/internal/entity/photo_fixtures.go @@ -768,10 +768,11 @@ var PhotoFixtures = PhotoMap{ PhotoUID: "pt9jtdre2lvl0y22", TakenAt: time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), TakenAtLocal: time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), - TakenSrc: "location", + TakenSrc: "name", PhotoTitle: "TitleToBeSet", TitleSrc: "location", PhotoDescription: "photo description blacklist", + DescriptionSrc: "meta", PhotoPath: "1990", PhotoName: "Photo15", PhotoQuality: 0, @@ -798,7 +799,6 @@ var PhotoFixtures = PhotoMap{ PhotoYear: 0, PhotoMonth: 0, Details: DetailsFixtures.Pointer("blacklist", 1000015), - DescriptionSrc: "location", Camera: CameraFixtures.Pointer("canon-eos-6d"), CameraID: CameraFixtures.Pointer("canon-eos-6d").ID, Lens: LensFixtures.Pointer("lens-f-380"), diff --git a/internal/entity/photo_test.go b/internal/entity/photo_test.go index 00f9d9477..48e6042b5 100644 --- a/internal/entity/photo_test.go +++ b/internal/entity/photo_test.go @@ -441,7 +441,7 @@ func TestPhoto_UpdateTitle(t *testing.T) { }) t.Run("no location", func(t *testing.T) { m := PhotoFixtures.Get("19800101_000002_D640C559") - classifyLabels := &classify.Labels{{Name: "classify", Uncertainty: 30, Source: "manual", Priority: 5, Categories: []string{"flower", "plant"}}} + classifyLabels := &classify.Labels{{Name: "classify", Uncertainty: 30, Source: SrcManual, Priority: 5, Categories: []string{"flower", "plant"}}} assert.Equal(t, "", m.PhotoTitle) err := m.UpdateTitle(*classifyLabels) if err != nil { @@ -474,21 +474,21 @@ func TestPhoto_UpdateTitle(t *testing.T) { func TestPhoto_AddLabels(t *testing.T) { t.Run("add label", func(t *testing.T) { m := PhotoFixtures.Get("19800101_000002_D640C559") - classifyLabels := classify.Labels{{Name: "cactus", Uncertainty: 30, Source: "manual", Priority: 5, Categories: []string{"plant"}}} + classifyLabels := classify.Labels{{Name: "cactus", Uncertainty: 30, Source: SrcManual, Priority: 5, Categories: []string{"plant"}}} len1 := len(m.Labels) m.AddLabels(classifyLabels) assert.Greater(t, len(m.Labels), len1) }) t.Run("update label", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") - classifyLabels := classify.Labels{{Name: "landscape", Uncertainty: 10, Source: "manual", Priority: 5, Categories: []string{"plant"}}} + classifyLabels := classify.Labels{{Name: "landscape", Uncertainty: 10, Source: SrcManual, Priority: 5, Categories: []string{"plant"}}} assert.Equal(t, 20, m.Labels[0].Uncertainty) - assert.Equal(t, "image", m.Labels[0].LabelSrc) + assert.Equal(t, SrcImage, m.Labels[0].LabelSrc) len1 := len(m.Labels) m.AddLabels(classifyLabels) assert.Equal(t, len(m.Labels), len1) assert.Equal(t, 10, m.Labels[0].Uncertainty) - assert.Equal(t, "manual", m.Labels[0].LabelSrc) + assert.Equal(t, SrcManual, m.Labels[0].LabelSrc) }) } @@ -496,19 +496,19 @@ func TestPhoto_SetTitle(t *testing.T) { t.Run("empty title", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, "TitleToBeSet", m.PhotoTitle) - m.SetTitle("", "manual") + m.SetTitle("", SrcManual) assert.Equal(t, "TitleToBeSet", m.PhotoTitle) }) t.Run("title not from the same source", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, "TitleToBeSet", m.PhotoTitle) - m.SetTitle("NewTitleSet", "image") + m.SetTitle("NewTitleSet", SrcAuto) assert.Equal(t, "TitleToBeSet", m.PhotoTitle) }) t.Run("success", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, "TitleToBeSet", m.PhotoTitle) - m.SetTitle("NewTitleSet", "location") + m.SetTitle("NewTitleSet", SrcLocation) assert.Equal(t, "NewTitleSet", m.PhotoTitle) }) } @@ -517,19 +517,19 @@ func TestPhoto_SetDescription(t *testing.T) { t.Run("empty description", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, "photo description blacklist", m.PhotoDescription) - m.SetDescription("", "manual") + m.SetDescription("", SrcManual) assert.Equal(t, "photo description blacklist", m.PhotoDescription) }) t.Run("description not from the same source", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, "photo description blacklist", m.PhotoDescription) - m.SetDescription("new photo description", "image") + m.SetDescription("new photo description", SrcName) assert.Equal(t, "photo description blacklist", m.PhotoDescription) }) t.Run("success", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, "photo description blacklist", m.PhotoDescription) - m.SetDescription("new photo description", "manual") + m.SetDescription("new photo description", SrcManual) assert.Equal(t, "new photo description", m.PhotoDescription) }) } @@ -538,14 +538,14 @@ func TestPhoto_SetTakenAt(t *testing.T) { t.Run("empty taken", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) - m.SetTakenAt(time.Time{}, time.Time{}, "", "manual") + m.SetTakenAt(time.Time{}, time.Time{}, "", SrcManual) assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) }) t.Run("taken not from the same source", func(t *testing.T) { m := PhotoFixtures.Get("Photo15") assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) m.SetTakenAt(time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), - time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), "", "image") + time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), "", SrcAuto) assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) }) t.Run("success", func(t *testing.T) { @@ -553,7 +553,7 @@ func TestPhoto_SetTakenAt(t *testing.T) { assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal) m.SetTakenAt(time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), - time.Date(2019, 12, 11, 10, 7, 18, 0, time.UTC), "", "location") + time.Date(2019, 12, 11, 10, 7, 18, 0, time.UTC), "", SrcMeta) assert.Equal(t, time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) assert.Equal(t, time.Date(2019, 12, 11, 10, 7, 18, 0, time.UTC), m.TakenAtLocal) }) @@ -562,7 +562,7 @@ func TestPhoto_SetTakenAt(t *testing.T) { assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal) m.SetTakenAt(time.Date(2123, 12, 11, 9, 7, 18, 0, time.UTC), - time.Date(2123, 12, 11, 10, 7, 18, 0, time.UTC), "", "location") + time.Date(2123, 12, 11, 10, 7, 18, 0, time.UTC), "", SrcManual) assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal) }) @@ -571,7 +571,7 @@ func TestPhoto_SetTakenAt(t *testing.T) { assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) assert.Equal(t, time.Date(2013, 11, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal) m.SetTakenAt(time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), - time.Time{}, "test", "location") + time.Time{}, "test", SrcXmp) assert.Equal(t, time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), m.TakenAt) assert.Equal(t, time.Date(2019, 12, 11, 9, 7, 18, 0, time.UTC), m.TakenAtLocal) }) @@ -583,7 +583,7 @@ func TestPhoto_SetCoordinates(t *testing.T) { assert.Equal(t, float32(1.234), m.PhotoLat) assert.Equal(t, float32(4.321), m.PhotoLng) assert.Equal(t, 3, m.PhotoAltitude) - m.SetCoordinates(0, 0, 5, "manual") + m.SetCoordinates(0, 0, 5, SrcManual) assert.Equal(t, float32(1.234), m.PhotoLat) assert.Equal(t, float32(4.321), m.PhotoLng) assert.Equal(t, 3, m.PhotoAltitude) @@ -593,7 +593,7 @@ func TestPhoto_SetCoordinates(t *testing.T) { assert.Equal(t, float32(1.234), m.PhotoLat) assert.Equal(t, float32(4.321), m.PhotoLng) assert.Equal(t, 3, m.PhotoAltitude) - m.SetCoordinates(5.555, 5.555, 5, "image") + m.SetCoordinates(5.555, 5.555, 5, SrcImage) assert.Equal(t, float32(1.234), m.PhotoLat) assert.Equal(t, float32(4.321), m.PhotoLng) assert.Equal(t, 3, m.PhotoAltitude) diff --git a/internal/entity/src.go b/internal/entity/src.go new file mode 100644 index 000000000..ec1d628cd --- /dev/null +++ b/internal/entity/src.go @@ -0,0 +1,33 @@ +package entity + +import "github.com/photoprism/photoprism/internal/classify" + +type Priorities map[string]int + +// Data source names. +const ( + SrcAuto = "" + SrcManual = "manual" + SrcEstimate = "estimate" + SrcName = "name" + SrcMeta = "meta" + SrcXmp = "xmp" + SrcYaml = "yaml" + SrcImage = classify.SrcImage + SrcKeyword = classify.SrcKeyword + SrcLocation = classify.SrcLocation +) + +// Data source priorities. +var SrcPriority = Priorities{ + SrcAuto: 1, + SrcEstimate: 2, + SrcName: 4, + SrcYaml: 8, + SrcLocation: 8, + SrcImage: 8, + SrcKeyword: 16, + SrcMeta: 16, + SrcXmp: 32, + SrcManual: 64, +}