Added an option to merge artists without MBIDs to those with one, fixes #642

This commit is contained in:
emeric
2025-04-04 18:30:14 +02:00
parent 7b12dc0f70
commit faabb7ec4a
35 changed files with 1080 additions and 174 deletions
@@ -38,4 +38,7 @@ namespace lms::core
private:
T _value{};
};
template<typename Tag>
using TaggedBool = TaggedType<Tag, bool>;
} // namespace lms::core
+35 -24
View File
@@ -256,30 +256,6 @@ namespace lms::db
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Artist>>("SELECT a FROM artist a").where("a.id = ?").bind(id));
}
bool Artist::exists(Session& session, ArtistId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT 1 FROM artist").where("id = ?").bind(id)) == 1;
}
RangeResults<ArtistId> Artist::findOrphanIds(Session& session, std::optional<Range> range)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<ArtistId>(R"(SELECT DISTINCT a.id FROM artist a
WHERE NOT EXISTS (
SELECT 1
FROM track t
INNER JOIN track_artist_link t_a_l
ON t_a_l.artist_id = a.id
WHERE t.id = t_a_l.track_id
)
AND NOT EXISTS (
SELECT 1
FROM artist_info ai
WHERE ai.artist_id = a.id))") };
return utils::execRangeQuery<ArtistId>(query, range);
}
RangeResults<ArtistId> Artist::findIds(Session& session, const FindParameters& params)
{
session.checkReadTransaction();
@@ -304,6 +280,41 @@ AND NOT EXISTS (
utils::forEachQueryRangeResult(query, params.range, func);
}
RangeResults<ArtistId> Artist::findOrphanIds(Session& session, std::optional<Range> range)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<ArtistId>(R"(SELECT DISTINCT a.id FROM artist a
WHERE NOT EXISTS (
SELECT 1
FROM track t
INNER JOIN track_artist_link t_a_l
ON t_a_l.artist_id = a.id
WHERE t.id = t_a_l.track_id
)
AND NOT EXISTS (
SELECT 1
FROM artist_info ai
WHERE ai.artist_id = a.id))") };
return utils::execRangeQuery<ArtistId>(query, range);
}
bool Artist::exists(Session& session, ArtistId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT 1 FROM artist").where("id = ?").bind(id)) == 1;
}
std::optional<core::UUID> Artist::getMBID() const
{
return core::UUID::fromString(_mbid);
}
bool Artist::hasMBID() const
{
// TODO optim this
return getMBID().has_value();
}
ObjectPtr<Image> Artist::getImage() const
{
return ObjectPtr<Image>{ _image };
+40 -1
View File
@@ -70,7 +70,7 @@ namespace lms::db
void ArtistInfo::find(Session& session, ArtistId id, const std::function<void(const pointer&)>& func)
{
find(session, id, std::nullopt, std::move(func));
find(session, id, std::nullopt, func);
}
void ArtistInfo::find(Session& session, ArtistInfoId& lastRetrievedId, std::size_t count, const std::function<void(const pointer&)>& func)
@@ -85,6 +85,45 @@ namespace lms::db
});
}
void ArtistInfo::findArtistNameNoLongerMatch(Session& session, std::optional<Range> range, const std::function<void(const pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<ArtistInfo>>("SELECT a_i FROM artist_info a_i") };
query.join("artist a ON a_i.artist_id = a.id");
query.where("a_i.mbid_matched = FALSE");
query.where("a_i.name <> a.name");
utils::applyRange(query, range);
utils::forEachQueryResult(query, [&](const pointer& info) {
func(info);
});
}
void ArtistInfo::findWithArtistNameAmbiguity(Session& session, std::optional<Range> range, bool allowArtistMBIDFallback, const std::function<void(const pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<ArtistInfo>>("SELECT a_i FROM artist_info a_i") };
query.join("artist a ON a_i.artist_id = a.id");
query.where("a_i.mbid_matched = FALSE");
if (!allowArtistMBIDFallback)
{
query.where("a.mbid <> ''");
}
else
{
query.where(R"(
(a.mbid <> '' AND EXISTS (SELECT 1 FROM artist a2 WHERE a2.name = a.name AND a2.mbid <> '' AND a2.mbid <> a.mbid))
OR (a.mbid = '' AND (SELECT COUNT(*) FROM artist a2 WHERE a2.name = a.name AND a2.mbid <> '') = 1))");
}
utils::applyRange(query, range);
utils::forEachQueryResult(query, [&](const pointer& info) {
func(info);
});
}
Artist::pointer ArtistInfo::getArtist() const
{
return _artist;
+21 -1
View File
@@ -35,7 +35,7 @@ namespace lms::db
{
namespace
{
static constexpr Version LMS_DATABASE_VERSION{ 85 };
static constexpr Version LMS_DATABASE_VERSION{ 86 };
}
VersionInfo::VersionInfo()
@@ -1159,6 +1159,25 @@ FROM tracklist)");
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET scan_version = scan_version + 1");
}
void migrateFromV85(Session& session)
{
dropIndexes(session);
// Artist merging feature
utils::executeCommand(*session.getDboSession(), "ALTER TABLE scan_settings ADD COLUMN allow_mbid_artist_merge BOLLEAN DEFAULT(false)");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE track_artist_link ADD COLUMN artist_name TEXT NULL DEFAULT('')");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE track_artist_link ADD COLUMN artist_sort_name TEXT NULL DEFAULT('')");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE track_artist_link ADD COLUMN artist_mbid_matched BOOLEAN NOT NULL DEFAULT(false)");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE artist_info ADD COLUMN name TEXT NULL DEFAULT('')");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE artist_info ADD COLUMN sort_name TEXT NULL DEFAULT('')");
utils::executeCommand(*session.getDboSession(), "ALTER TABLE artist_info ADD COLUMN mbid_matched BOOLEAN NOT NULL DEFAULT(false)");
// Just increment the scan version of the settings to make the next scan rescan everything
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET scan_version = scan_version + 1");
}
bool doDbMigration(Session& session)
{
constexpr std::string_view outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" };
@@ -1220,6 +1239,7 @@ FROM tracklist)");
{ 82, migrateFromV82 },
{ 83, migrateFromV83 },
{ 84, migrateFromV84 },
{ 85, migrateFromV85 },
};
bool migrationPerformed{};
+9
View File
@@ -103,6 +103,15 @@ namespace lms::db
}
}
void ScanSettings::setAllowMBIDArtistMerge(bool value)
{
if (_allowMBIDArtistMerge != value)
{
_allowMBIDArtistMerge = value;
incScanVersion();
}
}
void ScanSettings::incScanVersion()
{
_scanVersion += 1;
+4 -2
View File
@@ -195,13 +195,14 @@ namespace lms::db
auto transaction{ createWriteTransaction() };
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_id_idx ON artist(id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_image_idx ON artist(image_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_name_idx ON artist(name)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_name_mbid_idx ON artist(name, mbid)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_sort_name_nocase_idx ON artist(sort_name COLLATE NOCASE)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_mbid_idx ON artist(mbid)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_info_path_idx ON artist_info(absolute_file_path)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_info_directory_id_idx ON artist_info(directory_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_info_artist_id_idx ON artist_info(artist_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_info_mbid_matched_artist_idx ON artist_info(mbid_matched, artist_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS auth_token_user_domain_idx ON auth_token(user_id, domain)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS auth_token_domain_expiry_idx ON auth_token(domain, expiry)");
@@ -296,8 +297,9 @@ namespace lms::db
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS tracklist_entry_tracklist_track_idx ON tracklist_entry(tracklist_id, track_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_artist_idx ON track_artist_link(artist_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_artist_mbid_matched_artist_idx ON track_artist_link(artist_mbid_matched, artist_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_artist_track_idx ON track_artist_link(artist_id, track_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_artist_type_idx ON track_artist_link(artist_id,type)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_artist_type_idx ON track_artist_link(artist_id, type)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_track_artist_idx ON track_artist_link(track_id, artist_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_track_type_idx ON track_artist_link(track_id,type)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_artist_link_type_track_artist_idx ON track_artist_link(type, track_id, artist_id)");
+71 -4
View File
@@ -55,28 +55,41 @@ namespace lms::db
}
} // namespace
TrackArtistLink::TrackArtistLink(ObjectPtr<Track> track, ObjectPtr<Artist> artist, TrackArtistLinkType type, std::string_view subType)
TrackArtistLink::TrackArtistLink(const ObjectPtr<Track>& track, const ObjectPtr<Artist>& artist, TrackArtistLinkType type, std::string_view subType, bool artistMBIDMatched)
: _type{ type }
, _subType{ subType }
, _artistMBIDMatched{ artistMBIDMatched }
, _track{ getDboPtr(track) }
, _artist{ getDboPtr(artist) }
{
}
TrackArtistLink::pointer TrackArtistLink::create(Session& session, ObjectPtr<Track> track, ObjectPtr<Artist> artist, TrackArtistLinkType type, std::string_view subType)
TrackArtistLink::pointer TrackArtistLink::create(Session& session, const ObjectPtr<Track>& track, const ObjectPtr<Artist>& artist, TrackArtistLinkType type, std::string_view subType, bool artistMBIDMatched)
{
session.checkWriteTransaction();
TrackArtistLink::pointer res{ session.getDboSession()->add(std::make_unique<TrackArtistLink>(track, artist, type, subType)) };
TrackArtistLink::pointer res{ session.getDboSession()->add(std::make_unique<TrackArtistLink>(track, artist, type, subType, artistMBIDMatched)) };
session.getDboSession()->flush();
return res;
}
std::size_t TrackArtistLink::getCount(Session& session)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM track_artist_link"));
}
TrackArtistLink::pointer TrackArtistLink::create(Session& session, const ObjectPtr<Track>& track, const ObjectPtr<Artist>& artist, TrackArtistLinkType type, bool artistMBIDMatched)
{
return create(session, track, artist, type, std::string_view{}, artistMBIDMatched);
}
TrackArtistLink::pointer TrackArtistLink::find(Session& session, TrackArtistLinkId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<TrackArtistLink>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<TrackArtistLink>>("SELECT t_a_l from track_artist_link t_a_l").where("t_a_l.id = ?").bind(id));
}
void TrackArtistLink::find(Session& session, TrackId trackId, const std::function<void(const TrackArtistLink::pointer& link, const ObjectPtr<Artist>& artist)>& func)
@@ -113,4 +126,58 @@ namespace lms::db
});
return res;
}
void TrackArtistLink::findArtistNameNoLongerMatch(Session& session, std::optional<Range> range, const std::function<void(const TrackArtistLink::pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<TrackArtistLink>>("SELECT t_a_l FROM track_artist_link t_a_l") };
query.join("artist a ON t_a_l.artist_id = a.id");
query.where("t_a_l.artist_mbid_matched = FALSE");
query.where("t_a_l.artist_name <> a.name");
utils::applyRange(query, range);
utils::forEachQueryResult(query, [&](const TrackArtistLink::pointer& link) {
func(link);
});
}
void TrackArtistLink::findWithArtistNameAmbiguity(Session& session, std::optional<Range> range, bool allowArtistMBIDFallback, const std::function<void(const TrackArtistLink::pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<TrackArtistLink>>("SELECT t_a_l FROM track_artist_link t_a_l") };
query.join("artist a ON t_a_l.artist_id = a.id");
query.where("t_a_l.artist_mbid_matched = FALSE");
if (!allowArtistMBIDFallback)
{
query.where("a.mbid <> ''");
}
else
{
query.where(R"(
(a.mbid <> '' AND EXISTS (SELECT 1 FROM artist a2 WHERE a2.name = a.name AND a2.mbid <> '' AND a2.mbid <> a.mbid))
OR (a.mbid = '' AND (SELECT COUNT(*) FROM artist a2 WHERE a2.name = a.name AND a2.mbid <> '') = 1))");
}
utils::applyRange(query, range);
utils::forEachQueryResult(query, [&](const TrackArtistLink::pointer& link) {
func(link);
});
}
void TrackArtistLink::setArtist(ObjectPtr<Artist> artist)
{
_artist = getDboPtr(artist);
}
void TrackArtistLink::setArtistName(std::string_view artistName)
{
_artistName = artistName;
}
void TrackArtistLink::setArtistSortName(std::string_view artistSortName)
{
_artistSortName = artistSortName;
}
} // namespace lms::db
@@ -134,8 +134,10 @@ namespace lms::db
// Accessors
const std::string& getName() const { return _name; }
const std::string& getSortName() const { return _sortName; }
std::optional<core::UUID> getMBID() const { return core::UUID::fromString(_mbid); }
std::optional<core::UUID> getMBID() const;
bool hasMBID() const;
ObjectPtr<Image> getImage() const;
void visitLinks(std::function<void(const ObjectPtr<TrackArtistLink>& link)> visitor) const;
// No artistLinkTypes means get them all
RangeResults<ArtistId> findSimilarArtistIds(core::EnumSet<TrackArtistLinkType> artistLinkTypes = {}, std::optional<Range> range = std::nullopt) const;
@@ -51,6 +51,8 @@ namespace lms::db
static void find(Session& session, ArtistId id, const std::function<void(const pointer&)>& func);
static pointer find(Session& session, const std::filesystem::path& path);
static void find(Session& session, ArtistInfoId& lastRetrievedId, std::size_t count, const std::function<void(const pointer&)>& func);
static void findArtistNameNoLongerMatch(Session& session, std::optional<Range> range, const std::function<void(const pointer&)>& func);
static void findWithArtistNameAmbiguity(Session& session, std::optional<Range> range, bool allowArtistMBIDFallback, const std::function<void(const pointer&)>& func);
// getters
const std::filesystem::path& getAbsoluteFilePath() const { return _absoluteFilePath; }
@@ -58,20 +60,26 @@ namespace lms::db
ObjectPtr<Directory> getDirectory() const;
ObjectPtr<Artist> getArtist() const;
DirectoryId getDirectoryId() const { return _directory.id(); }
std::string_view getName() const { return _name; }
std::string_view getSortName() const { return _name; }
std::string_view getType() const { return _type; }
std::string_view getGender() const { return _gender; }
std::string_view getDisambiguation() const { return _disambiguation; }
std::string_view getBiography() const { return _biography; }
bool isMBIDMatched() const { return _MBIDMatched; }
// setters
void setAbsoluteFilePath(const std::filesystem::path& filePath);
void setLastWriteTime(Wt::WDateTime time) { _fileLastWrite = time; }
void setDirectory(ObjectPtr<Directory> directory);
void setArtist(ObjectPtr<Artist> artist);
void setName(std::string_view name) { _name = name; }
void setSortName(std::string_view sortName) { _sortName = sortName; }
void setType(std::string_view type) { _type = type; }
void setGender(std::string_view gender) { _gender = gender; }
void setDisambiguation(std::string_view disambiguation) { _disambiguation = disambiguation; }
void setBiography(std::string_view biography) { _biography = biography; };
void setMBIDMatched(bool matched) { _MBIDMatched = matched; }
template<class Action>
void persist(Action& a)
@@ -79,11 +87,15 @@ namespace lms::db
Wt::Dbo::field(a, _absoluteFilePath, "absolute_file_path");
Wt::Dbo::field(a, _fileLastWrite, "file_last_write");
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _sortName, "sort_name");
Wt::Dbo::field(a, _type, "type");
Wt::Dbo::field(a, _gender, "gender");
Wt::Dbo::field(a, _disambiguation, "disambiguation");
Wt::Dbo::field(a, _biography, "biography");
Wt::Dbo::field(a, _MBIDMatched, "mbid_matched");
Wt::Dbo::belongsTo(a, _directory, "directory", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _artist, "artist", Wt::Dbo::OnDeleteCascade);
}
@@ -97,11 +109,18 @@ namespace lms::db
std::string _fileStem;
Wt::WDateTime _fileLastWrite;
// this info may be redondant with what found in the linked artist
// but we actually need them in case of artist merge/split
std::string _name;
std::string _sortName;
std::string _type;
std::string _gender;
std::string _disambiguation;
std::string _biography;
bool _MBIDMatched{};
Wt::Dbo::ptr<Directory> _directory;
Wt::Dbo::ptr<Artist> _artist;
};
@@ -71,6 +71,7 @@ namespace lms::db
std::vector<std::string> getArtistTagDelimiters() const;
std::vector<std::string> getDefaultTagDelimiters() const;
bool getSkipSingleReleasePlayLists() const { return _skipSingleReleasePlayLists; }
bool getAllowMBIDArtistMerge() const { return _allowMBIDArtistMerge; }
// Setters
void setUpdateStartTime(Wt::WTime t) { _startTime = t; }
@@ -80,6 +81,7 @@ namespace lms::db
void setArtistTagDelimiters(std::span<const std::string_view> delimiters);
void setDefaultTagDelimiters(std::span<const std::string_view> delimiters);
void setSkipSingleReleasePlayLists(bool value);
void setAllowMBIDArtistMerge(bool value);
void incScanVersion();
template<class Action>
@@ -93,6 +95,7 @@ namespace lms::db
Wt::Dbo::field(a, _artistTagDelimiters, "artist_tag_delimiters");
Wt::Dbo::field(a, _defaultTagDelimiters, "default_tag_delimiters");
Wt::Dbo::field(a, _skipSingleReleasePlayLists, "skip_single_release_playlists");
Wt::Dbo::field(a, _allowMBIDArtistMerge, "allow_mbid_artist_merge");
}
private:
@@ -103,6 +106,7 @@ namespace lms::db
std::string _extraTagsToScan;
std::string _artistTagDelimiters;
std::string _defaultTagDelimiters;
bool _skipSingleReleasePlayLists{ false };
bool _skipSingleReleasePlayLists{};
bool _allowMBIDArtistMerge{};
};
} // namespace lms::db
@@ -80,32 +80,51 @@ namespace lms::db
};
TrackArtistLink() = default;
TrackArtistLink(ObjectPtr<Track> track, ObjectPtr<Artist> artist, TrackArtistLinkType type, std::string_view subType);
TrackArtistLink(const ObjectPtr<Track>& track, const ObjectPtr<Artist>& artist, TrackArtistLinkType type, std::string_view subType, bool artistMBIDMatched);
static void find(Session& session, TrackId trackId, const std::function<void(const TrackArtistLink::pointer&, const ObjectPtr<Artist>&)>&);
static void find(Session& session, const FindParameters& parameters, const std::function<void(const TrackArtistLink::pointer&)>&);
static void find(Session& session, TrackId trackId, const std::function<void(const pointer&, const ObjectPtr<Artist>&)>& func);
static void find(Session& session, const FindParameters& parameters, const std::function<void(const pointer&)>& func);
static pointer find(Session& session, TrackArtistLinkId linkId);
static pointer create(Session& session, ObjectPtr<Track> track, ObjectPtr<Artist> artist, TrackArtistLinkType type, std::string_view subType = {});
static std::size_t getCount(Session& session);
static pointer create(Session& session, const ObjectPtr<Track>& track, const ObjectPtr<Artist>& artist, TrackArtistLinkType type, std::string_view subType, bool artistMBIDMatched = false);
static pointer create(Session& session, const ObjectPtr<Track>& track, const ObjectPtr<Artist>& artist, TrackArtistLinkType type, bool artistMBIDMatched = false);
static core::EnumSet<TrackArtistLinkType> findUsedTypes(Session& session, ArtistId _artist);
static void findArtistNameNoLongerMatch(Session& session, std::optional<Range> range, const std::function<void(const pointer&)>& func);
static void findWithArtistNameAmbiguity(Session& session, std::optional<Range> range, bool allowArtistMBIDFallback, const std::function<void(const pointer&)>& func);
// accessors
ObjectPtr<Track> getTrack() const { return _track; }
ObjectPtr<Artist> getArtist() const { return _artist; }
TrackArtistLinkType getType() const { return _type; }
std::string_view getSubType() const { return _subType; }
std::string_view getArtistName() const { return _artistName; }
std::string_view getArtistSortName() const { return _artistSortName; }
bool isArtistMBIDMatched() const { return _artistMBIDMatched; }
// setters
void setArtist(ObjectPtr<Artist> artist);
void setArtistName(std::string_view artistName);
void setArtistSortName(std::string_view artistSortName);
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _type, "type");
Wt::Dbo::field(a, _subType, "subtype");
Wt::Dbo::field(a, _artistName, "artist_name");
Wt::Dbo::field(a, _artistSortName, "artist_sort_name");
Wt::Dbo::field(a, _artistMBIDMatched, "artist_mbid_matched");
Wt::Dbo::belongsTo(a, _track, "track", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _artist, "artist", Wt::Dbo::OnDeleteCascade);
}
private:
TrackArtistLinkType _type;
TrackArtistLinkType _type{ TrackArtistLinkType::Artist };
std::string _subType;
std::string _artistName; // as it was in the tags
std::string _artistSortName; // as it was in the tags
bool _artistMBIDMatched{};
Wt::Dbo::ptr<Track> _track;
Wt::Dbo::ptr<Artist> _artist;
+99
View File
@@ -109,4 +109,103 @@ namespace lms::db::tests
EXPECT_TRUE(visited);
}
}
TEST_F(DatabaseFixture, ArtistInfo_findArtistNameNoLongerMatch)
{
ScopedArtistInfo artistInfo{ session };
ScopedArtist artist{ session, "MyArtist" };
{
auto transaction{ session.createWriteTransaction() };
artistInfo.get().modify()->setArtist(artist.get());
artistInfo.get().modify()->setName("MyArtist");
artistInfo.get().modify()->setMBIDMatched(false);
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
ArtistInfo::findArtistNameNoLongerMatch(session, std::nullopt, [&](const ArtistInfo::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
{
auto transaction{ session.createWriteTransaction() };
artist.get().modify()->setName("MyArtist2");
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
ArtistInfo::findArtistNameNoLongerMatch(session, std::nullopt, [&](const ArtistInfo::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
}
TEST_F(DatabaseFixture, ArtistInfo_findWithArtistNameAmbiguity_split)
{
ScopedArtistInfo artistInfo1{ session };
ScopedArtist artist1{ session, "MyArtist", core::UUID::fromString("b227426f-98b8-4b39-b3a7-ff25e7711e9b") };
{
auto transaction{ session.createWriteTransaction() };
artistInfo1.get().modify()->setArtist(artist1.get());
artistInfo1.get().modify()->setName("MyArtist");
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
ArtistInfo::findWithArtistNameAmbiguity(session, std::nullopt, true /*allow fallback*/, [&](const ArtistInfo::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
ArtistInfo::findWithArtistNameAmbiguity(session, std::nullopt, false /*allow fallback*/, [&](const ArtistInfo::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
ScopedArtist artist2{ session, "MyArtist", core::UUID::fromString("97d1fb6f-db09-4760-b0b3-816559bcb632") };
ScopedArtistInfo artistInfo2{ session };
{
auto transaction{ session.createWriteTransaction() };
artistInfo2.get().modify()->setArtist(artist2.get());
artistInfo2.get().modify()->setName("MyArtist");
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
ArtistInfo::findWithArtistNameAmbiguity(session, std::nullopt, true /*allow fallback*/, [&](const ArtistInfo::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
ArtistInfo::findWithArtistNameAmbiguity(session, std::nullopt, false /*allow fallback*/, [&](const ArtistInfo::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
}
} // namespace lms::db::tests
+1
View File
@@ -19,6 +19,7 @@ add_executable(test-database
StarredRelease.cpp
StarredTrack.cpp
Track.cpp
TrackArtistLink.cpp
TrackBookmark.cpp
TrackEmbeddedImage.cpp
TrackFeatures.cpp
+2
View File
@@ -34,6 +34,7 @@
#include "database/StarredArtist.hpp"
#include "database/StarredRelease.hpp"
#include "database/StarredTrack.hpp"
#include "database/TrackArtistLink.hpp"
#include "database/TrackEmbeddedImage.hpp"
#include "database/TrackEmbeddedImageLink.hpp"
#include "database/TrackLyrics.hpp"
@@ -365,6 +366,7 @@ VALUES
EXPECT_FALSE(StarredRelease::find(session, StarredReleaseId{}));
EXPECT_FALSE(StarredTrack::find(session, StarredTrackId{}));
EXPECT_FALSE(Track::find(session, TrackId{}));
EXPECT_FALSE(TrackArtistLink::find(session, TrackArtistLinkId{}));
EXPECT_FALSE(TrackList::find(session, TrackListId{}));
EXPECT_FALSE(TrackLyrics::find(session, TrackLyricsId{}));
EXPECT_FALSE(UIState::find(session, UIStateId{}));
+186
View File
@@ -0,0 +1,186 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Common.hpp"
#include "database/Types.hpp"
#include "database/TrackArtistLink.hpp"
namespace lms::db::tests
{
TEST_F(DatabaseFixture, TrackArtistLink_findArtistNameNoLongerMatch)
{
ScopedTrack track{ session };
ScopedArtist artist{ session, "MyArtist" };
{
auto transaction{ session.createWriteTransaction() };
auto link{ session.create<TrackArtistLink>(track.get(), artist.get(), TrackArtistLinkType::Artist, false) };
link.modify()->setArtistName("MyArtist");
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
TrackArtistLink::findArtistNameNoLongerMatch(session, std::nullopt, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
{
auto transaction{ session.createWriteTransaction() };
artist.get().modify()->setName("MyArtist2");
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
TrackArtistLink::findArtistNameNoLongerMatch(session, std::nullopt, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
}
TEST_F(DatabaseFixture, TrackArtistLink_findWithArtistNameAmbiguity_split)
{
ScopedTrack track{ session };
ScopedArtist artist1{ session, "MyArtist", core::UUID::fromString("b227426f-98b8-4b39-b3a7-ff25e7711e9b") };
{
auto transaction{ session.createWriteTransaction() };
auto link{ session.create<TrackArtistLink>(track.get(), artist1.get(), TrackArtistLinkType::Artist, false) };
link.modify()->setArtistName("MyArtist");
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, true /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, false /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
ScopedArtist artist2{ session, "MyArtist", core::UUID::fromString("97d1fb6f-db09-4760-b0b3-816559bcb632") };
{
auto transaction{ session.createReadTransaction() };
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, true /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, false /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
}
TEST_F(DatabaseFixture, TrackArtistLink_findWithArtistNameAmbiguity_merge)
{
ScopedTrack track{ session };
ScopedArtist artist1{ session, "MyArtist" };
{
auto transaction{ session.createWriteTransaction() };
auto link{ session.create<TrackArtistLink>(track.get(), artist1.get(), TrackArtistLinkType::Artist, false) };
link.modify()->setArtistName("MyArtist");
}
{
auto transaction{ session.createReadTransaction() };
{
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, true /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = false;
});
ASSERT_FALSE(visited);
}
{
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, false /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
}
ScopedArtist artist2{ session, "MyArtist", core::UUID::fromString("97d1fb6f-db09-4760-b0b3-816559bcb632") };
{
auto transaction{ session.createReadTransaction() };
{
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, true /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_TRUE(visited);
}
{
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, false /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
}
ScopedArtist artist3{ session, "MyArtist", core::UUID::fromString("3d46c4fb-110d-4d4f-a2d5-5ca57ef1d582") };
{
auto transaction{ session.createReadTransaction() };
{
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, true /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
{
bool visited{};
TrackArtistLink::findWithArtistNameAmbiguity(session, std::nullopt, false /*allow fallback*/, [&](const TrackArtistLink::pointer&) {
visited = true;
});
ASSERT_FALSE(visited);
}
}
}
} // namespace lms::db::tests
+2
View File
@@ -1,4 +1,5 @@
add_library(lmsscanner STATIC
impl/helpers/ArtistHelpers.cpp
impl/scanners/ArtistInfoFileScanner.cpp
impl/scanners/AudioFileScanOperation.cpp
impl/scanners/AudioFileScanner.cpp
@@ -7,6 +8,7 @@ add_library(lmsscanner STATIC
impl/scanners/PlayListFileScanner.cpp
impl/scanners/Utils.cpp
impl/steps/FileScanQueue.cpp
impl/steps/ScanStepArtistReconciliation.cpp
impl/steps/ScanStepAssociateArtistImages.cpp
impl/steps/ScanStepAssociateExternalLyrics.cpp
impl/steps/ScanStepAssociatePlayListTracks.cpp
@@ -35,6 +35,7 @@
#include "scanners/LyricsFileScanner.hpp"
#include "scanners/PlayListFileScanner.hpp"
#include "steps/ScanStepArtistReconciliation.hpp"
#include "steps/ScanStepAssociateArtistImages.hpp"
#include "steps/ScanStepAssociateExternalLyrics.hpp"
#include "steps/ScanStepAssociatePlayListTracks.hpp"
@@ -348,7 +349,7 @@ namespace lms::scanner
} };
_fileScanners.clear();
_fileScanners.emplace_back(std::make_unique<ArtistInfoFileScanner>(_db));
_fileScanners.emplace_back(std::make_unique<ArtistInfoFileScanner>(_settings, _db));
_fileScanners.emplace_back(std::make_unique<AudioFileScanner>(_db, _settings));
_fileScanners.emplace_back(std::make_unique<ImageFileScanner>(_db));
_fileScanners.emplace_back(std::make_unique<LyricsFileScanner>(_db));
@@ -370,6 +371,7 @@ namespace lms::scanner
_scanSteps.emplace_back(std::make_unique<ScanStepDiscoverFiles>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepScanFiles>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepCheckForRemovedFiles>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepArtistReconciliation>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepAssociatePlayListTracks>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepUpdateLibraryFields>(params));
_scanSteps.emplace_back(std::make_unique<ScanStepAssociateArtistImages>(params));
@@ -386,7 +388,7 @@ namespace lms::scanner
{
ScannerSettings newSettings;
newSettings.skipDuplicateMBID = core::Service<core::IConfig>::get()->getBool("scanner-skip-duplicate-mbid", false);
newSettings.skipDuplicateTrackMBID = core::Service<core::IConfig>::get()->getBool("scanner-skip-duplicate-mbid", false);
{
auto transaction{ _db.getTLSSession().createReadTransaction() };
@@ -39,6 +39,9 @@ namespace lms::scanner
{
class IFileScanner;
// Main goals to keepthe scanner fast:
// - single pass on files: only 1 filesystem exploration must be done (no further reads triggered by parsed values)
// - stable: 1 single scan (full or not) is enough: successive scans must have no effect if there is no change in the files
class ScannerService : public IScannerService
{
public:
@@ -38,11 +38,12 @@ namespace lms::scanner
std::size_t scanVersion{};
Wt::WTime startTime;
db::ScanSettings::UpdatePeriod updatePeriod{ db::ScanSettings::UpdatePeriod::Never };
bool skipDuplicateMBID{};
bool skipDuplicateTrackMBID{};
std::vector<std::string> extraTags;
std::vector<std::string> artistTagDelimiters;
std::vector<std::string> defaultTagDelimiters;
bool skipSingleReleasePlayLists{};
bool allowArtistMBIDFallback{ true }; // TODO false?
std::vector<MediaLibraryInfo> mediaLibraries;
@@ -0,0 +1,151 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ArtistHelpers.hpp"
#include "core/ILogger.hpp"
#include "database/Session.hpp"
#include "metadata/Types.hpp"
namespace lms::scanner::helpers
{
namespace
{
db::Artist::pointer createArtist(db::Session& session, const metadata::Artist& artistInfo)
{
db::Artist::pointer artist{ session.create<db::Artist>(artistInfo.name) };
if (artistInfo.mbid)
artist.modify()->setMBID(artistInfo.mbid);
artist.modify()->setSortName(artistInfo.sortName ? *artistInfo.sortName : artistInfo.name);
return artist;
}
std::string optionalMBIDAsString(const std::optional<core::UUID>& uuid)
{
return uuid ? std::string{ uuid->getAsString() } : "<no MBID>";
}
void updateArtistIfNeeded(db::Artist::pointer artist, const metadata::Artist& artistInfo)
{
// MBID may be set
if (artist->getMBID() != artistInfo.mbid)
artist.modify()->setMBID(artistInfo.mbid);
// Name may have been updated
if (artist->getName() != artistInfo.name)
{
LMS_LOG(DBUPDATER, DEBUG, "Artist [" << optionalMBIDAsString(artist->getMBID()) << "], updated name from '" << artist->getName() << "' to '" << artistInfo.name << "'");
artist.modify()->setName(artistInfo.name);
}
// Sortname may have been updated
// As the sort name is quite often not filled in, we update it only if already set (for now?)
if (artistInfo.sortName && *artistInfo.sortName != artist->getSortName())
{
LMS_LOG(DBUPDATER, DEBUG, "Artist [" << optionalMBIDAsString(artist->getMBID()) << "], updated sort name from '" << artist->getSortName() << "' to '" << *artistInfo.sortName << "'");
artist.modify()->setSortName(*artistInfo.sortName);
}
}
} // namespace
db::Artist::pointer getOrCreateArtistByMBID(db::Session& session, const metadata::Artist& artistInfo, AllowFallbackOnMBIDEntry allowFallbackOnMBIDEntries)
{
assert(artistInfo.mbid.has_value());
db::Artist::pointer artist{ db::Artist::find(session, *artistInfo.mbid) };
if (artist)
{
updateArtistIfNeeded(artist, artistInfo);
}
else
{
if (allowFallbackOnMBIDEntries.value())
{
// an artist with the same name may already exist, let's recycle it
for (const db::Artist::pointer& artistWithSameName : db::Artist::find(session, artistInfo.name))
{
if (!artistWithSameName->hasMBID())
{
artist = artistWithSameName;
updateArtistIfNeeded(artist, artistInfo);
break;
}
}
}
if (!artist)
artist = createArtist(session, artistInfo);
}
return artist;
}
db::Artist::pointer getOrCreateArtistByName(db::Session& session, const metadata::Artist& artistInfo, AllowFallbackOnMBIDEntry allowFallbackOnMBIDEntries)
{
db::Artist::pointer artist;
// Here we can have only one artist with no MBID, others all have mbids
const std::vector<db::Artist::pointer> artistsWithSameName{ db::Artist::find(session, artistInfo.name) };
const auto itArtistWithoutMBID{ std::find_if(std::begin(artistsWithSameName), std::end(artistsWithSameName), [](const db::Artist::pointer& artist) { return !artist->hasMBID(); }) };
const std::size_t artistCountWithMBID{ artistsWithSameName.size() - (itArtistWithoutMBID != std::end(artistsWithSameName) ? 1 : 0) };
if (!allowFallbackOnMBIDEntries.value() || artistCountWithMBID > 1)
{
if (itArtistWithoutMBID != std::end(artistsWithSameName))
{
artist = *itArtistWithoutMBID;
updateArtistIfNeeded(artist, artistInfo);
}
else
artist = createArtist(session, artistInfo);
}
else
{
const auto itArtistWithMBID{ std::find_if(std::begin(artistsWithSameName), std::end(artistsWithSameName), [](const db::Artist::pointer& artist) { return artist->hasMBID(); }) };
if (itArtistWithMBID != std::end(artistsWithSameName))
{
artist = *itArtistWithMBID;
// not updating artist here: consider metadata quality is less good
}
else if (itArtistWithoutMBID != std::end(artistsWithSameName))
{
artist = *itArtistWithoutMBID;
updateArtistIfNeeded(artist, artistInfo);
}
else
artist = createArtist(session, artistInfo);
}
return artist;
}
db::Artist::pointer getOrCreateArtist(db::Session& session, const metadata::Artist& artistInfo, AllowFallbackOnMBIDEntry allowFallbackOnMBIDEntries)
{
// First try to get by MBID
if (artistInfo.mbid)
return getOrCreateArtistByMBID(session, artistInfo, allowFallbackOnMBIDEntries);
// Fall back on artist name (collisions may occur)
return getOrCreateArtistByName(session, artistInfo, allowFallbackOnMBIDEntries);
}
} // namespace lms::scanner::helpers
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "core/TaggedType.hpp"
#include "database/Artist.hpp"
namespace lms::metadata
{
struct Artist;
}
namespace lms::scanner::helpers
{
using AllowFallbackOnMBIDEntry = core::TaggedBool<struct AllowFallbackOnMBIDEntryTag>;
db::Artist::pointer getOrCreateArtistByMBID(db::Session& session, const metadata::Artist& artistInfo, AllowFallbackOnMBIDEntry allowFallbackOnMBIDEntries);
db::Artist::pointer getOrCreateArtistByName(db::Session& session, const metadata::Artist& artistInfo, AllowFallbackOnMBIDEntry allowFallbackOnMBIDEntries);
db::Artist::pointer getOrCreateArtist(db::Session& session, const metadata::Artist& artistInfo, AllowFallbackOnMBIDEntry allowFallbackOnMBIDEntries);
} // namespace lms::scanner::helpers
@@ -32,7 +32,10 @@
#include "IFileScanOperation.hpp"
#include "ScanContext.hpp"
#include "ScannerSettings.hpp"
#include "Utils.hpp"
#include "helpers/ArtistHelpers.hpp"
#include "metadata/Types.hpp"
namespace lms::scanner
{
@@ -41,10 +44,13 @@ namespace lms::scanner
class ArtistInfoFileScanOperation : public IFileScanOperation
{
public:
ArtistInfoFileScanOperation(const FileToScan& file, db::Db& db)
ArtistInfoFileScanOperation(const FileToScan& file, const ScannerSettings& settings, db::Db& db)
: _file{ file.file }
, _mediaLibrary{ file.mediaLibrary }
, _db{ db } {}
, _settings{ settings }
, _db{ db }
{
}
~ArtistInfoFileScanOperation() override = default;
ArtistInfoFileScanOperation(const ArtistInfoFileScanOperation&) = delete;
ArtistInfoFileScanOperation& operator=(const ArtistInfoFileScanOperation&) = delete;
@@ -59,6 +65,7 @@ namespace lms::scanner
const std::filesystem::path _file;
const MediaLibraryInfo _mediaLibrary;
const ScannerSettings& _settings;
db::Db& _db;
std::optional<metadata::ArtistInfo> _parsedArtistInfo;
@@ -76,12 +83,7 @@ namespace lms::scanner
}
_parsedArtistInfo = metadata::parseArtistInfo(ifs);
if (!_parsedArtistInfo->mbid.has_value())
{
LMS_LOG(DBUPDATER, DEBUG, "Discarding artist info in file " << _file << ": no mbid set");
_parsedArtistInfo.reset();
}
else if (_parsedArtistInfo->name.empty())
if (_parsedArtistInfo->name.empty())
{
LMS_LOG(DBUPDATER, DEBUG, "Discarding artist info in file " << _file << ": no name set");
_parsedArtistInfo.reset();
@@ -125,6 +127,8 @@ namespace lms::scanner
artistInfo.modify()->setAbsoluteFilePath(_file);
}
artistInfo.modify()->setName(_parsedArtistInfo->name);
artistInfo.modify()->setSortName(_parsedArtistInfo->sortName);
artistInfo.modify()->setLastWriteTime(fileInfo->lastWriteTime);
artistInfo.modify()->setType(_parsedArtistInfo->type);
artistInfo.modify()->setGender(_parsedArtistInfo->gender);
@@ -134,12 +138,8 @@ namespace lms::scanner
db::MediaLibrary::pointer mediaLibrary{ db::MediaLibrary::find(dbSession, _mediaLibrary.id) }; // may be null if settings are updated in // => next scan will correct this
artistInfo.modify()->setDirectory(utils::getOrCreateDirectory(dbSession, _file.parent_path(), mediaLibrary));
db::Artist::pointer artist{ db::Artist::find(dbSession, *_parsedArtistInfo->mbid) };
if (!artist)
artist = dbSession.create<db::Artist>(_parsedArtistInfo->name, _parsedArtistInfo->mbid);
artist.modify()->setName(_parsedArtistInfo->name);
artist.modify()->setSortName(_parsedArtistInfo->sortName);
const metadata::Artist artistMetadata{ _parsedArtistInfo->mbid, _parsedArtistInfo->name, _parsedArtistInfo->sortName.empty() ? std::nullopt : std::make_optional<std::string>(_parsedArtistInfo->sortName) };
db::Artist::pointer artist{ helpers::getOrCreateArtist(dbSession, artistMetadata, helpers::AllowFallbackOnMBIDEntry{ _settings.allowArtistMBIDFallback }) };
artistInfo.modify()->setArtist(artist);
if (added)
@@ -155,8 +155,9 @@ namespace lms::scanner
}
} // namespace
ArtistInfoFileScanner::ArtistInfoFileScanner(db::Db& db)
: _db{ db }
ArtistInfoFileScanner::ArtistInfoFileScanner(const ScannerSettings& settings, db::Db& db)
: _settings{ settings }
, _db{ db }
{
}
@@ -202,6 +203,6 @@ namespace lms::scanner
std::unique_ptr<IFileScanOperation> ArtistInfoFileScanner::createScanOperation(const FileToScan& fileToScan) const
{
return std::make_unique<ArtistInfoFileScanOperation>(fileToScan, _db);
return std::make_unique<ArtistInfoFileScanOperation>(fileToScan, _settings, _db);
}
} // namespace lms::scanner
@@ -31,10 +31,12 @@ namespace lms
namespace lms::scanner
{
struct ScannerSettings;
class ArtistInfoFileScanner : public IFileScanner
{
public:
ArtistInfoFileScanner(db::Db& db);
ArtistInfoFileScanner(const ScannerSettings& _settings, db::Db& db);
~ArtistInfoFileScanner() override = default;
ArtistInfoFileScanner(const ArtistInfoFileScanner&) = delete;
ArtistInfoFileScanner& operator=(const ArtistInfoFileScanner&) = delete;
@@ -45,6 +47,7 @@ namespace lms::scanner
bool needsScan(ScanContext& context, const FileToScan& file) const override;
std::unique_ptr<IFileScanOperation> createScanOperation(const FileToScan& fileToScan) const override;
const ScannerSettings& _settings;
db::Db& _db;
};
} // namespace lms::scanner
@@ -46,92 +46,30 @@
#include "ScanContext.hpp"
#include "ScannerSettings.hpp"
#include "Utils.hpp"
#include "helpers/ArtistHelpers.hpp"
namespace lms::scanner
{
namespace
{
db::Artist::pointer createArtist(db::Session& session, const metadata::Artist& artistInfo)
void createTrackArtistLinks(db::Session& session, const db::Track::pointer& track, db::TrackArtistLinkType linkType, std::string_view role, std::span<const metadata::Artist> artists, helpers::AllowFallbackOnMBIDEntry allowArtistMBIDFallback)
{
db::Artist::pointer artist{ session.create<db::Artist>(artistInfo.name) };
if (artistInfo.mbid)
artist.modify()->setMBID(artistInfo.mbid);
artist.modify()->setSortName(artistInfo.sortName ? *artistInfo.sortName : artistInfo.name);
return artist;
}
std::string optionalMBIDAsString(const std::optional<core::UUID>& uuid)
{
return uuid ? std::string{ uuid->getAsString() } : "<no MBID>";
}
void updateArtistIfNeeded(db::Artist::pointer artist, const metadata::Artist& artistInfo)
{
// Name may have been updated
if (artist->getName() != artistInfo.name)
for (const metadata::Artist& artistInfo : artists)
{
LMS_LOG(DBUPDATER, DEBUG, "Artist [" << optionalMBIDAsString(artist->getMBID()) << "], updated name from '" << artist->getName() << "' to '" << artistInfo.name << "'");
artist.modify()->setName(artistInfo.name);
}
db::Artist::pointer artist{ helpers::getOrCreateArtist(session, artistInfo, allowArtistMBIDFallback) };
// Sortname may have been updated
// As the sort name is quite often not filled in, we update it only if already set (for now?)
if (artistInfo.sortName && *artistInfo.sortName != artist->getSortName())
{
LMS_LOG(DBUPDATER, DEBUG, "Artist [" << optionalMBIDAsString(artist->getMBID()) << "], updated sort name from '" << artist->getSortName() << "' to '" << *artistInfo.sortName << "'");
artist.modify()->setSortName(*artistInfo.sortName);
const bool matchedUsingMbid{ artist->getMBID() == artistInfo.mbid };
db::TrackArtistLink::pointer link{ session.create<db::TrackArtistLink>(track, artist, linkType, role, matchedUsingMbid) };
link.modify()->setArtistName(artistInfo.name);
if (artistInfo.sortName)
link.modify()->setArtistSortName(*artistInfo.sortName);
}
}
std::vector<db::Artist::pointer> getOrCreateArtists(db::Session& session, const std::vector<metadata::Artist>& artistsInfo, bool allowFallbackOnMBIDEntries)
void createTrackArtistLinks(db::Session& session, const db::Track::pointer& track, db::TrackArtistLinkType linkType, std::span<const metadata::Artist> artists, helpers::AllowFallbackOnMBIDEntry allowArtistMBIDFallback)
{
std::vector<db::Artist::pointer> artists;
for (const metadata::Artist& artistInfo : artistsInfo)
{
db::Artist::pointer artist;
// First try to get by MBID
if (artistInfo.mbid)
{
artist = db::Artist::find(session, *artistInfo.mbid);
if (!artist)
artist = createArtist(session, artistInfo);
else
updateArtistIfNeeded(artist, artistInfo);
artists.emplace_back(std::move(artist));
continue;
}
// Fall back on artist name (collisions may occur)
if (!artistInfo.name.empty())
{
for (const db::Artist::pointer& sameNamedArtist : db::Artist::find(session, artistInfo.name))
{
// Do not fallback on artist that is correctly tagged
if (!allowFallbackOnMBIDEntries && sameNamedArtist->getMBID())
continue;
artist = sameNamedArtist;
break;
}
// No Artist found with the same name and without MBID -> creating
if (!artist)
artist = createArtist(session, artistInfo);
else
updateArtistIfNeeded(artist, artistInfo);
artists.emplace_back(std::move(artist));
continue;
}
}
return artists;
constexpr std::string_view noRole{};
createTrackArtistLinks(session, track, linkType, noRole, artists, allowArtistMBIDFallback);
}
db::ReleaseType::pointer getOrCreateReleaseType(db::Session& session, std::string_view name)
@@ -609,7 +547,7 @@ namespace lms::scanner
return;
}
if (_parsedTrack->mbid && (!track || _settings.skipDuplicateMBID))
if (_parsedTrack->mbid && (!track || _settings.skipDuplicateTrackMBID))
{
std::vector<db::Track::pointer> duplicateTracks{ db::Track::findByMBID(dbSession, *_parsedTrack->mbid) };
@@ -627,7 +565,7 @@ namespace lms::scanner
}
// Skip duplicate track MBID
if (_settings.skipDuplicateMBID)
if (_settings.skipDuplicateTrackMBID)
{
for (db::Track::pointer& otherTrack : duplicateTracks)
{
@@ -739,41 +677,20 @@ namespace lms::scanner
track.modify()->setDirectory(directory);
track.modify()->clearArtistLinks();
// Do not fallback on artists with the same name but having a MBID for artist and releaseArtists, as it may be corrected by properly tagging files
for (const db::Artist::pointer& artist : getOrCreateArtists(dbSession, _parsedTrack->artists, false))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, artist, db::TrackArtistLinkType::Artist));
const helpers::AllowFallbackOnMBIDEntry allowFallback{ _settings.allowArtistMBIDFallback };
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::Artist, _parsedTrack->artists, allowFallback);
if (_parsedTrack->medium && _parsedTrack->medium->release)
{
for (const db::Artist::pointer& releaseArtist : getOrCreateArtists(dbSession, _parsedTrack->medium->release->artists, false))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, releaseArtist, db::TrackArtistLinkType::ReleaseArtist));
}
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::ReleaseArtist, _parsedTrack->medium->release->artists, allowFallback);
// Allow fallbacks on artists with the same name even if they have MBID, since there is no tag to indicate the MBID of these artists
// We could ask MusicBrainz to get all the information, but that would heavily slow down the import process
for (const db::Artist::pointer& conductor : getOrCreateArtists(dbSession, _parsedTrack->conductorArtists, true))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, conductor, db::TrackArtistLinkType::Conductor));
for (const db::Artist::pointer& composer : getOrCreateArtists(dbSession, _parsedTrack->composerArtists, true))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, composer, db::TrackArtistLinkType::Composer));
for (const db::Artist::pointer& lyricist : getOrCreateArtists(dbSession, _parsedTrack->lyricistArtists, true))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, lyricist, db::TrackArtistLinkType::Lyricist));
for (const db::Artist::pointer& mixer : getOrCreateArtists(dbSession, _parsedTrack->mixerArtists, true))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, mixer, db::TrackArtistLinkType::Mixer));
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::Conductor, _parsedTrack->conductorArtists, allowFallback);
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::Composer, _parsedTrack->composerArtists, allowFallback);
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::Lyricist, _parsedTrack->lyricistArtists, allowFallback);
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::Mixer, _parsedTrack->mixerArtists, allowFallback);
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::Remixer, _parsedTrack->remixerArtists, allowFallback);
for (const auto& [role, performers] : _parsedTrack->performerArtists)
{
for (const db::Artist::pointer& performer : getOrCreateArtists(dbSession, performers, true))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, performer, db::TrackArtistLinkType::Performer, role));
}
for (const db::Artist::pointer& producer : getOrCreateArtists(dbSession, _parsedTrack->producerArtists, true))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, producer, db::TrackArtistLinkType::Producer));
for (const db::Artist::pointer& remixer : getOrCreateArtists(dbSession, _parsedTrack->remixerArtists, true))
track.modify()->addArtistLink(db::TrackArtistLink::create(dbSession, track, remixer, db::TrackArtistLinkType::Remixer));
createTrackArtistLinks(dbSession, track, db::TrackArtistLinkType::Performer, role, performers, allowFallback);
track.modify()->setScanVersion(_settings.scanVersion);
if (_parsedTrack->medium && _parsedTrack->medium->release)
@@ -0,0 +1,228 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScanStepArtistReconciliation.hpp"
#include <cassert>
#include <ostream>
#include "core/ILogger.hpp"
#include "database/Artist.hpp"
#include "database/ArtistInfo.hpp"
#include "database/Db.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackArtistLink.hpp"
#include "database/TrackList.hpp"
#include "metadata/Types.hpp"
#include "ScannerSettings.hpp"
#include "helpers/ArtistHelpers.hpp"
namespace lms::scanner
{
namespace
{
std::ostream& operator<<(std::ostream& os, const db::Artist::pointer& artist)
{
os << artist->getName();
if (const auto mbid{ artist->getMBID() })
os << " [" << mbid->getAsString() << "]";
return os;
}
void recomputeArtist(db::Session& session, db::TrackArtistLink::pointer link, bool allowArtistMBIDFallback)
{
assert(!link->isArtistMBIDMatched());
metadata::Artist artistInfo{ std::nullopt, link->getArtistName(), link->getArtistSortName().empty() ? std::nullopt : std::make_optional<std::string>(link->getArtistSortName()) };
db::Artist::pointer newArtist{ helpers::getOrCreateArtistByName(session, artistInfo, helpers::AllowFallbackOnMBIDEntry{ allowArtistMBIDFallback }) };
LMS_LOG(DB, INFO, "Reconcile artist link for track " << link->getTrack()->getAbsoluteFilePath() << ", type " << static_cast<int>(link->getType()) << " from " << link->getArtist() << " to " << newArtist);
assert(newArtist != link->getArtist());
link.modify()->setArtist(newArtist);
}
void recomputeArtist(db::Session& session, db::ArtistInfo::pointer artistInfo, bool allowArtistMBIDFallback)
{
assert(!artistInfo->isMBIDMatched());
const metadata::Artist artistMetadata{ std::nullopt, artistInfo->getName(), artistInfo->getSortName().empty() ? std::nullopt : std::make_optional<std::string>(artistInfo->getSortName()) };
db::Artist::pointer newArtist{ helpers::getOrCreateArtistByName(session, artistMetadata, helpers::AllowFallbackOnMBIDEntry{ allowArtistMBIDFallback }) };
LMS_LOG(DB, INFO, "Reconcile artist link for artist info " << artistInfo->getAbsoluteFilePath() << " from " << artistInfo->getArtist() << " to " << newArtist);
assert(newArtist != artistInfo->getArtist());
artistInfo.modify()->setArtist(newArtist);
}
} // namespace
void ScanStepArtistReconciliation::process(ScanContext& context)
{
// Reconcile artist links
{
// Order is important
updateLinksForArtistNameNoLongerMatch(context);
updateLinksWithArtistNameAmbiguity(context);
}
// Reconcile artist info
{
// Order is important
updateArtistInfoForArtistNameNoLongerMatch(context);
updateArtistInfoWithArtistNameAmbiguity(context);
}
}
void ScanStepArtistReconciliation::updateArtistInfoForArtistNameNoLongerMatch(ScanContext& context)
{
static constexpr std::size_t batchSize{ 50 };
const bool allowArtistMBIDFallback{ _settings.allowArtistMBIDFallback };
db::Session& session{ _db.getTLSSession() };
std::vector<db::ArtistInfo::pointer> artistInfo;
while (true)
{
artistInfo.clear();
{
auto transaction{ session.createReadTransaction() };
db::ArtistInfo::findArtistNameNoLongerMatch(session, db::Range{ .offset = 0, .size = batchSize }, [&](const db::ArtistInfo::pointer& link) {
artistInfo.push_back(link);
});
}
if (artistInfo.empty())
break;
{
auto transaction{ session.createWriteTransaction() };
for (db::ArtistInfo::pointer& info : artistInfo)
{
recomputeArtist(session, info, allowArtistMBIDFallback);
context.currentStepStats.processedElems++;
}
_progressCallback(context.currentStepStats);
}
}
}
void ScanStepArtistReconciliation::updateArtistInfoWithArtistNameAmbiguity(ScanContext& context)
{
static constexpr std::size_t batchSize{ 50 };
const bool allowArtistMBIDFallback{ _settings.allowArtistMBIDFallback };
db::Session& session{ _db.getTLSSession() };
std::vector<db::ArtistInfo::pointer> artistInfo;
while (true)
{
artistInfo.clear();
{
auto transaction{ session.createReadTransaction() };
db::ArtistInfo::findWithArtistNameAmbiguity(session, db::Range{ .offset = 0, .size = batchSize }, allowArtistMBIDFallback, [&](const db::ArtistInfo::pointer& info) {
artistInfo.push_back(info);
});
}
if (artistInfo.empty())
break;
{
auto transaction{ session.createWriteTransaction() };
for (db::ArtistInfo::pointer& info : artistInfo)
{
recomputeArtist(session, info, allowArtistMBIDFallback);
context.currentStepStats.processedElems++;
}
_progressCallback(context.currentStepStats);
}
}
}
void ScanStepArtistReconciliation::updateLinksForArtistNameNoLongerMatch(ScanContext& context)
{
static constexpr std::size_t batchSize{ 50 };
const bool allowArtistMBIDFallback{ _settings.allowArtistMBIDFallback };
db::Session& session{ _db.getTLSSession() };
std::vector<db::TrackArtistLink::pointer> links;
while (true)
{
links.clear();
{
auto transaction{ session.createReadTransaction() };
db::TrackArtistLink::findArtistNameNoLongerMatch(session, db::Range{ .offset = 0, .size = batchSize }, [&](const db::TrackArtistLink::pointer& link) {
links.push_back(link);
});
}
if (links.empty())
break;
{
auto transaction{ session.createWriteTransaction() };
for (db::TrackArtistLink::pointer& link : links)
{
recomputeArtist(session, link, allowArtistMBIDFallback);
context.currentStepStats.processedElems++;
}
_progressCallback(context.currentStepStats);
}
}
}
void ScanStepArtistReconciliation::updateLinksWithArtistNameAmbiguity(ScanContext& context)
{
static constexpr std::size_t batchSize{ 50 };
const bool allowArtistMBIDFallback{ _settings.allowArtistMBIDFallback };
db::Session& session{ _db.getTLSSession() };
std::vector<db::TrackArtistLink::pointer> links;
while (true)
{
links.clear();
{
auto transaction{ session.createReadTransaction() };
db::TrackArtistLink::findWithArtistNameAmbiguity(session, db::Range{ .offset = 0, .size = batchSize }, allowArtistMBIDFallback, [&](const db::TrackArtistLink::pointer& link) {
links.push_back(link);
});
}
if (links.empty())
break;
{
auto transaction{ session.createWriteTransaction() };
for (db::TrackArtistLink::pointer& link : links)
{
recomputeArtist(session, link, allowArtistMBIDFallback);
context.currentStepStats.processedElems++;
}
_progressCallback(context.currentStepStats);
}
}
}
} // namespace lms::scanner
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "ScanStepBase.hpp"
namespace lms::scanner
{
class ScanStepArtistReconciliation : public ScanStepBase
{
public:
using ScanStepBase::ScanStepBase;
private:
ScanStep getStep() const override { return ScanStep::ReconciliateArtists; }
core::LiteralString getStepName() const override { return "Artist reconciliation"; }
void process(ScanContext& context) override;
void updateLinksForArtistNameNoLongerMatch(ScanContext& context);
void updateLinksWithArtistNameAmbiguity(ScanContext& context);
void updateArtistInfoForArtistNameNoLongerMatch(ScanContext& context);
void updateArtistInfoWithArtistNameAmbiguity(ScanContext& context);
};
} // namespace lms::scanner
@@ -57,12 +57,11 @@ namespace lms::scanner
, _fileScanners(std::cbegin(initParams.fileScanners), std::cend(initParams.fileScanners))
{
}
protected:
~ScanStepBase() override = default;
ScanStepBase(const ScanStepBase&) = delete;
ScanStepBase& operator=(const ScanStepBase&) = delete;
protected:
const ScannerSettings& _settings;
ProgressCallback _progressCallback;
bool& _abortScan;
@@ -75,6 +75,7 @@ namespace lms::scanner
DiscoverFiles,
FetchTrackFeatures,
Optimize,
ReconciliateArtists,
ReloadSimilarityEngine,
RemoveOrphanedDbEntries,
ScanFiles,
+18 -6
View File
@@ -70,7 +70,8 @@ namespace lms::ui
static inline constexpr Field UpdatePeriodField{ "update-period" };
static inline constexpr Field UpdateStartTimeField{ "update-start-time" };
static inline constexpr Field SimilarityEngineTypeField{ "similarity-engine-type" };
static inline constexpr Field SkipSingleReleasePlayLists{ "skip-single-release-playlists" };
static inline constexpr Field SkipSingleReleasePlayListsField{ "skip-single-release-playlists" };
static inline constexpr Field AllowMBIDArtistMergeField{ "allow-mbid-artist-merge" };
using UpdatePeriodModel = ValueStringModel<ScanSettings::UpdatePeriod>;
@@ -81,12 +82,14 @@ namespace lms::ui
addField(UpdatePeriodField);
addField(UpdateStartTimeField);
addField(SimilarityEngineTypeField);
addField(SkipSingleReleasePlayLists);
addField(SkipSingleReleasePlayListsField);
addField(AllowMBIDArtistMergeField);
setValidator(UpdatePeriodField, createMandatoryValidator());
setValidator(UpdateStartTimeField, createMandatoryValidator());
setValidator(SimilarityEngineTypeField, createMandatoryValidator());
setValidator(SkipSingleReleasePlayLists, createMandatoryValidator());
setValidator(SkipSingleReleasePlayListsField, createMandatoryValidator());
setValidator(AllowMBIDArtistMergeField, createMandatoryValidator());
}
std::shared_ptr<UpdatePeriodModel> updatePeriodModel() { return _updatePeriodModel; }
@@ -113,7 +116,8 @@ namespace lms::ui
setReadOnly(DatabaseSettingsModel::UpdateStartTimeField, true);
}
setValue(SkipSingleReleasePlayLists, scanSettings->getSkipSingleReleasePlayLists());
setValue(SkipSingleReleasePlayListsField, scanSettings->getSkipSingleReleasePlayLists());
setValue(AllowMBIDArtistMergeField, scanSettings->getAllowMBIDArtistMerge());
auto similarityEngineTypeRow{ _similarityEngineTypeModel->getRowFromValue(scanSettings->getSimilarityEngineType()) };
if (similarityEngineTypeRow)
@@ -145,10 +149,15 @@ namespace lms::ui
}
{
const bool skipSingleReleasePlayLists{ Wt::asNumber(value(SkipSingleReleasePlayLists)) != 0 };
const bool skipSingleReleasePlayLists{ Wt::asNumber(value(SkipSingleReleasePlayListsField)) != 0 };
scanSettings.modify()->setSkipSingleReleasePlayLists(skipSingleReleasePlayLists);
}
{
const bool allowMBIDArtistMerge{ Wt::asNumber(value(AllowMBIDArtistMergeField)) != 0 };
scanSettings.modify()->setAllowMBIDArtistMerge(allowMBIDArtistMerge);
}
{
const auto similarityEngineTypeRow{ _similarityEngineTypeModel->getRowFromString(valueText(SimilarityEngineTypeField)) };
if (similarityEngineTypeRow)
@@ -336,7 +345,10 @@ namespace lms::ui
t->setFormWidget(DatabaseSettingsModel::UpdateStartTimeField, std::move(updateStartTime));
// Skip playlists
t->setFormWidget(DatabaseSettingsModel::SkipSingleReleasePlayLists, std::make_unique<Wt::WCheckBox>());
t->setFormWidget(DatabaseSettingsModel::SkipSingleReleasePlayListsField, std::make_unique<Wt::WCheckBox>());
// Allow to merge artists without MBID with those with one
t->setFormWidget(DatabaseSettingsModel::AllowMBIDArtistMergeField, std::make_unique<Wt::WCheckBox>());
// Similarity engine type
auto similarityEngineType{ std::make_unique<Wt::WComboBox>() };
+5
View File
@@ -330,6 +330,11 @@ namespace lms::ui
.arg(stepStats.progress()));
break;
case ScanStep::ReconciliateArtists:
_stepStatus->setText(Wt::WString::tr("Lms.Admin.ScannerController.step-reconciliate-artists")
.arg(stepStats.processedElems));
break;
case ScanStep::RemoveOrphanedDbEntries:
_stepStatus->setText(Wt::WString::tr("Lms.Admin.ScannerController.step-removing-orphaned-entries")
.arg(stepStats.processedElems));