Merge branch 'subsonic-scan-tracker' into develop

This commit is contained in:
emeric
2024-03-23 15:56:07 +01:00
16 changed files with 647 additions and 162 deletions
+2 -4
View File
@@ -25,8 +25,7 @@
namespace lms::core namespace lms::core
{ {
std::unique_ptr<IChildProcessManager> std::unique_ptr<IChildProcessManager> createChildProcessManager(boost::asio::io_context& ioContext)
createChildProcessManager(boost::asio::io_context& ioContext)
{ {
return std::make_unique<ChildProcessManager>(ioContext); return std::make_unique<ChildProcessManager>(ioContext);
} }
@@ -36,8 +35,7 @@ namespace lms::core
{ {
} }
std::unique_ptr<IChildProcess> std::unique_ptr<IChildProcess> ChildProcessManager::spawnChildProcess(const std::filesystem::path& path, const IChildProcess::Args& args)
ChildProcessManager::spawnChildProcess(const std::filesystem::path& path, const IChildProcess::Args& args)
{ {
return std::make_unique<ChildProcess>(_ioContext, path, args); return std::make_unique<ChildProcess>(_ioContext, path, args);
} }
+31 -5
View File
@@ -40,6 +40,7 @@ namespace lms::db
{ {
session.checkReadTransaction(); session.checkReadTransaction();
// TODO remove distinct and use group by
auto query{ session.getDboSession().query<ResultType>("SELECT DISTINCT " + std::string{ itemToSelect } + " FROM artist a") }; auto query{ session.getDboSession().query<ResultType>("SELECT DISTINCT " + std::string{ itemToSelect } + " FROM artist a") };
if (params.sortMethod == ArtistSortMethod::LastWritten if (params.sortMethod == ArtistSortMethod::LastWritten
|| params.writtenAfter.isValid() || params.writtenAfter.isValid()
@@ -64,13 +65,13 @@ namespace lms::db
std::vector<std::string> clauses; std::vector<std::string> clauses;
std::vector<std::string> sortClauses; std::vector<std::string> sortClauses;
for (std::string_view keyword : params.keywords) for (const std::string_view keyword : params.keywords)
{ {
clauses.push_back("a.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'"); clauses.push_back("a.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'");
query.bind("%" + utils::escapeLikeKeyword(keyword) + "%"); query.bind("%" + utils::escapeLikeKeyword(keyword) + "%");
} }
for (std::string_view keyword : params.keywords) for (const std::string_view keyword : params.keywords)
{ {
sortClauses.push_back("a.sort_name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'"); sortClauses.push_back("a.sort_name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'");
query.bind("%" + utils::escapeLikeKeyword(keyword) + "%"); query.bind("%" + utils::escapeLikeKeyword(keyword) + "%");
@@ -184,6 +185,31 @@ namespace lms::db
return session.getDboSession().query<int>("SELECT COUNT(*) FROM artist"); return session.getDboSession().query<int>("SELECT COUNT(*) FROM artist");
} }
void Artist::find(Session& session, ArtistId& lastRetrievedArtist, std::size_t count, const std::function<void(const Artist::pointer&)>& func, MediaLibraryId library)
{
session.checkReadTransaction();
auto query{ session.getDboSession().query<Wt::Dbo::ptr<Artist>>("SELECT a FROM artist a")
.orderBy("a.id")
.where("a.id > ?").bind(lastRetrievedArtist)
.limit(static_cast<int>(count)) };
if (library.isValid())
{
query.join("track t ON t.id = t_a_l.track_id");
query.join("track_artist_link t_a_l ON t_a_l.artist_id = a.id");
query.where("t.media_library_id = ?").bind(library);
}
auto collection{ query.resultList() };
for (auto itResult{ collection.begin() }; itResult != collection.end(); ++itResult)
{
func(*itResult);
lastRetrievedArtist = (*itResult)->getId();
}
}
std::vector<Artist::pointer> Artist::find(Session& session, std::string_view name) std::vector<Artist::pointer> Artist::find(Session& session, std::string_view name)
{ {
session.checkReadTransaction(); session.checkReadTransaction();
@@ -286,7 +312,7 @@ namespace lms::db
.groupBy("a.id") .groupBy("a.id")
.orderBy("COUNT(*) DESC, RANDOM()") }; .orderBy("COUNT(*) DESC, RANDOM()") };
for (TrackArtistLinkType type : artistLinkTypes) for (const TrackArtistLinkType type : artistLinkTypes)
query.bind(type); query.bind(type);
return utils::execQuery<ArtistId>(query, range); return utils::execQuery<ArtistId>(query, range);
@@ -304,7 +330,7 @@ namespace lms::db
where.And(WhereClause("a.id = ?")).bind(getId().toString()); where.And(WhereClause("a.id = ?")).bind(getId().toString());
{ {
WhereClause clusterClause; WhereClause clusterClause;
for (ClusterTypeId clusterTypeId : clusterTypeIds) for (const ClusterTypeId clusterTypeId : clusterTypeIds)
clusterClause.Or(WhereClause("c_type.id = ?")).bind(clusterTypeId.toString()); clusterClause.Or(WhereClause("c_type.id = ?")).bind(clusterTypeId.toString());
where.And(clusterClause); where.And(clusterClause);
@@ -320,7 +346,7 @@ namespace lms::db
Wt::Dbo::collection<Wt::Dbo::ptr<Cluster>> queryRes = query; Wt::Dbo::collection<Wt::Dbo::ptr<Cluster>> queryRes = query;
std::map<ClusterTypeId, std::vector<Cluster::pointer>> clustersByType; std::map<ClusterTypeId, std::vector<Cluster::pointer>> clustersByType;
for (Cluster::pointer cluster : queryRes) for (const Cluster::pointer& cluster : queryRes)
{ {
if (clustersByType[cluster->getType()->getId()].size() < size) if (clustersByType[cluster->getType()->getId()].size() < size)
clustersByType[cluster->getType()->getId()].push_back(cluster); clustersByType[cluster->getType()->getId()].push_back(cluster);
+24
View File
@@ -293,6 +293,30 @@ namespace lms::db
return utils::execQuery<ReleaseId>(query, range); return utils::execQuery<ReleaseId>(query, range);
} }
void Release::find(Session& session, ReleaseId& lastRetrievedRelease, std::size_t count, const std::function<void(const Release::pointer&)>& func, MediaLibraryId library)
{
session.checkReadTransaction();
auto query{ session.getDboSession().query<Wt::Dbo::ptr<Release>>("SELECT r FROM release r")
.orderBy("r.id")
.where("r.id > ?").bind(lastRetrievedRelease)
.limit(static_cast<int>(count)) };
if (library.isValid())
{
query.join("track t ON t.release_id = r.id");
query.where("t.media_library_id = ?").bind(library);
}
auto collection{ query.resultList() };
for (auto itResult{ collection.begin() }; itResult != collection.end(); ++itResult)
{
func(*itResult);
lastRetrievedRelease = (*itResult)->getId();
}
}
RangeResults<Release::pointer> Release::find(Session& session, const FindParameters& params) RangeResults<Release::pointer> Release::find(Session& session, const FindParameters& params)
{ {
session.checkReadTransaction(); session.checkReadTransaction();
+7 -12
View File
@@ -228,27 +228,22 @@ namespace lms::db
.resultValue(); .resultValue();
} }
void Track::find(Session& session, TrackId& lastRetrievedTrack, std::size_t batchSize, bool& moreResults, const std::function<void(const Track::pointer&)>& func) void Track::find(Session& session, TrackId& lastRetrievedTrack, std::size_t count, const std::function<void(const Track::pointer&)>& func, MediaLibraryId library)
{ {
session.checkReadTransaction(); session.checkReadTransaction();
auto collection{ session.getDboSession().find<Track>() auto query{ session.getDboSession().find<Track>()
.orderBy("id") .orderBy("id")
.where("id > ?").bind(lastRetrievedTrack) .where("id > ?").bind(lastRetrievedTrack)
.limit(static_cast<int>(batchSize) + 1) .limit(static_cast<int>(count)) };
.resultList() };
moreResults = false; if (library.isValid())
query.where("media_library_id = ?").bind(library);
auto collection{query.resultList()};
std::size_t count{};
for (auto itResult{ collection.begin() }; itResult != collection.end(); ++itResult) for (auto itResult{ collection.begin() }; itResult != collection.end(); ++itResult)
{ {
if (count++ == batchSize)
{
moreResults = true;
break;
}
func(*itResult); func(*itResult);
lastRetrievedTrack = (*itResult)->getId(); lastRetrievedTrack = (*itResult)->getId();
} }
@@ -86,6 +86,7 @@ namespace lms::db
static pointer find(Session& session, const core::UUID& MBID); static pointer find(Session& session, const core::UUID& MBID);
static pointer find(Session& session, ArtistId id); static pointer find(Session& session, ArtistId id);
static std::vector<pointer> find(Session& session, std::string_view name); // exact match on name field static std::vector<pointer> find(Session& session, std::string_view name); // exact match on name field
static void find(Session& session, ArtistId& lastRetrievedArtist, std::size_t count, const std::function<void(const Artist::pointer&)>& func, MediaLibraryId library = {});
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);
static RangeResults<ArtistId> findIds(Session& session, const FindParameters& parameters); static RangeResults<ArtistId> findIds(Session& session, const FindParameters& parameters);
@@ -32,17 +32,16 @@ namespace lms::db
using ValueType = Wt::Dbo::dbo_default_traits::IdType; using ValueType = Wt::Dbo::dbo_default_traits::IdType;
IdType() = default; IdType() = default;
IdType(ValueType id) : _id {id} { assert(isValid()); } IdType(ValueType id) : _id{ id } { assert(isValid()); }
bool isValid() const { return _id != Wt::Dbo::dbo_default_traits::invalidId(); } bool isValid() const { return _id != Wt::Dbo::dbo_default_traits::invalidId(); }
std::string toString() const { assert(isValid()); return std::to_string(_id); } std::string toString() const { assert(isValid()); return std::to_string(_id); }
ValueType getValue() const { return _id; } ValueType getValue() const { return _id; }
auto operator<=>(const IdType& other) const = default; \
auto operator<=>(const IdType& other) const = default;
private: private:
Wt::Dbo::dbo_default_traits::IdType _id {Wt::Dbo::dbo_default_traits::invalidId()}; Wt::Dbo::dbo_default_traits::IdType _id{ Wt::Dbo::dbo_default_traits::invalidId() };
}; };
#define LMS_DECLARE_IDTYPE(name) \ #define LMS_DECLARE_IDTYPE(name) \
@@ -51,6 +50,7 @@ namespace lms::db
{ \ { \
public: \ public: \
using IdType::IdType; \ using IdType::IdType; \
auto operator<=>(const name& other) const = default; \
};\ };\
} \ } \
namespace std \ namespace std \
@@ -122,6 +122,7 @@ namespace lms::db
static pointer find(Session& session, const core::UUID& MBID); static pointer find(Session& session, const core::UUID& MBID);
static std::vector<pointer> find(Session& session, const std::string& name, const std::filesystem::path& releaseDirectory); 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 void find(Session& session, ReleaseId& lastRetrievedRelease, std::size_t count, const std::function<void(const Release::pointer&)>& func, MediaLibraryId library = {});
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);
static RangeResults<ReleaseId> findIds(Session& session, const FindParameters& parameters); static RangeResults<ReleaseId> findIds(Session& session, const FindParameters& parameters);
+1 -1
View File
@@ -110,7 +110,7 @@ namespace lms::db
static std::size_t getCount(Session& session); static std::size_t getCount(Session& session);
static pointer findByPath(Session& session, const std::filesystem::path& p); static pointer findByPath(Session& session, const std::filesystem::path& p);
static pointer find(Session& session, TrackId id); static pointer find(Session& session, TrackId id);
static void find(Session& session, TrackId& lastRetrievedTrack, std::size_t batchSize, bool& moreResults, const std::function<void(const Track::pointer&)>& func); static void find(Session& session, TrackId& lastRetrievedTrack, std::size_t count, const std::function<void(const Track::pointer&)>& func, MediaLibraryId library = {});
static bool exists(Session& session, TrackId id); static bool exists(Session& session, TrackId id);
static std::vector<pointer> findByRecordingMBID(Session& session, const core::UUID& MBID); static std::vector<pointer> findByRecordingMBID(Session& session, const core::UUID& MBID);
static std::vector<pointer> findByMBID(Session& session, const core::UUID& MBID); static std::vector<pointer> findByMBID(Session& session, const core::UUID& MBID);
+93 -3
View File
@@ -77,6 +77,96 @@ namespace lms::db::tests
} }
} }
TEST_F(DatabaseFixture, Artist_findByRangedIdBased)
{
ScopedTrack track1{ session, "MyTrackFile1" };
ScopedTrack track2{ session, "MyTrackFile2" };
ScopedTrack track3{ session, "MyTrackFile3" };
ScopedArtist artist1{ session, "MyArtist1" };
ScopedArtist artist2{ session, "MyArtist2" };
ScopedArtist artist3{ session, "MyArtist3" };
ScopedMediaLibrary library{ session };
ScopedMediaLibrary otherLibrary{ session };
{
auto transaction{ session.createWriteTransaction() };
track2.get().modify()->setMediaLibrary(library.get());
TrackArtistLink::create(session, track1.get(), artist1.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track2.get(), artist2.get(), TrackArtistLinkType::Artist);
TrackArtistLink::create(session, track3.get(), artist3.get(), TrackArtistLinkType::Artist);
}
{
auto transaction{ session.createReadTransaction() };
ArtistId lastRetrievedId;
std::vector<Artist::pointer> visitedArtists;
Artist::find(session, lastRetrievedId, 10, [&](const Artist::pointer& artist)
{
visitedArtists.push_back(artist);
});
ASSERT_EQ(visitedArtists.size(), 3);
EXPECT_EQ(visitedArtists[0]->getId(), artist1.getId());
EXPECT_EQ(visitedArtists[1]->getId(), artist2.getId());
EXPECT_EQ(visitedArtists[2]->getId(), artist3.getId());
EXPECT_EQ(lastRetrievedId, artist3.getId());
}
{
auto transaction{ session.createReadTransaction() };
ArtistId lastRetrievedId{ artist1.getId() };
std::vector<Artist::pointer> visitedArtists;
Artist::find(session, lastRetrievedId, 1, [&](const Artist::pointer& artist)
{
visitedArtists.push_back(artist);
});
ASSERT_EQ(visitedArtists.size(), 1);
EXPECT_EQ(visitedArtists[0]->getId(), artist2.getId());
EXPECT_EQ(lastRetrievedId, artist2.getId());
}
{
auto transaction{ session.createReadTransaction() };
ArtistId lastRetrievedId{ artist1.getId() };
std::vector<Artist::pointer> visitedArtists;
Artist::find(session, lastRetrievedId, 0, [&](const Artist::pointer& artist)
{
visitedArtists.push_back(artist);
});
ASSERT_EQ(visitedArtists.size(), 0);
EXPECT_EQ(lastRetrievedId, artist1.getId());
}
{
auto transaction{ session.createReadTransaction() };
ArtistId lastRetrievedId;
std::vector<Artist::pointer> visitedArtists;
Artist::find(session, lastRetrievedId, 10, [&](const Artist::pointer& artist)
{
visitedArtists.push_back(artist);
}, otherLibrary.getId());
ASSERT_EQ(visitedArtists.size(), 0);
EXPECT_EQ(lastRetrievedId, ArtistId{});
}
{
auto transaction{ session.createReadTransaction() };
ArtistId lastRetrievedId;
std::vector<Artist::pointer> visitedArtists;
Artist::find(session, lastRetrievedId, 10, [&](const Artist::pointer& artist)
{
visitedArtists.push_back(artist);
}, library.getId());
ASSERT_EQ(visitedArtists.size(), 1);
EXPECT_EQ(visitedArtists[0]->getId(), artist2.getId());
EXPECT_EQ(lastRetrievedId, artist2.getId());
}
}
TEST_F(DatabaseFixture, MultipleArtists) TEST_F(DatabaseFixture, MultipleArtists)
{ {
{ {
@@ -409,7 +499,7 @@ namespace lms::db::tests
EXPECT_EQ(artists.front()->getId(), artist1.getId()); EXPECT_EQ(artists.front()->getId(), artist1.getId());
EXPECT_EQ(Artist::find(session, R"(MyArtistFoo)").size(), 0); EXPECT_EQ(Artist::find(session, R"(MyArtistFoo)").size(), 0);
} }
{ {
const auto artists{ Artist::find(session, R"(%MyArtist)") }; const auto artists{ Artist::find(session, R"(%MyArtist)") };
ASSERT_TRUE(artists.size() == 1); ASSERT_TRUE(artists.size() == 1);
EXPECT_EQ(artists.front()->getId(), artist2.getId()); EXPECT_EQ(artists.front()->getId(), artist2.getId());
@@ -420,12 +510,12 @@ namespace lms::db::tests
ASSERT_TRUE(artists.size() == 1); ASSERT_TRUE(artists.size() == 1);
ASSERT_EQ(artists.front()->getId(), artist3.getId()); ASSERT_EQ(artists.front()->getId(), artist3.getId());
EXPECT_EQ(Artist::find(session, R"(%CMyArtist)").size(), 0); EXPECT_EQ(Artist::find(session, R"(%CMyArtist)").size(), 0);
} }
} }
{ {
auto transaction{ session.createReadTransaction() }; auto transaction{ session.createReadTransaction() };
{ {
const auto artists{ Artist::findIds(session, Artist::FindParameters {}.setKeywords({"MyArtist"})) }; const auto artists{ Artist::findIds(session, Artist::FindParameters {}.setKeywords({"MyArtist"})) };
EXPECT_EQ(artists.results.size(), 6); EXPECT_EQ(artists.results.size(), 6);
} }
+90
View File
@@ -74,6 +74,96 @@ namespace lms::db::tests
} }
} }
TEST_F(DatabaseFixture, Release_findByRangedIdBased)
{
ScopedTrack track1{ session, "MyTrackFile1" };
ScopedTrack track2{ session, "MyTrackFile2" };
ScopedTrack track3{ session, "MyTrackFile3" };
ScopedRelease release1{ session, "MyRelease1" };
ScopedRelease release2{ session, "MyRelease2" };
ScopedRelease release3{ session, "MyRelease3" };
ScopedMediaLibrary library{ session };
ScopedMediaLibrary otherLibrary{ session };
{
auto transaction{ session.createWriteTransaction() };
track2.get().modify()->setMediaLibrary(library.get());
track1.get().modify()->setRelease(release1.get());
track2.get().modify()->setRelease(release2.get());
track3.get().modify()->setRelease(release3.get());
}
{
auto transaction{ session.createReadTransaction() };
ReleaseId lastRetrievedId;
std::vector<Release::pointer> visitedReleases;
Release::find(session, lastRetrievedId, 10, [&](const Release::pointer& release)
{
visitedReleases.push_back(release);
});
ASSERT_EQ(visitedReleases.size(), 3);
EXPECT_EQ(visitedReleases[0]->getId(), release1.getId());
EXPECT_EQ(visitedReleases[1]->getId(), release2.getId());
EXPECT_EQ(visitedReleases[2]->getId(), release3.getId());
EXPECT_EQ(lastRetrievedId, release3.getId());
}
{
auto transaction{ session.createReadTransaction() };
ReleaseId lastRetrievedId{ release1.getId() };
std::vector<Release::pointer> visitedReleases;
Release::find(session, lastRetrievedId, 1, [&](const Release::pointer& release)
{
visitedReleases.push_back(release);
});
ASSERT_EQ(visitedReleases.size(), 1);
EXPECT_EQ(visitedReleases[0]->getId(), release2.getId());
EXPECT_EQ(lastRetrievedId, release2.getId());
}
{
auto transaction{ session.createReadTransaction() };
ReleaseId lastRetrievedId{ release1.getId() };
std::vector<Release::pointer> visitedReleases;
Release::find(session, lastRetrievedId, 0, [&](const Release::pointer& release)
{
visitedReleases.push_back(release);
});
ASSERT_EQ(visitedReleases.size(), 0);
EXPECT_EQ(lastRetrievedId, release1.getId());
}
{
auto transaction{ session.createReadTransaction() };
ReleaseId lastRetrievedId;
std::vector<Release::pointer> visitedReleases;
Release::find(session, lastRetrievedId, 10, [&](const Release::pointer& release)
{
visitedReleases.push_back(release);
}, otherLibrary.getId());
ASSERT_EQ(visitedReleases.size(), 0);
EXPECT_EQ(lastRetrievedId, ReleaseId{});
}
{
auto transaction{ session.createReadTransaction() };
ReleaseId lastRetrievedId;
std::vector<Release::pointer> visitedReleases;
Release::find(session, lastRetrievedId, 10, [&](const Release::pointer& release)
{
visitedReleases.push_back(release);
}, library.getId());
ASSERT_EQ(visitedReleases.size(), 1);
EXPECT_EQ(visitedReleases[0]->getId(), release2.getId());
EXPECT_EQ(lastRetrievedId, release2.getId());
}
}
TEST_F(DatabaseFixture, Release_singleTrack) TEST_F(DatabaseFixture, Release_singleTrack)
{ {
ScopedRelease release{ session, "MyRelease" }; ScopedRelease release{ session, "MyRelease" };
+37 -9
View File
@@ -68,14 +68,20 @@ namespace lms::db::tests
ScopedTrack track1{ session, "MyTrackFile1" }; ScopedTrack track1{ session, "MyTrackFile1" };
ScopedTrack track2{ session, "MyTrackFile1" }; ScopedTrack track2{ session, "MyTrackFile1" };
ScopedTrack track3{ session, "MyTrackFile1" }; ScopedTrack track3{ session, "MyTrackFile1" };
ScopedMediaLibrary library{ session };
ScopedMediaLibrary otherLibrary{ session };
{
auto transaction{ session.createWriteTransaction() };
track2.get().modify()->setMediaLibrary(library.get());
}
{ {
auto transaction{ session.createReadTransaction() }; auto transaction{ session.createReadTransaction() };
bool moreResults;
TrackId lastRetrievedTrackId; TrackId lastRetrievedTrackId;
std::vector<Track::pointer> visitedTracks; std::vector<Track::pointer> visitedTracks;
Track::find(session, lastRetrievedTrackId, 10, moreResults, [&](const Track::pointer& track) Track::find(session, lastRetrievedTrackId, 10, [&](const Track::pointer& track)
{ {
visitedTracks.push_back(track); visitedTracks.push_back(track);
}); });
@@ -83,40 +89,62 @@ namespace lms::db::tests
EXPECT_EQ(visitedTracks[0]->getId(), track1.getId()); EXPECT_EQ(visitedTracks[0]->getId(), track1.getId());
EXPECT_EQ(visitedTracks[1]->getId(), track2.getId()); EXPECT_EQ(visitedTracks[1]->getId(), track2.getId());
EXPECT_EQ(visitedTracks[2]->getId(), track3.getId()); EXPECT_EQ(visitedTracks[2]->getId(), track3.getId());
EXPECT_FALSE(moreResults);
EXPECT_EQ(lastRetrievedTrackId, track3.getId()); EXPECT_EQ(lastRetrievedTrackId, track3.getId());
} }
{ {
auto transaction{ session.createReadTransaction() }; auto transaction{ session.createReadTransaction() };
bool moreResults;
TrackId lastRetrievedTrackId{ track1.getId() }; TrackId lastRetrievedTrackId{ track1.getId() };
std::vector<Track::pointer> visitedTracks; std::vector<Track::pointer> visitedTracks;
Track::find(session, lastRetrievedTrackId, 1, moreResults, [&](const Track::pointer& track) Track::find(session, lastRetrievedTrackId, 1, [&](const Track::pointer& track)
{ {
visitedTracks.push_back(track); visitedTracks.push_back(track);
}); });
ASSERT_EQ(visitedTracks.size(), 1); ASSERT_EQ(visitedTracks.size(), 1);
EXPECT_EQ(visitedTracks[0]->getId(), track2.getId()); EXPECT_EQ(visitedTracks[0]->getId(), track2.getId());
EXPECT_TRUE(moreResults);
EXPECT_EQ(lastRetrievedTrackId, track2.getId()); EXPECT_EQ(lastRetrievedTrackId, track2.getId());
} }
{ {
auto transaction{ session.createReadTransaction() }; auto transaction{ session.createReadTransaction() };
bool moreResults;
TrackId lastRetrievedTrackId{ track1.getId() }; TrackId lastRetrievedTrackId{ track1.getId() };
std::vector<Track::pointer> visitedTracks; std::vector<Track::pointer> visitedTracks;
Track::find(session, lastRetrievedTrackId, 0, moreResults, [&](const Track::pointer& track) Track::find(session, lastRetrievedTrackId, 0, [&](const Track::pointer& track)
{ {
visitedTracks.push_back(track); visitedTracks.push_back(track);
}); });
ASSERT_EQ(visitedTracks.size(), 0); ASSERT_EQ(visitedTracks.size(), 0);
EXPECT_TRUE(moreResults);
EXPECT_EQ(lastRetrievedTrackId, track1.getId()); EXPECT_EQ(lastRetrievedTrackId, track1.getId());
} }
{
auto transaction{ session.createReadTransaction() };
TrackId lastRetrievedTrackId{};
std::vector<Track::pointer> visitedTracks;
Track::find(session, lastRetrievedTrackId, 10, [&](const Track::pointer& track)
{
visitedTracks.push_back(track);
}, otherLibrary.getId());
ASSERT_EQ(visitedTracks.size(), 0);
EXPECT_EQ(lastRetrievedTrackId, TrackId{});
}
{
auto transaction{ session.createReadTransaction() };
TrackId lastRetrievedTrackId{};
std::vector<Track::pointer> visitedTracks;
Track::find(session, lastRetrievedTrackId, 10, [&](const Track::pointer& track)
{
visitedTracks.push_back(track);
}, library.getId());
ASSERT_EQ(visitedTracks.size(), 1);
EXPECT_EQ(visitedTracks[0]->getId(), track2.getId());
EXPECT_EQ(lastRetrievedTrackId, track2.getId());
}
} }
TEST_F(DatabaseFixture, Track_MediaLibrary) TEST_F(DatabaseFixture, Track_MediaLibrary)
@@ -99,8 +99,8 @@ namespace lms::scanner
std::vector<Track::pointer> tracksToRemove; std::vector<Track::pointer> tracksToRemove;
TrackId lastCheckedTrackID; TrackId lastCheckedTrackID;
bool moreResults{ true }; bool endReached{};
while (moreResults) while (!endReached)
{ {
if (_abortScan) if (_abortScan)
break; break;
@@ -108,8 +108,12 @@ namespace lms::scanner
tracksToRemove.clear(); tracksToRemove.clear();
{ {
auto transaction{ session.createReadTransaction() }; auto transaction{ session.createReadTransaction() };
Track::find(session, lastCheckedTrackID, batchSize, moreResults, [&](const Track::pointer& track)
endReached = true;
Track::find(session, lastCheckedTrackID, batchSize, [&](const Track::pointer& track)
{ {
endReached = false;
if (!checkFile(track->getPath())) if (!checkFile(track->getPath()))
tracksToRemove.push_back(track); tracksToRemove.push_back(track);
+1
View File
@@ -26,6 +26,7 @@ namespace lms::api::subsonic
{ {
struct ClientInfo struct ClientInfo
{ {
std::string ipAddress;
std::string name; std::string name;
std::string user; std::string user;
std::string password; std::string password;
+5 -2
View File
@@ -380,13 +380,16 @@ namespace lms::api::subsonic
} }
} }
ClientInfo SubsonicResource::getClientInfo(const Wt::Http::ParameterMap& parameters) ClientInfo SubsonicResource::getClientInfo(const Wt::Http::Request& request)
{ {
const auto& parameters{ request.getParameterMap() };
ClientInfo res; ClientInfo res;
if (hasParameter(parameters, "t")) if (hasParameter(parameters, "t"))
throw TokenAuthenticationNotSupportedForLDAPUsersError{}; throw TokenAuthenticationNotSupportedForLDAPUsersError{};
res.ipAddress = request.clientAddress();
// Mandatory parameters // Mandatory parameters
res.name = getMandatoryParameterAs<std::string>(parameters, "c"); res.name = getMandatoryParameterAs<std::string>(parameters, "c");
res.version = getMandatoryParameterAs<ProtocolVersion>(parameters, "v"); res.version = getMandatoryParameterAs<ProtocolVersion>(parameters, "v");
@@ -399,7 +402,7 @@ namespace lms::api::subsonic
RequestContext SubsonicResource::buildRequestContext(const Wt::Http::Request& request) RequestContext SubsonicResource::buildRequestContext(const Wt::Http::Request& request)
{ {
const Wt::Http::ParameterMap& parameters{ request.getParameterMap() }; const Wt::Http::ParameterMap& parameters{ request.getParameterMap() };
const ClientInfo clientInfo{ getClientInfo(parameters) }; const ClientInfo clientInfo{ getClientInfo(request) };
const db::UserId userId{ authenticateUser(request, clientInfo) }; const db::UserId userId{ authenticateUser(request, clientInfo) };
bool enableOpenSubsonic{ _openSubsonicDisabledClients.find(clientInfo.name) == std::cend(_openSubsonicDisabledClients) }; bool enableOpenSubsonic{ _openSubsonicDisabledClients.find(clientInfo.name) == std::cend(_openSubsonicDisabledClients) };
bool enableDefaultCover{ _defaultCoverClients.find(clientInfo.name) != std::cend(_openSubsonicDisabledClients) }; bool enableDefaultCover{ _defaultCoverClients.find(clientInfo.name) != std::cend(_openSubsonicDisabledClients) };
+1 -1
View File
@@ -47,7 +47,7 @@ namespace lms::api::subsonic
ProtocolVersion getServerProtocolVersion(const std::string& clientName) const; ProtocolVersion getServerProtocolVersion(const std::string& clientName) const;
static void checkProtocolVersion(ProtocolVersion client, ProtocolVersion server); static void checkProtocolVersion(ProtocolVersion client, ProtocolVersion server);
ClientInfo getClientInfo(const Wt::Http::ParameterMap& parameters); ClientInfo getClientInfo(const Wt::Http::Request& request);
RequestContext buildRequestContext(const Wt::Http::Request& request); RequestContext buildRequestContext(const Wt::Http::Request& request);
db::UserId authenticateUser(const Wt::Http::Request& request, const ClientInfo& clientInfo); db::UserId authenticateUser(const Wt::Http::Request& request, const ClientInfo& clientInfo);
+281 -57
View File
@@ -19,6 +19,11 @@
#include "Searching.hpp" #include "Searching.hpp"
#include <chrono>
#include <mutex>
#include <map>
#include "core/Random.hpp"
#include "database/Artist.hpp" #include "database/Artist.hpp"
#include "database/Release.hpp" #include "database/Release.hpp"
#include "database/Session.hpp" #include "database/Session.hpp"
@@ -34,12 +39,280 @@ namespace lms::api::subsonic
{ {
using namespace db; using namespace db;
namespace
{
// Search endpoints can be used to scan/sync the database
// This class is used to keep track of the current scans, in order to retrieve the last objectId
// to speed up the query of the following range (avoid the 'offset' cost)
template <typename ObjectId>
class ScanTracker
{
public:
struct ScanInfo
{
std::string clientAddress;
std::string clientName;
std::string userName;
MediaLibraryId library;
std::size_t offset{};
auto operator<=>(const ScanInfo&) const = default;
};
ObjectId extractLastRetrievedObjectId(const ScanInfo& info);
void setObjectId(const ScanInfo& info, ObjectId lastRetrievedId);
private:
using ClockType = std::chrono::steady_clock;
struct Entry
{
ClockType::time_point timePoint;
ObjectId objectId;
};
static constexpr std::size_t maxScanCount{ 50 };
static constexpr ClockType::duration maxEntryDuration{ std::chrono::seconds{30} };
std::mutex _mutex;
std::map<ScanInfo, Entry> _ongoingScans;
};
template<typename ObjectId>
ObjectId ScanTracker<ObjectId>::extractLastRetrievedObjectId(const ScanInfo& scanInfo)
{
ObjectId res;
{
const std::scoped_lock lock{ _mutex };
auto it{ _ongoingScans.find(scanInfo) };
if (it != _ongoingScans.end())
{
res = it->second.objectId;
_ongoingScans.erase(it);
}
}
return res;
}
template<typename ObjectId>
void ScanTracker<ObjectId>::setObjectId(const ScanInfo& scanInfo, ObjectId lastRetrievedId)
{
const ClockType::time_point now{ ClockType::now() };
const std::scoped_lock lock{ _mutex };
// clean outdated scan entries; we do this to not have to flush everything each time we add/remove entries in the database
std::erase_if(_ongoingScans, [&](const auto& entry) { return now > entry.second.timePoint + maxEntryDuration; });
// prevent the cache size from going out of control
if (_ongoingScans.size() == maxScanCount)
_ongoingScans.erase(core::random::pickRandom(_ongoingScans));
_ongoingScans[scanInfo] = { now, lastRetrievedId };
}
void findRequestedArtists(RequestContext& context, bool id3, const std::vector<std::string_view>& keywords, MediaLibraryId mediaLibrary, const User::pointer& user, Response::Node& searchResultNode)
{
static ScanTracker<ArtistId> currentScansInProgress;
const std::size_t artistCount{ getParameterAs<std::size_t>(context.parameters, "artistCount").value_or(20) };
if (artistCount == 0)
return;
if (artistCount > defaultMaxCountSize)
throw ParameterValueTooHighGenericError{ "artistCount", defaultMaxCountSize };
const std::size_t artistOffset{ getParameterAs<std::size_t>(context.parameters, "artistOffset").value_or(0) };
ArtistId lastRetrievedId;
auto findArtists{ [&]
{
Artist::FindParameters params;
params.setKeywords(keywords);
params.setRange(Range{ artistOffset, artistCount });
params.setMediaLibrary(mediaLibrary);
Artist::find(context.dbSession, params, [&](const Artist::pointer& artist)
{
searchResultNode.addArrayChild("artist", createArtistNode(context, artist, user, id3));
lastRetrievedId = artist->getId();
});
} };
if (!keywords.empty())
{
findArtists();
}
else
{
ScanTracker<ArtistId>::ScanInfo scanInfo
{
.clientAddress = context.clientInfo.ipAddress,
.clientName = context.clientInfo.name,
.userName = context.clientInfo.user,
.library = mediaLibrary,
.offset = artistOffset
};
if (ArtistId cachedLastRetrievedId{ currentScansInProgress.extractLastRetrievedObjectId(scanInfo) }; cachedLastRetrievedId.isValid())
{
Artist::find(context.dbSession, cachedLastRetrievedId, artistCount, [&](const Artist::pointer& artist)
{
searchResultNode.addArrayChild("artist", createArtistNode(context, artist, user, id3));
}, mediaLibrary);
lastRetrievedId = cachedLastRetrievedId;
}
else
{
findArtists();
}
if (lastRetrievedId.isValid())
{
scanInfo.offset = artistOffset + artistCount;
currentScansInProgress.setObjectId(scanInfo, lastRetrievedId);
}
}
}
void findRequestedAlbums(RequestContext& context, bool id3, const std::vector<std::string_view>& keywords, MediaLibraryId mediaLibrary, const User::pointer& user, Response::Node& searchResultNode)
{
static ScanTracker<ReleaseId> currentScansInProgress;
const std::size_t albumCount{ getParameterAs<std::size_t>(context.parameters, "albumCount").value_or(20) };
if (albumCount == 0)
return;
if (albumCount > defaultMaxCountSize)
throw ParameterValueTooHighGenericError{ "albumCount", defaultMaxCountSize };
const std::size_t albumOffset{ getParameterAs<std::size_t>(context.parameters, "albumOffset").value_or(0) };
ReleaseId lastRetrievedId;
auto findReleases{ [&]
{
Release::FindParameters params;
params.setKeywords(keywords);
params.setRange(Range{ albumOffset, albumCount });
params.setMediaLibrary(mediaLibrary);
Release::find(context.dbSession, params, [&](const Release::pointer& release)
{
searchResultNode.addArrayChild("album", createAlbumNode(context, release, user, id3));
lastRetrievedId = release->getId();
});
} };
if (!keywords.empty())
{
findReleases();
}
else
{
ScanTracker<ReleaseId>::ScanInfo scanInfo
{
.clientAddress = context.clientInfo.ipAddress,
.clientName = context.clientInfo.name,
.userName = context.clientInfo.user,
.library = mediaLibrary,
.offset = albumOffset
};
if (ReleaseId cachedLastRetrievedId{ currentScansInProgress.extractLastRetrievedObjectId(scanInfo) }; cachedLastRetrievedId.isValid())
{
Release::find(context.dbSession, cachedLastRetrievedId, albumCount, [&](const Release::pointer& release)
{
searchResultNode.addArrayChild("album", createAlbumNode(context, release, user, id3));
}, mediaLibrary);
lastRetrievedId = cachedLastRetrievedId;
}
else
{
findReleases();
}
if (lastRetrievedId.isValid())
{
scanInfo.offset = albumOffset + albumCount;
currentScansInProgress.setObjectId(scanInfo, lastRetrievedId);
}
}
}
void findRequestedTracks(RequestContext& context, const std::vector<std::string_view>& keywords, MediaLibraryId mediaLibrary, const User::pointer& user, Response::Node& searchResultNode)
{
static ScanTracker<TrackId> currentScansInProgress;
const std::size_t songCount{ getParameterAs<std::size_t>(context.parameters, "songCount").value_or(20) };
if (songCount == 0)
return;
if (songCount > defaultMaxCountSize)
throw ParameterValueTooHighGenericError{ "songCount", defaultMaxCountSize };
const std::size_t songOffset{ getParameterAs<std::size_t>(context.parameters, "songOffset").value_or(0) };
TrackId lastRetrievedId;
auto findTracks{ [&]
{
Track::FindParameters params;
params.setKeywords(keywords);
params.setRange(Range{ songOffset, songCount });
params.setMediaLibrary(mediaLibrary);
Track::find(context.dbSession, params, [&](const Track::pointer& track)
{
searchResultNode.addArrayChild("song", createSongNode(context, track, user));
lastRetrievedId = track->getId();
});
} };
if (!keywords.empty())
{
findTracks();
}
else
{
ScanTracker<TrackId>::ScanInfo scanInfo
{
.clientAddress = context.clientInfo.ipAddress,
.clientName = context.clientInfo.name,
.userName = context.clientInfo.user,
.library = mediaLibrary,
.offset = songOffset
};
if (TrackId cachedLastRetrievedId{ currentScansInProgress.extractLastRetrievedObjectId(scanInfo) }; cachedLastRetrievedId.isValid())
{
Track::find(context.dbSession, cachedLastRetrievedId, songCount, [&](const Track::pointer& track)
{
searchResultNode.addArrayChild("song", createSongNode(context, track, user));
}, mediaLibrary);
lastRetrievedId = cachedLastRetrievedId;
}
else
{
findTracks();
}
if (lastRetrievedId.isValid())
{
scanInfo.offset = songOffset + songCount;
currentScansInProgress.setObjectId(scanInfo, lastRetrievedId);
}
}
}
}
namespace namespace
{ {
Response handleSearchRequestCommon(RequestContext& context, bool id3) Response handleSearchRequestCommon(RequestContext& context, bool id3)
{ {
// Mandatory params // Mandatory params
std::string queryString{ getMandatoryParameterAs<std::string>(context.parameters, "query") }; const std::string queryString{ getMandatoryParameterAs<std::string>(context.parameters, "query") };
std::string_view query{ queryString }; std::string_view query{ queryString };
// Optional params // Optional params
@@ -49,25 +322,12 @@ namespace lms::api::subsonic
if (context.clientInfo.name == "Symfonium") if (context.clientInfo.name == "Symfonium")
query = core::stringUtils::stringTrim(query, "\""); query = core::stringUtils::stringTrim(query, "\"");
std::vector<std::string_view> keywords{ core::stringUtils::splitString(query, ' ') }; std::vector<std::string_view> keywords;
if (!query.empty())
// Optional params keywords = core::stringUtils::splitString(query, ' ');
std::size_t artistCount{ getParameterAs<std::size_t>(context.parameters, "artistCount").value_or(20) };
std::size_t artistOffset{ getParameterAs<std::size_t>(context.parameters, "artistOffset").value_or(0) };
std::size_t albumCount{ getParameterAs<std::size_t>(context.parameters, "albumCount").value_or(20) };
std::size_t albumOffset{ getParameterAs<std::size_t>(context.parameters, "albumOffset").value_or(0) };
std::size_t songCount{ getParameterAs<std::size_t>(context.parameters, "songCount").value_or(20) };
std::size_t songOffset{ getParameterAs<std::size_t>(context.parameters, "songOffset").value_or(0) };
if (artistCount > defaultMaxCountSize)
throw ParameterValueTooHighGenericError{ "artistCount", defaultMaxCountSize };
else if (albumCount > defaultMaxCountSize)
throw ParameterValueTooHighGenericError{ "albumCount", defaultMaxCountSize };
else if (songCount > defaultMaxCountSize)
throw ParameterValueTooHighGenericError{ "songCount", defaultMaxCountSize };
Response response{ Response::createOkResponse(context.serverProtocolVersion) }; Response response{ Response::createOkResponse(context.serverProtocolVersion) };
Response::Node& searchResult2Node{ response.createNode(id3 ? "searchResult3" : "searchResult2") }; Response::Node& searchResultNode{ response.createNode(id3 ? "searchResult3" : "searchResult2") };
auto transaction{ context.dbSession.createReadTransaction() }; auto transaction{ context.dbSession.createReadTransaction() };
@@ -75,44 +335,9 @@ namespace lms::api::subsonic
if (!user) if (!user)
throw UserNotAuthorizedError{}; throw UserNotAuthorizedError{};
if (artistCount > 0) findRequestedArtists(context, id3, keywords, mediaLibrary, user, searchResultNode);
{ findRequestedAlbums(context, id3, keywords, mediaLibrary, user, searchResultNode);
Artist::FindParameters params; findRequestedTracks(context, keywords, mediaLibrary, user, searchResultNode);
params.setKeywords(keywords);
params.setRange(Range{ artistOffset, artistCount });
params.setMediaLibrary(mediaLibrary);
Artist::find(context.dbSession, params, [&](const Artist::pointer& artist)
{
searchResult2Node.addArrayChild("artist", createArtistNode(context, artist, user, id3));
});
}
if (albumCount > 0)
{
Release::FindParameters params;
params.setKeywords(keywords);
params.setRange(Range{ albumOffset, albumCount });
params.setMediaLibrary(mediaLibrary);
Release::find(context.dbSession, params, [&](const Release::pointer& release)
{
searchResult2Node.addArrayChild("album", createAlbumNode(context, release, user, id3));
});
}
if (songCount > 0)
{
Track::FindParameters params;
params.setKeywords(keywords);
params.setRange(Range{ songOffset, songCount });
params.setMediaLibrary(mediaLibrary);
Track::find(context.dbSession, params, [&](const Track::pointer& track)
{
searchResult2Node.addArrayChild("song", createSongNode(context, track, user));
});
}
return response; return response;
} }
@@ -127,5 +352,4 @@ namespace lms::api::subsonic
{ {
return handleSearchRequestCommon(context, true /* id3 */); return handleSearchRequestCommon(context, true /* id3 */);
} }
} }