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 }});
}
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()
.find<Release>()
.where("name = ?").bind(std::string(name, 0, _maxNameLength))
.query<Wt::Dbo::ptr<Release>>("SELECT DISTINCT r from release r")
.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() };
return std::vector<Release::pointer>(res.begin(), res.end());
@@ -19,6 +19,7 @@
#pragma once
#include <filesystem>
#include <optional>
#include <vector>
@@ -61,7 +62,7 @@ namespace Database
ArtistId artist; // only releases that involved this user
EnumSet<TrackArtistLinkType> trackArtistLinkTypes; // and 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)
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 bool exists(Session& session, ReleaseId id);
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 RangeResults<pointer> find(Session& session, const FindParameters& parameters);
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)
{
ScopedRelease release1{ session, "MyRelease" };
@@ -35,10 +35,11 @@
using namespace Database;
namespace Scanner
{
namespace
{
Artist::pointer
createArtist(Session& session, const MetaData::Artist& artistInfo)
Artist::pointer createArtist(Session& session, const MetaData::Artist& artistInfo)
{
Artist::pointer artist{ session.create<Artist>(artistInfo.name) };
@@ -50,8 +51,7 @@ namespace
return artist;
}
void
updateArtistIfNeeded(Artist::pointer artist, const MetaData::Artist& artistInfo)
void updateArtistIfNeeded(Artist::pointer artist, const MetaData::Artist& artistInfo)
{
// Name may have been updated
if (artist->getName() != artistInfo.name)
@@ -66,8 +66,7 @@ namespace
}
}
std::vector<Artist::pointer>
getOrCreateArtists(Session& session, const std::vector<MetaData::Artist>& artistsInfo, bool allowFallbackOnMBIDEntries)
std::vector<Artist::pointer> getOrCreateArtists(Session& session, const std::vector<MetaData::Artist>& artistsInfo, bool allowFallbackOnMBIDEntries)
{
std::vector<Artist::pointer> artists;
@@ -176,8 +175,7 @@ namespace
return res;
}
void
updateReleaseIfNeeded(Release::pointer release, const MetaData::Release& releaseInfo)
void updateReleaseIfNeeded(Release::pointer release, const MetaData::Release& releaseInfo)
{
if (release->getName() != releaseInfo.name)
release.modify()->setName(releaseInfo.name);
@@ -196,8 +194,7 @@ namespace
release.modify()->setArtistDisplayName(releaseInfo.artistDisplayName);
}
Release::pointer
getOrCreateRelease(Session& session, const MetaData::Release& releaseInfo)
Release::pointer getOrCreateRelease(Session& session, const MetaData::Release& releaseInfo, const std::filesystem::path& expectedReleaseDirectory)
{
Release::pointer release;
@@ -212,10 +209,10 @@ namespace
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())
{
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
if (sameNamedRelease->getMBID())
@@ -236,8 +233,7 @@ namespace
return Release::pointer{};
}
std::vector<Cluster::pointer>
getOrCreateClusters(Session& session, const MetaData::Tags& tags)
std::vector<Cluster::pointer> getOrCreateClusters(Session& session, const MetaData::Tags& tags)
{
std::vector<Cluster::pointer> clusters;
@@ -260,8 +256,7 @@ namespace
return clusters;
}
MetaData::ParserReadStyle
getParserReadStyle()
MetaData::ParserReadStyle getParserReadStyle()
{
std::string_view readStyle{ Service<IConfig>::get()->getString("scanner-parser-read-style", "accurate") };
@@ -276,16 +271,13 @@ namespace
}
} // namespace
namespace Scanner
{
ScanStepScanFiles::ScanStepScanFiles(InitParams& initParams)
: ScanStepBase{ initParams }
, _metadataParser{ MetaData::createParser(MetaData::ParserType::TagLib, getParserReadStyle()) } // For now, always use TagLib
{
}
void
ScanStepScanFiles::process(ScanContext& context)
void ScanStepScanFiles::process(ScanContext& context)
{
_metadataParser->setClusterTypeNames(_settings.clusterTypeNames);
@@ -317,8 +309,7 @@ namespace Scanner
}, &excludeDirFileName);
}
void
ScanStepScanFiles::scanAudioFile(const std::filesystem::path& file, ScanContext& context)
void ScanStepScanFiles::scanAudioFile(const std::filesystem::path& file, ScanContext& context)
{
ScanStats& stats{ context.stats };
Wt::WDateTime lastWriteTime;
@@ -488,7 +479,7 @@ namespace Scanner
track.modify()->setScanVersion(_settings.scanVersion);
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
track.modify()->setRelease({});
track.modify()->setTotalTrack(trackInfo->medium ? trackInfo->medium->trackCount : std::nullopt);