Merge branch 'new-json-parser' into develop
This commit is contained in:
@@ -32,11 +32,11 @@ namespace Database
|
|||||||
{
|
{
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
Wt::Dbo::Query<ClusterId> createQuery(Session& session, const Cluster::FindParameters& params)
|
Wt::Dbo::Query<Cluster::ClusterFindResult> createQuery(Session& session, const Cluster::FindParameters& params)
|
||||||
{
|
{
|
||||||
session.checkSharedLocked();
|
session.checkSharedLocked();
|
||||||
|
|
||||||
auto query{ session.getDboSession().query<ClusterId>("SELECT DISTINCT c.id FROM cluster c") };
|
auto query{ session.getDboSession().query<Cluster::ClusterFindResult>("SELECT DISTINCT c.id,c.name FROM cluster c") };
|
||||||
|
|
||||||
if (params.track.isValid() || params.release.isValid())
|
if (params.track.isValid() || params.release.isValid())
|
||||||
{
|
{
|
||||||
@@ -74,7 +74,7 @@ namespace Database
|
|||||||
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster");
|
return session.getDboSession().query<int>("SELECT COUNT(*) FROM cluster");
|
||||||
}
|
}
|
||||||
|
|
||||||
RangeResults<ClusterId> Cluster::find(Session& session, const FindParameters& params)
|
RangeResults<Cluster::ClusterFindResult> Cluster::find(Session& session, const FindParameters& params)
|
||||||
{
|
{
|
||||||
session.checkSharedLocked();
|
session.checkSharedLocked();
|
||||||
auto query{ createQuery(session, params) };
|
auto query{ createQuery(session, params) };
|
||||||
|
|||||||
@@ -26,73 +26,94 @@
|
|||||||
#include "services/database/User.hpp"
|
#include "services/database/User.hpp"
|
||||||
#include "utils/Logger.hpp"
|
#include "utils/Logger.hpp"
|
||||||
|
|
||||||
namespace Database {
|
namespace Database
|
||||||
|
|
||||||
// Session living class handling the database and the login
|
|
||||||
Db::Db(const std::filesystem::path& dbPath, std::size_t connectionCount)
|
|
||||||
{
|
{
|
||||||
LMS_LOG(DB, INFO) << "Creating connection pool on file " << dbPath.string();
|
namespace
|
||||||
|
{
|
||||||
|
class Connection : public Wt::Dbo::backend::Sqlite3
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Connection(const std::filesystem::path& dbPath)
|
||||||
|
: Wt::Dbo::backend::Sqlite3{ dbPath.string() }
|
||||||
|
, _dbPath{ dbPath }
|
||||||
|
{
|
||||||
|
prepare();
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<Wt::Dbo::backend::Sqlite3> connection {std::make_unique<Wt::Dbo::backend::Sqlite3>(dbPath.string())};
|
private:
|
||||||
// connection->setProperty("show-queries", "true");
|
Connection(const Connection&) = delete;
|
||||||
connection->executeSql("pragma journal_mode=WAL");
|
Connection& operator=(const Connection&) = delete;
|
||||||
connection->executeSql("pragma synchronous=normal");
|
|
||||||
|
|
||||||
auto connectionPool = std::make_unique<Wt::Dbo::FixedSqlConnectionPool>(std::move(connection), connectionCount);
|
std::unique_ptr<SqlConnection> clone() const override
|
||||||
connectionPool->setTimeout(std::chrono::seconds(10));
|
{
|
||||||
|
return std::make_unique<Connection>(_dbPath);
|
||||||
|
}
|
||||||
|
|
||||||
_connectionPool = std::move(connectionPool);
|
void prepare()
|
||||||
}
|
{
|
||||||
|
LMS_LOG(DB, DEBUG) << "Setting per-connection settings...";
|
||||||
|
executeSql("pragma journal_mode=WAL");
|
||||||
|
executeSql("pragma synchronous=normal");
|
||||||
|
executeSql("pragma analysis_limit=1000"); // to help make analyze command faster
|
||||||
|
LMS_LOG(DB, DEBUG) << "Setting per-connection settings done!";
|
||||||
|
}
|
||||||
|
|
||||||
Db::~Db()
|
std::filesystem::path _dbPath;
|
||||||
{
|
};
|
||||||
LMS_LOG(DB, DEBUG) << "Optimizing db...";
|
}
|
||||||
executeSql("pragma optimize");
|
|
||||||
LMS_LOG(DB, DEBUG) << "Optimizing db DONE";
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
// Session living class handling the database and the login
|
||||||
Db::executeSql(const std::string& sql)
|
Db::Db(const std::filesystem::path& dbPath, std::size_t connectionCount)
|
||||||
{
|
{
|
||||||
ScopedConnection connection {*_connectionPool};
|
LMS_LOG(DB, INFO) << "Creating connection pool on file " << dbPath.string();
|
||||||
connection->executeSql(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
Session&
|
auto connection{ std::make_unique<Connection>(dbPath.string()) };
|
||||||
Db::getTLSSession()
|
// connection->setProperty("show-queries", "true");
|
||||||
{
|
|
||||||
static thread_local Session* tlsSession {};
|
|
||||||
|
|
||||||
if (!tlsSession)
|
auto connectionPool{ std::make_unique<Wt::Dbo::FixedSqlConnectionPool>(std::move(connection), connectionCount) };
|
||||||
{
|
connectionPool->setTimeout(std::chrono::seconds{ 10 });
|
||||||
auto newSession {std::make_unique<Session>(*this)};
|
|
||||||
tlsSession = newSession.get();
|
|
||||||
|
|
||||||
{
|
_connectionPool = std::move(connectionPool);
|
||||||
std::scoped_lock lock {_tlsSessionsMutex};
|
}
|
||||||
_tlsSessions.push_back(std::move(newSession));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return *tlsSession;
|
void Db::executeSql(const std::string& sql)
|
||||||
}
|
{
|
||||||
|
ScopedConnection connection{ *_connectionPool };
|
||||||
|
connection->executeSql(sql);
|
||||||
|
}
|
||||||
|
|
||||||
Db::ScopedConnection::ScopedConnection(Wt::Dbo::SqlConnectionPool& pool)
|
Session& Db::getTLSSession()
|
||||||
: _connectionPool {pool}
|
{
|
||||||
, _connection {_connectionPool.getConnection()}
|
static thread_local Session* tlsSession{};
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
Db::ScopedConnection::~ScopedConnection()
|
if (!tlsSession)
|
||||||
{
|
{
|
||||||
_connectionPool.returnConnection(std::move(_connection));
|
auto newSession{ std::make_unique<Session>(*this) };
|
||||||
}
|
tlsSession = newSession.get();
|
||||||
|
|
||||||
Wt::Dbo::SqlConnection* Db::ScopedConnection::operator->() const
|
{
|
||||||
{
|
std::scoped_lock lock{ _tlsSessionsMutex };
|
||||||
return _connection.get();
|
_tlsSessions.push_back(std::move(newSession));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return *tlsSession;
|
||||||
|
}
|
||||||
|
|
||||||
|
Db::ScopedConnection::ScopedConnection(Wt::Dbo::SqlConnectionPool& pool)
|
||||||
|
: _connectionPool{ pool }
|
||||||
|
, _connection{ _connectionPool.getConnection() }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Db::ScopedConnection::~ScopedConnection()
|
||||||
|
{
|
||||||
|
_connectionPool.returnConnection(std::move(_connection));
|
||||||
|
}
|
||||||
|
|
||||||
|
Wt::Dbo::SqlConnection* Db::ScopedConnection::operator->() const
|
||||||
|
{
|
||||||
|
return _connection.get();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -654,7 +654,6 @@ CREATE TABLE IF NOT EXISTS "track_backup" (
|
|||||||
{41, migrateFromV41},
|
{41, migrateFromV41},
|
||||||
};
|
};
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
{
|
||||||
auto uniqueTransaction{ session.createUniqueTransaction() };
|
auto uniqueTransaction{ session.createUniqueTransaction() };
|
||||||
|
|
||||||
@@ -670,26 +669,24 @@ CREATE TABLE IF NOT EXISTS "track_backup" (
|
|||||||
throw LmsException{ outdatedMsg };
|
throw LmsException{ outdatedMsg };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (version == LMS_DATABASE_VERSION)
|
if (version > LMS_DATABASE_VERSION)
|
||||||
{
|
|
||||||
LMS_LOG(DB, DEBUG) << "Lms database version " << LMS_DATABASE_VERSION << ": up to date!";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else if (version > LMS_DATABASE_VERSION)
|
|
||||||
{
|
|
||||||
throw LmsException{ "Server binary outdated, please upgrade it to handle this database" };
|
throw LmsException{ "Server binary outdated, please upgrade it to handle this database" };
|
||||||
}
|
|
||||||
|
|
||||||
if (version < migrationFunctions.begin()->first)
|
if (version < migrationFunctions.begin()->first)
|
||||||
throw LmsException{ outdatedMsg };
|
throw LmsException{ outdatedMsg };
|
||||||
|
|
||||||
LMS_LOG(DB, INFO) << "Migrating database from version " << version << "...";
|
while (version < LMS_DATABASE_VERSION)
|
||||||
|
{
|
||||||
|
LMS_LOG(DB, INFO) << "Migrating database from version " << version << " to " << version + 1 << "...";
|
||||||
|
|
||||||
auto itMigrationFunc{ migrationFunctions.find(version) };
|
auto itMigrationFunc{ migrationFunctions.find(version) };
|
||||||
assert(itMigrationFunc != std::cend(migrationFunctions));
|
assert(itMigrationFunc != std::cend(migrationFunctions));
|
||||||
itMigrationFunc->second(session);
|
itMigrationFunc->second(session);
|
||||||
|
|
||||||
VersionInfo::get(session).modify()->setVersion(++version);
|
VersionInfo::get(session).modify()->setVersion(++version);
|
||||||
|
|
||||||
|
LMS_LOG(DB, INFO) << "Migration complete to version " << version;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,145 +46,145 @@
|
|||||||
namespace Database
|
namespace Database
|
||||||
{
|
{
|
||||||
|
|
||||||
Session::Session(Db& db)
|
Session::Session(Db& db)
|
||||||
: _db {db}
|
: _db{ db }
|
||||||
{
|
{
|
||||||
_session.setConnectionPool(_db.getConnectionPool());
|
_session.setConnectionPool(_db.getConnectionPool());
|
||||||
|
|
||||||
_session.mapClass<VersionInfo>("version_info");
|
_session.mapClass<VersionInfo>("version_info");
|
||||||
_session.mapClass<Artist>("artist");
|
_session.mapClass<Artist>("artist");
|
||||||
_session.mapClass<AuthToken>("auth_token");
|
_session.mapClass<AuthToken>("auth_token");
|
||||||
_session.mapClass<Cluster>("cluster");
|
_session.mapClass<Cluster>("cluster");
|
||||||
_session.mapClass<ClusterType>("cluster_type");
|
_session.mapClass<ClusterType>("cluster_type");
|
||||||
_session.mapClass<Listen>("listen");
|
_session.mapClass<Listen>("listen");
|
||||||
_session.mapClass<Release>("release");
|
_session.mapClass<Release>("release");
|
||||||
_session.mapClass<ScanSettings>("scan_settings");
|
_session.mapClass<ScanSettings>("scan_settings");
|
||||||
_session.mapClass<StarredArtist>("starred_artist");
|
_session.mapClass<StarredArtist>("starred_artist");
|
||||||
_session.mapClass<StarredRelease>("starred_release");
|
_session.mapClass<StarredRelease>("starred_release");
|
||||||
_session.mapClass<StarredTrack>("starred_track");
|
_session.mapClass<StarredTrack>("starred_track");
|
||||||
_session.mapClass<Track>("track");
|
_session.mapClass<Track>("track");
|
||||||
_session.mapClass<TrackBookmark>("track_bookmark");
|
_session.mapClass<TrackBookmark>("track_bookmark");
|
||||||
_session.mapClass<TrackArtistLink>("track_artist_link");
|
_session.mapClass<TrackArtistLink>("track_artist_link");
|
||||||
_session.mapClass<TrackFeatures>("track_features");
|
_session.mapClass<TrackFeatures>("track_features");
|
||||||
_session.mapClass<TrackList>("tracklist");
|
_session.mapClass<TrackList>("tracklist");
|
||||||
_session.mapClass<TrackListEntry>("tracklist_entry");
|
_session.mapClass<TrackListEntry>("tracklist_entry");
|
||||||
_session.mapClass<User>("user");
|
_session.mapClass<User>("user");
|
||||||
}
|
}
|
||||||
|
|
||||||
UniqueTransaction::UniqueTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session)
|
UniqueTransaction::UniqueTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session)
|
||||||
: _lock {mutex},
|
: _lock{ mutex },
|
||||||
_transaction {session}
|
_transaction{ session }
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedTransaction::SharedTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session)
|
SharedTransaction::SharedTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session)
|
||||||
: _lock {mutex},
|
: _lock{ mutex },
|
||||||
_transaction {session}
|
_transaction{ session }
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void Session::checkUniqueLocked()
|
||||||
Session::checkUniqueLocked()
|
{
|
||||||
{
|
assert(_db.getMutex().isUniqueLocked());
|
||||||
assert(_db.getMutex().isUniqueLocked());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void Session::checkSharedLocked()
|
||||||
Session::checkSharedLocked()
|
{
|
||||||
{
|
assert(_db.getMutex().isSharedLocked());
|
||||||
assert(_db.getMutex().isSharedLocked());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
UniqueTransaction
|
UniqueTransaction Session::createUniqueTransaction()
|
||||||
Session::createUniqueTransaction()
|
{
|
||||||
{
|
return UniqueTransaction{ _db.getMutex(), _session };
|
||||||
return UniqueTransaction {_db.getMutex(), _session};
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SharedTransaction
|
SharedTransaction Session::createSharedTransaction()
|
||||||
Session::createSharedTransaction()
|
{
|
||||||
{
|
return SharedTransaction{ _db.getMutex(), _session };
|
||||||
return SharedTransaction {_db.getMutex(), _session};
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void Session::prepareTables()
|
||||||
Session::prepareTables()
|
{
|
||||||
{
|
LMS_LOG(DB, INFO) << "Preparing tables...";
|
||||||
// Creation case
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_session.createTables();
|
|
||||||
|
|
||||||
LMS_LOG(DB, INFO) << "Tables created";
|
// Initial creation case
|
||||||
}
|
try
|
||||||
catch (Wt::Dbo::Exception& e)
|
{
|
||||||
{
|
_session.createTables();
|
||||||
LMS_LOG(DB, ERROR) << "Cannot create tables: " << e.what();
|
LMS_LOG(DB, INFO) << "Tables created";
|
||||||
}
|
}
|
||||||
|
catch (Wt::Dbo::Exception& e)
|
||||||
|
{
|
||||||
|
LMS_LOG(DB, DEBUG) << "Cannot create tables: " << e.what();
|
||||||
|
if (std::string_view{ e.what() }.find("already exists") == std::string_view::npos)
|
||||||
|
{
|
||||||
|
LMS_LOG(DB, ERROR) << "Cannot create tables: " << e.what();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Migration::doDbMigration(*this);
|
Migration::doDbMigration(*this);
|
||||||
|
|
||||||
// Indexes
|
// Indexes
|
||||||
{
|
{
|
||||||
auto uniqueTransaction {createUniqueTransaction()};
|
auto uniqueTransaction{ createUniqueTransaction() };
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS artist_name_idx ON artist(name)");
|
_session.execute("CREATE INDEX IF NOT EXISTS artist_name_idx ON artist(name)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS artist_sort_name_nocase_idx ON artist(sort_name COLLATE NOCASE)");
|
_session.execute("CREATE INDEX IF NOT EXISTS artist_sort_name_nocase_idx ON artist(sort_name COLLATE NOCASE)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS artist_mbid_idx ON artist(mbid)");
|
_session.execute("CREATE INDEX IF NOT EXISTS artist_mbid_idx ON artist(mbid)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS auth_token_user_idx ON auth_token(user_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS auth_token_user_idx ON auth_token(user_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS auth_token_expiry_idx ON auth_token(expiry)");
|
_session.execute("CREATE INDEX IF NOT EXISTS auth_token_expiry_idx ON auth_token(expiry)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS auth_token_value_idx ON auth_token(value)");
|
_session.execute("CREATE INDEX IF NOT EXISTS auth_token_value_idx ON auth_token(value)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS cluster_name_idx ON cluster(name)");
|
_session.execute("CREATE INDEX IF NOT EXISTS cluster_name_idx ON cluster(name)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS cluster_cluster_type_idx ON cluster(cluster_type_id)");
|
_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 cluster_type_name_idx ON cluster_type(name)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS release_name_idx ON release(name)");
|
_session.execute("CREATE INDEX IF NOT EXISTS release_name_idx ON release(name)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS release_name_nocase_idx ON release(name COLLATE NOCASE)");
|
_session.execute("CREATE INDEX IF NOT EXISTS release_name_nocase_idx ON release(name COLLATE NOCASE)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS release_mbid_idx ON release(mbid)");
|
_session.execute("CREATE INDEX IF NOT EXISTS release_mbid_idx ON release(mbid)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_file_last_write_idx ON track(file_last_write)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_file_last_write_idx ON track(file_last_write)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_path_idx ON track(file_path)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_path_idx ON track(file_path)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_name_idx ON track(name)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_name_idx ON track(name)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_name_nocase_idx ON track(name COLLATE NOCASE)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_name_nocase_idx ON track(name COLLATE NOCASE)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_mbid_idx ON track(mbid)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_mbid_idx ON track(mbid)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_recording_mbid_idx ON track(recording_mbid)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_recording_mbid_idx ON track(recording_mbid)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_release_idx ON track(release_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_release_idx ON track(release_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_date_idx ON track(date)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_date_idx ON track(date)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_original_date_idx ON track(original_date)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_original_date_idx ON track(original_date)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS tracklist_name_idx ON tracklist(name)");
|
_session.execute("CREATE INDEX IF NOT EXISTS tracklist_name_idx ON tracklist(name)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS tracklist_user_idx ON tracklist(user_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS tracklist_user_idx ON tracklist(user_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_features_track_idx ON track_features(track_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_features_track_idx ON track_features(track_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_artist_idx ON track_artist_link(artist_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_artist_idx ON track_artist_link(artist_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_track_idx ON track_artist_link(track_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_track_idx ON track_artist_link(track_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_type_idx ON track_artist_link(type)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_type_idx ON track_artist_link(type)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_artist_type_idx ON track_artist_link(artist_id,type)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_artist_link_artist_type_idx ON track_artist_link(artist_id,type)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_bookmark_user_idx ON track_bookmark(user_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_bookmark_user_idx ON track_bookmark(user_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS track_bookmark_user_track_idx ON track_bookmark(user_id,track_id)");
|
_session.execute("CREATE INDEX IF NOT EXISTS track_bookmark_user_track_idx ON track_bookmark(user_id,track_id)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS listen_scrobbler_idx ON listen(scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS listen_scrobbler_idx ON listen(scrobbler)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS listen_user_scrobbler_idx ON listen(user_id,scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS listen_user_scrobbler_idx ON listen(user_id,scrobbler)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS listen_user_track_scrobbler_date_time_idx ON listen(user_id,track_id,scrobbler,date_time)");
|
_session.execute("CREATE INDEX IF NOT EXISTS listen_user_track_scrobbler_date_time_idx ON listen(user_id,track_id,scrobbler,date_time)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS starred_artist_user_scrobbler_idx ON starred_artist(user_id,scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS starred_artist_user_scrobbler_idx ON starred_artist(user_id,scrobbler)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS starred_artist_artist_user_scrobbler_idx ON starred_artist(artist_id,user_id,scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS starred_artist_artist_user_scrobbler_idx ON starred_artist(artist_id,user_id,scrobbler)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS starred_release_user_scrobbler_idx ON starred_release(user_id,scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS starred_release_user_scrobbler_idx ON starred_release(user_id,scrobbler)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS starred_release_release_user_scrobbler_idx ON starred_release(release_id,user_id,scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS starred_release_release_user_scrobbler_idx ON starred_release(release_id,user_id,scrobbler)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS starred_track_user_scrobbler_idx ON starred_track(user_id,scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS starred_track_user_scrobbler_idx ON starred_track(user_id,scrobbler)");
|
||||||
_session.execute("CREATE INDEX IF NOT EXISTS starred_track_track_user_scrobbler_idx ON starred_track(track_id,user_id,scrobbler)");
|
_session.execute("CREATE INDEX IF NOT EXISTS starred_track_track_user_scrobbler_idx ON starred_track(track_id,user_id,scrobbler)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial settings tables
|
// Initial settings tables
|
||||||
{
|
{
|
||||||
auto uniqueTransaction {createUniqueTransaction()};
|
auto uniqueTransaction{ createUniqueTransaction() };
|
||||||
|
|
||||||
ScanSettings::init(*this);
|
ScanSettings::init(*this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void Session::analyze()
|
||||||
Session::optimize()
|
{
|
||||||
{
|
LMS_LOG(DB, INFO) << "Analyzing database...";
|
||||||
LMS_LOG(DB, DEBUG) << "Optimizing db...";
|
{
|
||||||
{
|
auto uniqueTransaction{ createUniqueTransaction() };
|
||||||
auto uniqueTransaction {createUniqueTransaction()};
|
_session.execute("ANALYZE");
|
||||||
_session.execute("ANALYZE");
|
}
|
||||||
}
|
LMS_LOG(DB, INFO) << "Database Analyze complete";
|
||||||
LMS_LOG(DB, DEBUG) << "Optimized db!";
|
}
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <tuple>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <Wt/Dbo/Dbo.h>
|
#include <Wt/Dbo/Dbo.h>
|
||||||
@@ -58,10 +59,12 @@ namespace Database {
|
|||||||
Cluster() = default;
|
Cluster() = default;
|
||||||
|
|
||||||
// Find utility
|
// Find utility
|
||||||
static std::size_t getCount(Session& session);
|
// As clusters only have a name, this is an optim to directly get the cluster names
|
||||||
static RangeResults<ClusterId> find(Session& session, const FindParameters& range);
|
using ClusterFindResult = std::tuple<ClusterId, std::string>;
|
||||||
static pointer find(Session& session, ClusterId id);
|
static std::size_t getCount(Session& session);
|
||||||
static RangeResults<ClusterId> findOrphans(Session& session, Range range);
|
static RangeResults<ClusterFindResult> find(Session& session, const FindParameters& range);
|
||||||
|
static pointer find(Session& session, ClusterId id);
|
||||||
|
static RangeResults<ClusterId> findOrphans(Session& session, Range range);
|
||||||
|
|
||||||
// Accessors
|
// Accessors
|
||||||
const std::string& getName() const { return _name; }
|
const std::string& getName() const { return _name; }
|
||||||
|
|||||||
@@ -27,52 +27,47 @@
|
|||||||
|
|
||||||
namespace Database {
|
namespace Database {
|
||||||
|
|
||||||
class Session;
|
class Session;
|
||||||
class Db
|
class Db
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Db(const std::filesystem::path& dbPath, std::size_t connectionCount = 10);
|
Db(const std::filesystem::path& dbPath, std::size_t connectionCount = 10);
|
||||||
~Db();
|
|
||||||
|
|
||||||
Db(const Db&) = delete;
|
Session& getTLSSession();
|
||||||
Db(Db&&) = delete;
|
|
||||||
Db& operator=(const Db&) = delete;
|
|
||||||
Db& operator=(Db&&) = delete;
|
|
||||||
|
|
||||||
Session& getTLSSession();
|
void executeSql(const std::string& sql);
|
||||||
|
|
||||||
void executeSql(const std::string& sql);
|
private:
|
||||||
|
Db(const Db&) = delete;
|
||||||
|
Db& operator=(const Db&) = delete;
|
||||||
|
|
||||||
private:
|
friend class Session;
|
||||||
friend class Session;
|
|
||||||
|
|
||||||
RecursiveSharedMutex& getMutex() { return _sharedMutex; }
|
RecursiveSharedMutex& getMutex() { return _sharedMutex; }
|
||||||
Wt::Dbo::SqlConnectionPool& getConnectionPool() { return *_connectionPool; }
|
Wt::Dbo::SqlConnectionPool& getConnectionPool() { return *_connectionPool; }
|
||||||
|
|
||||||
class ScopedConnection
|
class ScopedConnection
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ScopedConnection(Wt::Dbo::SqlConnectionPool& pool);
|
ScopedConnection(Wt::Dbo::SqlConnectionPool& pool);
|
||||||
~ScopedConnection();
|
~ScopedConnection();
|
||||||
|
|
||||||
ScopedConnection(const ScopedConnection& ) = delete;
|
Wt::Dbo::SqlConnection* operator->() const;
|
||||||
ScopedConnection(ScopedConnection&& ) = delete;
|
|
||||||
ScopedConnection& operator=(const ScopedConnection& ) = delete;
|
|
||||||
ScopedConnection& operator=(ScopedConnection&& ) = delete;
|
|
||||||
|
|
||||||
Wt::Dbo::SqlConnection* operator->() const;
|
private:
|
||||||
|
ScopedConnection(const ScopedConnection&) = delete;
|
||||||
|
ScopedConnection& operator=(const ScopedConnection&) = delete;
|
||||||
|
|
||||||
private:
|
Wt::Dbo::SqlConnectionPool& _connectionPool;
|
||||||
Wt::Dbo::SqlConnectionPool& _connectionPool;
|
std::unique_ptr<Wt::Dbo::SqlConnection> _connection;
|
||||||
std::unique_ptr<Wt::Dbo::SqlConnection> _connection;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
RecursiveSharedMutex _sharedMutex;
|
RecursiveSharedMutex _sharedMutex;
|
||||||
std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool;
|
std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool;
|
||||||
|
|
||||||
std::mutex _tlsSessionsMutex;
|
std::mutex _tlsSessionsMutex;
|
||||||
std::vector<std::unique_ptr<Session>> _tlsSessions;
|
std::vector<std::unique_ptr<Session>> _tlsSessions;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|
||||||
|
|||||||
@@ -28,68 +28,66 @@
|
|||||||
|
|
||||||
namespace Database
|
namespace Database
|
||||||
{
|
{
|
||||||
class UniqueTransaction
|
class UniqueTransaction
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
friend class Session;
|
friend class Session;
|
||||||
UniqueTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session);
|
UniqueTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session);
|
||||||
|
|
||||||
std::unique_lock<RecursiveSharedMutex> _lock;
|
std::unique_lock<RecursiveSharedMutex> _lock;
|
||||||
Wt::Dbo::Transaction _transaction;
|
Wt::Dbo::Transaction _transaction;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SharedTransaction
|
class SharedTransaction
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
friend class Session;
|
friend class Session;
|
||||||
SharedTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session);
|
SharedTransaction(RecursiveSharedMutex& mutex, Wt::Dbo::Session& session);
|
||||||
|
|
||||||
std::shared_lock<RecursiveSharedMutex> _lock;
|
std::shared_lock<RecursiveSharedMutex> _lock;
|
||||||
Wt::Dbo::Transaction _transaction;
|
Wt::Dbo::Transaction _transaction;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Db;
|
class Db;
|
||||||
class Session
|
class Session
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Session (Db& database);
|
Session(Db& database);
|
||||||
|
|
||||||
Session(const Session&) = delete;
|
[[nodiscard]] UniqueTransaction createUniqueTransaction();
|
||||||
Session(Session&&) = delete;
|
[[nodiscard]] SharedTransaction createSharedTransaction();
|
||||||
Session& operator=(const Session&) = delete;
|
|
||||||
Session& operator=(Session&&) = delete;
|
|
||||||
|
|
||||||
[[nodiscard]] UniqueTransaction createUniqueTransaction();
|
void checkUniqueLocked();
|
||||||
[[nodiscard]] SharedTransaction createSharedTransaction();
|
void checkSharedLocked();
|
||||||
|
|
||||||
void checkUniqueLocked();
|
void analyze();
|
||||||
void checkSharedLocked();
|
|
||||||
|
|
||||||
void optimize();
|
void prepareTables(); // need to run only once at startup
|
||||||
|
|
||||||
void prepareTables(); // need to run only once at startup
|
Wt::Dbo::Session& getDboSession() { return _session; }
|
||||||
|
Db& getDb() { return _db; }
|
||||||
|
|
||||||
Wt::Dbo::Session& getDboSession() { return _session; }
|
template <typename Object, typename... Args>
|
||||||
Db& getDb() { return _db; }
|
typename Object::pointer create(Args&&... args)
|
||||||
|
{
|
||||||
|
checkUniqueLocked();
|
||||||
|
|
||||||
template <typename Object, typename... Args>
|
typename Object::pointer res{ Object::create(*this, std::forward<Args>(args)...) };
|
||||||
typename Object::pointer create(Args&&... args)
|
getDboSession().flush();
|
||||||
{
|
|
||||||
checkUniqueLocked();
|
|
||||||
|
|
||||||
typename Object::pointer res {Object::create(*this, std::forward<Args>(args)...)};
|
if (res->hasOnPostCreated())
|
||||||
getDboSession().flush();
|
res.modify()->onPostCreated();
|
||||||
|
|
||||||
if (res->hasOnPostCreated())
|
return res;
|
||||||
res.modify()->onPostCreated();
|
}
|
||||||
|
|
||||||
return res;
|
private:
|
||||||
}
|
Session(const Session&) = delete;
|
||||||
|
Session& operator=(const Session&) = delete;
|
||||||
|
|
||||||
private:
|
Db& _db;
|
||||||
Db& _db;
|
Wt::Dbo::Session _session;
|
||||||
Wt::Dbo::Session _session;
|
};
|
||||||
};
|
|
||||||
} // namespace Database
|
} // namespace Database
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -48,13 +48,17 @@ TEST_F(DatabaseFixture, Cluster)
|
|||||||
EXPECT_EQ(Cluster::getCount(session), 1);
|
EXPECT_EQ(Cluster::getCount(session), 1);
|
||||||
EXPECT_EQ(cluster->getType()->getId(), clusterType.getId());
|
EXPECT_EQ(cluster->getType()->getId(), clusterType.getId());
|
||||||
|
|
||||||
auto clusters{ Cluster::find(session, Cluster::FindParameters {}) };
|
{
|
||||||
ASSERT_EQ(clusters.results.size(), 1);
|
const auto clusters{ Cluster::find(session, Cluster::FindParameters {}) };
|
||||||
EXPECT_EQ(clusters.results.front(), cluster.getId());
|
ASSERT_EQ(clusters.results.size(), 1);
|
||||||
|
EXPECT_EQ(std::get<ClusterId>(clusters.results.front()), cluster.getId());
|
||||||
|
}
|
||||||
|
|
||||||
clusters = Cluster::findOrphans(session, Range{});
|
{
|
||||||
ASSERT_EQ(clusters.results.size(), 1);
|
const auto clusters{ Cluster::findOrphans(session, Range{}) };
|
||||||
EXPECT_EQ(clusters.results.front(), cluster.getId());
|
ASSERT_EQ(clusters.results.size(), 1);
|
||||||
|
EXPECT_EQ(clusters.results.front(), cluster.getId());
|
||||||
|
}
|
||||||
|
|
||||||
auto clusterTypes{ ClusterType::find(session, Range {}) };
|
auto clusterTypes{ ClusterType::find(session, Range {}) };
|
||||||
ASSERT_EQ(clusterTypes.results.size(), 1);
|
ASSERT_EQ(clusterTypes.results.size(), 1);
|
||||||
@@ -114,7 +118,7 @@ TEST_F(DatabaseFixture, Cluster_singleTrack)
|
|||||||
auto transaction{ session.createSharedTransaction() };
|
auto transaction{ session.createSharedTransaction() };
|
||||||
auto clusters{ Cluster::find(session, Cluster::FindParameters {}.setTrack(track.getId())) };
|
auto clusters{ Cluster::find(session, Cluster::FindParameters {}.setTrack(track.getId())) };
|
||||||
ASSERT_EQ(clusters.results.size(), 1);
|
ASSERT_EQ(clusters.results.size(), 1);
|
||||||
EXPECT_EQ(clusters.results.front(), cluster1.getId());
|
EXPECT_EQ(std::get<ClusterId>(clusters.results.front()), cluster1.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -317,9 +321,9 @@ TEST_F(DatabaseFixture, Cluster_singleTrackSingleReleaseSingleCluster)
|
|||||||
{
|
{
|
||||||
auto transaction{ session.createSharedTransaction() };
|
auto transaction{ session.createSharedTransaction() };
|
||||||
|
|
||||||
auto clusters{ Cluster::find(session, Cluster::FindParameters{}.setRelease(release.getId())) };
|
const auto clusters{ Cluster::find(session, Cluster::FindParameters{}.setRelease(release.getId())) };
|
||||||
ASSERT_EQ(clusters.results.size(), 1);
|
ASSERT_EQ(clusters.results.size(), 1);
|
||||||
EXPECT_EQ(clusters.results.front(), cluster.getId());
|
EXPECT_EQ(std::get<ClusterId>(clusters.results.front()), cluster.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -1106,5 +1110,3 @@ TEST_F(DatabaseFixture, MultipleTracksMultipleReleasesMultiClusters)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ DatabaseFixture::SetUpTestCase()
|
|||||||
{
|
{
|
||||||
Database::Session s {_tmpDb->getDb()};
|
Database::Session s {_tmpDb->getDb()};
|
||||||
s.prepareTables();
|
s.prepareTables();
|
||||||
s.optimize();
|
s.analyze();
|
||||||
|
|
||||||
// remove default created entries
|
// remove default created entries
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -299,8 +299,7 @@ ScannerService::scan(bool forceScan)
|
|||||||
|
|
||||||
LMS_LOG(DBUPDATER, INFO) << "Scan " << (_abortScan ? "aborted" : "complete") << ". Changes = " << stats.nbChanges() << " (added = " << stats.additions << ", removed = " << stats.deletions << ", updated = " << stats.updates << "), Not changed = " << stats.skips << ", Scanned = " << stats.scans << " (errors = " << stats.errors.size() << "), features fetched = " << stats.featuresFetched << ", duplicates = " << stats.duplicates.size();
|
LMS_LOG(DBUPDATER, INFO) << "Scan " << (_abortScan ? "aborted" : "complete") << ". Changes = " << stats.nbChanges() << " (added = " << stats.additions << ", removed = " << stats.deletions << ", updated = " << stats.updates << "), Not changed = " << stats.skips << ", Scanned = " << stats.scans << " (errors = " << stats.errors.size() << "), features fetched = " << stats.featuresFetched << ", duplicates = " << stats.duplicates.size();
|
||||||
|
|
||||||
// TODO make it a scan step
|
_dbSession.analyze();
|
||||||
_dbSession.optimize();
|
|
||||||
|
|
||||||
if (!_abortScan)
|
if (!_abortScan)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -288,12 +288,12 @@ namespace API::Subsonic
|
|||||||
|
|
||||||
checkUserTypeIsAllowed(requestContext, itEntryPoint->second.allowedUserTypes);
|
checkUserTypeIsAllowed(requestContext, itEntryPoint->second.allowedUserTypes);
|
||||||
|
|
||||||
Response resp{ (itEntryPoint->second.func)(requestContext) };
|
const Response resp{ (itEntryPoint->second.func)(requestContext) };
|
||||||
|
|
||||||
resp.write(response.out(), format);
|
resp.write(response.out(), format);
|
||||||
response.setMimeType(std::string{ ResponseFormatToMimeType(format) });
|
response.setMimeType(std::string{ ResponseFormatToMimeType(format) });
|
||||||
|
|
||||||
LMS_LOG(API_SUBSONIC, DEBUG) << "Request " << requestId << " '" << requestPath << "' handled!";
|
LMS_LOG(API_SUBSONIC, DEBUG) << "Request " << requestId << " '" << requestPath << "' handled!";
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,12 +20,8 @@
|
|||||||
#include "SubsonicResponse.hpp"
|
#include "SubsonicResponse.hpp"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <Wt/Json/Array.h>
|
#include <cmath>
|
||||||
#include <Wt/Json/Object.h>
|
#include <climits>
|
||||||
#include <Wt/Json/Value.h>
|
|
||||||
#include <Wt/Json/Serializer.h>
|
|
||||||
|
|
||||||
#include <boost/property_tree/json_parser.hpp>
|
|
||||||
#include <boost/property_tree/xml_parser.hpp>
|
#include <boost/property_tree/xml_parser.hpp>
|
||||||
|
|
||||||
#include "utils/Exception.hpp"
|
#include "utils/Exception.hpp"
|
||||||
@@ -57,59 +53,66 @@ namespace API::Subsonic
|
|||||||
_value = value;
|
_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Node::setAttribute(std::string_view key, std::string_view value)
|
void Response::Node::setAttribute(Key key, std::string_view value)
|
||||||
{
|
{
|
||||||
_attributes[std::string{ key }] = std::string{ value };
|
_attributes[key] = std::string{ value };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Node::addChild(const std::string& key, Node node)
|
void Response::Node::addChild(Key key, Node&& node)
|
||||||
{
|
{
|
||||||
assert(!_value);
|
assert(!_value);
|
||||||
_children[key].emplace_back(std::move(node));
|
assert(_children.find(key) == std::cend(_children));
|
||||||
|
_children[key] = std::move(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Node::createEmptyArrayChild(std::string_view key)
|
void Response::Node::createEmptyArrayChild(Key key)
|
||||||
{
|
{
|
||||||
assert(!_value);
|
assert(!_value);
|
||||||
|
assert(_children.find(key) == std::cend(_children));
|
||||||
_childrenArrays.emplace(key, std::vector<Node>{});
|
_childrenArrays.emplace(key, std::vector<Node>{});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Node::addArrayChild(std::string_view key, Node node)
|
void Response::Node::addArrayChild(Key key, Node&& node)
|
||||||
{
|
{
|
||||||
assert(!_value);
|
assert(!_value);
|
||||||
_childrenArrays[std::string{ key }].emplace_back(std::move(node));
|
assert(_children.find(key) == std::cend(_children));
|
||||||
|
_childrenArrays[key].emplace_back(std::move(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Node::createEmptyArrayValue(std::string_view key)
|
void Response::Node::createEmptyArrayValue(Key key)
|
||||||
{
|
{
|
||||||
assert(!_value);
|
assert(!_value);
|
||||||
|
assert(_children.find(key) == std::cend(_children));
|
||||||
_childrenValues.emplace(key, ValuesType{});
|
_childrenValues.emplace(key, ValuesType{});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Node::addArrayValue(std::string_view key, std::string_view value)
|
void Response::Node::addArrayValue(Key key, std::string_view value)
|
||||||
{
|
{
|
||||||
assert(!_value);
|
assert(!_value);
|
||||||
auto& values{ _childrenValues[std::string{ key }] };
|
assert(_children.find(key) == std::cend(_children));
|
||||||
|
auto& values{ _childrenValues[key] };
|
||||||
values.push_back(std::string{ value });
|
values.push_back(std::string{ value });
|
||||||
assert(std::all_of(std::cbegin(values) + 1, std::cend(values), [&](const ValueType& value) {return value.index() == values.front().index();}));
|
assert(std::all_of(std::cbegin(values) + 1, std::cend(values), [&](const ValueType& value) {return value.index() == values.front().index();}));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Node::addArrayValue(std::string_view key, long long value)
|
void Response::Node::addArrayValue(Key key, long long value)
|
||||||
{
|
{
|
||||||
assert(!_value);
|
assert(!_value);
|
||||||
auto& values{ _childrenValues[std::string{ key }] };
|
auto& values{ _childrenValues[key] };
|
||||||
values.push_back(value);
|
values.push_back(value);
|
||||||
assert(std::all_of(std::cbegin(values) + 1, std::cend(values), [&](const ValueType& value) {return value.index() == values.front().index();}));
|
assert(std::all_of(std::cbegin(values) + 1, std::cend(values), [&](const ValueType& value) {return value.index() == values.front().index();}));
|
||||||
}
|
}
|
||||||
|
|
||||||
Response::Node& Response::Node::createChild(const std::string& key)
|
Response::Node& Response::Node::createChild(Key key)
|
||||||
{
|
{
|
||||||
_children[key].emplace_back();
|
assert(!_value);
|
||||||
return _children[key].back();
|
return _children[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
Response::Node& Response::Node::createArrayChild(const std::string& key)
|
Response::Node& Response::Node::createArrayChild(Key key)
|
||||||
{
|
{
|
||||||
|
assert(!_value);
|
||||||
|
assert(_children.find(key) == std::cend(_children));
|
||||||
_childrenArrays[key].emplace_back();
|
_childrenArrays[key].emplace_back();
|
||||||
return _childrenArrays[key].back();
|
return _childrenArrays[key].back();
|
||||||
}
|
}
|
||||||
@@ -152,22 +155,22 @@ namespace API::Subsonic
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::addNode(const std::string& key, Node node)
|
void Response::addNode(Node::Key key, Node&& node)
|
||||||
{
|
{
|
||||||
return _root._children["subsonic-response"].front().addChild(key, std::move(node));
|
return _root._children["subsonic-response"].addChild(key, std::move(node));
|
||||||
}
|
}
|
||||||
|
|
||||||
Response::Node& Response::createNode(const std::string& key)
|
Response::Node& Response::createNode(Node::Key key)
|
||||||
{
|
{
|
||||||
return _root._children["subsonic-response"].front().createChild(key);
|
return _root._children["subsonic-response"].createChild(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
Response::Node& Response::createArrayNode(const std::string& key)
|
Response::Node& Response::createArrayNode(Node::Key key)
|
||||||
{
|
{
|
||||||
return _root._children["subsonic-response"].front().createArrayChild(key);
|
return _root._children["subsonic-response"].createArrayChild(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::write(std::ostream& os, ResponseFormat format)
|
void Response::write(std::ostream& os, ResponseFormat format) const
|
||||||
{
|
{
|
||||||
switch (format)
|
switch (format)
|
||||||
{
|
{
|
||||||
@@ -180,22 +183,22 @@ namespace API::Subsonic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::writeXML(std::ostream& os)
|
void Response::writeXML(std::ostream& os) const
|
||||||
{
|
{
|
||||||
std::function<boost::property_tree::ptree(const Node&)> nodeToPropertyTree = [&](const Node& node)
|
std::function<boost::property_tree::ptree(const Node&)> nodeToPropertyTree = [&](const Node& node)
|
||||||
{
|
{
|
||||||
boost::property_tree::ptree res;
|
boost::property_tree::ptree res;
|
||||||
|
|
||||||
for (auto itAttribute : node._attributes)
|
for (const auto& [key, value] : node._attributes)
|
||||||
{
|
{
|
||||||
if (std::holds_alternative<std::string>(itAttribute.second))
|
if (std::holds_alternative<std::string>(value))
|
||||||
res.put("<xmlattr>." + itAttribute.first, std::get<std::string>(itAttribute.second));
|
res.put("<xmlattr>." + std::string{ key.get() }, std::get<std::string>(value));
|
||||||
else if (std::holds_alternative<bool>(itAttribute.second))
|
else if (std::holds_alternative<bool>(value))
|
||||||
res.put("<xmlattr>." + itAttribute.first, std::get<bool>(itAttribute.second));
|
res.put("<xmlattr>." + std::string{ key.get() }, std::get<bool>(value));
|
||||||
else if (std::holds_alternative<float>(itAttribute.second))
|
else if (std::holds_alternative<float>(value))
|
||||||
res.put("<xmlattr>." + itAttribute.first, std::get<float>(itAttribute.second));
|
res.put("<xmlattr>." + std::string{ key.get() }, std::get<float>(value));
|
||||||
else if (std::holds_alternative<long long>(itAttribute.second))
|
else if (std::holds_alternative<long long>(value))
|
||||||
res.put("<xmlattr>." + itAttribute.first, std::get<long long>(itAttribute.second));
|
res.put("<xmlattr>." + std::string{ key.get() }, std::get<long long>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto valueToPropertyTree = [](const Node::ValueType& value)
|
auto valueToPropertyTree = [](const Node::ValueType& value)
|
||||||
@@ -215,22 +218,21 @@ namespace API::Subsonic
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (const auto& [key, childNodes] : node._children)
|
for (const auto& [key, childNode] : node._children)
|
||||||
{
|
{
|
||||||
for (const Node& childNode : childNodes)
|
res.add_child(std::string{ key.get() }, nodeToPropertyTree(childNode));
|
||||||
res.add_child(key, nodeToPropertyTree(childNode));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& [key, childArrayNodes] : node._childrenArrays)
|
for (const auto& [key, childArrayNodes] : node._childrenArrays)
|
||||||
{
|
{
|
||||||
for (const Node& childNode : childArrayNodes)
|
for (const Node& childNode : childArrayNodes)
|
||||||
res.add_child(key, nodeToPropertyTree(childNode));
|
res.add_child(std::string{ key.get() }, nodeToPropertyTree(childNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& [key, childArrayValues] : node._childrenValues)
|
for (const auto& [key, childArrayValues] : node._childrenValues)
|
||||||
{
|
{
|
||||||
for (const Response::Node::ValueType& value : childArrayValues)
|
for (const Response::Node::ValueType& value : childArrayValues)
|
||||||
res.add_child(key, valueToPropertyTree(value));
|
res.add_child(std::string{ key.get() }, valueToPropertyTree(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,63 +243,136 @@ namespace API::Subsonic
|
|||||||
boost::property_tree::write_xml(os, root);
|
boost::property_tree::write_xml(os, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::writeJSON(std::ostream& os)
|
void Response::JsonSerializer::serializeNode(std::ostream& os, const Response::Node& node)
|
||||||
{
|
{
|
||||||
namespace Json = Wt::Json;
|
os << '{';
|
||||||
|
|
||||||
std::function<Json::Object(const Response::Node&)> nodeToJsonObject = [&](const Response::Node& node)
|
bool first{ true };
|
||||||
|
|
||||||
|
for (const auto& [key, value] : node._attributes)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
os << ',';
|
||||||
|
|
||||||
|
serializeEscapedString(os, key.get());
|
||||||
|
os << ':';
|
||||||
|
serializeValue(os, value);
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node._value)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
os << ',';
|
||||||
|
|
||||||
|
os << "\"value\":";
|
||||||
|
serializeValue(os, *node._value);
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (const auto& [key, childNode] : node._children)
|
||||||
{
|
{
|
||||||
Json::Object res;
|
if (!first)
|
||||||
|
os << ',';
|
||||||
|
|
||||||
auto valueToJsonValue{ [](const Node::ValueType& value) -> Json::Value
|
serializeEscapedString(os, key.get());
|
||||||
|
os << ':';
|
||||||
|
serializeNode(os, childNode);
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& [key, childArrayNodes] : node._childrenArrays)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
os << ',';
|
||||||
|
|
||||||
|
serializeEscapedString(os, key.get());
|
||||||
|
os << ":[";
|
||||||
|
|
||||||
|
bool firstChild{ true };
|
||||||
|
for (const Response::Node& childNode : childArrayNodes)
|
||||||
{
|
{
|
||||||
Json::Value res;
|
if (!firstChild)
|
||||||
std::visit([&](const auto& rawValue)
|
os << ",";
|
||||||
{
|
|
||||||
res = Json::Value{ rawValue };
|
|
||||||
}, value);
|
|
||||||
return res;
|
|
||||||
} };
|
|
||||||
|
|
||||||
for (auto itAttribute : node._attributes)
|
serializeNode(os, childNode);
|
||||||
res[itAttribute.first] = valueToJsonValue(itAttribute.second);
|
firstChild = false;
|
||||||
|
|
||||||
if (node._value)
|
|
||||||
{
|
|
||||||
res["value"] = valueToJsonValue(*node._value);
|
|
||||||
}
|
}
|
||||||
else
|
os << ']';
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& [key, childValues] : node._childrenValues)
|
||||||
|
{
|
||||||
|
if (!first)
|
||||||
|
os << ',';
|
||||||
|
|
||||||
|
serializeEscapedString(os, key.get());
|
||||||
|
os << ":[";
|
||||||
|
|
||||||
|
bool firstChild{ true };
|
||||||
|
for (const Node::ValueType& childValue : childValues)
|
||||||
{
|
{
|
||||||
for (const auto& [key, childNodes] : node._children)
|
if (!firstChild)
|
||||||
{
|
os << ",";
|
||||||
for (const Response::Node& childNode : childNodes)
|
|
||||||
res[key] = nodeToJsonObject(childNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& [key, childArrayNodes] : node._childrenArrays)
|
serializeValue(os, childValue);
|
||||||
{
|
|
||||||
Json::Array array;
|
|
||||||
for (const Response::Node& childNode : childArrayNodes)
|
|
||||||
array.emplace_back(nodeToJsonObject(childNode));
|
|
||||||
|
|
||||||
res[key] = std::move(array);
|
firstChild = false;
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& [key, childValues] : node._childrenValues)
|
|
||||||
{
|
|
||||||
Json::Array array;
|
|
||||||
for (const Node::ValueType& childValue : childValues)
|
|
||||||
array.emplace_back(valueToJsonValue(childValue));
|
|
||||||
|
|
||||||
res[key] = std::move(array);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
os << ']';
|
||||||
|
|
||||||
return res;
|
first = false;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Json::Object root{ nodeToJsonObject(_root) };
|
os << '}';
|
||||||
os << Json::serialize(root);
|
}
|
||||||
|
|
||||||
|
void Response::JsonSerializer::serializeValue(std::ostream& os, const Node::ValueType& value)
|
||||||
|
{
|
||||||
|
if (std::holds_alternative<std::string>(value))
|
||||||
|
{
|
||||||
|
serializeEscapedString(os, std::get<std::string>(value));
|
||||||
|
}
|
||||||
|
else if (std::holds_alternative<bool>(value))
|
||||||
|
{
|
||||||
|
os << (std::get<bool>(value) ? "true" : "false");
|
||||||
|
}
|
||||||
|
else if (std::holds_alternative<float>(value))
|
||||||
|
{
|
||||||
|
const float d{ std::get<float>(value) };
|
||||||
|
if (std::isnan(d) || std::fabs(d) == std::numeric_limits<float>::infinity())
|
||||||
|
os << "null";
|
||||||
|
else
|
||||||
|
os << d;
|
||||||
|
}
|
||||||
|
else if (std::holds_alternative<long long>(value))
|
||||||
|
{
|
||||||
|
os << std::get<long long>(value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::JsonSerializer::serializeEscapedString(std::ostream& os, std::string_view str)
|
||||||
|
{
|
||||||
|
os << '\"';
|
||||||
|
StringUtils::writeJSEscapedString(os, str);
|
||||||
|
os << '\"';
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::writeJSON(std::ostream& os) const
|
||||||
|
{
|
||||||
|
JsonSerializer serializer;
|
||||||
|
serializer.serializeNode(os, _root);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -191,17 +191,30 @@ namespace API::Subsonic
|
|||||||
class Node
|
class Node
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void setAttribute(std::string_view key, std::string_view value);
|
class Key
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
template<std::size_t N>
|
||||||
|
constexpr Key(const char (&str)[N]) : _str{ str } {}
|
||||||
|
constexpr std::string_view get() const { return _str; }
|
||||||
|
|
||||||
|
bool constexpr operator<(const Key& other) const { return _str < other._str; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::string_view _str;
|
||||||
|
};
|
||||||
|
|
||||||
|
void setAttribute(Key key, std::string_view value);
|
||||||
|
|
||||||
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
|
template <typename T, std::enable_if_t<std::is_arithmetic<T>::value>* = nullptr>
|
||||||
void setAttribute(std::string_view key, T value)
|
void setAttribute(Key key, T value)
|
||||||
{
|
{
|
||||||
if constexpr (std::is_same<bool, T>::value)
|
if constexpr (std::is_same<bool, T>::value)
|
||||||
_attributes[std::string{ key }] = value;
|
_attributes[key] = value;
|
||||||
else if constexpr (std::is_floating_point<T>::value)
|
else if constexpr (std::is_floating_point<T>::value)
|
||||||
_attributes[std::string{ key }] = static_cast<float>(value);
|
_attributes[key] = static_cast<float>(value);
|
||||||
else if constexpr (std::is_integral<T>::value)
|
else if constexpr (std::is_integral<T>::value)
|
||||||
_attributes[std::string{ key }] = static_cast<long long>(value);
|
_attributes[key] = static_cast<long long>(value);
|
||||||
else
|
else
|
||||||
static_assert("Unhandled type");
|
static_assert("Unhandled type");
|
||||||
}
|
}
|
||||||
@@ -209,28 +222,28 @@ namespace API::Subsonic
|
|||||||
// A Node has either a single value or an array of values or some children
|
// A Node has either a single value or an array of values or some children
|
||||||
void setValue(std::string_view value);
|
void setValue(std::string_view value);
|
||||||
void setValue(long long value);
|
void setValue(long long value);
|
||||||
Node& createChild(const std::string& key);
|
Node& createChild(Key key);
|
||||||
Node& createArrayChild(const std::string& key);
|
Node& createArrayChild(Key key);
|
||||||
|
|
||||||
void addChild(const std::string& key, Node node);
|
void addChild(Key key, Node&& node);
|
||||||
void createEmptyArrayChild(std::string_view key);
|
void createEmptyArrayChild(Key key);
|
||||||
void addArrayChild(std::string_view key, Node node);
|
void addArrayChild(Key key, Node&& node);
|
||||||
void createEmptyArrayValue(std::string_view key);
|
void createEmptyArrayValue(Key key);
|
||||||
void addArrayValue(std::string_view key, std::string_view value);
|
void addArrayValue(Key key, std::string_view value);
|
||||||
void addArrayValue(std::string_view key, long long value);
|
void addArrayValue(Key key, long long value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setVersionAttribute(ProtocolVersion version);
|
void setVersionAttribute(ProtocolVersion version);
|
||||||
|
|
||||||
friend class Response;
|
friend class Response;
|
||||||
using ValueType = std::variant<std::string, bool, float, long long>;
|
using ValueType = std::variant<std::string, bool, float, long long>;
|
||||||
std::map<std::string, ValueType> _attributes;
|
std::map<Key, ValueType> _attributes;
|
||||||
std::optional<ValueType> _value;
|
std::optional<ValueType> _value;
|
||||||
std::map<std::string, std::vector<Node>> _children;
|
std::map<Key, Node> _children;
|
||||||
std::map<std::string, std::vector<Node>> _childrenArrays;
|
std::map<Key, std::vector<Node>> _childrenArrays;
|
||||||
|
|
||||||
using ValuesType = std::vector<ValueType>;
|
using ValuesType = std::vector<ValueType>;
|
||||||
std::map<std::string, ValuesType> _childrenValues;
|
std::map<Key, ValuesType> _childrenValues;
|
||||||
};
|
};
|
||||||
|
|
||||||
static Response createOkResponse(ProtocolVersion protocolVersion);
|
static Response createOkResponse(ProtocolVersion protocolVersion);
|
||||||
@@ -242,16 +255,25 @@ namespace API::Subsonic
|
|||||||
Response(Response&&) = default;
|
Response(Response&&) = default;
|
||||||
Response& operator=(Response&&) = default;
|
Response& operator=(Response&&) = default;
|
||||||
|
|
||||||
void addNode(const std::string& key, Node node);
|
void addNode(Node::Key key, Node&& node);
|
||||||
Node& createNode(const std::string& key);
|
Node& createNode(Node::Key key);
|
||||||
Node& createArrayNode(const std::string& key);
|
Node& createArrayNode(Node::Key key);
|
||||||
|
|
||||||
void write(std::ostream& os, ResponseFormat format);
|
void write(std::ostream& os, ResponseFormat format) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Response createResponseCommon(ProtocolVersion protocolVersion, const Error* error = nullptr);
|
static Response createResponseCommon(ProtocolVersion protocolVersion, const Error* error = nullptr);
|
||||||
void writeJSON(std::ostream& os);
|
|
||||||
void writeXML(std::ostream& os);
|
class JsonSerializer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void serializeNode(std::ostream& os, const Node& node);
|
||||||
|
void serializeValue(std::ostream& os, const Node::ValueType& node);
|
||||||
|
void serializeEscapedString(std::ostream&, std::string_view str);
|
||||||
|
};
|
||||||
|
|
||||||
|
void writeJSON(std::ostream& os) const;
|
||||||
|
void writeXML(std::ostream& os) const;
|
||||||
|
|
||||||
Response() = default;
|
Response() = default;
|
||||||
Node _root;
|
Node _root;
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ namespace API::Subsonic
|
|||||||
throw NotImplementedGenericError{};
|
throw NotImplementedGenericError{};
|
||||||
|
|
||||||
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
||||||
Response::Node& albumListNode{ response.createNode(id3 ? "albumList2" : "albumList") };
|
Response::Node& albumListNode{ response.createNode(id3 ? Response::Node::Key{ "albumList2" } : Response::Node::Key{ "albumList" }) };
|
||||||
|
|
||||||
for (const ReleaseId releaseId : releases.results)
|
for (const ReleaseId releaseId : releases.results)
|
||||||
{
|
{
|
||||||
@@ -153,7 +153,7 @@ namespace API::Subsonic
|
|||||||
throw UserNotAuthorizedError{};
|
throw UserNotAuthorizedError{};
|
||||||
|
|
||||||
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
||||||
Response::Node& starredNode{ response.createNode(id3 ? "starred2" : "starred") };
|
Response::Node& starredNode{ response.createNode(id3 ? Response::Node::Key{ "starred2" } : Response::Node::Key{ "starred" }) };
|
||||||
|
|
||||||
Scrobbling::IScrobblingService& scrobbling{ *Service<Scrobbling::IScrobblingService>::get() };
|
Scrobbling::IScrobblingService& scrobbling{ *Service<Scrobbling::IScrobblingService>::get() };
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ namespace API::Subsonic
|
|||||||
std::size_t count{ getParameterAs<std::size_t>(context.parameters, "count").value_or(20) };
|
std::size_t count{ getParameterAs<std::size_t>(context.parameters, "count").value_or(20) };
|
||||||
|
|
||||||
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
||||||
Response::Node& artistInfoNode{ response.createNode(id3 ? "artistInfo2" : "artistInfo") };
|
Response::Node& artistInfoNode{ response.createNode(id3 ? Response::Node::Key{ "artistInfo2" } : Response::Node::Key{ "artistInfo" }) };
|
||||||
|
|
||||||
{
|
{
|
||||||
auto transaction{ context.dbSession.createSharedTransaction() };
|
auto transaction{ context.dbSession.createSharedTransaction() };
|
||||||
@@ -236,7 +236,7 @@ namespace API::Subsonic
|
|||||||
throw UserNotAuthorizedError{};
|
throw UserNotAuthorizedError{};
|
||||||
|
|
||||||
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
||||||
Response::Node& similarSongsNode{ response.createNode(id3 ? "similarSongs2" : "similarSongs") };
|
Response::Node& similarSongsNode{ response.createNode(id3 ? Response::Node::Key{ "similarSongs2" } : Response::Node::Key{ "similarSongs" }) };
|
||||||
for (const TrackId trackId : tracks)
|
for (const TrackId trackId : tracks)
|
||||||
{
|
{
|
||||||
const Track::pointer track{ Track::find(context.dbSession, trackId) };
|
const Track::pointer track{ Track::find(context.dbSession, trackId) };
|
||||||
|
|||||||
@@ -28,8 +28,7 @@ namespace API::Subsonic::Scan
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
Response::Node
|
Response::Node createStatusResponseNode()
|
||||||
createStatusResponseNode()
|
|
||||||
{
|
{
|
||||||
Response::Node statusResponse;
|
Response::Node statusResponse;
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ namespace API::Subsonic
|
|||||||
for (const TrackListEntry::pointer& entry : entries)
|
for (const TrackListEntry::pointer& entry : entries)
|
||||||
playlistNode.addArrayChild("entry", createSongNode(entry->getTrack(), context.dbSession, user));
|
playlistNode.addArrayChild("entry", createSongNode(entry->getTrack(), context.dbSession, user));
|
||||||
|
|
||||||
response.addNode("playlist", playlistNode);
|
response.addNode("playlist", std::move(playlistNode));
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ namespace API::Subsonic
|
|||||||
|
|
||||||
if (artists.size() == 1)
|
if (artists.size() == 1)
|
||||||
{
|
{
|
||||||
albumNode.setAttribute(id3 ? "artistId" : "parent", idToString(artists.front()->getId()));
|
albumNode.setAttribute(id3 ? Response::Node::Key{ "artistId" } : Response::Node::Key{ "parent" }, idToString(artists.front()->getId()));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -124,15 +124,16 @@ namespace API::Subsonic
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Report the first GENRE for this track
|
// Report the first GENRE for this track
|
||||||
if (ClusterType::pointer clusterType{ ClusterType::find(dbSession, "GENRE") })
|
const ClusterType::pointer genreClusterType{ ClusterType::find(dbSession, "GENRE") };
|
||||||
|
if (genreClusterType)
|
||||||
{
|
{
|
||||||
auto clusters{ release->getClusterGroups({clusterType}, 1) };
|
auto clusters{ release->getClusterGroups({genreClusterType}, 1) };
|
||||||
if (!clusters.empty() && !clusters.front().empty())
|
if (!clusters.empty() && !clusters.front().empty())
|
||||||
albumNode.setAttribute("genre", clusters.front().front()->getName());
|
albumNode.setAttribute("genre", clusters.front().front()->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (const Wt::WDateTime dateTime{ Service<Scrobbling::IScrobblingService>::get()->getStarredDateTime(user->getId(), release->getId()) }; dateTime.isValid())
|
if (const Wt::WDateTime dateTime{ Service<Scrobbling::IScrobblingService>::get()->getStarredDateTime(user->getId(), release->getId()) }; dateTime.isValid())
|
||||||
albumNode.setAttribute("starred", StringUtils::toISO8601String(dateTime)); // TODO report correct date/time
|
albumNode.setAttribute("starred", StringUtils::toISO8601String(dateTime));
|
||||||
|
|
||||||
// OpenSubsonic specific fields (must always be set)
|
// OpenSubsonic specific fields (must always be set)
|
||||||
if (!id3)
|
if (!id3)
|
||||||
@@ -140,7 +141,7 @@ namespace API::Subsonic
|
|||||||
|
|
||||||
{
|
{
|
||||||
const Wt::WDateTime dateTime{ Service<Scrobbling::IScrobblingService>::get()->getLastListenDateTime(user->getId(), release->getId()) };
|
const Wt::WDateTime dateTime{ Service<Scrobbling::IScrobblingService>::get()->getLastListenDateTime(user->getId(), release->getId()) };
|
||||||
albumNode.setAttribute("played", dateTime.isValid() ? StringUtils::toISO8601String(dateTime) : "");
|
albumNode.setAttribute("played", dateTime.isValid() ? StringUtils::toISO8601String(dateTime) : std::string{ "" });
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -148,7 +149,7 @@ namespace API::Subsonic
|
|||||||
albumNode.setAttribute("musicBrainzId", mbid ? mbid->getAsString() : "");
|
albumNode.setAttribute("musicBrainzId", mbid ? mbid->getAsString() : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto addClusters{ [&](std::string_view field, std::string_view clusterTypeName)
|
auto addClusters{ [&](Response::Node::Key field, std::string_view clusterTypeName)
|
||||||
{
|
{
|
||||||
albumNode.createEmptyArrayValue(field);
|
albumNode.createEmptyArrayValue(field);
|
||||||
|
|
||||||
@@ -159,35 +160,23 @@ namespace API::Subsonic
|
|||||||
params.setRelease(release->getId());
|
params.setRelease(release->getId());
|
||||||
params.setClusterType(clusterType->getId());
|
params.setClusterType(clusterType->getId());
|
||||||
|
|
||||||
for (const ClusterId clusterId : Cluster::find(dbSession, params).results)
|
for (const auto& cluster : Cluster::find(dbSession, params).results)
|
||||||
{
|
albumNode.addArrayValue(field, std::get<std::string>(cluster));
|
||||||
Cluster::pointer cluster{ Cluster::find(dbSession, clusterId) };
|
|
||||||
if (cluster)
|
|
||||||
albumNode.addArrayValue(field, cluster->getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} };
|
} };
|
||||||
|
|
||||||
addClusters("moods", "MOOD");
|
addClusters("moods", "MOOD");
|
||||||
|
|
||||||
// Genres
|
// Genres
|
||||||
|
albumNode.createEmptyArrayChild("genres");
|
||||||
|
if (genreClusterType)
|
||||||
{
|
{
|
||||||
albumNode.createEmptyArrayChild("genres");
|
Cluster::FindParameters params;
|
||||||
|
params.setRelease(release->getId());
|
||||||
|
params.setClusterType(genreClusterType->getId());
|
||||||
|
|
||||||
ClusterType::pointer clusterType{ ClusterType::find(dbSession, "GENRE") };
|
for (const auto& cluster : Cluster::find(dbSession, params).results)
|
||||||
if (clusterType)
|
albumNode.addArrayChild("genres", createItemGenreNode(std::get<std::string>(cluster)));
|
||||||
{
|
|
||||||
Cluster::FindParameters params;
|
|
||||||
params.setRelease(release->getId());
|
|
||||||
params.setClusterType(clusterType->getId());
|
|
||||||
|
|
||||||
for (const ClusterId clusterId : Cluster::find(dbSession, params).results)
|
|
||||||
{
|
|
||||||
Cluster::pointer cluster{ Cluster::find(dbSession, clusterId) };
|
|
||||||
if (cluster)
|
|
||||||
albumNode.addArrayChild("genres", createItemGenreNode(cluster));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
albumNode.createEmptyArrayChild("artists");
|
albumNode.createEmptyArrayChild("artists");
|
||||||
|
|||||||
@@ -23,11 +23,11 @@
|
|||||||
|
|
||||||
namespace API::Subsonic
|
namespace API::Subsonic
|
||||||
{
|
{
|
||||||
Response::Node createItemGenreNode(const Database::Cluster::pointer& cluster)
|
Response::Node createItemGenreNode(std::string_view name)
|
||||||
{
|
{
|
||||||
Response::Node genreNode;
|
Response::Node genreNode;
|
||||||
|
|
||||||
genreNode.setAttribute("name", cluster->getName());
|
genreNode.setAttribute("name", name);
|
||||||
|
|
||||||
return genreNode;
|
return genreNode;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "services/database/Object.hpp"
|
#include <string_view>
|
||||||
#include "SubsonicResponse.hpp"
|
#include "SubsonicResponse.hpp"
|
||||||
|
|
||||||
namespace Database
|
namespace Database
|
||||||
@@ -29,5 +29,5 @@ namespace Database
|
|||||||
|
|
||||||
namespace API::Subsonic
|
namespace API::Subsonic
|
||||||
{
|
{
|
||||||
Response::Node createItemGenreNode(const Database::ObjectPtr<Database::Cluster>& cluster);
|
Response::Node createItemGenreNode(std::string_view name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,7 +152,8 @@ namespace API::Subsonic
|
|||||||
trackResponse.setAttribute("starred", StringUtils::toISO8601String(dateTime));
|
trackResponse.setAttribute("starred", StringUtils::toISO8601String(dateTime));
|
||||||
|
|
||||||
// Report the first GENRE for this track
|
// Report the first GENRE for this track
|
||||||
if (ClusterType::pointer genreClusterType{ ClusterType::find(dbSession, "GENRE") })
|
const ClusterType::pointer genreClusterType{ ClusterType::find(dbSession, "GENRE") };
|
||||||
|
if (genreClusterType)
|
||||||
{
|
{
|
||||||
auto clusters{ track->getClusterGroups({genreClusterType}, 1) };
|
auto clusters{ track->getClusterGroups({genreClusterType}, 1) };
|
||||||
if (!clusters.empty() && !clusters.front().empty())
|
if (!clusters.empty() && !clusters.front().empty())
|
||||||
@@ -186,7 +187,7 @@ namespace API::Subsonic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto addArtistLinks{ [&](std::string_view nodeName, TrackArtistLinkType type)
|
auto addArtistLinks{ [&](Response::Node::Key nodeName, TrackArtistLinkType type)
|
||||||
{
|
{
|
||||||
trackResponse.createEmptyArrayChild(nodeName);
|
trackResponse.createEmptyArrayChild(nodeName);
|
||||||
|
|
||||||
@@ -209,7 +210,7 @@ namespace API::Subsonic
|
|||||||
if (release)
|
if (release)
|
||||||
trackResponse.setAttribute("displayAlbumArtist", release->getArtistDisplayName());
|
trackResponse.setAttribute("displayAlbumArtist", release->getArtistDisplayName());
|
||||||
|
|
||||||
auto addClusters{ [&](std::string_view field, std::string_view clusterTypeName)
|
auto addClusters{ [&](Response::Node::Key field, std::string_view clusterTypeName)
|
||||||
{
|
{
|
||||||
trackResponse.createEmptyArrayValue(field);
|
trackResponse.createEmptyArrayValue(field);
|
||||||
|
|
||||||
@@ -220,35 +221,23 @@ namespace API::Subsonic
|
|||||||
params.setTrack(track->getId());
|
params.setTrack(track->getId());
|
||||||
params.setClusterType(clusterType->getId());
|
params.setClusterType(clusterType->getId());
|
||||||
|
|
||||||
for (const ClusterId clusterId : Cluster::find(dbSession, params).results)
|
for (const auto& cluster : Cluster::find(dbSession, params).results)
|
||||||
{
|
trackResponse.addArrayValue(field, std::get<std::string>(cluster));
|
||||||
Cluster::pointer cluster {Cluster::find(dbSession, clusterId)};
|
|
||||||
if (cluster)
|
|
||||||
trackResponse.addArrayValue(field, cluster->getName());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} };
|
} };
|
||||||
|
|
||||||
addClusters("moods", "MOOD");
|
addClusters("moods", "MOOD");
|
||||||
|
|
||||||
// Genres
|
// Genres
|
||||||
|
trackResponse.createEmptyArrayChild("genres");
|
||||||
|
if (genreClusterType)
|
||||||
{
|
{
|
||||||
trackResponse.createEmptyArrayChild("genres");
|
Cluster::FindParameters params;
|
||||||
|
params.setTrack(track->getId());
|
||||||
|
params.setClusterType(genreClusterType->getId());
|
||||||
|
|
||||||
ClusterType::pointer clusterType{ ClusterType::find(dbSession, "GENRE") };
|
for (const auto& cluster : Cluster::find(dbSession, params).results)
|
||||||
if (clusterType)
|
trackResponse.addArrayChild("genres", createItemGenreNode(std::get<std::string>(cluster)));
|
||||||
{
|
|
||||||
Cluster::FindParameters params;
|
|
||||||
params.setTrack(track->getId());
|
|
||||||
params.setClusterType(clusterType->getId());
|
|
||||||
|
|
||||||
for (const ClusterId clusterId : Cluster::find(dbSession, params).results)
|
|
||||||
{
|
|
||||||
Cluster::pointer cluster{ Cluster::find(dbSession, clusterId) };
|
|
||||||
if (cluster)
|
|
||||||
trackResponse.addArrayChild("genres", createItemGenreNode(cluster));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trackResponse.addChild("replayGain", createReplayGainNode(track));
|
trackResponse.addChild("replayGain", createReplayGainNode(track));
|
||||||
|
|||||||
@@ -17,18 +17,19 @@
|
|||||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "utils/StreamLogger.hpp"
|
#include "utils/StreamLogger.hpp"
|
||||||
|
|
||||||
StreamLogger::StreamLogger(std::ostream& os, EnumSet<Severity> severities)
|
StreamLogger::StreamLogger(std::ostream& os, EnumSet<Severity> severities)
|
||||||
: _os {os}
|
: _os{ os }
|
||||||
, _severities {severities}
|
, _severities{ severities }
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void StreamLogger::processLog(const Log& log)
|
||||||
StreamLogger::processLog(const Log& log)
|
|
||||||
{
|
{
|
||||||
if (_severities.contains(log.getSeverity()))
|
if (_severities.contains(log.getSeverity()))
|
||||||
_os << "[" << getSeverityName(log.getSeverity()) << "] [" << getModuleName(log.getModule()) << "] " << log.getMessage() << std::endl;
|
_os << std::this_thread::get_id() << " [" << getSeverityName(log.getSeverity()) << "] [" << getModuleName(log.getModule()) << "] " << log.getMessage() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -219,7 +219,7 @@ namespace StringUtils
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string jsEscape(const std::string& str)
|
std::string jsEscape(std::string_view str)
|
||||||
{
|
{
|
||||||
static const std::unordered_map<char, std::string_view> escapeMap
|
static const std::unordered_map<char, std::string_view> escapeMap
|
||||||
{
|
{
|
||||||
@@ -249,6 +249,28 @@ namespace StringUtils
|
|||||||
return escaped;
|
return escaped;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void writeJSEscapedString(std::ostream& os, std::string_view str)
|
||||||
|
{
|
||||||
|
static constexpr std::pair<char, std::string_view> charsToEscape[]
|
||||||
|
{
|
||||||
|
{'\\', "\\\\" },
|
||||||
|
{ '\n', "\\n" },
|
||||||
|
{ '\r', "\\r" },
|
||||||
|
{ '\t', "\\t" },
|
||||||
|
{ '"', "\\\"" },
|
||||||
|
{ '\'', "\\\'" },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const char c : str)
|
||||||
|
{
|
||||||
|
auto itEntry{ std::find_if(std::cbegin(charsToEscape), std::cend(charsToEscape), [=](const auto& entry) { return entry.first == c;}) };
|
||||||
|
if (itEntry != std::cend(charsToEscape))
|
||||||
|
os << itEntry->second;
|
||||||
|
else
|
||||||
|
os << c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::string escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar)
|
std::string escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
|
|||||||
@@ -19,14 +19,25 @@
|
|||||||
|
|
||||||
#include "utils/WtLogger.hpp"
|
#include "utils/WtLogger.hpp"
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
|
#include <sstream>
|
||||||
#include <Wt/WApplication.h>
|
#include <Wt/WApplication.h>
|
||||||
#include <Wt/WLogger.h>
|
#include <Wt/WLogger.h>
|
||||||
|
|
||||||
#include "utils/Logger.hpp"
|
#include "utils/Logger.hpp"
|
||||||
|
|
||||||
void
|
namespace
|
||||||
WtLogger::processLog(const Log& log)
|
|
||||||
{
|
{
|
||||||
Wt::log(getSeverityName(log.getSeverity())) << Wt::WLogger::sep << "[" << getModuleName(log.getModule()) << "]" << Wt::WLogger::sep << log.getMessage();
|
std::string to_string(std::thread::id id)
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << id;
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WtLogger::processLog(const Log& log)
|
||||||
|
{
|
||||||
|
Wt::log(getSeverityName(log.getSeverity())) << Wt::WLogger::sep << to_string(std::this_thread::get_id()) << Wt::WLogger::sep << "[" << getModuleName(log.getModule()) << "]" << Wt::WLogger::sep << log.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,33 +26,33 @@
|
|||||||
|
|
||||||
enum class Severity
|
enum class Severity
|
||||||
{
|
{
|
||||||
FATAL,
|
FATAL,
|
||||||
ERROR,
|
ERROR,
|
||||||
WARNING,
|
WARNING,
|
||||||
INFO,
|
INFO,
|
||||||
DEBUG,
|
DEBUG,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Module
|
enum class Module
|
||||||
{
|
{
|
||||||
API_SUBSONIC,
|
API_SUBSONIC,
|
||||||
AUTH,
|
AUTH,
|
||||||
AV,
|
AV,
|
||||||
CHILDPROCESS,
|
CHILDPROCESS,
|
||||||
COVER,
|
COVER,
|
||||||
DB,
|
DB,
|
||||||
DBUPDATER,
|
DBUPDATER,
|
||||||
FEATURE,
|
FEATURE,
|
||||||
HTTP,
|
HTTP,
|
||||||
MAIN,
|
MAIN,
|
||||||
METADATA,
|
METADATA,
|
||||||
REMOTE,
|
REMOTE,
|
||||||
SCROBBLING,
|
SCROBBLING,
|
||||||
SERVICE,
|
SERVICE,
|
||||||
RECOMMENDATION,
|
RECOMMENDATION,
|
||||||
TRANSCODE,
|
TRANSCODE,
|
||||||
UI,
|
UI,
|
||||||
UTILS,
|
UTILS,
|
||||||
};
|
};
|
||||||
|
|
||||||
const char* getModuleName(Module mod);
|
const char* getModuleName(Module mod);
|
||||||
@@ -61,30 +61,32 @@ const char* getSeverityName(Severity sev);
|
|||||||
class Logger;
|
class Logger;
|
||||||
class Log
|
class Log
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Log(Logger* logger, Module module, Severity severity);
|
Log(Logger* logger, Module module, Severity severity);
|
||||||
~Log();
|
~Log();
|
||||||
|
|
||||||
Module getModule() const { return _module; }
|
Module getModule() const { return _module; }
|
||||||
Severity getSeverity() const { return _severity; }
|
Severity getSeverity() const { return _severity; }
|
||||||
std::string getMessage() const;
|
std::string getMessage() const;
|
||||||
|
|
||||||
std::ostringstream& getOstream() { return _oss; }
|
std::ostringstream& getOstream() { return _oss; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Module _module;
|
Log(const Log&) = delete;
|
||||||
Severity _severity;
|
Log& operator=(const Log&) = delete;
|
||||||
std::ostringstream _oss;
|
|
||||||
Logger* _logger {};
|
Module _module;
|
||||||
|
Severity _severity;
|
||||||
|
std::ostringstream _oss;
|
||||||
|
Logger* _logger{};
|
||||||
};
|
};
|
||||||
|
|
||||||
class Logger
|
class Logger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~Logger() = default;
|
virtual ~Logger() = default;
|
||||||
virtual void processLog(const Log& log) = 0;
|
virtual void processLog(const Log& log) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define LMS_LOG(module, severity) Log(Service<Logger>::get(), Module::module, Severity::severity).getOstream()
|
#define LMS_LOG(module, severity) Log{Service<Logger>::get(), Module::module, Severity::severity}.getOstream()
|
||||||
#define LMS_LOG_EX(module, severity) Log(Service<Logger>::get(), module, severity).getOstream()
|
#define LMS_LOG_EX(module, severity) Log{Service<Logger>::get(), module, severity}.getOstream()
|
||||||
|
|
||||||
|
|||||||
@@ -83,7 +83,8 @@ namespace StringUtils {
|
|||||||
|
|
||||||
[[nodiscard]] std::string replaceInString(std::string_view str, const std::string& from, const std::string& to);
|
[[nodiscard]] std::string replaceInString(std::string_view str, const std::string& from, const std::string& to);
|
||||||
|
|
||||||
[[nodiscard]] std::string jsEscape(const std::string& str);
|
[[nodiscard]] std::string jsEscape(std::string_view str);
|
||||||
|
void writeJSEscapedString(std::ostream& os, std::string_view str);
|
||||||
|
|
||||||
[[nodiscard]] std::string escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar);
|
[[nodiscard]] std::string escapeString(std::string_view str, std::string_view charsToEscape, char escapeChar);
|
||||||
|
|
||||||
|
|||||||
+4
-1
@@ -238,7 +238,10 @@ int main(int argc, char* argv[])
|
|||||||
{
|
{
|
||||||
Database::Session session {database};
|
Database::Session session {database};
|
||||||
session.prepareTables();
|
session.prepareTables();
|
||||||
session.optimize();
|
|
||||||
|
// force optimize in case scanner aborted during a large import:
|
||||||
|
// queries may be too slow to even be able to relaunch a scan sing the web interface
|
||||||
|
session.analyze();
|
||||||
}
|
}
|
||||||
|
|
||||||
UserInterface::LmsApplicationManager appManager;
|
UserInterface::LmsApplicationManager appManager;
|
||||||
|
|||||||
Reference in New Issue
Block a user