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
@@ -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;