Fallback on same album name only if in the same directory. fixes #370

This commit is contained in:
emeric
2023-11-20 09:51:11 +01:00
parent b68b3ce0a6
commit b02021ac8f
4 changed files with 247 additions and 219 deletions
+6 -4
View File
@@ -196,13 +196,15 @@ namespace Database
return session.getDboSession().add(std::unique_ptr<Release> {new Release{ name, MBID }}); return session.getDboSession().add(std::unique_ptr<Release> {new Release{ name, MBID }});
} }
std::vector<Release::pointer> Release::find(Session& session, const std::string& name) std::vector<Release::pointer> Release::find(Session& session, const std::string& name, const std::filesystem::path& releaseDirectory)
{ {
session.checkWriteTransaction(); session.checkReadTransaction();
auto res{ session.getDboSession() auto res{ session.getDboSession()
.find<Release>() .query<Wt::Dbo::ptr<Release>>("SELECT DISTINCT r from release r")
.where("name = ?").bind(std::string(name, 0, _maxNameLength)) .join("track t ON t.release_id = r.id")
.where("r.name = ?").bind(std::string(name, 0, _maxNameLength))
.where("t.file_path LIKE ?").bind(Utils::escapeLikeKeyword(releaseDirectory.string()) + "%")
.resultList() }; .resultList() };
return std::vector<Release::pointer>(res.begin(), res.end()); return std::vector<Release::pointer>(res.begin(), res.end());
@@ -19,6 +19,7 @@
#pragma once #pragma once
#include <filesystem>
#include <optional> #include <optional>
#include <vector> #include <vector>
@@ -61,7 +62,7 @@ namespace Database
ArtistId artist; // only releases that involved this user ArtistId artist; // only releases that involved this user
EnumSet<TrackArtistLinkType> trackArtistLinkTypes; // and for these link types EnumSet<TrackArtistLinkType> trackArtistLinkTypes; // and for these link types
EnumSet<TrackArtistLinkType> excludedTrackArtistLinkTypes; // but not for these link types EnumSet<TrackArtistLinkType> excludedTrackArtistLinkTypes; // but not for these link types
std::optional<ReleaseTypePrimary> primaryType; // if, set, matching this primary type std::optional<ReleaseTypePrimary> primaryType; // if set, matching this primary type
EnumSet<ReleaseTypeSecondary> secondaryTypes; // Matching all this (if any) EnumSet<ReleaseTypeSecondary> secondaryTypes; // Matching all this (if any)
FindParameters& setClusters(const std::vector<ClusterId>& _clusters) { clusters = _clusters; return *this; } FindParameters& setClusters(const std::vector<ClusterId>& _clusters) { clusters = _clusters; return *this; }
@@ -86,7 +87,7 @@ namespace Database
static std::size_t getCount(Session& session); static std::size_t getCount(Session& session);
static bool exists(Session& session, ReleaseId id); static bool exists(Session& session, ReleaseId id);
static pointer find(Session& session, const UUID& MBID); static pointer find(Session& session, const UUID& MBID);
static std::vector<pointer> find(Session& session, const std::string& name); static std::vector<pointer> find(Session& session, const std::string& name, const std::filesystem::path& releaseDirectory);
static pointer find(Session& session, ReleaseId id); static pointer find(Session& session, ReleaseId id);
static RangeResults<pointer> find(Session& session, const FindParameters& parameters); static RangeResults<pointer> find(Session& session, const FindParameters& parameters);
static void find(Session& session, const FindParameters& parameters, std::function<void(const pointer&)> func); static void find(Session& session, const FindParameters& parameters, std::function<void(const pointer&)> func);
@@ -132,6 +132,40 @@ TEST_F(DatabaseFixture, Release_singleTrack)
} }
} }
TEST_F(DatabaseFixture, Release_findByNameAndPath)
{
ScopedRelease release1{ session, "MyRelease" };
ScopedRelease release2{ session, "MyRelease" };
ScopedTrack track1{ session, "MyTrack" };
ScopedTrack track2{ session, "MyTrack" };
{
auto transaction{ session.createWriteTransaction() };
track1.get().modify()->setRelease(release1.get());
track1.get().modify()->setPath("/tmp/foo/foo.mp3");
track2.get().modify()->setRelease(release2.get());
track2.get().modify()->setPath("/tmp/bar/bar.mp3");
}
{
auto transaction{ session.createReadTransaction() };
std::cout << "OK HERE" << std::endl;
{
const auto releases{ Release::find(session, "MyRelease", "/tmp/foo") };
ASSERT_EQ(releases.size(), 1);
EXPECT_EQ(releases.front()->getId(), release1.getId());
}
{
const auto releases{ Release::find(session, "MyRelease", "/tmp/bar") };
ASSERT_EQ(releases.size(), 1);
EXPECT_EQ(releases.front()->getId(), release2.getId());
}
}
}
TEST_F(DatabaseFixture, MulitpleReleaseSearchByName) TEST_F(DatabaseFixture, MulitpleReleaseSearchByName)
{ {
ScopedRelease release1{ session, "MyRelease" }; ScopedRelease release1{ session, "MyRelease" };
@@ -35,10 +35,11 @@
using namespace Database; using namespace Database;
namespace namespace Scanner
{ {
Artist::pointer namespace
createArtist(Session& session, const MetaData::Artist& artistInfo) {
Artist::pointer createArtist(Session& session, const MetaData::Artist& artistInfo)
{ {
Artist::pointer artist{ session.create<Artist>(artistInfo.name) }; Artist::pointer artist{ session.create<Artist>(artistInfo.name) };
@@ -50,8 +51,7 @@ namespace
return artist; return artist;
} }
void void updateArtistIfNeeded(Artist::pointer artist, const MetaData::Artist& artistInfo)
updateArtistIfNeeded(Artist::pointer artist, const MetaData::Artist& artistInfo)
{ {
// Name may have been updated // Name may have been updated
if (artist->getName() != artistInfo.name) if (artist->getName() != artistInfo.name)
@@ -66,8 +66,7 @@ namespace
} }
} }
std::vector<Artist::pointer> std::vector<Artist::pointer> getOrCreateArtists(Session& session, const std::vector<MetaData::Artist>& artistsInfo, bool allowFallbackOnMBIDEntries)
getOrCreateArtists(Session& session, const std::vector<MetaData::Artist>& artistsInfo, bool allowFallbackOnMBIDEntries)
{ {
std::vector<Artist::pointer> artists; std::vector<Artist::pointer> artists;
@@ -176,8 +175,7 @@ namespace
return res; return res;
} }
void void updateReleaseIfNeeded(Release::pointer release, const MetaData::Release& releaseInfo)
updateReleaseIfNeeded(Release::pointer release, const MetaData::Release& releaseInfo)
{ {
if (release->getName() != releaseInfo.name) if (release->getName() != releaseInfo.name)
release.modify()->setName(releaseInfo.name); release.modify()->setName(releaseInfo.name);
@@ -196,8 +194,7 @@ namespace
release.modify()->setArtistDisplayName(releaseInfo.artistDisplayName); release.modify()->setArtistDisplayName(releaseInfo.artistDisplayName);
} }
Release::pointer Release::pointer getOrCreateRelease(Session& session, const MetaData::Release& releaseInfo, const std::filesystem::path& expectedReleaseDirectory)
getOrCreateRelease(Session& session, const MetaData::Release& releaseInfo)
{ {
Release::pointer release; Release::pointer release;
@@ -212,10 +209,10 @@ namespace
return release; return release;
} }
// Fall back on release name (collisions may occur) // Fall back on release name (collisions may occur), if and only if it is in the current directory
if (!releaseInfo.name.empty()) if (!releaseInfo.name.empty())
{ {
for (const Release::pointer& sameNamedRelease : Release::find(session, releaseInfo.name)) for (const Release::pointer& sameNamedRelease : Release::find(session, releaseInfo.name, expectedReleaseDirectory))
{ {
// do not fallback on properly tagged releases // do not fallback on properly tagged releases
if (sameNamedRelease->getMBID()) if (sameNamedRelease->getMBID())
@@ -236,8 +233,7 @@ namespace
return Release::pointer{}; return Release::pointer{};
} }
std::vector<Cluster::pointer> std::vector<Cluster::pointer> getOrCreateClusters(Session& session, const MetaData::Tags& tags)
getOrCreateClusters(Session& session, const MetaData::Tags& tags)
{ {
std::vector<Cluster::pointer> clusters; std::vector<Cluster::pointer> clusters;
@@ -260,8 +256,7 @@ namespace
return clusters; return clusters;
} }
MetaData::ParserReadStyle MetaData::ParserReadStyle getParserReadStyle()
getParserReadStyle()
{ {
std::string_view readStyle{ Service<IConfig>::get()->getString("scanner-parser-read-style", "accurate") }; std::string_view readStyle{ Service<IConfig>::get()->getString("scanner-parser-read-style", "accurate") };
@@ -274,18 +269,15 @@ namespace
throw LmsException{ "Invalid value for 'scanner-parser-read-style'" }; throw LmsException{ "Invalid value for 'scanner-parser-read-style'" };
} }
} // namespace } // namespace
namespace Scanner
{
ScanStepScanFiles::ScanStepScanFiles(InitParams& initParams) ScanStepScanFiles::ScanStepScanFiles(InitParams& initParams)
: ScanStepBase{ initParams } : ScanStepBase{ initParams }
, _metadataParser{ MetaData::createParser(MetaData::ParserType::TagLib, getParserReadStyle()) } // For now, always use TagLib , _metadataParser{ MetaData::createParser(MetaData::ParserType::TagLib, getParserReadStyle()) } // For now, always use TagLib
{ {
} }
void void ScanStepScanFiles::process(ScanContext& context)
ScanStepScanFiles::process(ScanContext& context)
{ {
_metadataParser->setClusterTypeNames(_settings.clusterTypeNames); _metadataParser->setClusterTypeNames(_settings.clusterTypeNames);
@@ -317,8 +309,7 @@ namespace Scanner
}, &excludeDirFileName); }, &excludeDirFileName);
} }
void void ScanStepScanFiles::scanAudioFile(const std::filesystem::path& file, ScanContext& context)
ScanStepScanFiles::scanAudioFile(const std::filesystem::path& file, ScanContext& context)
{ {
ScanStats& stats{ context.stats }; ScanStats& stats{ context.stats };
Wt::WDateTime lastWriteTime; Wt::WDateTime lastWriteTime;
@@ -488,7 +479,7 @@ namespace Scanner
track.modify()->setScanVersion(_settings.scanVersion); track.modify()->setScanVersion(_settings.scanVersion);
if (trackInfo->medium && trackInfo->medium->release) if (trackInfo->medium && trackInfo->medium->release)
track.modify()->setRelease(getOrCreateRelease(dbSession, *trackInfo->medium->release)); track.modify()->setRelease(getOrCreateRelease(dbSession, *trackInfo->medium->release, file.parent_path()));
else else
track.modify()->setRelease({}); track.modify()->setRelease({});
track.modify()->setTotalTrack(trackInfo->medium ? trackInfo->medium->trackCount : std::nullopt); track.modify()->setTotalTrack(trackInfo->medium ? trackInfo->medium->trackCount : std::nullopt);