From 4523c2d28ab46e367f401647a2362aae07e0f8d7 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sat, 6 Jan 2018 12:15:38 -0800 Subject: [PATCH] Ensure extended key/value pairs attached to tracks are properly deduped. --- src/core/library/track/IndexerTrack.cpp | 31 +++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/core/library/track/IndexerTrack.cpp b/src/core/library/track/IndexerTrack.cpp index f64bda744..9d143c0b1 100644 --- a/src/core/library/track/IndexerTrack.cpp +++ b/src/core/library/track/IndexerTrack.cpp @@ -426,6 +426,8 @@ void IndexerTrack::ProcessNonStandardMetadata(db::Connection& connection) { MetadataMap unknownFields(this->internalMetadata->metadata); removeKnownFields(unknownFields); + std::map> processed; + db::Statement selectMetaKey("SELECT id FROM meta_keys WHERE name=?", connection); db::Statement selectMetaValue("SELECT id FROM meta_values WHERE meta_key_id=? AND content=?", connection); db::Statement insertMetaValue("INSERT INTO meta_values (meta_key_id,content) VALUES (?,?)", connection); @@ -503,10 +505,31 @@ void IndexerTrack::ProcessNonStandardMetadata(db::Connection& connection) { /* now that we have a keyId and a valueId, create the relationship */ if (valueId != 0 && keyId != 0) { - insertTrackMeta.Reset(); - insertTrackMeta.BindInt64(0, this->id); - insertTrackMeta.BindInt64(1, valueId); - insertTrackMeta.Step(); + + /* we allow multiple values for the same key (for example, multiple composers + for a track. but we don't allow duplicates. keep track of what keys and + values we've already attached to this track, and dont add dupes. */ + + bool process = true; + if (processed.find(valueId) != processed.end()) { + auto keys = processed[valueId]; + if (keys.find(keyId) != keys.end()) { + process = false; + } + else { + keys.insert(keyId); + } + } + else { + processed[valueId] = { keyId }; + } + + if (process) { + insertTrackMeta.Reset(); + insertTrackMeta.BindInt64(0, this->id); + insertTrackMeta.BindInt64(1, valueId); + insertTrackMeta.Step(); + } } } }