Put all directories and images in database, use the info to associate an image to each artist

This commit is contained in:
emeric
2024-07-06 13:53:41 +02:00
parent 45c8b82865
commit d1324c1c3a
59 changed files with 2023 additions and 960 deletions
+1
View File
@@ -3,6 +3,7 @@ add_library(lmsdatabase SHARED
impl/AuthToken.cpp
impl/Cluster.cpp
impl/Db.cpp
impl/Directory.cpp
impl/Image.cpp
impl/Listen.cpp
impl/MediaLibrary.cpp
+9 -3
View File
@@ -22,6 +22,7 @@
#include "core/ILogger.hpp"
#include "database/Cluster.hpp"
#include "database/Directory.hpp"
#include "database/Image.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
@@ -222,19 +223,19 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQueryResults<Artist::pointer>(session.getDboSession()->find<Artist>().where("name = ?").bind(std::string{ name, 0, _maxNameLength }).orderBy("LENGTH(mbid) DESC")); // put mbid entries first
return utils::fetchQueryResults<Artist::pointer>(session.getDboSession()->query<Wt::Dbo::ptr<Artist>>("SELECT a FROM artist a").where("a.name = ?").bind(std::string{ name, 0, _maxNameLength }).orderBy("LENGTH(a.mbid) DESC")); // put mbid entries first
}
Artist::pointer Artist::find(Session& session, const core::UUID& mbid)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Artist>().where("mbid = ?").bind(std::string{ mbid.getAsString() }));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Artist>>("SELECT a FROM artist a").where("a.mbid = ?").bind(std::string{ mbid.getAsString() }));
}
Artist::pointer Artist::find(Session& session, ArtistId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Artist>().where("id = ?").bind(id));
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)
@@ -364,4 +365,9 @@ namespace lms::db
_sortName = std::string(sortName, 0, _maxNameLength);
}
void Artist::setImage(ObjectPtr<Image> image)
{
_image = getDboPtr(image);
}
} // namespace lms::db
+1
View File
@@ -20,6 +20,7 @@
#include "database/Cluster.hpp"
#include "database/Artist.hpp"
#include "database/Directory.hpp"
#include "database/MediaLibrary.hpp"
#include "database/Release.hpp"
#include "database/ScanSettings.hpp"
+162
View File
@@ -0,0 +1,162 @@
/*
* 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 "database/Directory.hpp"
#include "database/Session.hpp"
#include "IdTypeTraits.hpp"
#include "PathTraits.hpp"
#include "Utils.hpp"
namespace lms::db
{
namespace
{
Wt::Dbo::Query<Wt::Dbo::ptr<Directory>> createQuery(Session& session, const Directory::FindParameters& params)
{
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<Directory>>("SELECT d FROM directory d") };
if (params.artist.isValid())
{
query.join("track t ON t.directory_id = d.id")
.join("artist a ON a.id = t_a_l.artist_id")
.join("track_artist_link t_a_l ON t_a_l.track_id = t.id")
.where("a.id = ?")
.bind(params.artist);
if (!params.trackArtistLinkTypes.empty())
{
std::ostringstream oss;
bool first{ true };
for (TrackArtistLinkType linkType : params.trackArtistLinkTypes)
{
if (!first)
oss << " OR ";
oss << "t_a_l.type = ?";
query.bind(linkType);
first = false;
}
query.where(oss.str());
}
query.groupBy("d.id");
}
return query;
}
} // namespace
Directory::Directory(const std::filesystem::path& p)
{
setAbsolutePath(p);
}
Directory::pointer Directory::create(Session& session, const std::filesystem::path& p)
{
return session.getDboSession()->add(std::unique_ptr<Directory>{ new Directory{ p } });
}
std::size_t Directory::getCount(Session& session)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM directory"));
}
Directory::pointer Directory::find(Session& session, DirectoryId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Directory>>("SELECT d from directory d").where("d.id = ?").bind(id));
}
Directory::pointer Directory::find(Session& session, const std::filesystem::path& path)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Directory>>("SELECT d from directory d").where("d.absolute_path = ?").bind(path));
}
void Directory::find(Session& session, DirectoryId& lastRetrievedDirectory, std::size_t count, const std::function<void(const Directory::pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<Directory>>("SELECT d from directory d").orderBy("d.id").where("d.id > ?").bind(lastRetrievedDirectory).limit(static_cast<int>(count)) };
utils::forEachQueryResult(query, [&](const Directory::pointer& image) {
func(image);
lastRetrievedDirectory = image->getId();
});
}
void Directory::find(Session& session, const FindParameters& params, const std::function<void(const Directory::pointer&)>& func)
{
auto query{ createQuery(session, params) };
utils::forEachQueryResult(query, [&func](const Directory::pointer& dir) {
func(dir);
});
}
RangeResults<DirectoryId> Directory::findOrphanIds(Session& session, std::optional<Range> range)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<DirectoryId>("SELECT d.id FROM directory d") };
query.leftJoin("directory d_child ON d.id = d_child.parent_directory_id");
query.leftJoin("track t ON d.id = t.directory_id");
query.leftJoin("image i ON d.id = i.directory_id");
query.where("d_child.id IS NULL");
query.where("t.directory_id IS NULL");
query.where("i.directory_id IS NULL");
return utils::execRangeQuery<DirectoryId>(query, range);
}
void Directory::setAbsolutePath(const std::filesystem::path& p)
{
assert(p.is_absolute());
if (!p.has_filename() && p.has_parent_path())
{
_absolutePath = p.parent_path();
_name = _absolutePath.filename();
}
else
{
_absolutePath = p;
_name = p.filename();
}
}
void Directory::setParent(ObjectPtr<Directory> parent)
{
#ifndef NDEBUG
if (parent)
{
assert(_absolutePath.has_parent_path());
assert(parent->getAbsolutePath() == _absolutePath.parent_path());
}
#endif
_parent = getDboPtr(parent);
}
} // namespace lms::db
+62 -3
View File
@@ -22,6 +22,7 @@
#include <Wt/Dbo/WtSqlTraits.h>
#include "database/Artist.hpp"
#include "database/Directory.hpp"
#include "database/Session.hpp"
#include "IdTypeTraits.hpp"
@@ -30,9 +31,24 @@
namespace lms::db
{
Image::Image(const std::filesystem::path& p)
: _path{ p }
namespace
{
Wt::Dbo::Query<Wt::Dbo::ptr<Image>> createQuery(Session& session, const Image::FindParameters& params)
{
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<Image>>("SELECT i FROM image i") };
if (params.directory.isValid())
query.where("i.directory_id = ?").bind(params.directory);
if (!params.fileStem.empty())
query.where("i.stem = ?").bind(params.fileStem);
return query;
}
} // namespace
Image::Image(const std::filesystem::path& p)
{
setAbsoluteFilePath(p);
}
Image::pointer Image::create(Session& session, const std::filesystem::path& p)
@@ -51,6 +67,49 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Image>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Image>>("SELECT i from image i").where("i.id = ?").bind(id));
}
Image::pointer Image::find(Session& session, const std::filesystem::path& path)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Image>>("SELECT i from image i").where("i.absolute_file_path = ?").bind(path));
}
void Image::find(Session& session, ImageId& lastRetrievedImage, std::size_t count, const std::function<void(const Image::pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<Image>>("SELECT i from image i").orderBy("i.id").where("i.id > ?").bind(lastRetrievedImage).limit(static_cast<int>(count)) };
utils::forEachQueryResult(query, [&](const Image::pointer& image) {
func(image);
lastRetrievedImage = image->getId();
});
}
RangeResults<Image::pointer> Image::find(Session& session, const FindParameters& params)
{
session.checkReadTransaction();
auto query{ createQuery(session, params) };
return utils::execRangeQuery<Image::pointer>(query, params.range);
}
void Image::find(Session& session, const FindParameters& params, const std::function<void(const Image::pointer&)>& func)
{
auto query{ createQuery(session, params) };
utils::forEachQueryResult(query, [&](const Image::pointer& image) {
func(image);
});
}
void Image::setAbsoluteFilePath(const std::filesystem::path& p)
{
assert(p.is_absolute());
_fileAbsolutePath = p;
_fileStem = p.stem().string();
}
} // namespace lms::db
+1 -1
View File
@@ -210,7 +210,7 @@ namespace lms::db
Listen::pointer Listen::find(Session& session, ListenId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Listen>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Listen>>("SELECT l from listen l").where("l.id = ?").bind(id));
}
RangeResults<ListenId> Listen::find(Session& session, const FindParameters& parameters)
+135 -1
View File
@@ -35,7 +35,7 @@ namespace lms::db
{
namespace
{
static constexpr Version LMS_DATABASE_VERSION{ 60 };
static constexpr Version LMS_DATABASE_VERSION{ 61 };
}
VersionInfo::VersionInfo()
@@ -485,6 +485,7 @@ SELECT
"id" integer primary key autoincrement,
"version" integer not null,
"path" text not null,
"stem" text not null,
"file_last_write" text,
"file_size" integer not null,
"width" integer not null,
@@ -496,6 +497,138 @@ SELECT
// Just increment the scan version of the settings to make the next scheduled scan rescan everything
session.getDboSession()->execute("UPDATE scan_settings SET scan_version = scan_version + 1");
}
void migrateFromV60(Session& session)
{
// Dedicated directory table
session.getDboSession()->execute(R"(CREATE TABLE IF NOT EXISTS "directory" (
"id" integer primary key autoincrement,
"version" integer not null,
"absolute_path" text not null,
"name" text not null,
"parent_directory_id" bigint,
constraint "fk_directory_directory" foreign key ("parent_directory_id") references "directory" ("id") on delete cascade deferrable initially deferred
))");
// Add a ref in track, need to recreate a new table
session.getDboSession()->execute(R"(
CREATE TABLE IF NOT EXISTS "track_backup" (
"id" integer primary key autoincrement,
"version" integer not null,
"scan_version" integer not null,
"track_number" integer,
"disc_number" integer,
"total_track" integer,
"disc_subtitle" text not null,
"name" text not null,
"duration" integer,
"bitrate" integer not null,
"bits_per_sample" integer not null,
"channel_count" integer not null,
"sample_rate" integer not null,
"date" text,
"year" integer,
"original_date" text,
"original_year" integer,
"absolute_file_path" text not null,
"relative_file_path" text not null,
"file_size" bigint not null,
"file_last_write" text,
"file_added" text,
"has_cover" boolean not null,
"mbid" text not null,
"recording_mbid" text not null,
"copyright" text not null,
"copyright_url" text not null,
"track_replay_gain" real,
"release_replay_gain" real,
"artist_display_name" text not null,
"release_id" bigint,
"media_library_id" bigint,
"directory_id" bigint,
constraint "fk_track_release" foreign key ("release_id") references "release" ("id") on delete cascade deferrable initially deferred,
constraint "fk_track_media_library" foreign key ("media_library_id") references "media_library" ("id") on delete set null deferrable initially deferred,
constraint "fk_track_directory" foreign key ("directory_id") references "directory" ("id") on delete cascade deferrable initially deferred
))");
// Migrate data, with the new directory_id field set to null
session.getDboSession()->execute(R"(INSERT INTO track_backup
SELECT
id,
version,
scan_version,
track_number,
disc_number,
total_track,
disc_subtitle,
name,
duration,
bitrate,
bits_per_sample,
channel_count,
sample_rate,
date,
year,
original_date,
original_year,
absolute_file_path,
relative_file_path,
file_size,
file_last_write,
file_added,
has_cover,
mbid,
recording_mbid,
copyright,
copyright_url,
track_replay_gain,
release_replay_gain,
artist_display_name,
release_id,
media_library_id,
NULL
FROM track)");
session.getDboSession()->execute("DROP TABLE track");
session.getDboSession()->execute("ALTER TABLE track_backup RENAME TO track");
// Add a ref in image + rename path to absolute_file_path, need to recreate a new table
session.getDboSession()->execute(R"(
CREATE TABLE IF NOT EXISTS "image_backup" (
"id" integer primary key autoincrement,
"version" integer not null,
"absolute_file_path" text not null,
"stem" text not null,
"file_last_write" text,
"file_size" integer not null,
"width" integer not null,
"height" integer not null,
"artist_id" bigint,
"directory_id" bigint,
constraint "fk_image_artist" foreign key ("artist_id") references "artist" ("id") on delete cascade deferrable initially deferred,
constraint "fk_image_directory" foreign key ("directory_id") references "directory" ("id") on delete cascade deferrable initially deferred
))");
// Migrate data, with the new directory_id field set to null
session.getDboSession()->execute(R"(INSERT INTO image_backup
SELECT
id,
version,
path,
stem,
file_last_write,
file_size,
width,
height,
artist_id,
NULL
FROM image
)");
session.getDboSession()->execute("DROP TABLE image");
session.getDboSession()->execute("ALTER TABLE image_backup RENAME TO image");
// Just increment the scan version of the settings to make the next scheduled scan rescan everything
session.getDboSession()->execute("UPDATE scan_settings SET scan_version = scan_version + 1");
}
} // namespace
bool doDbMigration(Session& session)
@@ -534,6 +667,7 @@ SELECT
{ 57, migrateFromV57 },
{ 58, migrateFromV58 },
{ 59, migrateFromV59 },
{ 60, migrateFromV60 },
};
bool migrationPerformed{};
+4 -4
View File
@@ -226,14 +226,14 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<ReleaseType>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<ReleaseType>>("SELECT r_t from release_type r_t").where("r_t.id = ?").bind(id));
}
ReleaseType::pointer ReleaseType::find(Session& session, std::string_view name)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<ReleaseType>().where("name = ?").bind(name));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<ReleaseType>>("SELECT r_t from release_type r_t").where("r_t.name = ?").bind(name));
}
Release::Release(const std::string& name, const std::optional<core::UUID>& MBID)
@@ -258,14 +258,14 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Release>().where("mbid = ?").bind(mbid.getAsString()));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Release>>("SELECT r from release r").where("r.mbid = ?").bind(mbid.getAsString()));
}
Release::pointer Release::find(Session& session, ReleaseId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Release>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Release>>("SELECT r from release r").where("r.id = ?").bind(id));
}
bool Release::exists(Session& session, ReleaseId id)
+9
View File
@@ -26,6 +26,7 @@
#include "database/AuthToken.hpp"
#include "database/Cluster.hpp"
#include "database/Db.hpp"
#include "database/Directory.hpp"
#include "database/Image.hpp"
#include "database/Listen.hpp"
#include "database/MediaLibrary.hpp"
@@ -93,6 +94,7 @@ namespace lms::db
_session.mapClass<AuthToken>("auth_token");
_session.mapClass<Cluster>("cluster");
_session.mapClass<ClusterType>("cluster_type");
_session.mapClass<Directory>("directory");
_session.mapClass<Image>("image");
_session.mapClass<Listen>("listen");
_session.mapClass<MediaLibrary>("media_library");
@@ -179,7 +181,14 @@ namespace lms::db
_session.execute("CREATE INDEX IF NOT EXISTS cluster_cluster_type_idx ON cluster(cluster_type_id)");
_session.execute("CREATE INDEX IF NOT EXISTS cluster_type_name_idx ON cluster_type(name)");
_session.execute("CREATE INDEX IF NOT EXISTS directory_id_idx ON directory(id)");
_session.execute("CREATE INDEX IF NOT EXISTS directory_path_idx ON directory(absolute_path)");
_session.execute("CREATE INDEX IF NOT EXISTS image_artist_idx ON image(artist_id)");
_session.execute("CREATE INDEX IF NOT EXISTS image_directory_idx ON image(directory_id)");
_session.execute("CREATE INDEX IF NOT EXISTS image_id_idx ON image(id)");
_session.execute("CREATE INDEX IF NOT EXISTS image_path_idx ON image(absolute_file_path)");
_session.execute("CREATE INDEX IF NOT EXISTS image_stem_idx ON image(stem)");
_session.execute("CREATE INDEX IF NOT EXISTS listen_backend_idx ON listen(backend)");
_session.execute("CREATE INDEX IF NOT EXISTS listen_id_idx ON listen(id)");
+6 -5
View File
@@ -24,6 +24,7 @@
#include "core/ILogger.hpp"
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Directory.hpp"
#include "database/MediaLibrary.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
@@ -219,21 +220,21 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Track>().where("absolute_file_path = ?").bind(p.string()));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Track>>("SELECT t from track t").where("t.absolute_file_path = ?").bind(p.string()));
}
Track::pointer Track::find(Session& session, TrackId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<Track>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<Track>>("SELECT t from track t").where("t.id = ?").bind(id));
}
void Track::find(Session& session, TrackId& lastRetrievedTrack, std::size_t count, const std::function<void(const Track::pointer&)>& func, MediaLibraryId library)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->find<Track>().orderBy("id").where("id > ?").bind(lastRetrievedTrack).limit(static_cast<int>(count)) };
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<Track>>("SELECT t from track t").orderBy("t.id").where("t.id > ?").bind(lastRetrievedTrack).limit(static_cast<int>(count)) };
if (library.isValid())
query.where("media_library_id = ?").bind(library);
@@ -255,14 +256,14 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQueryResults<Track::pointer>(session.getDboSession()->find<Track>().where("mbid = ?").bind(mbid.getAsString()));
return utils::fetchQueryResults<Track::pointer>(session.getDboSession()->query<Wt::Dbo::ptr<Track>>("SELECT t from track t").where("t.mbid = ?").bind(mbid.getAsString()));
}
std::vector<Track::pointer> Track::findByRecordingMBID(Session& session, const core::UUID& mbid)
{
session.checkReadTransaction();
return utils::fetchQueryResults<Track::pointer>(session.getDboSession()->find<Track>().where("recording_mbid = ?").bind(mbid.getAsString()));
return utils::fetchQueryResults<Track::pointer>(session.getDboSession()->query<Wt::Dbo::ptr<Track>>("SELECT t from track t").where("t.recording_mbid = ?").bind(mbid.getAsString()));
}
RangeResults<TrackId> Track::findIdsTrackMBIDDuplicates(Session& session, std::optional<Range> range)
+1
View File
@@ -23,6 +23,7 @@
#include <boost/property_tree/ptree.hpp>
#include "core/ILogger.hpp"
#include "database/Directory.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
+2 -2
View File
@@ -136,7 +136,7 @@ namespace lms::db
session.checkReadTransaction();
assert(userId.isValid());
return utils::fetchQuerySingleResult(session.getDboSession()->find<TrackList>().where("name = ?").bind(name).where("type = ?").bind(type).where("user_id = ?").bind(userId));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<TrackList>>("select t_l from tracklist t_l").where("t_l.name = ?").bind(name).where("t_l.type = ?").bind(type).where("t_l.user_id = ?").bind(userId));
}
RangeResults<TrackListId> TrackList::find(Session& session, const FindParameters& params)
@@ -157,7 +157,7 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<TrackList>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<TrackList>>("select t_l from tracklist t_l").where("t_l.id = ?").bind(id));
}
bool TrackList::isEmpty() const
+3 -3
View File
@@ -78,17 +78,17 @@ namespace lms::db
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->find<User>().where("type = ?").bind(UserType::DEMO));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<User>>("SELECT u from user u").where("u.type = ?").bind(UserType::DEMO));
}
User::pointer User::find(Session& session, UserId id)
{
return utils::fetchQuerySingleResult(session.getDboSession()->find<User>().where("id = ?").bind(id));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<User>>("SELECT u from user u").where("u.id = ?").bind(id));
}
User::pointer User::find(Session& session, std::string_view name)
{
return utils::fetchQuerySingleResult(session.getDboSession()->find<User>().where("login_name = ?").bind(name));
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<User>>("SELECT u from user u").where("u.login_name = ?").bind(name));
}
void User::setSubsonicDefaultTranscodingOutputBitrate(Bitrate bitrate)
@@ -153,6 +153,7 @@ namespace lms::db
void setName(std::string_view name) { _name = name; }
void setMBID(const std::optional<core::UUID>& mbid) { _MBID = mbid ? mbid->getAsString() : ""; }
void setSortName(const std::string& sortName);
void setImage(ObjectPtr<Image> image);
template<class Action>
void persist(Action& a)
@@ -0,0 +1,97 @@
/*
* 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/>.
*/
#pragma once
#include <filesystem>
#include <functional>
#include <Wt/Dbo/Dbo.h>
#include "core/EnumSet.hpp"
#include "database/ArtistId.hpp"
#include "database/DirectoryId.hpp"
#include "database/Object.hpp"
#include "database/Types.hpp"
namespace lms::db
{
class Session;
class Directory final : public Object<Directory, DirectoryId>
{
public:
Directory() = default;
struct FindParameters
{
std::optional<Range> range;
ArtistId artist; // only tracks that involve this artist
core::EnumSet<TrackArtistLinkType> trackArtistLinkTypes; // and for these link types
FindParameters& setRange(std::optional<Range> _range)
{
range = _range;
return *this;
}
FindParameters& setArtist(ArtistId _artist, core::EnumSet<TrackArtistLinkType> _trackArtistLinkTypes = {})
{
artist = _artist;
trackArtistLinkTypes = _trackArtistLinkTypes;
return *this;
}
};
// find
static std::size_t getCount(Session& session);
static pointer find(Session& session, DirectoryId id);
static pointer find(Session& session, const std::filesystem::path& path);
static void find(Session& session, DirectoryId& lastRetrievedDirectory, std::size_t count, const std::function<void(const Directory::pointer&)>& func);
static void find(Session& session, const FindParameters& parameters, const std::function<void(const Directory::pointer&)>& func);
static RangeResults<DirectoryId> findOrphanIds(Session& session, std::optional<Range> range = std::nullopt);
// getters
const std::filesystem::path& getAbsolutePath() const { return _absolutePath; }
std::string_view getName() const { return _name; }
ObjectPtr<Directory> getParent() const { return _parent; }
// setters
void setAbsolutePath(const std::filesystem::path& p);
void setParent(ObjectPtr<Directory> parent);
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _absolutePath, "absolute_path");
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::belongsTo(a, _parent, "parent_directory", Wt::Dbo::OnDeleteCascade);
}
private:
friend class Session;
Directory(const std::filesystem::path& p);
static pointer create(Session& session, const std::filesystem::path& p);
std::filesystem::path _absolutePath;
std::string _name;
Wt::Dbo::ptr<Directory> _parent;
};
} // namespace lms::db
@@ -0,0 +1,24 @@
/*
* 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/>.
*/
#pragma once
#include "database/IdType.hpp"
LMS_DECLARE_IDTYPE(DirectoryId)
+41 -4
View File
@@ -20,17 +20,21 @@
#pragma once
#include <filesystem>
#include <functional>
#include <Wt/Dbo/Dbo.h>
#include <Wt/WDateTime.h>
#include "database/ArtistId.hpp"
#include "database/DirectoryId.hpp"
#include "database/ImageId.hpp"
#include "database/Object.hpp"
#include "database/Types.hpp"
namespace lms::db
{
class Artist;
class Directory;
class Session;
class Image final : public Object<Image, ImageId>
@@ -38,29 +42,59 @@ namespace lms::db
public:
Image() = default;
struct FindParameters
{
std::optional<Range> range;
std::string fileStem; // if set, images with this file stem
DirectoryId directory; // if set, images in this directory
FindParameters& setRange(std::optional<Range> _range)
{
range = _range;
return *this;
}
FindParameters& setFileStem(std::string_view _fileStem)
{
fileStem = _fileStem;
return *this;
}
FindParameters& setDirectory(DirectoryId _directory)
{
directory = _directory;
return *this;
}
};
// find
static std::size_t getCount(Session& session);
static pointer find(Session& session, ImageId id);
static pointer find(Session& session, const std::filesystem::path& file);
static RangeResults<pointer> find(Session& session, const FindParameters& params);
static void find(Session& session, const FindParameters& parameters, const std::function<void(const Image::pointer&)>& func);
static void find(Session& session, ImageId& lastRetrievedImage, std::size_t count, const std::function<void(const Image::pointer&)>& func);
// getters
const std::filesystem::path& getPath() const { return _path; }
const std::filesystem::path& getAbsoluteFilePath() const { return _fileAbsolutePath; }
std::string_view getFileStem() const { return _fileStem; }
const Wt::WDateTime& getLastWriteTime() const { return _fileLastWrite; }
std::size_t getFileSize() const { return _fileSize; }
std::size_t getWidth() const { return _width; }
std::size_t getHeight() const { return _height; }
// setters
void setPath(const std::filesystem::path& p) { _path = p; }
void setAbsoluteFilePath(const std::filesystem::path& p);
void setLastWriteTime(Wt::WDateTime time) { _fileLastWrite = time; }
void setFileSize(std::size_t fileSize) { _fileSize = fileSize; }
void setWidth(std::size_t width) { _width = width; }
void setHeight(std::size_t height) { _height = height; }
void setArtist(const ObjectPtr<Artist>& artist) { _artist = getDboPtr(artist); }
void setDirectory(const ObjectPtr<Directory>& directory) { _directory = getDboPtr(directory); }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _path, "path");
Wt::Dbo::field(a, _fileAbsolutePath, "absolute_file_path");
Wt::Dbo::field(a, _fileStem, "stem");
Wt::Dbo::field(a, _fileLastWrite, "file_last_write");
Wt::Dbo::field(a, _fileSize, "file_size");
@@ -68,6 +102,7 @@ namespace lms::db
Wt::Dbo::field(a, _height, "height");
Wt::Dbo::belongsTo(a, _artist, "artist", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _directory, "directory", Wt::Dbo::OnDeleteCascade);
}
private:
@@ -75,12 +110,14 @@ namespace lms::db
Image(const std::filesystem::path& p);
static pointer create(Session& session, const std::filesystem::path& p);
std::filesystem::path _path;
std::filesystem::path _fileAbsolutePath;
std::string _fileStem;
Wt::WDateTime _fileLastWrite;
int _fileSize{};
int _width{};
int _height{};
Wt::Dbo::ptr<Artist> _artist;
Wt::Dbo::ptr<Directory> _directory;
};
} // namespace lms::db
@@ -50,6 +50,7 @@ namespace lms::db
class Artist;
class Cluster;
class ClusterType;
class Directory;
class MediaLibrary;
class Release;
class Session;
@@ -225,6 +226,7 @@ namespace lms::db
void setRelease(ObjectPtr<Release> release) { _release = getDboPtr(release); }
void setClusters(const std::vector<ObjectPtr<Cluster>>& clusters);
void setMediaLibrary(ObjectPtr<MediaLibrary> mediaLibrary) { _mediaLibrary = getDboPtr(mediaLibrary); }
void setDirectory(ObjectPtr<Directory> directory) { _directory = getDboPtr(directory); }
std::size_t getScanVersion() const { return _scanVersion; }
std::optional<std::size_t> getTrackNumber() const { return _trackNumber; }
@@ -263,6 +265,7 @@ namespace lms::db
std::vector<ObjectPtr<Cluster>> getClusters() const;
std::vector<ClusterId> getClusterIds() const;
ObjectPtr<MediaLibrary> getMediaLibrary() const { return _mediaLibrary; }
ObjectPtr<Directory> getDirectory() const { return _directory; }
std::vector<std::vector<ObjectPtr<Cluster>>> getClusterGroups(const std::vector<ClusterTypeId>& clusterTypes, std::size_t size) const;
@@ -299,6 +302,7 @@ namespace lms::db
Wt::Dbo::field(a, _artistDisplayName, "artist_display_name");
Wt::Dbo::belongsTo(a, _release, "release", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _mediaLibrary, "media_library", Wt::Dbo::OnDeleteSetNull); // don't delete track on media library removal, we want to wait for the next scan to have a chance to migrate files
Wt::Dbo::belongsTo(a, _directory, "directory", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _trackArtistLinks, Wt::Dbo::ManyToOne, "track");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
}
@@ -342,6 +346,7 @@ namespace lms::db
Wt::Dbo::ptr<Release> _release;
Wt::Dbo::ptr<MediaLibrary> _mediaLibrary;
Wt::Dbo::ptr<Directory> _directory;
Wt::Dbo::collection<Wt::Dbo::ptr<TrackArtistLink>> _trackArtistLinks;
Wt::Dbo::collection<Wt::Dbo::ptr<Cluster>> _clusters;
};
+1
View File
@@ -4,6 +4,7 @@ add_executable(test-database
Cluster.cpp
Common.cpp
DatabaseTest.cpp
Directory.cpp
Image.cpp
Listen.cpp
Migration.cpp
+155
View File
@@ -0,0 +1,155 @@
/*
* 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 "Common.hpp"
#include "database/Directory.hpp"
namespace lms::db::tests
{
using ScopedDirectory = ScopedEntity<db::Directory>;
TEST_F(DatabaseFixture, Directory)
{
ScopedDirectory directory{ session, "/path/to/dir/" };
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(Directory::getCount(session), 1);
Directory::pointer dir{ Directory::find(session, directory.getId()) };
ASSERT_NE(dir, Directory::pointer{});
EXPECT_EQ(dir->getAbsolutePath(), "/path/to/dir");
EXPECT_EQ(dir->getName(), "dir");
}
{
auto transaction{ session.createWriteTransaction() };
Directory::pointer dir{ Directory::find(session, directory.getId()) };
ASSERT_NE(dir, Directory::pointer{});
dir.modify()->setAbsolutePath("/path/to/another/dir2");
}
{
auto transaction{ session.createReadTransaction() };
Directory::pointer dir{ Directory::find(session, directory.getId()) };
ASSERT_NE(dir, Directory::pointer{});
EXPECT_EQ(dir->getAbsolutePath(), "/path/to/another/dir2");
EXPECT_EQ(dir->getName(), "dir2");
}
{
auto transaction{ session.createWriteTransaction() };
Directory::pointer dir{ Directory::find(session, directory.getId()) };
ASSERT_NE(dir, Directory::pointer{});
dir.modify()->setAbsolutePath("/foo/");
}
{
auto transaction{ session.createReadTransaction() };
Directory::pointer dir{ Directory::find(session, directory.getId()) };
ASSERT_NE(dir, Directory::pointer{});
EXPECT_EQ(dir->getAbsolutePath(), "/foo");
EXPECT_EQ(dir->getName(), "foo");
}
{
auto transaction{ session.createWriteTransaction() };
Directory::pointer dir{ Directory::find(session, directory.getId()) };
ASSERT_NE(dir, Directory::pointer{});
dir.modify()->setAbsolutePath("/");
}
{
auto transaction{ session.createReadTransaction() };
Directory::pointer dir{ Directory::find(session, directory.getId()) };
ASSERT_NE(dir, Directory::pointer{});
EXPECT_EQ(dir->getAbsolutePath(), "/");
EXPECT_EQ(dir->getName(), "");
}
{
auto transaction{ session.createReadTransaction() };
Directory::pointer dir{ Directory::find(session, "/") };
ASSERT_NE(dir, Directory::pointer{});
EXPECT_EQ(dir->getId(), directory.getId());
}
}
TEST_F(DatabaseFixture, parent)
{
ScopedDirectory parent{ session, "/path/to/dir/" };
ScopedDirectory child{ session, "/path/to/dir/child" };
{
auto transaction{ session.createReadTransaction() };
auto dir{ child->getParent() };
EXPECT_EQ(dir, Directory::pointer{});
}
{
auto transaction{ session.createWriteTransaction() };
child.get().modify()->setParent(parent.lockAndGet());
}
{
auto transaction{ session.createReadTransaction() };
auto dir{ child->getParent() };
ASSERT_NE(dir, Directory::pointer{});
EXPECT_EQ(dir->getId(), parent.getId());
}
}
TEST_F(DatabaseFixture, Directory_orphaned)
{
ScopedDirectory parent{ session, "/path/to/dir/" };
ScopedDirectory child{ session, "/path/to/dir/child" };
{
auto transaction{ session.createReadTransaction() };
const auto directories{ Directory::findOrphanIds(session).results };
EXPECT_EQ(directories.size(), 2);
}
{
auto transaction{ session.createWriteTransaction() };
child.get().modify()->setParent(parent.lockAndGet());
}
{
auto transaction{ session.createReadTransaction() };
const auto directories{ Directory::findOrphanIds(session).results };
ASSERT_EQ(directories.size(), 1);
EXPECT_EQ(directories.front(), child.getId());
}
}
} // namespace lms::db::tests
+38 -3
View File
@@ -19,10 +19,12 @@
#include "Common.hpp"
#include "database/Directory.hpp"
#include "database/Image.hpp"
namespace lms::db::tests
{
using ScopedDirectory = ScopedEntity<db::Directory>;
using ScopedImage = ScopedEntity<db::Image>;
TEST_F(DatabaseFixture, Image)
@@ -35,7 +37,8 @@ namespace lms::db::tests
Image::pointer img{ Image::find(session, image.getId()) };
ASSERT_NE(img, Image::pointer{});
EXPECT_EQ(img->getPath(), "/path/to/image");
EXPECT_EQ(img->getAbsoluteFilePath(), "/path/to/image");
EXPECT_EQ(img->getFileStem(), "image");
EXPECT_EQ(img->getWidth(), 0);
EXPECT_EQ(img->getHeight(), 0);
EXPECT_EQ(img->getFileSize(), 0);
@@ -46,7 +49,7 @@ namespace lms::db::tests
Image::pointer img{ Image::find(session, image.getId()) };
ASSERT_NE(img, Image::pointer{});
img.modify()->setPath("/path/to/another/image");
img.modify()->setAbsoluteFilePath("/path/to/another/image2");
img.modify()->setWidth(640);
img.modify()->setHeight(480);
img.modify()->setFileSize(1024 * 1024);
@@ -57,10 +60,42 @@ namespace lms::db::tests
Image::pointer img{ Image::find(session, image.getId()) };
ASSERT_NE(img, Image::pointer{});
EXPECT_EQ(img->getPath(), "/path/to/another/image");
EXPECT_EQ(img->getAbsoluteFilePath(), "/path/to/another/image2");
EXPECT_EQ(img->getFileStem(), "image2");
EXPECT_EQ(img->getWidth(), 640);
EXPECT_EQ(img->getHeight(), 480);
EXPECT_EQ(img->getFileSize(), 1024 * 1024);
}
{
auto transaction{ session.createReadTransaction() };
Image::pointer img{ Image::find(session, "/path/to/another/image2") };
ASSERT_NE(img, Image::pointer{});
EXPECT_EQ(img->getId(), image->getId());
}
}
TEST_F(DatabaseFixture, Image_inDirectory)
{
ScopedImage image{ session, "/path/to/image" };
ScopedDirectory directory{ session, "/path/to" };
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(Image::find(session, Image::FindParameters{}.setDirectory(directory.getId())).results.size(), 0);
}
{
auto transaction{ session.createWriteTransaction() };
image.get().modify()->setDirectory(directory.get());
}
{
auto transaction{ session.createReadTransaction() };
const auto results{ Image::find(session, Image::FindParameters{}.setDirectory(directory.getId())).results };
ASSERT_EQ(results.size(), 1);
EXPECT_EQ(results.front()->getId(), image.getId());
}
}
} // namespace lms::db::tests
+25
View File
@@ -18,8 +18,14 @@
*/
#include "Common.hpp"
#include "core/String.hpp"
#include "database/Db.hpp"
#include "database/Directory.hpp"
#include "database/Image.hpp"
#include "database/StarredArtist.hpp"
#include "database/StarredRelease.hpp"
#include "database/StarredTrack.hpp"
namespace lms::db::tests
{
@@ -318,5 +324,24 @@ VALUES
// Now perform full migration
db.getTLSSession().migrateSchemaIfNeeded();
// Now perform some dummy finds to ensure all fields are correctly mapped
{
auto transaction{ session.createReadTransaction() };
EXPECT_FALSE(Artist::find(session, ArtistId{}));
EXPECT_FALSE(Cluster::find(session, ClusterId{}));
EXPECT_FALSE(ClusterType::find(session, ClusterTypeId{}));
EXPECT_FALSE(Directory::find(session, DirectoryId{}));
EXPECT_FALSE(Image::find(session, ImageId{}));
EXPECT_FALSE(Listen::find(session, ListenId{}));
EXPECT_FALSE(Release::find(session, ReleaseId{}));
EXPECT_FALSE(StarredArtist::find(session, StarredArtistId{}));
EXPECT_FALSE(StarredRelease::find(session, StarredReleaseId{}));
EXPECT_FALSE(StarredTrack::find(session, StarredTrackId{}));
EXPECT_FALSE(Track::find(session, TrackId{}));
EXPECT_FALSE(TrackList::find(session, TrackListId{}));
EXPECT_FALSE(User::find(session, UserId{}));
}
}
} // namespace lms::db::tests