Split the lib in smaller libs to ease unit tests

This commit is contained in:
emeric
2020-02-13 18:04:35 +01:00
parent 1e2c1caeed
commit 15e53caa2d
131 changed files with 382 additions and 138 deletions
+11
View File
@@ -0,0 +1,11 @@
add_subdirectory(auth)
add_subdirectory(av)
add_subdirectory(cover)
add_subdirectory(database)
add_subdirectory(recommendation)
add_subdirectory(scanner)
add_subdirectory(subsonic)
add_subdirectory(utils)
+28
View File
@@ -0,0 +1,28 @@
add_library(lmsauth SHARED
impl/AuthTokenService.cpp
impl/PasswordService.cpp
impl/LoginThrottler.cpp
)
target_include_directories(lmsauth INTERFACE
include
)
target_include_directories(lmsauth PRIVATE
include/
)
target_link_libraries(lmsauth PRIVATE
lmsutils
lmsdatabase
)
target_link_libraries(lmsauth PUBLIC
pthread
boost_system
wt
)
install(TARGETS lmsauth DESTINATION lib)
+127
View File
@@ -0,0 +1,127 @@
/*
* Copyright (C) 2019 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/>.
*/
/* This file contains some classes in order to get info from file using the libavconv */
#include "AuthTokenService.hpp"
#include <Wt/Auth/HashFunction.h>
#include <Wt/Auth/PasswordStrengthValidator.h>
#include <Wt/WRandom.h>
#include "database/Session.hpp"
#include "database/User.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
namespace Auth {
std::unique_ptr<IAuthTokenService> createAuthTokenService(std::size_t maxThrottlerEntries)
{
return std::make_unique<AuthTokenService>(maxThrottlerEntries);
}
static const Wt::Auth::SHA1HashFunction sha1Function;
AuthTokenService::AuthTokenService(std::size_t maxThrottlerEntries)
: _loginThrottler {maxThrottlerEntries}
{
}
std::string
AuthTokenService::createAuthToken(Database::Session& session, Database::IdType userId, const Wt::WDateTime& expiry)
{
const std::string secret {Wt::WRandom::generateId(32)};
const std::string secretHash {sha1Function.compute(secret, {})};
auto transaction {session.createUniqueTransaction()};
Database::User::pointer user {Database::User::getById(session, userId)};
if (!user)
throw LmsException {"User deleted"};
Database::AuthToken::pointer authToken {Database::AuthToken::create(session, secretHash, expiry, user)};
LMS_LOG(UI, DEBUG) << "Created auth token for user '" << user->getLoginName() << "', expiry = " << expiry.toString();
if (user->getAuthTokensCount() >= 50)
Database::AuthToken::removeExpiredTokens(session, Wt::WDateTime::currentDateTime());
return secret;
}
static
std::optional<AuthTokenService::AuthTokenProcessResult::AuthTokenInfo>
processAuthToken(Database::Session& session, const std::string& secret)
{
const std::string secretHash {sha1Function.compute(secret, {})};
auto transaction {session.createUniqueTransaction()};
Database::AuthToken::pointer authToken {Database::AuthToken::getByValue(session, secretHash)};
if (!authToken)
return std::nullopt;
if (authToken->getExpiry() < Wt::WDateTime::currentDateTime())
{
authToken.remove();
return std::nullopt;
}
LMS_LOG(UI, DEBUG) << "Found auth token for user '" << authToken->getUser()->getLoginName() << "'!";
AuthTokenService::AuthTokenProcessResult::AuthTokenInfo res {authToken->getUser().id(), authToken->getExpiry()};
authToken.remove();
return res;
}
AuthTokenService::AuthTokenProcessResult
AuthTokenService::processAuthToken(Database::Session& session, const boost::asio::ip::address& clientAddress, const std::string& tokenValue)
{
// Do not waste too much resource on brute force attacks (optim)
{
std::shared_lock<std::shared_timed_mutex> lock {_mutex};
if (_loginThrottler.isClientThrottled(clientAddress))
return AuthTokenProcessResult {AuthTokenProcessResult::State::Throttled};
}
auto res {Auth::processAuthToken(session, tokenValue)};
{
std::unique_lock<std::shared_timed_mutex> lock {_mutex};
if (_loginThrottler.isClientThrottled(clientAddress))
return AuthTokenProcessResult {AuthTokenProcessResult::State::Throttled};
if (!res)
{
_loginThrottler.onBadClientAttempt(clientAddress);
return AuthTokenProcessResult {AuthTokenProcessResult::State::NotFound};
}
_loginThrottler.onGoodClientAttempt(clientAddress);
return AuthTokenProcessResult {AuthTokenProcessResult::State::Found, std::move(*res)};
}
}
} // namespace Auth
+60
View File
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2019 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/>.
*/
/* This file contains some classes in order to get info from file using the libavconv */
#pragma once
#include "auth/IAuthTokenService.hpp"
#include "LoginThrottler.hpp"
namespace Database
{
class Session;
}
namespace Auth {
class AuthTokenService : public IAuthTokenService
{
public:
AuthTokenService(std::size_t maxThrottlerEntries);
AuthTokenService() = default;
~AuthTokenService() = default;
AuthTokenService(const AuthTokenService&) = delete;
AuthTokenService& operator=(const AuthTokenService&) = delete;
AuthTokenService(AuthTokenService&&) = delete;
AuthTokenService& operator=(AuthTokenService&&) = delete;
AuthTokenProcessResult processAuthToken(Database::Session& session, const boost::asio::ip::address& clientAddress, const std::string& tokenValue) override;
std::string createAuthToken(Database::Session& session, Database::IdType userid, const Wt::WDateTime& expiry) override;
private:
std::shared_timed_mutex _mutex;
LoginThrottler _loginThrottler;
};
}
+102
View File
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2019 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/>.
*/
/* This file contains some classes in order to get info from file using the libavconv */
#include "LoginThrottler.hpp"
#include "utils/Logger.hpp"
#include "utils/Random.hpp"
namespace Auth {
static
boost::asio::ip::address_v6
getAddressWithMask(const boost::asio::ip::address_v6& address, std::size_t prefix)
{
assert(prefix % 8 == 0);
std::array<uint8_t, 16> truncatedBytes;
auto bytes {address.to_bytes()};
std::copy(std::cbegin(bytes), std::next(std::cbegin(bytes), prefix / 8), truncatedBytes.begin());
return boost::asio::ip::address_v6 {truncatedBytes};
}
static
boost::asio::ip::address
getAddressToThrottle(const boost::asio::ip::address& address)
{
return address.is_v6() ? getAddressWithMask(address.to_v6(), 64) : address;
}
void
LoginThrottler::removeOutdatedEntries()
{
const Wt::WDateTime now {Wt::WDateTime::currentDateTime()};
for (auto it {std::begin(_attemptsInfo)}; it != std::end(_attemptsInfo); )
{
if (it->second <= now)
it = _attemptsInfo.erase(it);
else
++it;
}
}
void
LoginThrottler::onBadClientAttempt(const boost::asio::ip::address& address)
{
const boost::asio::ip::address clientAddress {getAddressToThrottle(address)};
const Wt::WDateTime now {Wt::WDateTime::currentDateTime()};
if (_attemptsInfo.size() >= _maxEntries)
removeOutdatedEntries();
if (_attemptsInfo.size() >= _maxEntries)
_attemptsInfo.erase(Random::pickRandom(_attemptsInfo));
_attemptsInfo[address] = now.addSecs(3);
LMS_LOG(AUTH, DEBUG) << "Registering bad attempt for '" << clientAddress.to_string() << "'";
}
void
LoginThrottler::onGoodClientAttempt(const boost::asio::ip::address& address)
{
const boost::asio::ip::address clientAddress {getAddressToThrottle(address)};
_attemptsInfo.erase(clientAddress);
}
bool
LoginThrottler::isClientThrottled(const boost::asio::ip::address& address) const
{
const boost::asio::ip::address clientAddress {getAddressToThrottle(address)};
auto it {_attemptsInfo.find(clientAddress)};
if (it == _attemptsInfo.end())
return false;
return it->second > Wt::WDateTime::currentDateTime();
}
} // Auth
+56
View File
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2019 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/>.
*/
/* This file contains some classes in order to get info from file using the libavconv */
#pragma once
#include <shared_mutex>
#include <string>
#include <unordered_map>
#include <Wt/WDateTime.h>
#include "utils/NetAddress.hpp"
#include "utils/Exception.hpp"
namespace Auth {
class LoginThrottler
{
public:
LoginThrottler(std::size_t maxEntries) : _maxEntries {maxEntries} {}
// user must lock these calls to avoid races
bool isClientThrottled(const boost::asio::ip::address& address) const;
void onBadClientAttempt(const boost::asio::ip::address& address);
void onGoodClientAttempt(const boost::asio::ip::address& address);
private:
void removeOutdatedEntries();
const std::size_t _maxEntries;
std::unordered_map<boost::asio::ip::address, Wt::WDateTime> _attemptsInfo;
};
} // Auth
+120
View File
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2019 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/>.
*/
/* This file contains some classes in order to get info from file using the libavconv */
#include "PasswordService.hpp"
#include <Wt/Auth/HashFunction.h>
#include <Wt/Auth/PasswordStrengthValidator.h>
#include <Wt/WRandom.h>
#include "database/Session.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
namespace Auth {
std::unique_ptr<IPasswordService> createPasswordService(std::size_t maxThrottlerEntries)
{
return std::make_unique<PasswordService>(maxThrottlerEntries);
}
PasswordService::PasswordService(std::size_t maxThrottlerEntries)
: _loginThrottler{maxThrottlerEntries}
{
}
static
bool
checkUserPassword(Database::Session& session, const std::string& loginName, const std::string& password)
{
Database::User::PasswordHash passwordHash;
{
auto transaction {session.createSharedTransaction()};
const Database::User::pointer user {Database::User::getByLoginName(session, loginName)};
if (!user)
return false;
passwordHash = user->getPasswordHash();
}
const Wt::Auth::BCryptHashFunction hashFunc {6};
return hashFunc.verify(password, passwordHash.salt, passwordHash.hash);
}
PasswordService::PasswordCheckResult
PasswordService::checkUserPassword(Database::Session& session, const boost::asio::ip::address& clientAddress, const std::string& loginName, const std::string& password)
{
// Do not waste too much resource on brute force attacks (optim)
{
std::shared_lock<std::shared_timed_mutex> lock {_mutex};
if (_loginThrottler.isClientThrottled(clientAddress))
return PasswordCheckResult::Throttled;
}
const bool match {Auth::checkUserPassword(session, loginName, password)};
{
std::unique_lock<std::shared_timed_mutex> lock {_mutex};
if (_loginThrottler.isClientThrottled(clientAddress))
return PasswordCheckResult::Throttled;
if (match)
{
_loginThrottler.onGoodClientAttempt(clientAddress);
return PasswordCheckResult::Match;
}
else
{
_loginThrottler.onBadClientAttempt(clientAddress);
return PasswordCheckResult::Mismatch;
}
}
}
Database::User::PasswordHash
PasswordService::hashPassword(const std::string& password) const
{
const std::string salt {Wt::WRandom::generateId(32)};
const Wt::Auth::BCryptHashFunction hashFunc {6};
return {salt, hashFunc.compute(password, salt)};
}
bool
PasswordService::evaluatePasswordStrength(const std::string& loginName, const std::string& password) const
{
Wt::Auth::PasswordStrengthValidator validator;
validator.setMinimumLength(Wt::Auth::PasswordStrengthType::OneCharClass, 4);
validator.setMinimumLength(Wt::Auth::PasswordStrengthType::TwoCharClass, 4);
validator.setMinimumLength(Wt::Auth::PasswordStrengthType::PassPhrase, 4);
validator.setMinimumLength(Wt::Auth::PasswordStrengthType::ThreeCharClass, 4);
validator.setMinimumLength(Wt::Auth::PasswordStrengthType::FourCharClass, 4);
validator.setMinimumPassPhraseWords(1);
validator.setMinimumMatchLength(3);
return validator.evaluateStrength(password, loginName, "").isValid();
}
} // namespace Auth
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2019 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 <shared_mutex>
#include "auth/IPasswordService.hpp"
#include "LoginThrottler.hpp"
namespace Database
{
class Session;
}
namespace Auth {
class PasswordService : public IPasswordService
{
public:
PasswordService(std::size_t maxThrottlerEntries);
PasswordService() = default;
~PasswordService() = default;
PasswordService(const PasswordService&) = delete;
PasswordService& operator=(const PasswordService&) = delete;
PasswordService(PasswordService&&) = delete;
PasswordService& operator=(PasswordService&&) = delete;
// Password services
PasswordCheckResult checkUserPassword(Database::Session& session, const boost::asio::ip::address& clientAddress, const std::string& loginName, const std::string& password) override;
Database::User::PasswordHash hashPassword(const std::string& password) const override;
bool evaluatePasswordStrength(const std::string& loginName, const std::string& password) const override;
private:
std::shared_timed_mutex _mutex;
LoginThrottler _loginThrottler;
};
}
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2019 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/>.
*/
/* This file contains some classes in order to get info from file using the libavconv */
#pragma once
#include <optional>
#include <string>
#include <boost/asio/ip/address.hpp>
#include <Wt/WDateTime.h>
#include "database/Types.hpp"
namespace Database
{
class Session;
}
namespace Auth {
class IAuthTokenService
{
public:
// Auth Token services
struct AuthTokenProcessResult
{
enum class State
{
Found,
Throttled,
NotFound,
};
struct AuthTokenInfo
{
Database::IdType userId;
Wt::WDateTime expiry;
};
State state {State::NotFound};
std::optional<AuthTokenInfo> authTokenInfo {};
};
// Removed if found
virtual AuthTokenProcessResult processAuthToken(Database::Session& session, const boost::asio::ip::address& clientAddress, const std::string& tokenValue) = 0;
virtual std::string createAuthToken(Database::Session& session, Database::IdType userid, const Wt::WDateTime& expiry) = 0;
};
std::unique_ptr<IAuthTokenService> createAuthTokenService(std::size_t maxThrottlerEntryCount);
}
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2019 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 <optional>
#include <string>
#include <boost/asio/ip/address.hpp>
#include "database/User.hpp"
#include "database/Types.hpp"
namespace Database
{
class Session;
}
namespace Auth {
class IPasswordService
{
public:
virtual ~IPasswordService() = default;
// Password services
enum class PasswordCheckResult
{
Match,
Mismatch,
Throttled,
};
virtual PasswordCheckResult checkUserPassword(Database::Session& session, const boost::asio::ip::address& clientAddress, const std::string& loginName, const std::string& password) = 0;
virtual Database::User::PasswordHash hashPassword(const std::string& password) const = 0;
virtual bool evaluatePasswordStrength(const std::string& loginName, const std::string& password) const = 0;
};
std::unique_ptr<IPasswordService> createPasswordService(std::size_t maxThrottlerEntryCount);
}
+24
View File
@@ -0,0 +1,24 @@
add_library(lmsav SHARED
impl/AvInfo.cpp
impl/AvTranscoder.cpp
impl/AvTypes.cpp
)
target_include_directories(lmsav INTERFACE
include
)
target_include_directories(lmsav PRIVATE
include/
)
# TODO make these private
target_link_libraries(lmsav PUBLIC
lmsutils
avformat
avutil
)
install(TARGETS lmsav DESTINATION lib)
+258
View File
@@ -0,0 +1,258 @@
/*
* Copyright (C) 2015 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 "av/AvInfo.hpp"
#include <array>
#include "utils/Logger.hpp"
#include "utils/String.hpp"
namespace Av {
static std::string averror_to_string(int error)
{
std::array<char, 128> buf = {0};
if (av_strerror(error, buf.data(), buf.size()) == 0)
return std::string(&buf[0]);
else
return "Unknown error";
}
MediaFileException::MediaFileException(int avError)
: AvException("MediaFileException: " + averror_to_string(avError))
{
}
MediaFile::MediaFile(const std::filesystem::path& p)
: _p {p}
{
int error = avformat_open_input(&_context, _p.string().c_str(), nullptr, nullptr);
if (error < 0)
{
LMS_LOG(AV, ERROR) << "Cannot open " << _p.string() << ": " << averror_to_string(error);
throw MediaFileException(error);
}
error = avformat_find_stream_info(_context, nullptr);
if (error < 0)
{
LMS_LOG(AV, ERROR) << "Cannot find stream information on " << _p.string() << ": " << averror_to_string(error);
avformat_close_input(&_context);
throw MediaFileException(error);
}
}
MediaFile::~MediaFile()
{
avformat_close_input(&_context);
}
std::string
MediaFile::getFormatName() const
{
return _context->iformat->name;
}
std::chrono::milliseconds
MediaFile::getDuration() const
{
if (_context->duration == AV_NOPTS_VALUE)
return std::chrono::milliseconds(0); // TODO estimate
return std::chrono::milliseconds(_context->duration / AV_TIME_BASE * 1000);
}
void
getMetaDataFromDictionnary(AVDictionary* dictionnary, std::map<std::string, std::string>& res)
{
if (!dictionnary)
return;
AVDictionaryEntry *tag = NULL;
while ((tag = av_dict_get(dictionnary, "", tag, AV_DICT_IGNORE_SUFFIX)))
{
res[StringUtils::stringToUpper(tag->key)] = tag->value;
}
}
std::map<std::string, std::string>
MediaFile::getMetaData(void)
{
std::map<std::string, std::string> res;
getMetaDataFromDictionnary(_context->metadata, res);
// HACK for OGG files
// If we did not find tags, search metadata in streams
if (res.empty())
{
for (std::size_t i = 0; i < _context->nb_streams; ++i)
{
getMetaDataFromDictionnary(_context->streams[i]->metadata, res);
if (!res.empty())
break;
}
}
return res;
}
std::vector<StreamInfo>
MediaFile::getStreamInfo() const
{
std::vector<StreamInfo> res;
for (std::size_t i {}; i < _context->nb_streams; ++i)
{
AVStream* avstream { _context->streams[i]};
// Skip attached pics
if (avstream->disposition & AV_DISPOSITION_ATTACHED_PIC)
continue;
if (!avstream->codecpar)
{
LMS_LOG(AV, ERROR) << "Skipping stream " << i << " since no codecpar is set";
continue;
}
if (avstream->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
continue;
res.push_back( {i, static_cast<std::size_t>(avstream->codecpar->bit_rate)} );
}
return res;
}
std::optional<std::size_t>
MediaFile::getBestStream() const
{
int res = av_find_best_stream(_context,
AVMEDIA_TYPE_AUDIO,
-1, // Auto
-1, // Auto
NULL,
0);
if (res < 0)
return std::nullopt;
return res;
}
bool
MediaFile::hasAttachedPictures(void) const
{
for (std::size_t i = 0; i < _context->nb_streams; ++i)
{
if (_context->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC)
return true;
}
return false;
}
std::vector<Picture>
MediaFile::getAttachedPictures(std::size_t nbMaxPictures) const
{
static const std::map<int, std::string> codecMimeMap =
{
{ AV_CODEC_ID_BMP, "image/x-bmp" },
{ AV_CODEC_ID_GIF, "image/gif" },
{ AV_CODEC_ID_MJPEG, "image/jpeg" },
{ AV_CODEC_ID_PNG, "image/png" },
{ AV_CODEC_ID_PNG, "image/x-png" },
{ AV_CODEC_ID_PPM, "image/x-portable-pixmap" },
};
std::vector<Picture> pictures;
for (std::size_t i = 0; i < _context->nb_streams; ++i)
{
AVStream *avstream = _context->streams[i];
// Skip attached pics
if (!(avstream->disposition & AV_DISPOSITION_ATTACHED_PIC))
continue;
if (avstream->codecpar == nullptr)
{
LMS_LOG(AV, ERROR) << "Skipping stream " << i << " since no codecpar is set";
continue;
}
Picture picture;
auto itMime = codecMimeMap.find(avstream->codecpar->codec_id);
if (itMime != codecMimeMap.end())
{
picture.mimeType = itMime->second;
}
else
{
picture.mimeType = "application/octet-stream";
LMS_LOG(AV, ERROR) << "CODEC ID " << avstream->codecpar->codec_id << " not handled in mime type conversion";
}
AVPacket pkt = avstream->attached_pic;
std::copy(pkt.data, pkt.data + pkt.size, std::back_inserter(picture.data));
pictures.push_back( picture );
if (pictures.size() >= nbMaxPictures)
break;
}
return pictures;
}
std::optional<MediaFileFormat>
guessMediaFileFormat(const std::filesystem::path& file)
{
AVOutputFormat* format {av_guess_format(NULL,file.string().c_str(),NULL)};
if (!format || !format->name)
return {};
auto formats {StringUtils::splitString(format->name, ",")};
if (formats.size() > 1)
LMS_LOG(AV, INFO) << "File '" << file.string() << "' reported several formats: '" << format->name << "'";
std::vector<std::string> mimeTypes;
if (format->mime_type)
mimeTypes = StringUtils::splitString(format->mime_type, ",");
if (mimeTypes.empty())
LMS_LOG(AV, INFO) << "File '" << file.string() << "', no mime type found!";
else if (mimeTypes.size() > 1)
LMS_LOG(AV, INFO) << "File '" << file.string() << "' reported several mime types: '" << format->mime_type << "'";
MediaFileFormat res;
res.format = formats.front();
res.mimeType = mimeTypes.empty() ? "application/octet-stream" : mimeTypes.front();
return res;
}
} // namespace Av
+257
View File
@@ -0,0 +1,257 @@
/*
* Copyright (C) 2015 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 "av/AvTranscoder.hpp"
#include <atomic>
#include <mutex>
#include "av/AvInfo.hpp"
#include "utils/IConfig.hpp"
#include "utils/Path.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
namespace Av {
#define LMS_LOG_TRANSCODE(sev) LMS_LOG(TRANSCODE, sev) << "[" << _id << "] - "
static std::atomic<size_t> globalId {};
static std::filesystem::path ffmpegPath;
void
Transcoder::init()
{
ffmpegPath = ServiceProvider<IConfig>::get()->getPath("ffmpeg-file", "/usr/bin/ffmpeg");
if (!std::filesystem::exists(ffmpegPath))
throw LmsException {"File '" + ffmpegPath.string() + "' does not exist!"};
}
Transcoder::Transcoder(const std::filesystem::path& filePath, const TranscodeParameters& parameters)
: _filePath {filePath},
_parameters {parameters},
_id {globalId++}
{
}
bool
Transcoder::start()
{
if (!std::filesystem::exists(_filePath))
return false;
else if (!std::filesystem::is_regular_file( _filePath) )
return false;
LMS_LOG_TRANSCODE(INFO) << "Transcoding file '" << _filePath.string() << "'";
std::vector<std::string> args;
args.emplace_back(ffmpegPath.string());
// Make sure we do not produce anything in the stderr output
// in order not to block the whole forked process
args.emplace_back("-loglevel");
args.emplace_back("quiet");
args.emplace_back("-nostdin");
// input Offset
if (_parameters.offset)
{
args.emplace_back("-ss");
args.emplace_back(std::to_string((*_parameters.offset).count()));
}
// Input file
args.emplace_back("-i");
args.emplace_back(_filePath.string());
// Stream mapping, if set
if (_parameters.stream)
{
args.emplace_back("-map");
args.emplace_back("0:" + std::to_string(*_parameters.stream));
}
if (_parameters.stripMetadata)
{
// Strip metadata
args.emplace_back("-map_metadata");
args.emplace_back("-1");
}
// Skip video flows (including covers)
args.emplace_back("-vn");
// Codecs and formats
if (_parameters.encoding)
{
// Output bitrates
args.emplace_back("-b:a");
args.emplace_back(std::to_string(_parameters.bitrate));
switch (*_parameters.encoding)
{
case Encoding::MP3:
args.emplace_back("-f");
args.emplace_back("mp3");
break;
case Encoding::OGG_OPUS:
args.emplace_back("-acodec");
args.emplace_back("libopus");
args.emplace_back("-f");
args.emplace_back("ogg");
break;
case Encoding::MATROSKA_OPUS:
args.emplace_back("-acodec");
args.emplace_back("libopus");
args.emplace_back("-f");
args.emplace_back("matroska");
break;
case Encoding::OGG_VORBIS:
args.emplace_back("-acodec");
args.emplace_back("libvorbis");
args.emplace_back("-f");
args.emplace_back("ogg");
break;
case Encoding::WEBM_VORBIS:
args.emplace_back("-acodec");
args.emplace_back("libvorbis");
args.emplace_back("-f");
args.emplace_back("webm");
break;
default:
return false;
}
_outputMimeType = encodingToMimetype(*_parameters.encoding);
}
else
{
auto mediaFileFormat {guessMediaFileFormat(_filePath)};
if (!mediaFileFormat)
{
LMS_LOG(AV, ERROR) << "Cannot guess media file format for '" << _filePath.string() << "'";
return false;
}
args.emplace_back("-acodec");
args.emplace_back("copy");
args.emplace_back("-f");
args.emplace_back(mediaFileFormat->format);
_outputMimeType = mediaFileFormat->mimeType;
}
args.emplace_back("pipe:1");
LMS_LOG_TRANSCODE(DEBUG) << "Dumping args (" << args.size() << ")";
for (const std::string& arg : args)
LMS_LOG_TRANSCODE(DEBUG) << "Arg = '" << arg << "'";
// make sure only one thread is executing this part of code
{
static std::mutex transcoderMutex;
std::lock_guard<std::mutex> lock {transcoderMutex};
_child = std::make_shared<redi::ipstream>();
// Caution: stdin must have been closed before
_child->open(ffmpegPath.string(), args);
if (!_child->is_open())
{
LMS_LOG_TRANSCODE(DEBUG) << "Exec failed!";
return false;
}
if (_child->out().eof())
{
LMS_LOG_TRANSCODE(DEBUG) << "Early end of file!";
return false;
}
}
LMS_LOG_TRANSCODE(DEBUG) << "Stream opened!";
return true;
}
void
Transcoder::process(std::vector<unsigned char>& output, std::size_t maxSize)
{
if (!_child || _isComplete)
return;
if (_child->out().fail())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout FAILED 2";
}
if (_child->out().eof())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout ENDED 2";
}
output.resize(maxSize);
//Read on the output stream
_child->out().read(reinterpret_cast<char*>(&output[0]), maxSize);
output.resize(_child->out().gcount());
if (_child->out().fail())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout FAILED";
}
if (_child->out().eof())
{
LMS_LOG_TRANSCODE(DEBUG) << "Stdout EOF!";
_child->clear();
_isComplete = true;
_child.reset();
}
_total += output.size();
LMS_LOG_TRANSCODE(DEBUG) << "nb bytes = " << output.size() << ", total = " << _total;
}
Transcoder::~Transcoder()
{
LMS_LOG_TRANSCODE(DEBUG) << ", ~Transcoder called! Total produced bytes = " << _total;
if (_child)
{
LMS_LOG_TRANSCODE(DEBUG) << "Child still here!";
_child->rdbuf()->kill(SIGKILL);
LMS_LOG_TRANSCODE(DEBUG) << "Closing...";
_child->rdbuf()->close();
LMS_LOG_TRANSCODE(DEBUG) << "Closing DONE";
}
}
} // namespace Transcode
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 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 "av/AvTypes.hpp"
namespace Av {
const char* encodingToMimetype(Encoding encoding)
{
switch (encoding)
{
case Encoding::MP3: return "audio/mpeg";
case Encoding::OGG_OPUS: return "audio/opus";
case Encoding::MATROSKA_OPUS: return "audio/x-matroska";
case Encoding::OGG_VORBIS: return "audio/ogg";
case Encoding::WEBM_VORBIS: return "audio/webm";
}
throw AvException {"Invalid encoding"};
}
}
+103
View File
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2015 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/>.
*/
/* This file contains some classes in order to get info from file using the libavconv */
#pragma once
extern "C"
{
#define __STDC_CONSTANT_MACROS
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/error.h>
}
#include <chrono>
#include <filesystem>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include "AvTypes.hpp"
namespace Av
{
void AvInit();
struct Picture
{
std::string mimeType;
std::vector<uint8_t> data;
};
struct StreamInfo
{
size_t id;
std::size_t bitrate;
};
class MediaFileException : public AvException
{
public:
MediaFileException(int avError);
};
class MediaFile
{
public:
MediaFile(const std::filesystem::path& p);
~MediaFile();
MediaFile(const MediaFile&) = delete;
MediaFile& operator=(const MediaFile&) = delete;
MediaFile(MediaFile&&) = delete;
MediaFile& operator=(MediaFile&&) = delete;
std::string getFormatName() const;
const std::filesystem::path& getPath() const {return _p;};
std::chrono::milliseconds getDuration() const;
std::map<std::string, std::string> getMetaData(void);
std::vector<StreamInfo> getStreamInfo() const;
std::optional<std::size_t> getBestStream() const; // none if failure/unknown
bool hasAttachedPictures(void) const;
std::vector<Picture> getAttachedPictures(std::size_t nbMaxPictures) const;
private:
std::filesystem::path _p;
AVFormatContext* _context {};
};
struct MediaFileFormat
{
std::string mimeType;
std::string format;
};
std::optional<MediaFileFormat> guessMediaFileFormat(const std::filesystem::path& file);
} // namespace Av
+76
View File
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2015 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 <chrono>
#include <filesystem>
#include <optional>
#include <pstreams/pstream.h>
#include "AvTypes.hpp"
namespace Av {
struct TranscodeParameters
{
std::optional<Encoding> encoding; // If not set, no transcoding is performed
std::size_t bitrate {128000};
std::optional<std::size_t> stream; // Id of the stream to be transcoded (auto detect by default)
std::optional<std::chrono::seconds> offset;
bool stripMetadata {true};
};
class Transcoder
{
public:
static void init();
Transcoder(const std::filesystem::path& file, const TranscodeParameters& parameters);
~Transcoder();
Transcoder(const Transcoder&) = delete;
Transcoder& operator=(const Transcoder&) = delete;
Transcoder(Transcoder&&) = delete;
Transcoder& operator=(Transcoder&&) = delete;
bool start();
const std::string& getOutputMimeType() const { return _outputMimeType; }
void process(std::vector<unsigned char>& output, std::size_t maxSize);
bool isComplete(void) const { return _isComplete; }
const TranscodeParameters& getParameters() const { return _parameters; }
private:
const std::filesystem::path _filePath;
const TranscodeParameters _parameters;
std::shared_ptr<redi::ipstream> _child;
bool _isComplete {};
std::size_t _total {};
const std::size_t _id {};
std::string _outputMimeType;
};
} // namespace Av
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019 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 <string>
#include "utils/Exception.hpp"
namespace Av {
class AvException : public LmsException
{
public:
AvException(const std::string& msg) : LmsException(msg) {}
};
enum class Encoding
{
// Values are important and must not be changed
MP3,
OGG_OPUS,
MATROSKA_OPUS,
OGG_VORBIS,
WEBM_VORBIS,
};
const char* encodingToMimetype(Encoding encoding);
}
+27
View File
@@ -0,0 +1,27 @@
add_library(lmscover SHARED
impl/CoverArtGrabber.cpp
impl/Image.cpp
)
target_include_directories(lmscover INTERFACE
include
)
target_include_directories(lmscover PRIVATE
include
${IMAGEMAGICKXX_INCLUDE_DIRS}
)
target_compile_options(lmscover PRIVATE
${IMAGEMAGICKXX_CFLAGS_OTHER}
)
target_link_libraries(lmscover PRIVATE
lmsav
lmsdatabase
${IMAGEMAGICKXX_LIBRARIES}
)
install(TARGETS lmscover DESTINATION lib)
+268
View File
@@ -0,0 +1,268 @@
/*
* Copyright (C) 2013 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 "CoverArtGrabber.hpp"
#include "av/AvInfo.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "utils/Logger.hpp"
namespace {
bool
isFileSupported(const std::filesystem::path& file, const std::vector<std::filesystem::path>& extensions)
{
return (std::find(std::cbegin(extensions), std::cend(extensions), file.extension()) != std::cend(extensions));
}
} // namespace
namespace CoverArt {
std::unique_ptr<IGrabber> createGrabber(const std::filesystem::path& execPath)
{
return std::make_unique<Grabber>(execPath);
}
Grabber::Grabber(const std::filesystem::path& execPath)
{
init(execPath);
}
Grabber::~Grabber()
{
deinit();
}
void
Grabber::setDefaultCover(const std::filesystem::path& p)
{
if (!_defaultCover.load(p))
throw LmsException("Cannot read default cover file '" + p.string() + "'");
}
Image
Grabber::getDefaultCover(std::size_t size)
{
LMS_LOG(COVER, DEBUG) << "Getting a default cover using size = " << size;
std::unique_lock<std::mutex> lock(_mutex);
auto it = _defaultCovers.find(size);
if (it == _defaultCovers.end())
{
Image cover = _defaultCover;
LMS_LOG(COVER, DEBUG) << "default cover size = " << cover.getSize().width << " x " << cover.getSize().height;
LMS_LOG(COVER, DEBUG) << "Scaling cover to size = " << size;
cover.scale(Geometry{size, size});
LMS_LOG(COVER, DEBUG) << "Scaling DONE";
auto res = _defaultCovers.insert(std::make_pair(size, cover));
assert(res.second);
it = res.first;
}
return it->second;
}
static std::optional<Image>
getFromAvMediaFile(const Av::MediaFile& input)
{
std::vector<Image> res;
for (auto& picture : input.getAttachedPictures(2))
{
Image image;
if (image.load(picture.data))
return image;
else
LMS_LOG(COVER, ERROR) << "Cannot load embedded cover file in '" << input.getPath().string() << "'";
}
LMS_LOG(COVER, DEBUG) << "No cover found in media file '" << input.getPath().string() << "'";
return std::nullopt;
}
std::optional<Image>
Grabber::getFromDirectory(const std::filesystem::path& p) const
{
for (auto coverPath : getCoverPaths(p))
{
Image image;
if (image.load(coverPath))
return image;
else
LMS_LOG(COVER, ERROR) << "Cannot load image in file '" << coverPath.string() << "'";
}
LMS_LOG(COVER, DEBUG) << "No cover found in directory '" << p.string() << "'";
return std::nullopt;
}
std::vector<std::filesystem::path>
Grabber::getCoverPaths(const std::filesystem::path& directoryPath) const
{
std::vector<std::filesystem::path> res;
std::error_code ec;
// TODO handle preferred file names
std::filesystem::directory_iterator itPath(directoryPath, ec);
std::filesystem::directory_iterator itEnd;
while (!ec && itPath != itEnd)
{
const std::filesystem::path path {*itPath};
itPath.increment(ec);
if (!std::filesystem::is_regular_file(path))
continue;
if (!isFileSupported(path, _fileExtensions))
continue;
if (std::filesystem::file_size(path) > _maxFileSize)
{
LMS_LOG(COVER, INFO) << "Cover file '" << path.string() << " is too big (" << std::filesystem::file_size(path) << "), limit is " << _maxFileSize;
continue;
}
res.push_back(path);
}
return res;
}
std::optional<Image>
Grabber::getFromTrack(const std::filesystem::path& p) const
{
try
{
Av::MediaFile input(p);
return getFromAvMediaFile(input);
}
catch (Av::MediaFileException& e)
{
LMS_LOG(COVER, ERROR) << "Cannot get covers from track " << p.string() << ": " << e.what();
return std::nullopt;
}
}
Image
Grabber::getFromTrack(Database::Session& dbSession, Database::IdType trackId, std::size_t size)
{
using namespace Database;
std::optional<Image> cover;
bool hasCover {};
bool isMultiDisc {};
std::filesystem::path trackPath;
{
auto transaction {dbSession.createSharedTransaction()};
Track::pointer track = Track::getById(dbSession, trackId);
if (track)
{
hasCover = track->hasCover();
trackPath = track->getPath();
auto release {track->getRelease()};
if (release && release->getTotalDiscNumber() > 1)
isMultiDisc = true;
}
}
if (hasCover)
cover = getFromTrack(trackPath);
if (!cover)
cover = getFromDirectory(trackPath.parent_path());
if (!cover && isMultiDisc)
{
if (trackPath.parent_path().has_parent_path())
cover = getFromDirectory(trackPath.parent_path().parent_path());
}
if (!cover)
cover = getDefaultCover(size);
else
cover->scale(Geometry {size, size});
return *cover;
}
Image
Grabber::getFromRelease(Database::Session& session, Database::IdType releaseId, std::size_t size)
{
std::optional<Image> cover;
std::optional<Database::IdType> trackId;
{
auto transaction {session.createSharedTransaction()};
auto release {Database::Release::getById(session, releaseId)};
if (release)
{
auto tracks {release->getTracks()};
if (!tracks.empty())
trackId = tracks.front().id();
}
}
if (trackId)
return getFromTrack(session, *trackId, size);
if (!cover)
cover = getDefaultCover(size);
else
cover->scale(Geometry {size, size});
return *cover;
}
std::vector<uint8_t>
Grabber::getFromTrack(Database::Session& session, Database::IdType trackId, Format format, std::size_t width)
{
const Image cover {getFromTrack(session, trackId, width)};
assert(format == Format::JPEG);
return cover.save(format);
}
std::vector<uint8_t>
Grabber::getFromRelease(Database::Session& session, Database::IdType releaseId, Format format, std::size_t width)
{
const Image cover {getFromRelease(session, releaseId, width)};
assert(format == Format::JPEG);
return cover.save(format);
}
} // namespace CoverArt
+77
View File
@@ -0,0 +1,77 @@
/*
* Copyright (C) 2015 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 <map>
#include <mutex>
#include <optional>
#include <vector>
#include "cover/ICoverArtGrabber.hpp"
#include "database/Types.hpp"
#include "Image.hpp"
namespace Database
{
class Session;
}
namespace CoverArt
{
class Grabber : public IGrabber
{
public:
Grabber(const std::filesystem::path& execPath);
~Grabber();
Grabber(const Grabber&) = delete;
Grabber& operator=(const Grabber&) = delete;
Grabber(Grabber&&) = delete;
Grabber& operator=(Grabber&&) = delete;
void setDefaultCover(const std::filesystem::path& defaultCoverPath) override;
std::vector<uint8_t> getFromTrack(Database::Session& dbSession, Database::IdType trackId, Format format, std::size_t width) override;
std::vector<uint8_t> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, Format format, std::size_t width) override;
private:
Image getFromTrack(Database::Session& dbSession, Database::IdType trackId, std::size_t size);
Image getFromRelease(Database::Session& dbSession, Database::IdType releaseId, std::size_t size);
std::optional<Image> getFromTrack(const std::filesystem::path& path) const;
std::vector<std::filesystem::path> getCoverPaths(const std::filesystem::path& directoryPath) const;
std::optional<Image> getFromDirectory(const std::filesystem::path& path) const;
Image getDefaultCover(std::size_t size);
Image _defaultCover;
std::mutex _mutex;
std::map<std::size_t /* size */, Image> _defaultCovers;
static inline const std::vector<std::filesystem::path> _fileExtensions {".jpg", ".jpeg", ".png", ".bmp"}; // TODO parametrize
static inline const std::size_t _maxFileSize {10000000};
static inline const std::vector<std::filesystem::path> _preferredFileNames {"cover", "front"}; // TODO parametrize
};
} // namespace CoverArt
+148
View File
@@ -0,0 +1,148 @@
/*
* Copyright (C) 2015 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 "Image.hpp"
#include "utils/Logger.hpp"
namespace CoverArt {
void
init(const std::filesystem::path& path)
{
Magick::InitializeMagick(path.string().c_str());
}
void
deinit()
{
MagickCore::MagickCoreTerminus();
}
static
std::string
formatToMagick(Format format)
{
switch (format)
{
case Format::JPEG: return "JPEG";
}
return "JPEG";
}
std::string
formatToMimeType(Format format)
{
switch (format)
{
case Format::JPEG: return "JPEG";
}
return "application/octet-stream";
}
bool
Image::load(const std::vector<unsigned char>& rawData)
{
try
{
Magick::Blob blob {&rawData[0], rawData.size()};
_image.read(blob);
return true;
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception while loading raw image: " << e.what();
return false;
}
}
bool
Image::load(const std::filesystem::path& p)
{
try
{
_image.read(p.string());
return true;
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception while loading image from file '" << p.string() << "': " << e.what();
return false;
}
}
Geometry
Image::getSize() const
{
Magick::Geometry geometry {_image.size()};
return {geometry.width(), geometry.height()};
}
bool
Image::scale(Geometry geometry)
{
if (geometry.width == 0 || geometry.height == 0)
return false;
try
{
_image.resize( Magick::Geometry(geometry.width, geometry.height ) );
return true;
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception during scale: " << e.what();
return false;
}
}
std::vector<uint8_t>
Image::save(Format format) const
{
std::vector<uint8_t> res;
try
{
Magick::Image outputImage {_image};
outputImage.magick(formatToMagick(format));
Magick::Blob blob;
outputImage.write(&blob);
auto begin = static_cast<const uint8_t*>(blob.data());
std::copy(begin, begin + blob.length(), std::back_inserter(res));
return res;
}
catch (Magick::Exception& e)
{
LMS_LOG(COVER, ERROR) << "Caught Magick exception during save:" << e.what();
res.clear();
return res;
}
}
} // namespace CoverArt
+57
View File
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2015 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 <vector>
#include <Magick++.h>
#include "cover/CoverArt.hpp"
namespace CoverArt
{
void init(const std::filesystem::path& path);
void deinit();
class Image
{
public:
// input
bool load(const std::vector<unsigned char>& rawData);
bool load(const std::filesystem::path& p);
Geometry getSize() const;
// Operations
bool scale(Geometry geometry);
// output
std::vector<uint8_t> save(Format format) const;
private:
Magick::Image _image;
};
} // namespace CoverArt
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2015 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 <string>
namespace CoverArt
{
enum class Format
{
JPEG,
};
std::string formatToMimeType(Format format);
struct Geometry
{
std::size_t width;
std::size_t height;
};
}
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2015 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 <vector>
#include "database/Types.hpp"
#include "cover/CoverArt.hpp"
namespace Database {
class Session;
}
namespace CoverArt {
class IGrabber
{
public:
virtual ~IGrabber() = default;
virtual void setDefaultCover(const std::filesystem::path& defaultCoverPath) = 0;
virtual std::vector<uint8_t> getFromTrack(Database::Session& dbSession, Database::IdType trackId, Format format, std::size_t width) = 0;
virtual std::vector<uint8_t> getFromRelease(Database::Session& dbSession, Database::IdType releaseId, Format format, std::size_t width) = 0;
};
std::unique_ptr<IGrabber> createGrabber(const std::filesystem::path& execPath);
} // namespace CoverArt
+37
View File
@@ -0,0 +1,37 @@
add_library(lmsdatabase SHARED
impl/Artist.cpp
impl/Cluster.cpp
impl/Db.cpp
impl/TrackArtistLink.cpp
impl/TrackFeatures.cpp
impl/TrackList.cpp
impl/Release.cpp
impl/ScanSettings.cpp
impl/Session.cpp
impl/SessionPool.cpp
impl/SqlQuery.cpp
impl/Track.cpp
impl/TrackBookmark.cpp
impl/User.cpp
)
target_include_directories(lmsdatabase INTERFACE
include
)
target_include_directories(lmsdatabase PRIVATE
include
)
target_link_libraries(lmsdatabase PRIVATE
wtdbosqlite3
)
target_link_libraries(lmsdatabase PUBLIC
lmsutils
wtdbo
)
install(TARGETS lmsdatabase DESTINATION lib)
+395
View File
@@ -0,0 +1,395 @@
/*
* Copyright (C) 2015 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/Artist.hpp"
#include <Wt/Dbo/WtSqlTraits.h>
#include "database/Cluster.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/User.hpp"
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
namespace Database
{
Artist::Artist(const std::string& name, const std::optional<UUID>& MBID)
: _name {std::string(name, 0 , _maxNameLength)},
_sortName {_name},
_MBID {MBID ? MBID->getAsString() : ""}
{
}
std::vector<Artist::pointer>
Artist::getByName(Session& session, const std::string& name)
{
session.checkSharedLocked();
Wt::Dbo::collection<Artist::pointer> res = session.getDboSession().find<Artist>().where("name = ?").bind( std::string{name, 0, _maxNameLength} );
return std::vector<Artist::pointer>(res.begin(), res.end());
}
Artist::pointer
Artist::getByMBID(Session& session, const UUID& mbid)
{
session.checkSharedLocked();
return session.getDboSession().find<Artist>().where("mbid = ?").bind(std::string {mbid.getAsString()});
}
Artist::pointer
Artist::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<Artist>().where("id = ?").bind(id);
}
Artist::pointer
Artist::create(Session& session, const std::string& name, const std::optional<UUID>& MBID)
{
session.checkUniqueLocked();
Artist::pointer res {session.getDboSession().add(std::make_unique<Artist>(name, MBID))};
session.getDboSession().flush();
return res;
}
std::vector<Artist::pointer>
Artist::getAll(Session& session, std::optional<std::size_t> offset, std::optional<std::size_t> size)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().find<Artist>()
.offset(offset ? static_cast<int>(*offset) : -1)
.limit(size ? static_cast<int>(*size) : -1)
.orderBy("sort_name COLLATE NOCASE");
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<IdType>
Artist::getAllIds(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<IdType> res = session.getDboSession().query<IdType>("SELECT id FROM artist");
return std::vector<IdType>(res.begin(), res.end());
}
std::vector<Artist::pointer>
Artist::getAllOrphans(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<Wt::Dbo::ptr<Artist>> res {session.getDboSession().query<Wt::Dbo::ptr<Artist>>("SELECT DISTINCT a FROM artist a WHERE NOT EXISTS(SELECT 1 FROM track t INNER JOIN track_artist_link t_a_l ON t_a_l.artist_id = a.id WHERE t.id = t_a_l.track_id)")};
return std::vector<pointer>(res.begin(), res.end());
}
static
Wt::Dbo::Query<Artist::pointer>
getQuery(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
std::optional<TrackArtistLink::Type> linkType)
{
session.checkSharedLocked();
WhereClause where;
std::ostringstream oss;
oss << "SELECT DISTINCT a FROM artist a";
for (auto keyword : keywords)
where.And(WhereClause("a.name LIKE ?")).bind("%%" + keyword + "%%");
if (!clusterIds.empty() || linkType)
{
oss << " INNER JOIN track t ON t.id = t_a_l.track_id INNER JOIN track_artist_link t_a_l ON t_a_l.artist_id = a.id";
if (!clusterIds.empty())
{
oss << " INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.Or(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
if (linkType)
where.And(WhereClause {"t_a_l.type = ?"}.bind(std::to_string(static_cast<int>(*linkType))));
}
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(DISTINCT c.id) = " << clusterIds.size();
oss << " ORDER BY a.sort_name COLLATE NOCASE";
Wt::Dbo::Query<Artist::pointer> query = session.getDboSession().query<Artist::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
{
query.bind(bindArg);
}
return query;
}
std::vector<Artist::pointer>
Artist::getByClusters(Session& session, const std::set<IdType>& clusters)
{
assert(!clusters.empty());
session.checkSharedLocked();
bool more;
return getByFilter(session, clusters, {}, {}, {}, {}, more);
}
std::vector<Artist::pointer>
Artist::getByFilter(Session& session,
const std::set<IdType>& clusters,
const std::vector<std::string>& keywords,
std::optional<TrackArtistLink::Type> linkType,
std::optional<std::size_t> offset,
std::optional<std::size_t> size,
bool& moreResults)
{
session.checkSharedLocked();
Wt::Dbo::collection<Artist::pointer> collection = getQuery(session, clusters, keywords, linkType)
.limit(size ? static_cast<int>(*size) + 1 : -1)
.offset(offset ? static_cast<int>(*offset) : -1);
auto res {std::vector<pointer>(collection.begin(), collection.end())};
if (size && res.size() == static_cast<std::size_t>(*size) + 1)
{
moreResults = true;
res.pop_back();
}
else
moreResults = false;
return res;
}
std::vector<Artist::pointer>
Artist::getLastAdded(Session& session, Wt::WDateTime after, std::optional<std::size_t> limit)
{
session.checkSharedLocked();
Wt::Dbo::collection<Artist::pointer> res = session.getDboSession().query<Artist::pointer>("SELECT a from artist a INNER JOIN track_artist_link t_a_l ON t_a_l.artist_id = a.id INNER JOIN track t ON t.id = t_a_l.track_id")
.where("t.file_added > ?").bind(after)
.groupBy("a.id")
.orderBy("t.file_added DESC")
.limit(limit ? static_cast<int>(*limit) : -1);
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Wt::Dbo::ptr<Release>>
Artist::getReleases(const std::set<IdType>& clusterIds) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
WhereClause where;
std::ostringstream oss;
oss << "SELECT DISTINCT r FROM release r INNER JOIN artist a ON a.id = t_a_l.artist_id INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id INNER JOIN track t ON t.release_id = r.id";
if (!clusterIds.empty())
{
oss << " INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.Or(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
where.And(WhereClause("a.id = ?")).bind(std::to_string(id()));
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(DISTINCT c.id) = " << clusterIds.size();
oss << " ORDER BY t.year,r.name";
Wt::Dbo::Query<Release::pointer> query = session()->query<Release::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
Wt::Dbo::collection<Wt::Dbo::ptr<Release>> res = query;
return std::vector<Wt::Dbo::ptr<Release>>(res.begin(), res.end());
}
std::size_t
Artist::getReleaseCount() const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
int res = session()->query<int>("SELECT COUNT(DISTINCT r.id) FROM release r INNER JOIN artist a ON a.id = t_a_l.artist_id INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id INNER JOIN track t ON t.release_id = r.id")
.where("a.id = ?").bind(self()->id());
return res;
}
std::vector<Wt::Dbo::ptr<Track>>
Artist::getTracks(std::optional<TrackArtistLink::Type> linkType) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
auto query {session()->query<Wt::Dbo::ptr<Track>>("SELECT DISTINCT t FROM track t INNER JOIN artist a ON a.id = t_a_l.artist_id INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id")
.where("a.id = ?").bind(self()->id())
.orderBy("t.year,t.release_id,t.disc_number,t.track_number")};
if (linkType)
query.where("t_a_l.type = ?").bind(*linkType);
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> tracks {query.resultList()};
return std::vector<Wt::Dbo::ptr<Track>>(tracks.begin(), tracks.end());
}
std::vector<Wt::Dbo::ptr<Track>>
Artist::getTracksWithRelease(std::optional<TrackArtistLink::Type> linkType) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
auto query {session()->query<Wt::Dbo::ptr<Track>>("SELECT t FROM track t INNER JOIN artist a ON a.id = t_a_l.artist_id INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id INNER JOIN release r ON r.id = t.release_id")
.where("a.id = ?").bind(self()->id())
.orderBy("t.year,r.name,t.disc_number,t.track_number")};
if (linkType)
query.where("t_a_l.type = ?").bind(*linkType);
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> tracks {query.resultList()};
return std::vector<Wt::Dbo::ptr<Track>>(tracks.begin(), tracks.end());
}
std::vector<Wt::Dbo::ptr<Track>>
Artist::getRandomTracks(std::optional<std::size_t> count) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> tracks {session()->query<Wt::Dbo::ptr<Track>>("SELECT t from track t INNER JOIN artist a ON a.id = t_a_l.artist_id INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id")
.where("a.id = ?").bind(self()->id())
.orderBy("RANDOM()")
.limit(count ? static_cast<int>(*count) : -1)};
return std::vector<Wt::Dbo::ptr<Track>>(tracks.begin(), tracks.end());
}
std::vector<Wt::Dbo::ptr<Artist>>
Artist::getSimilarArtists(std::optional<std::size_t> offset, std::optional<std::size_t> count) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::Query<pointer> query {session()->query<pointer>(
"SELECT a FROM artist a"
" INNER JOIN track_artist_link t_a_l ON t_a_l.artist_id = a.id"
" INNER JOIN track t ON t.id = t_a_l.track_id"
" INNER JOIN track_cluster t_c ON t_c.track_id = t.id"
" WHERE "
" t_c.cluster_id IN (SELECT c.id from cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN artist a ON a.id = t_a_l.artist_id INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id WHERE a.id = ?)"
" AND a.id <> ?"
)
.bind(self()->id())
.bind(self()->id())
.groupBy("a.id")
.orderBy("COUNT(*) DESC")
.limit(count ? static_cast<int>(*count) : -1)
.offset(offset ? static_cast<int>(*offset) : -1)};
Wt::Dbo::collection<pointer> res = query;
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<std::vector<Wt::Dbo::ptr<Cluster>>>
Artist::getClusterGroups(std::vector<ClusterType::pointer> clusterTypes, std::size_t size) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
WhereClause where;
std::ostringstream oss;
oss << "SELECT c FROM cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN cluster_type c_type ON c.cluster_type_id = c_type.id INNER JOIN artist a ON t_a_l.artist_id = a.id INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id";
where.And(WhereClause("a.id = ?")).bind(std::to_string(self()->id()));
{
WhereClause clusterClause;
for (auto clusterType : clusterTypes)
clusterClause.Or(WhereClause("c_type.id = ?")).bind(std::to_string(clusterType.id()));
where.And(clusterClause);
}
oss << " " << where.get();
oss << "GROUP BY c.id ORDER BY COUNT(DISTINCT c.id) DESC";
Wt::Dbo::Query<Cluster::pointer> query = session()->query<Cluster::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
Wt::Dbo::collection<Cluster::pointer> queryRes = query;
std::map<IdType, std::vector<Cluster::pointer>> clusters;
for (auto cluster : queryRes)
{
if (clusters[cluster->getType().id()].size() < size)
clusters[cluster->getType().id()].push_back(cluster);
}
std::vector<std::vector<Cluster::pointer>> res;
for (auto cluster_list : clusters)
res.push_back(cluster_list.second);
return res;
}
void
Artist::setSortName(const std::string& sortName)
{
_sortName = std::string(sortName, 0 , _maxNameLength);
}
} // namespace Database
+206
View File
@@ -0,0 +1,206 @@
/*
* Copyright (C) 2013-2016 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/Cluster.hpp"
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "SqlQuery.hpp"
namespace Database {
Cluster::Cluster()
{
}
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name)
: _name(std::string(name, 0, _maxNameLength)),
_clusterType(type)
{
}
Cluster::pointer
Cluster::create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name)
{
session.checkUniqueLocked();
Cluster::pointer res {session.getDboSession().add(std::make_unique<Cluster>(type, name))};
session.getDboSession().flush();
return res;
}
std::vector<Cluster::pointer>
Cluster::getAll(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<Cluster::pointer> res {session.getDboSession().find<Cluster>()};
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
std::vector<Cluster::pointer>
Cluster::getAllOrphans(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<Cluster::pointer> res {session.getDboSession().query<Cluster::pointer>("SELECT DISTINCT c FROM cluster c WHERE NOT EXISTS(SELECT 1 FROM track_cluster t_c WHERE t_c.cluster_id = c.id)")};
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
Cluster::pointer
Cluster::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<Cluster>().where("id = ?").bind(id);
}
void
Cluster::addTrack(Wt::Dbo::ptr<Track> track)
{
_tracks.insert(track);
}
std::vector<Wt::Dbo::ptr<Track>>
Cluster::getTracks(std::optional<std::size_t> offset, std::optional<std::size_t> limit) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<Track::pointer> res
{session()->query<Track::pointer>("SELECT t FROM track t INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id")
.where("c.id = ?").bind(self()->id())
.offset(offset ? static_cast<int>(*offset) : -1)
.limit(limit ? static_cast<int>(*limit) : -1)};
return std::vector<Wt::Dbo::ptr<Track>>(res.begin(), res.end());
}
std::set<IdType>
Cluster::getTrackIds() const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<IdType> res = session()->query<IdType>("SELECT t_c.track_id FROM track_cluster t_c INNER JOIN cluster c ON c.id = t_c.cluster_id")
.where("c.id = ?").bind(self()->id());
return std::set<IdType>(res.begin(), res.end());
}
std::size_t
Cluster::getReleasesCount() const
{
assert(session());
assert(IdIsValid(self()->id()));
return session()->query<int>("SELECT COUNT(DISTINCT r.id) FROM release r INNER JOIN track t on t.release_id = r.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id")
.where("c.id = ?").bind(self()->id());
}
ClusterType::ClusterType(std::string name)
: _name(name)
{
}
std::vector<ClusterType::pointer>
ClusterType::getAllOrphans(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().query<Wt::Dbo::ptr<ClusterType>>("select c_t from cluster_type c_t LEFT OUTER JOIN cluster c ON c_t.id = c.cluster_type_id WHERE c.id IS NULL");
return std::vector<pointer>(res.begin(), res.end());
}
ClusterType::pointer
ClusterType::getByName(Session& session, const std::string& name)
{
session.checkSharedLocked();
return session.getDboSession().find<ClusterType>().where("name = ?").bind(name);
}
ClusterType::pointer
ClusterType::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<ClusterType>().where("id= ?").bind(id);
}
std::vector<ClusterType::pointer>
ClusterType::getAll(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().find<ClusterType>();
return std::vector<pointer>(res.begin(), res.end());
}
ClusterType::pointer
ClusterType::create(Session& session, const std::string& name)
{
session.checkUniqueLocked();
ClusterType::pointer res {session.getDboSession().add(std::make_unique<ClusterType>(name))};
session.getDboSession().flush();
return res;
}
Cluster::pointer
ClusterType::getCluster(const std::string& name) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
return session()->find<Cluster>()
.where("name = ?").bind(name)
.where("cluster_type_id = ?").bind(self()->id());
}
std::vector<Cluster::pointer>
ClusterType::getClusters() const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<Cluster::pointer> res = session()->find<Cluster>()
.where("cluster_type_id = ?").bind(self()->id())
.orderBy("name");
return std::vector<Cluster::pointer>(res.begin(), res.end());
}
} // namespace Database
+47
View File
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019 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/Db.hpp"
#include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include "database/User.hpp"
#include "utils/Logger.hpp"
namespace Database {
// Session living class handling the database and the login
Db::Db(const std::filesystem::path& dbPath)
{
LMS_LOG(DB, INFO) << "Creating connection pool on file " << dbPath.string();
std::unique_ptr<Wt::Dbo::backend::Sqlite3> connection {std::make_unique<Wt::Dbo::backend::Sqlite3>(dbPath.string())};
connection->executeSql("pragma journal_mode=WAL");
// connection->setProperty("show-queries", "true");
auto connectionPool = std::make_unique<Wt::Dbo::FixedSqlConnectionPool>(std::move(connection), 10);
connectionPool->setTimeout(std::chrono::seconds(10));
_connectionPool = std::move(connectionPool);
}
} // namespace Database
+493
View File
@@ -0,0 +1,493 @@
/*
* Copyright (C) 2015 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/Release.hpp"
#include "utils/Logger.hpp"
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/User.hpp"
#include "SqlQuery.hpp"
namespace Database
{
Release::Release(const std::string& name, const std::optional<UUID>& MBID)
: _name {std::string(name, 0 , _maxNameLength)},
_MBID {MBID ? MBID->getAsString() : ""}
{
}
std::vector<Release::pointer>
Release::getByName(Session& session, const std::string& name)
{
session.checkUniqueLocked();
Wt::Dbo::collection<Release::pointer> res = session.getDboSession().find<Release>().where("name = ?").bind( std::string(name, 0, _maxNameLength) );
return std::vector<Release::pointer>(res.begin(), res.end());
}
Release::pointer
Release::getByMBID(Session& session, const UUID& mbid)
{
session.checkSharedLocked();
return session.getDboSession().find<Release>().where("mbid = ?").bind(std::string {mbid.getAsString()});
}
Release::pointer
Release::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<Release>().where("id = ?").bind(id);
}
Release::pointer
Release::create(Session& session, const std::string& name, const std::optional<UUID>& MBID)
{
session.checkSharedLocked();
Release::pointer res {session.getDboSession().add(std::make_unique<Release>(name, MBID))};
session.getDboSession().flush();
return res;
}
std::size_t
Release::getCount(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> releases {session.getDboSession().find<Release>()};
return releases.size();
}
std::vector<Release::pointer>
Release::getAll(Session& session, std::optional<std::size_t> offset, std::optional<std::size_t> size)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().find<Release>()
.offset(offset ? static_cast<int>(*offset) : -1)
.limit(size ? static_cast<int>(*size) : -1)
.orderBy("name COLLATE NOCASE");
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<IdType>
Release::getAllIds(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<IdType> res = session.getDboSession().query<IdType>("SELECT id FROM release");
return std::vector<IdType>(res.begin(), res.end());
}
std::vector<Release::pointer>
Release::getAllOrderedByArtist(Session& session, std::optional<std::size_t> offset, std::optional<std::size_t> size)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().query<Wt::Dbo::ptr<Release>>(
"SELECT DISTINCT r FROM release r"
" INNER JOIN track t ON r.id = t.release_id"
" INNER JOIN track_artist_link t_a_l ON t_a_l.track_id = t.id"
" INNER JOIN artist a ON t_a_l.artist_id = a.id")
.offset(offset ? static_cast<int>(*offset) : -1)
.limit(size ? static_cast<int>(*size) : -1)
.orderBy("a.name COLLATE NOCASE, r.name COLLATE NOCASE");
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Release::pointer>
Release::getAllRandom(Session& session, std::optional<std::size_t> size)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().find<Release>()
.limit(size ? static_cast<int>(*size) : -1)
.orderBy("RANDOM()");
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Release::pointer>
Release::getAllOrphans(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<Release::pointer> res = session.getDboSession().query<Wt::Dbo::ptr<Release>>("select r from release r LEFT OUTER JOIN Track t ON r.id = t.release_id WHERE t.id IS NULL");
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Release::pointer>
Release::getLastAdded(Session& session, const Wt::WDateTime& after, std::optional<std::size_t> offset, std::optional<std::size_t> limit)
{
session.checkSharedLocked();
Wt::Dbo::collection<Release::pointer> res = session.getDboSession().query<Release::pointer>("SELECT r from release r INNER JOIN track t ON r.id = t.release_id")
.where("t.file_added > ?").bind(after)
.groupBy("r.id")
.orderBy("t.file_added DESC")
.offset(offset ? static_cast<int>(*offset) : -1)
.limit(limit ? static_cast<int>(*limit) : -1);
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Release::pointer>
Release::getByYear(Session& session, int yearFrom, int yearTo, std::optional<std::size_t> offset, std::optional<std::size_t> limit)
{
Wt::Dbo::collection<Release::pointer> res = session.getDboSession().query<Release::pointer>
("SELECT DISTINCT r from release r INNER JOIN track t ON r.id = t.release_id")
.where("t.year >= ?").bind(yearFrom)
.where("t.year <= ?").bind(yearTo)
.orderBy("t.year, r.name COLLATE NOCASE")
.offset(offset ? static_cast<int>(*offset) : -1)
.limit(limit ? static_cast<int>(*limit) : -1);
return std::vector<pointer>(res.begin(), res.end());
}
static
Wt::Dbo::Query<Release::pointer>
getQuery(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords)
{
WhereClause where;
std::ostringstream oss;
oss << "SELECT DISTINCT r FROM release r";
for (auto keyword : keywords)
where.And(WhereClause("r.name LIKE ?")).bind("%%" + keyword + "%%");
if (!clusterIds.empty())
{
oss << " INNER JOIN track t ON t.release_id = r.id INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.Or(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(*) = " << clusterIds.size();
oss << " ORDER BY r.name COLLATE NOCASE";
Wt::Dbo::Query<Release::pointer> query = session.getDboSession().query<Release::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
return query;
}
std::vector<Release::pointer>
Release::getByClusters(Session& session, const std::set<IdType>& clusters)
{
assert(!clusters.empty());
session.checkSharedLocked();
bool moreResults;
return getByFilter(session, clusters, {}, {}, {}, moreResults);
}
std::vector<Release::pointer>
Release::getByFilter(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
std::optional<std::size_t> offset,
std::optional<std::size_t> size,
bool& moreResults)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> collection = getQuery(session, clusterIds, keywords)
.limit(size ? static_cast<int>(*size) + 1 : -1)
.offset(offset ? static_cast<int>(*offset) : -1);
auto res {std::vector<pointer>(collection.begin(), collection.end())};
if (size && res.size() == static_cast<std::size_t>(*size) + 1)
{
moreResults = true;
res.pop_back();
}
else
moreResults = false;
return res;
}
std::optional<std::size_t>
Release::getTotalTrackNumber(void) const
{
return (_totalTrackNumber > 0) ? std::make_optional<std::size_t>(_totalTrackNumber) : std::nullopt;
}
std::optional<std::size_t>
Release::getTotalDiscNumber(void) const
{
return (_totalDiscNumber > 0) ? std::make_optional<std::size_t>(_totalDiscNumber) : std::nullopt;
}
std::optional<int>
Release::getReleaseYear(bool original) const
{
assert(session());
const std::string field {original ? "original_year" : "year"};
Wt::Dbo::collection<int> dates = session()->query<int>(
std::string{"SELECT "} + "t." + field + " FROM track t INNER JOIN release r ON r.id = t.release_id")
.where("r.id = ?")
.groupBy(field)
.bind(this->id());
// various dates => no date
if (dates.empty() || dates.size() > 1)
return std::nullopt;
auto date {dates.front()};
if (date > 0)
return date;
else
return std::nullopt;
}
std::optional<std::string>
Release::getCopyright() const
{
assert(session());
Wt::Dbo::collection<std::string> copyrights = session()->query<std::string>
("SELECT copyright FROM track t INNER JOIN release r ON r.id = t.release_id")
.where("r.id = ?")
.groupBy("copyright")
.bind(this->id());
std::vector<std::string> values(copyrights.begin(), copyrights.end());
// various copyrights => no copyright
if (values.empty() || values.size() > 1 || values.front().empty())
return std::nullopt;
return values.front();
}
std::optional<std::string>
Release::getCopyrightURL() const
{
assert(session());
Wt::Dbo::collection<std::string> copyrights = session()->query<std::string>
("SELECT copyright_url FROM track t INNER JOIN release r ON r.id = t.release_id")
.where("r.id = ?")
.groupBy("copyright_url")
.bind(this->id());
std::vector<std::string> values(copyrights.begin(), copyrights.end());
// various copyright URLs => no copyright URL
if (values.empty() || values.size() > 1 || values.front().empty())
return std::nullopt;
return values.front();
}
std::vector<Wt::Dbo::ptr<Artist>>
Release::getArtists(TrackArtistLink::Type linkType) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<Wt::Dbo::ptr<Artist>> res = session()->query<Wt::Dbo::ptr<Artist>>(
"SELECT DISTINCT a FROM artist a"
" INNER JOIN track_artist_link t_a_l ON t_a_l.artist_id = a.id"
" INNER JOIN track t ON t.id = t_a_l.track_id"
" INNER JOIN release r ON r.id = t.release_id")
.where("r.id = ?").bind(self()->id())
.where("t_a_l.type = ?").bind(linkType);
return std::vector<Wt::Dbo::ptr<Artist>>(res.begin(), res.end());
}
std::vector<Release::pointer>
Release::getSimilarReleases(std::optional<std::size_t> offset, std::optional<std::size_t> count) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::Query<pointer> query {session()->query<pointer>(
"SELECT r FROM release r"
" INNER JOIN track t ON t.release_id = r.id"
" INNER JOIN track_cluster t_c ON t_c.track_id = t.id"
" WHERE "
" t_c.cluster_id IN (SELECT c.id from cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN release r ON r.id = t.release_id WHERE r.id = ?)"
" AND r.id <> ?"
)
.bind(self()->id())
.bind(self()->id())
.groupBy("r.id")
.orderBy("COUNT(*) DESC")
.limit(count ? static_cast<int>(*count) : -1)
.offset(offset ? static_cast<int>(*offset) : -1)};
Wt::Dbo::collection<pointer> res = query;
return std::vector<pointer>(res.begin(), res.end());
}
bool
Release::hasVariousArtists() const
{
// TODO optimize
return getArtists().size() > 1;
}
std::vector<Wt::Dbo::ptr<Track>>
Release::getTracks(const std::set<IdType>& clusterIds) const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Release>::invalidId() );
assert(session());
WhereClause where;
std::ostringstream oss;
oss << "SELECT t FROM track t INNER JOIN release r ON t.release_id = r.id";
if (!clusterIds.empty())
{
oss << " INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.Or(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
where.And(WhereClause("r.id = ?")).bind(std::to_string(id()));
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(*) = " << clusterIds.size();
oss << " ORDER BY t.disc_number,t.track_number";
Wt::Dbo::Query<Track::pointer> query = session()->query<Track::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
{
query.bind(bindArg);
}
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > res = query;
return std::vector< Wt::Dbo::ptr<Track> > (res.begin(), res.end());
}
std::size_t
Release::getTracksCount() const
{
return _tracks.size();
}
std::chrono::milliseconds
Release::getDuration() const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Artist>::invalidId() );
assert(session());
using milli = std::chrono::duration<int, std::milli>;
Wt::Dbo::Query<milli> query {session()->query<milli>("SELECT SUM(duration) FROM track t INNER JOIN release r ON t.release_id = r.id")
.where("r.id = ?").bind(self()->id())};
return query.resultValue();
}
std::vector<std::vector<Wt::Dbo::ptr<Cluster>>>
Release::getClusterGroups(std::vector<ClusterType::pointer> clusterTypes, std::size_t size) const
{
assert(self());
assert(self()->id() != Wt::Dbo::dbo_traits<Artist>::invalidId() );
assert(session());
WhereClause where;
std::ostringstream oss;
oss << "SELECT c from cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN cluster_type c_type ON c.cluster_type_id = c_type.id INNER JOIN release r ON t.release_id = r.id ";
where.And(WhereClause("r.id = ?")).bind(std::to_string(self()->id()));
{
WhereClause clusterClause;
for (auto clusterType : clusterTypes)
clusterClause.Or(WhereClause("c_type.id = ?")).bind(std::to_string(clusterType.id()));
where.And(clusterClause);
}
oss << " " << where.get();
oss << " GROUP BY c.id ORDER BY COUNT(c.id) DESC";
Wt::Dbo::Query<Cluster::pointer> query = session()->query<Cluster::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
Wt::Dbo::collection<Cluster::pointer> queryRes = query;
std::map<IdType, std::vector<Cluster::pointer>> clusters;
for (auto cluster : queryRes)
{
if (clusters[cluster->getType().id()].size() < size)
clusters[cluster->getType().id()].push_back(cluster);
}
std::vector<std::vector<Cluster::pointer>> res;
for (auto cluster_list : clusters)
res.push_back(cluster_list.second);
return res;
}
} // namespace Database
+146
View File
@@ -0,0 +1,146 @@
/*
* Copyright (C) 2018 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/ScanSettings.hpp"
#include <Wt/Dbo/WtSqlTraits.h>
#include "utils/Logger.hpp"
#include "utils/String.hpp"
#include "database/Cluster.hpp"
#include "database/Session.hpp"
namespace {
const std::set<std::string> defaultClusterTypeNames =
{
"GENRE",
"ALBUMGROUPING",
"MOOD",
"ALBUMMOOD",
};
}
namespace Database {
void
ScanSettings::init(Session& session)
{
session.checkUniqueLocked();
pointer settings {get(session)};
if (settings)
return;
settings = session.getDboSession().add(std::make_unique<ScanSettings>());
settings.modify()->setClusterTypes(session, defaultClusterTypeNames );
}
ScanSettings::pointer
ScanSettings::get(Session& session)
{
session.checkSharedLocked();
return session.getDboSession().find<ScanSettings>();
}
std::set<std::filesystem::path>
ScanSettings::getAudioFileExtensions() const
{
auto extensions = StringUtils::splitString(_audioFileExtensions, " ");
return std::set<std::filesystem::path>(std::cbegin(extensions), std::cend(extensions));
}
void
ScanSettings::addAudioFileExtension(const std::filesystem::path& ext)
{
_audioFileExtensions += " " + ext.string();
}
std::vector<ClusterType::pointer>
ScanSettings::getClusterTypes() const
{
return std::vector<ClusterType::pointer>(std::cbegin(_clusterTypes), std::cend(_clusterTypes));
}
void
ScanSettings::setMediaDirectory(const std::filesystem::path& p)
{
_mediaDirectory = StringUtils::stringTrimEnd(p.string(), "/\\");
}
template <typename It>
std::set<std::string> getNames(It begin, It end)
{
std::set<std::string> names;
std::transform(begin, end, std::inserter(names, std::cbegin(names)),
[](const ClusterType::pointer& clusterType)
{
return clusterType->getName();
});
return names;
}
void
ScanSettings::setClusterTypes(Session& session, const std::set<std::string>& clusterTypeNames)
{
session.checkUniqueLocked();
bool needRescan {};
// Create any missing cluster type
for (const std::string& clusterTypeName : clusterTypeNames)
{
auto clusterType {ClusterType::getByName(session, clusterTypeName)};
if (!clusterType)
{
LMS_LOG(DB, INFO) << "Creating cluster type " << clusterTypeName;
clusterType = ClusterType::create(session, clusterTypeName);
_clusterTypes.insert(clusterType);
needRescan = true;
}
}
// Delete no longer existing cluster types
for (ClusterType::pointer& clusterType : _clusterTypes)
{
if (std::none_of(clusterTypeNames.begin(), clusterTypeNames.end(),
[clusterType](const std::string& name) { return name == clusterType->getName(); }))
{
LMS_LOG(DB, INFO) << "Deleting cluster type " << clusterType->getName();
clusterType.remove();
}
}
if (needRescan)
_scanVersion += 1;
}
void
ScanSettings::incScanVersion()
{
_scanVersion += 1;
}
} // namespace Database
+314
View File
@@ -0,0 +1,314 @@
/*
* Copyright (C) 2013 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/Session.hpp"
#include <map>
#include <mutex>
#include <thread>
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/ScanSettings.hpp"
#include "database/Track.hpp"
#include "database/TrackBookmark.hpp"
#include "database/TrackArtistLink.hpp"
#include "database/TrackList.hpp"
#include "database/TrackFeatures.hpp"
#include "database/User.hpp"
namespace Database {
#define LMS_DATABASE_VERSION 12
using Version = std::size_t;
class VersionInfo
{
public:
using pointer = Wt::Dbo::ptr<VersionInfo>;
static VersionInfo::pointer getOrCreate(Session& session)
{
session.checkUniqueLocked();
pointer versionInfo {session.getDboSession().find<VersionInfo>()};
if (!versionInfo)
return session.getDboSession().add(std::make_unique<VersionInfo>());
return versionInfo;
}
static VersionInfo::pointer get(Session& session)
{
session.checkSharedLocked();
return session.getDboSession().find<VersionInfo>();
}
Version getVersion() const { return _version; }
void setVersion(Version version) { _version = static_cast<int>(version); }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _version, "db_version");
}
private:
int _version {LMS_DATABASE_VERSION};
};
void
Session::doDatabaseMigrationIfNeeded()
{
auto uniqueTransaction {createUniqueTransaction()};
static const std::string outdatedMsg {"Outdated database, please rebuild it (delete the .db file and restart)"};
Version version;
try
{
version = VersionInfo::getOrCreate(*this)->getVersion();
LMS_LOG(DB, INFO) << "Database version = " << version << ", LMS binary version = " << LMS_DATABASE_VERSION;
if (version == LMS_DATABASE_VERSION)
return;
}
catch (std::exception& e)
{
LMS_LOG(DB, ERROR) << "Cannot get database version info: " << e.what();
throw LmsException {outdatedMsg};
}
while (version < LMS_DATABASE_VERSION)
{
LMS_LOG(DB, INFO) << "Migrating database from version " << version << "...";
if (version == 5)
{
_session.execute("DELETE FROM auth_token"); // format has changed
}
else if (version == 6)
{
// Just increment the scan version of the settings to make the next scheduled scan rescan everything
ScanSettings::get(*this).modify()->incScanVersion();
}
else if (version == 7)
{
_session.execute("DROP TABLE similarity_settings");
_session.execute("DROP TABLE similarity_settings_feature");
_session.execute("ALTER TABLE scan_settings ADD similarity_engine_type INTEGER NOT NULL DEFAULT(" + std::to_string(static_cast<int>(ScanSettings::SimilarityEngineType::Clusters)) + ")");
}
else if (version == 8)
{
// Better cover handling, need to rescan the whole files
// Just increment the scan version of the settings to make the next scheduled scan rescan everything
ScanSettings::get(*this).modify()->incScanVersion();
}
else if (version == 9)
{
_session.execute(R"(
CREATE TABLE IF NOT EXISTS "track_bookmark" (
"id" integer primary key autoincrement,
"version" integer not null,
"offset" integer,
"comment" text not null,
"track_id" bigint,
"user_id" bigint,
constraint "fk_track_bookmark_track" foreign key ("track_id") references "track" ("id") on delete cascade deferrable initially deferred,
constraint "fk_track_bookmark_user" foreign key ("user_id") references "user" ("id") on delete cascade deferrable initially deferred
);)");
}
else if (version == 10)
{
ScanSettings::get(*this).modify()->addAudioFileExtension(".m4b");
ScanSettings::get(*this).modify()->addAudioFileExtension(".alac");
}
else if (version == 11)
{
// Sanitize bad MBID, need to rescan the whole files
// Just increment the scan version of the settings to make the next scheduled scan rescan everything
ScanSettings::get(*this).modify()->incScanVersion();
}
else
{
LMS_LOG(DB, ERROR) << "Database version " << version << " cannot be handled using migration";
throw LmsException { LMS_DATABASE_VERSION > version ? outdatedMsg : "Server binary outdated, please upgrade it to handle this database"};
}
++version;
VersionInfo::get(*this).modify()->setVersion(LMS_DATABASE_VERSION);
}
}
Session::Session(Db& db)
: _db {db}
{
_session.setConnectionPool(_db.getConnectionPool());
_session.mapClass<VersionInfo>("version_info");
_session.mapClass<Artist>("artist");
_session.mapClass<AuthToken>("auth_token");
_session.mapClass<Cluster>("cluster");
_session.mapClass<ClusterType>("cluster_type");
_session.mapClass<Release>("release");
_session.mapClass<ScanSettings>("scan_settings");
_session.mapClass<Track>("track");
_session.mapClass<TrackBookmark>("track_bookmark");
_session.mapClass<TrackArtistLink>("track_artist_link");
_session.mapClass<TrackFeatures>("track_features");
_session.mapClass<TrackList>("tracklist");
_session.mapClass<TrackListEntry>("tracklist_entry");
_session.mapClass<User>("user");
}
enum class OwnedLock
{
None,
Shared,
Unique,
};
static thread_local std::map<std::shared_mutex*, OwnedLock> lockDebug;
UniqueTransaction::UniqueTransaction(std::shared_mutex& mutex, Wt::Dbo::Session& session)
: _lock {mutex},
_transaction {session}
{
assert(lockDebug[_lock.mutex()] == OwnedLock::None);
lockDebug[_lock.mutex()] = OwnedLock::Unique;
}
UniqueTransaction::~UniqueTransaction()
{
assert(lockDebug[_lock.mutex()] == OwnedLock::Unique);
lockDebug[_lock.mutex()] = OwnedLock::None;
}
SharedTransaction::SharedTransaction(std::shared_mutex& mutex, Wt::Dbo::Session& session)
: _lock {mutex},
_transaction {session}
{
assert(lockDebug[_lock.mutex()] == OwnedLock::None);
lockDebug[_lock.mutex()] = OwnedLock::Shared;
}
SharedTransaction::~SharedTransaction()
{
assert(lockDebug[_lock.mutex()] == OwnedLock::Shared);
lockDebug[_lock.mutex()] = OwnedLock::None;
}
void
Session::checkUniqueLocked()
{
assert(lockDebug[&_db.getMutex()] == OwnedLock::Unique);
}
void
Session::checkSharedLocked()
{
assert(lockDebug[&_db.getMutex()] != OwnedLock::None);
}
UniqueTransaction
Session::createUniqueTransaction()
{
return UniqueTransaction{_db.getMutex(), _session};
}
SharedTransaction
Session::createSharedTransaction()
{
return SharedTransaction{_db.getMutex(), _session};
}
void
Session::prepareTables()
{
// Creation case
try {
_session.createTables();
LMS_LOG(DB, INFO) << "Tables created";
}
catch (Wt::Dbo::Exception& e)
{
LMS_LOG(DB, ERROR) << "Cannot create tables: " << e.what();
}
doDatabaseMigrationIfNeeded();
// Indexes
{
auto uniqueTransaction {createUniqueTransaction()};
_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_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_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 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_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_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 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_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_release_idx ON track(release_id)");
_session.execute("CREATE INDEX IF NOT EXISTS track_year_idx ON track(year)");
_session.execute("CREATE INDEX IF NOT EXISTS track_original_year_idx ON track(original_year)");
_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 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_name_idx ON track_artist_link(name)");
_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_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)");
}
// Initial settings tables
{
auto uniqueTransaction {createUniqueTransaction()};
ScanSettings::init(*this);
}
}
void
Session::optimize()
{
auto uniqueTransaction {createUniqueTransaction()};
_session.execute("ANALYZE");
}
} // namespace Database
+69
View File
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 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/SessionPool.hpp"
#include "database/Session.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
namespace Database {
SessionPool::SessionPool(Db& database, std::size_t maxSessionCount)
: _db {database},
_maxSessionCount {maxSessionCount}
{
}
Session&
SessionPool::acquireSession()
{
std::scoped_lock lock {_mutex};
if (_freeSessions.empty())
{
if (_acquiredSessions.size() == _maxSessionCount)
throw LmsException {"Too many database sessions!"};
_freeSessions.emplace_back(std::make_unique<Session>(_db));
}
std::unique_ptr<Session> session {std::move(_freeSessions.back())};
_freeSessions.pop_back();
_acquiredSessions.push_back(std::move(session));
return *_acquiredSessions.back().get();
}
void
SessionPool::releaseSession(Session& sessionToRelease)
{
std::scoped_lock lock {_mutex};
auto it {std::find_if(std::begin(_acquiredSessions), std::end(_acquiredSessions), [&](const std::unique_ptr<Session>& session) { return session.get() == &sessionToRelease; })};
if (it == std::end(_acquiredSessions))
throw LmsException {"Unknown released Session!"};
std::unique_ptr<Session> session {std::move(*it)};
_acquiredSessions.erase(it);
_freeSessions.push_back(std::move(session));
}
} // namespace Database
+199
View File
@@ -0,0 +1,199 @@
/*
* Copyright (C) 2013 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 "SqlQuery.hpp"
#include <algorithm>
#include <cassert>
#include <sstream>
WhereClause&
WhereClause::And(const WhereClause& otherClause)
{
if (!otherClause._clause.empty()) {
if (!_clause.empty())
_clause += " AND ";
_clause += "(" + otherClause._clause + ")";
// Add associated bind args
for (const std::string& otherBindArg : otherClause._bindArgs)
{
_bindArgs.push_back(otherBindArg);
}
}
return *this;
}
WhereClause&
WhereClause::Or(const WhereClause& otherClause)
{
if (!otherClause._clause.empty()) {
if (!_clause.empty())
_clause += " OR ";
_clause += "(" + otherClause._clause + ")";
// Add associated bind args
for (const std::string& otherBindArg : otherClause._bindArgs)
{
_bindArgs.push_back(otherBindArg);
}
}
return *this;
}
std::string
WhereClause::get(void) const
{
if (!_clause.empty())
return "WHERE " + _clause;
else
return "";
}
WhereClause&
WhereClause::bind(const std::string& bindArg)
{
assert(_bindArgs.size() < static_cast<std::size_t>(std::count(_clause.begin(), _clause.end(), '?')));
_bindArgs.push_back(bindArg);
return *this;
}
InnerJoinClause::InnerJoinClause(const std::string& clause)
:_clause(clause)
{
}
InnerJoinClause&
InnerJoinClause::And(const InnerJoinClause& clause)
{
if (!_clause.empty())
_clause += " ";
_clause += "INNER JOIN " + clause._clause;
return *this;
}
SelectStatement::SelectStatement(const std::string& statement)
{
And(statement);
}
SelectStatement&
SelectStatement::And(const std::string& statement)
{
_statement.push_back(statement);
_statement.sort();
_statement.unique();
return *this;
}
std::string
SelectStatement::get() const
{
std::string res = "SELECT ";
for (std::list<std::string>::const_iterator it = _statement.begin(); it != _statement.end(); ++it)
{
if (it != _statement.begin())
res += ",";
res += *it;
}
return res;
}
GroupByStatement&
GroupByStatement::And(const GroupByStatement& statement)
{
if( _statement.empty() && !statement._statement.empty())
_statement = "GROUP BY ";
else if (!_statement.empty() && !statement._statement.empty())
_statement += ",";
_statement += statement._statement;
return *this;
}
FromClause::FromClause(const std::string& clause)
{
_clause.push_back(clause);
}
FromClause&
FromClause::And(const FromClause& clause)
{
for (const std::string fromClause : clause._clause)
{
_clause.push_back(fromClause);
}
_clause.sort();
_clause.unique();
return *this;
}
std::string
FromClause::get() const
{
std::ostringstream oss;
if (!_clause.empty())
{
oss << "FROM ";
for (std::list<std::string>::const_iterator it = _clause.begin(); it != _clause.end(); ++it) {
if (it != _clause.begin())
oss << ",";
oss << *it;
}
}
return oss.str();
}
std::string
SqlQuery::get(void) const
{
std::ostringstream oss;
oss << _selectStatement.get();
if (!_fromClause.get().empty())
oss << " " << _fromClause.get();
if (!_innerJoinClause.get().empty())
oss << " " << _innerJoinClause.get();
if (!_whereClause.get().empty())
oss << " " << _whereClause.get();
if (!_groupByStatement.get().empty())
oss << " " << _groupByStatement.get();
return oss.str();
}
+135
View File
@@ -0,0 +1,135 @@
/*
* Copyright (C) 2013 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 <list>
#include <string>
class WhereClause
{
public:
WhereClause() {}
WhereClause(const std::string& clause) { _clause = clause; }
WhereClause& And(const WhereClause& clause);
WhereClause& Or(const WhereClause& clause);
// Arguments binding (for each '?' in where clause)
WhereClause& bind(const std::string& arg);
std::string get() const;
const std::list<std::string>& getBindArgs(void) const {return _bindArgs;}
private:
std::string _clause; // WHERE clause
std::list<std::string> _bindArgs;
};
class InnerJoinClause
{
public:
InnerJoinClause() {}
InnerJoinClause(const std::string& clause);
InnerJoinClause& And(const InnerJoinClause& clause);
std::string get() const { return _clause;}
private:
std::string _clause;
};
class GroupByStatement
{
public:
GroupByStatement() {}
GroupByStatement(const std::string& statement) { _statement = statement; }
GroupByStatement& And(const GroupByStatement& statement);
std::string get() const {return _statement;}
private:
std::string _statement; // SELECT statement
};
class SelectStatement
{
public:
SelectStatement() {};
SelectStatement(const std::string& item);
SelectStatement& And(const std::string& item);
std::string get() const;
private:
std::list<std::string> _statement;
};
class FromClause
{
public:
FromClause() {}
FromClause(const std::string& clause);
FromClause& And(const FromClause& clause);
std::string get() const;
private:
std::list<std::string> _clause;
};
class SqlQuery
{
public:
SelectStatement& select(void) { return _selectStatement;}
SelectStatement& select(const std::string& statement) { _selectStatement = SelectStatement(statement); return _selectStatement; }
FromClause& from(void) { return _fromClause; }
FromClause& from(const std::string& clause) { _whereClause = WhereClause(clause); return _fromClause; }
InnerJoinClause& innerJoin(void) { return _innerJoinClause; }
WhereClause& where(void) { return _whereClause; }
const WhereClause& where(void) const { return _whereClause; }
GroupByStatement& groupBy(void) { return _groupByStatement; }
const GroupByStatement& groupBy(void) const { return _groupByStatement; }
std::string get(void) const;
private:
SelectStatement _selectStatement; // SELECT statement
InnerJoinClause _innerJoinClause; // INNER JOIN
FromClause _fromClause; // FROM tables
WhereClause _whereClause; // WHERE clause
GroupByStatement _groupByStatement; // GROUP BY statement
};
+465
View File
@@ -0,0 +1,465 @@
/*
* Copyright (C) 2013-2016 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/Track.hpp"
#include <Wt/Dbo/WtSqlTraits.h>
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Release.hpp"
#include "database/TrackFeatures.hpp"
#include "database/Session.hpp"
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
namespace Database {
Track::Track(const std::filesystem::path& p)
:
_filePath( p.string() )
{
}
std::vector<Track::pointer>
Track::getAll(Session& session, std::optional<std::size_t> limit)
{
session.checkSharedLocked();
Wt::Dbo::collection<Track::pointer> res {session.getDboSession().find<Track>()
.limit(limit ? static_cast<int>(*limit) : -1)};
return std::vector<Track::pointer>(std::cbegin(res), std::cend(res));
}
std::vector<Track::pointer>
Track::getAllRandom(Session& session, std::optional<std::size_t> limit)
{
session.checkSharedLocked();
Wt::Dbo::collection<Track::pointer> res {session.getDboSession().find<Track>()
.limit(limit ? static_cast<int>(*limit) : -1)
.orderBy("RANDOM()")};
return std::vector<Track::pointer>(std::cbegin(res), std::cend(res));
}
std::vector<IdType>
Track::getAllIds(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<IdType> res = session.getDboSession().query<IdType>("SELECT id FROM track");
return std::vector<IdType>(res.begin(), res.end());
}
Track::pointer
Track::getByPath(Session& session, const std::filesystem::path& p)
{
session.checkSharedLocked();
return session.getDboSession().find<Track>().where("file_path = ?").bind(p.string());
}
Track::pointer
Track::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<Track>()
.where("id = ?").bind(id);
}
Track::pointer
Track::getByMBID(Session& session, const UUID& mbid)
{
session.checkSharedLocked();
return session.getDboSession().find<Track>()
.where("mbid = ?").bind(std::string {mbid.getAsString()});
}
Track::pointer
Track::create(Session& session, const std::filesystem::path& p)
{
session.checkUniqueLocked();
Track::pointer res {session.getDboSession().add(std::make_unique<Track>(p))};
session.getDboSession().flush();
return res;
}
std::vector<std::filesystem::path>
Track::getAllPaths(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<std::string> res = session.getDboSession().query<std::string>("SELECT file_path FROM track");
return std::vector<std::filesystem::path>(res.begin(), res.end());
}
std::vector<Track::pointer>
Track::getMBIDDuplicates(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().query<pointer>( "SELECT track FROM track WHERE mbid in (SELECT mbid FROM track WHERE mbid <> '' GROUP BY mbid HAVING COUNT (*) > 1)").orderBy("track.release_id,track.disc_number,track.track_number,track.mbid");
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Track::pointer>
Track::getLastAdded(Session& session, const Wt::WDateTime& after, std::optional<std::size_t> limit)
{
session.checkSharedLocked();
Wt::Dbo::collection<Track::pointer> res = session.getDboSession().find<Track>()
.where("file_added > ?").bind(after)
.orderBy("file_added DESC")
.limit(limit ? static_cast<int>(*limit) : -1);
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Track::pointer>
Track::getAllWithMBIDAndMissingFeatures(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().query<pointer>
("SELECT t FROM track t")
.where("LENGTH(t.mbid) > 0")
.where("NOT EXISTS (SELECT * FROM track_features t_f WHERE t_f.track_id = t.id)");
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<IdType>
Track::getAllIdsWithFeatures(Session& session, std::optional<std::size_t> limit)
{
session.checkSharedLocked();
Wt::Dbo::collection<IdType> res = session.getDboSession().query<IdType>
("SELECT t.id FROM track t")
.where("EXISTS (SELECT * from track_features t_f WHERE t_f.track_id = t.id)")
.limit(limit ? static_cast<int>(*limit) : -1);
return std::vector<IdType>(res.begin(), res.end());
}
std::vector<Cluster::pointer>
Track::getClusters(void) const
{
std::vector< Cluster::pointer > clusters;
std::copy(_clusters.begin(), _clusters.end(), std::back_inserter(clusters));
return clusters;
}
std::vector<IdType>
Track::getClusterIds(void) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<IdType> res = session()->query<IdType>
("SELECT DISTINCT c.id FROM cluster c INNER JOIN track_cluster t_c ON t_c.cluster_id = c.id INNER JOIN track t ON t.id = t_c.track_id")
.where("t.id = ?").bind(self()->id());
return std::vector<IdType>(res.begin(), res.end());
}
bool
Track::hasTrackFeatures() const
{
return (_trackFeatures.lock() != Database::TrackFeatures::pointer());
}
static
Wt::Dbo::Query< Track::pointer >
getQuery(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords)
{
session.checkSharedLocked();
WhereClause where;
std::ostringstream oss;
oss << "SELECT t FROM track t";
for (auto keyword : keywords)
where.And(WhereClause("t.name LIKE ?")).bind("%%" + keyword + "%%");
if (!clusterIds.empty())
{
oss << " INNER JOIN cluster c ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id";
WhereClause clusterClause;
for (auto id : clusterIds)
clusterClause.Or(WhereClause("c.id = ?")).bind(std::to_string(id));
where.And(clusterClause);
}
oss << " " << where.get();
if (!clusterIds.empty())
oss << " GROUP BY t.id HAVING COUNT(*) = " << clusterIds.size();
oss << " ORDER BY t.name COLLATE NOCASE";
Wt::Dbo::Query<Track::pointer> query = session.getDboSession().query<Track::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
return query;
}
std::vector<Track::pointer>
Track::getByFilter(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
std::optional<std::size_t> offset,
std::optional<std::size_t> size,
bool& moreResults)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> collection = getQuery(session, clusterIds, keywords)
.limit(size ? static_cast<int>(*size) + 1 : -1)
.offset(offset ? static_cast<int>(*offset) : -1);
std::vector<pointer> res(collection.begin(), collection.end());
if (size && (res.size() == static_cast<std::size_t>(*size) + 1))
{
moreResults = true;
res.pop_back();
}
else
moreResults = false;
return res;
}
std::vector<Track::pointer>
Track::getSimilarTracks(Session& session,
const std::set<IdType>& tracks,
std::optional<std::size_t> offset,
std::optional<std::size_t> size)
{
assert(!tracks.empty());
session.checkSharedLocked();
std::ostringstream oss;
for (std::size_t i {}; i < tracks.size(); ++i)
{
if (!oss.str().empty())
oss << ", ";
oss << "?";
}
Wt::Dbo::Query<pointer> query {session.getDboSession().query<pointer>(
"SELECT t FROM track t"
" INNER JOIN track_cluster t_c ON t_c.track_id = t.id"
" AND t_c.cluster_id IN (SELECT c.id FROM cluster c INNER JOIN track_cluster t_c ON t_c.cluster_id = c.id WHERE t_c.track_id IN (" + oss.str() + "))"
" AND t.id NOT IN (" + oss.str() + ")")
.groupBy("t.id")
.orderBy("COUNT(*) DESC")
.limit(size ? static_cast<int>(*size) : -1)
.offset(offset ? static_cast<int>(*offset) : -1)};
for (IdType trackId : tracks)
query.bind(trackId );
for (IdType trackId : tracks)
query.bind(trackId );
Wt::Dbo::collection<pointer> res = query;
return std::vector<pointer>(res.begin(), res.end());
}
std::vector<Track::pointer>
Track::getByClusters(Session& session,
const std::set<IdType>& clusters)
{
assert(!clusters.empty());
session.checkSharedLocked();
bool moreResults;
return getByFilter(session,
clusters,
{},
{},
{},
moreResults);
}
void
Track::clearArtistLinks()
{
_trackArtistLinks.clear();
}
void
Track::addArtistLink(const Wt::Dbo::ptr<TrackArtistLink>& artistLink)
{
_trackArtistLinks.insert(artistLink);
}
void
Track::setClusters(const std::vector<Wt::Dbo::ptr<Cluster>>& clusters)
{
_clusters.clear();
for (const Wt::Dbo::ptr<Cluster>& cluster : clusters)
_clusters.insert(cluster);
}
void
Track::setFeatures(const Wt::Dbo::ptr<TrackFeatures>& features)
{
_trackFeatures = features;
}
std::optional<std::size_t>
Track::getTrackNumber(void) const
{
return (_trackNumber > 0) ? std::make_optional<std::size_t>(_trackNumber) : std::nullopt;
}
std::optional<std::size_t>
Track::getDiscNumber(void) const
{
return (_discNumber > 0) ? std::make_optional<std::size_t>(_discNumber) : std::nullopt;
}
std::optional<int>
Track::getYear() const
{
return (_year > 0) ? std::make_optional<int>(_year) : std::nullopt;
}
std::optional<int>
Track::getOriginalYear() const
{
return (_originalYear > 0) ? std::make_optional<int>(_originalYear) : std::nullopt;
}
std::optional<std::string>
Track::getCopyright() const
{
return _copyright != "" ? std::make_optional<std::string>(_copyright) : std::nullopt;
}
std::optional<std::string>
Track::getCopyrightURL() const
{
return _copyrightURL != "" ? std::make_optional<std::string>(_copyrightURL) : std::nullopt;
}
std::vector<Wt::Dbo::ptr<Artist>>
Track::getArtists(TrackArtistLink::Type type) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<Wt::Dbo::ptr<Artist>> artists {session()->query<Artist::pointer>("SELECT a from artist a INNER JOIN track_artist_link t_a_l ON a.id = t_a_l.artist_id INNER JOIN track t ON t.id = t_a_l.track_id")
.where("t.id = ?").bind(self()->id())
.where("t_a_l.type = ?").bind(type)};
return std::vector<Wt::Dbo::ptr<Artist>>(artists.begin(), artists.end());
}
std::vector<IdType>
Track::getArtistIds(TrackArtistLink::Type type) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
Wt::Dbo::collection<IdType> artists {session()->query<IdType>("SELECT a.id from artist a INNER JOIN track_artist_link t_a_l ON a.id = t_a_l.artist_id INNER JOIN track t ON t.id = t_a_l.track_id")
.where("t.id = ?").bind(self()->id())
.where("t_a_l.type = ?").bind(type)};
return std::vector<IdType>(artists.begin(), artists.end());
}
std::vector<Wt::Dbo::ptr<TrackArtistLink>>
Track::getArtistLinks() const
{
return std::vector<Wt::Dbo::ptr<TrackArtistLink>>(_trackArtistLinks.begin(), _trackArtistLinks.end());
}
Wt::Dbo::ptr<TrackFeatures>
Track::getTrackFeatures() const
{
return _trackFeatures.lock();
}
std::vector<std::vector<Cluster::pointer>>
Track::getClusterGroups(std::vector<ClusterType::pointer> clusterTypes, std::size_t size) const
{
assert(self());
assert(IdIsValid(self()->id()));
assert(session());
WhereClause where;
std::ostringstream oss;
oss << "SELECT c from cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN cluster_type c_type ON c.cluster_type_id = c_type.id";
where.And(WhereClause("t.id = ?")).bind(std::to_string(self()->id()));
{
WhereClause clusterClause;
for (auto clusterType : clusterTypes)
clusterClause.Or(WhereClause("c_type.id = ?")).bind(std::to_string(clusterType.id()));
where.And(clusterClause);
}
oss << " " << where.get();
oss << " GROUP BY c.id ORDER BY COUNT(c.id) DESC";
Wt::Dbo::Query<Cluster::pointer> query = session()->query<Cluster::pointer>( oss.str() );
for (const std::string& bindArg : where.getBindArgs())
query.bind(bindArg);
Wt::Dbo::collection<Cluster::pointer> queryRes = query;
std::map<IdType, std::vector<Cluster::pointer>> clusters;
for (auto cluster : queryRes)
{
if (clusters[cluster->getType().id()].size() < size)
clusters[cluster->getType().id()].push_back(cluster);
}
std::vector<std::vector<Cluster::pointer>> res;
for (auto cluster_list : clusters)
res.push_back(cluster_list.second);
return res;
}
} // namespace Database
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2013-2016 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/TrackArtistLink.hpp"
#include "database/Artist.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
namespace Database {
TrackArtistLink::TrackArtistLink(Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<Artist> artist, Type type)
: _type {type},
_track {track},
_artist {artist}
{
}
TrackArtistLink::pointer
TrackArtistLink::create(Session& session, Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<Artist> artist,Type type)
{
session.checkUniqueLocked();
TrackArtistLink::pointer res {session.getDboSession().add(std::make_unique<TrackArtistLink>(track, artist, type))};
session.getDboSession().flush();
return res;
}
}
+91
View File
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2020 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/TrackBookmark.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/User.hpp"
namespace Database {
TrackBookmark::TrackBookmark(Wt::Dbo::ptr<User> user, Wt::Dbo::ptr<Track> track)
: _user {user},
_track {track}
{
}
TrackBookmark::pointer
TrackBookmark::create(Session& session, Wt::Dbo::ptr<User> user, Wt::Dbo::ptr<Track> track)
{
session.checkUniqueLocked();
TrackBookmark::pointer res {session.getDboSession().add(std::make_unique<TrackBookmark>(user, track))};
session.getDboSession().flush();
return res;
}
std::vector<TrackBookmark::pointer>
TrackBookmark::getAll(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<TrackBookmark::pointer> res {session.getDboSession().find<TrackBookmark>()};
return std::vector<TrackBookmark::pointer>(std::cbegin(res), std::cend(res));
}
std::vector<TrackBookmark::pointer>
TrackBookmark::getByUser(Session& session, Wt::Dbo::ptr<User> user)
{
session.checkSharedLocked();
Wt::Dbo::collection<TrackBookmark::pointer> res
{
session.getDboSession().find<TrackBookmark>()
.where("user_id = ?").bind(user.id())
};
return std::vector<TrackBookmark::pointer>(std::cbegin(res), std::cend(res));
}
TrackBookmark::pointer
TrackBookmark::getByUser(Session& session, Wt::Dbo::ptr<User> user, Wt::Dbo::ptr<Track> track)
{
session.checkSharedLocked();
return session.getDboSession().find<TrackBookmark>()
.where("user_id = ?").bind(user.id())
.where("track_id = ?").bind(track.id());
}
TrackBookmark::pointer
TrackBookmark::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<TrackBookmark>()
.where("id = ?").bind(id);
}
} // namespace Database
+88
View File
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2018 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/TrackFeatures.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "utils/Logger.hpp"
namespace Database {
TrackFeatures::TrackFeatures(Wt::Dbo::ptr<Track> track, const std::string& jsonEncodedFeatures)
: _data(jsonEncodedFeatures),
_track(track)
{
}
TrackFeatures::pointer
TrackFeatures::create(Session& session, Wt::Dbo::ptr<Track> track, const std::string& jsonEncodedFeatures)
{
session.checkUniqueLocked();
return session.getDboSession().add(std::make_unique<TrackFeatures>(track, jsonEncodedFeatures));
}
FeatureValues
TrackFeatures::getFeatureValues(const FeatureName& featureNode) const
{
FeatureValuesMap featuresValuesMap {getFeatureValuesMap({featureNode})};
return std::move(featuresValuesMap[featureNode]);
}
FeatureValuesMap
TrackFeatures::getFeatureValuesMap(const std::unordered_set<FeatureName>& featureNames) const
{
try
{
std::istringstream iss {_data};
boost::property_tree::ptree root;
boost::property_tree::read_json(iss, root);
FeatureValuesMap res;
for (const FeatureName& featureName : featureNames)
{
FeatureValues& featureValues {res[featureName]};
auto node {root.get_child(featureName)};
bool hasChildren = false;
for (const auto& child : node.get_child(""))
{
hasChildren = true;
featureValues.push_back(child.second.get_value<double>());
}
if (!hasChildren)
featureValues.push_back(node.get_value<double>());
}
return res;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(DB, ERROR) << "Track " << _track.id() << ": ptree exception: " << error.what();
return {};
}
}
} // namespace Database
+310
View File
@@ -0,0 +1,310 @@
/*
* Copyright (C) 2014 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/TrackList.hpp"
#include <cassert>
#include "utils/Logger.hpp"
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/User.hpp"
#include "database/Track.hpp"
namespace Database {
TrackList::TrackList(const std::string& name, Type type, bool isPublic, Wt::Dbo::ptr<User> user)
: _name {name},
_type {type},
_isPublic {isPublic},
_user {user}
{
}
TrackList::pointer
TrackList::create(Session& session, const std::string& name, Type type, bool isPublic, Wt::Dbo::ptr<User> user)
{
session.checkUniqueLocked();
assert(user);
auto res = session.getDboSession().add( std::make_unique<TrackList>(name, type, isPublic, user) );
session.getDboSession().flush();
return res;
}
TrackList::pointer
TrackList::get(Session& session, const std::string& name, Type type, Wt::Dbo::ptr<User> user)
{
session.checkSharedLocked();
assert(user);
return session.getDboSession().find<TrackList>()
.where("name = ?").bind(name)
.where("type = ?").bind(type)
.where("user_id = ?").bind(user.id());
}
std::vector<TrackList::pointer>
TrackList::getAll(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<TrackList::pointer> res = session.getDboSession().find<TrackList>();
return std::vector<TrackList::pointer>(res.begin(), res.end());
}
std::vector<TrackList::pointer>
TrackList::getAll(Session& session, Wt::Dbo::ptr<User> user)
{
session.checkSharedLocked();
Wt::Dbo::collection<TrackList::pointer> res = session.getDboSession().find<TrackList>()
.where("user_id = ?").bind(user.id())
.orderBy("name COLLATE NOCASE");
return std::vector<TrackList::pointer>(res.begin(), res.end());
}
std::vector<TrackList::pointer>
TrackList::getAll(Session& session, Wt::Dbo::ptr<User> user, Type type)
{
session.checkSharedLocked();
Wt::Dbo::collection<TrackList::pointer> res = session.getDboSession().find<TrackList>()
.where("user_id = ?").bind(user.id())
.where("type = ?").bind(type)
.orderBy("name COLLATE NOCASE");
return std::vector<TrackList::pointer>(res.begin(), res.end());
}
TrackList::pointer
TrackList::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<TrackList>().where("id = ?").bind(id);
}
std::vector<Wt::Dbo::ptr<TrackListEntry>>
TrackList::getEntries(std::optional<std::size_t> offset, std::optional<std::size_t> size) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<Wt::Dbo::ptr<TrackListEntry>> entries =
session()->find<TrackListEntry>()
.where("tracklist_id = ?").bind(self().id())
.orderBy("id")
.limit(size ? static_cast<int>(*size) : -1)
.offset(offset ? static_cast<int>(*offset) : -1);
return std::vector<Wt::Dbo::ptr<TrackListEntry>>(entries.begin(), entries.end());
}
std::vector<Wt::Dbo::ptr<TrackListEntry>>
TrackList::getEntriesReverse(std::optional<std::size_t> offset, std::optional<std::size_t> size) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<Wt::Dbo::ptr<TrackListEntry>> entries =
session()->find<TrackListEntry>()
.where("tracklist_id = ?").bind(self().id())
.orderBy("id DESC")
.limit(size ? static_cast<int>(*size) : -1)
.offset(offset ? static_cast<int>(*offset) : -1);
return std::vector<Wt::Dbo::ptr<TrackListEntry>>(entries.begin(), entries.end());
}
Wt::Dbo::ptr<TrackListEntry>
TrackList::getEntry(std::size_t pos) const
{
Wt::Dbo::ptr<TrackListEntry> res;
auto entries = getEntries(pos, 1);
if (!entries.empty())
res = entries.front();
return res;
}
std::size_t
TrackList::getCount() const
{
return _entries.size();
}
std::vector<Wt::Dbo::ptr<Cluster>>
TrackList::getClusters() const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<Cluster::pointer> res = session()->query<Cluster::pointer>("SELECT c from cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN tracklist_entry p_e ON p_e.track_id = t.id INNER JOIN tracklist p ON p.id = p_e.tracklist_id")
.where("p.id = ?").bind(self()->id())
.groupBy("c.id")
.orderBy("COUNT(c.id) DESC");
return std::vector<Wt::Dbo::ptr<Cluster>>(res.begin(), res.end());
}
bool
TrackList::hasTrack(IdType trackId) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<TrackListEntry::pointer> res = session()->query<TrackListEntry::pointer>("SELECT p_e from tracklist_entry p_e INNER JOIN tracklist p ON p_e.tracklist_id = p.id")
.where("p_e.track_id = ?").bind(trackId)
.where("p.id = ?").bind(self()->id());
return res.size() > 0;
}
std::vector<Track::pointer>
TrackList::getSimilarTracks(std::optional<std::size_t> offset, std::optional<std::size_t> size) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::Query<Track::pointer> query {session()->query<Track::pointer>(
"SELECT t FROM track t"
" INNER JOIN track_cluster t_c ON t_c.track_id = t.id"
" WHERE "
" (t_c.cluster_id IN (SELECT c.id from cluster c INNER JOIN track t ON c.id = t_c.cluster_id INNER JOIN track_cluster t_c ON t_c.track_id = t.id INNER JOIN tracklist_entry p_e ON p_e.track_id = t.id INNER JOIN tracklist p ON p.id = p_e.tracklist_id WHERE p.id = ?)"
" AND t.id NOT IN (SELECT tracklist_t.id FROM track tracklist_t INNER JOIN tracklist_entry t_e ON t_e.track_id = tracklist_t.id WHERE t_e.tracklist_id = ?))"
)
.bind(self()->id())
.bind(self()->id())
.groupBy("t.id")
.orderBy("COUNT(*) DESC")
.limit(size ? static_cast<int>(*size) : -1)
.offset(offset ? static_cast<int>(*offset) : -1)};
Wt::Dbo::collection<Track::pointer> tracks = query;
return std::vector<Track::pointer>(tracks.begin(), tracks.end());
}
std::vector<IdType>
TrackList::getTrackIds() const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<IdType> res = session()->query<IdType>("SELECT p_e.track_id from tracklist_entry p_e INNER JOIN tracklist p ON p_e.tracklist_id = p.id")
.where("p.id = ?").bind(self()->id());
return std::vector<IdType>(res.begin(), res.end());
}
std::chrono::milliseconds
TrackList::getDuration() const
{
assert(session());
assert(IdIsValid(self()->id()));
using milli = std::chrono::duration<int, std::milli>;
Wt::Dbo::Query<milli> query {session()->query<milli>("SELECT SUM(duration) FROM track t INNER JOIN tracklist_entry p_e ON t.id = p_e.track_id")
.where("p_e.tracklist_id = ?").bind(self()->id())};
return query.resultValue();
}
std::vector<Artist::pointer>
TrackList::getTopArtists(std::size_t limit) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<Artist::pointer> res = session()->query<Artist::pointer>("SELECT a from artist a INNER JOIN track t ON t.id = t_a_l.track_id INNER JOIN track_artist_link t_a_l ON t_a_l.artist_id = a.id INNER JOIN tracklist_entry p_e ON p_e.track_id = t.id INNER JOIN tracklist p ON p.id = p_e.tracklist_id")
.where("p.id = ?").bind(self()->id())
.groupBy("a.id")
.orderBy("COUNT(a.id) DESC")
.limit(static_cast<int>(limit));
return std::vector<Artist::pointer>(res.begin(), res.end());
}
std::vector<Release::pointer>
TrackList::getTopReleases(std::size_t limit) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<Release::pointer> res = session()->query<Release::pointer>("SELECT r from release r INNER JOIN track t ON t.release_id = r.id INNER JOIN tracklist_entry p_e ON p_e.track_id = t.id INNER JOIN tracklist p ON p.id = p_e.tracklist_id")
.where("p.id = ?").bind(self()->id())
.groupBy("r.id")
.orderBy("COUNT(r.id) DESC")
.limit(static_cast<int>(limit));
return std::vector<Release::pointer>(res.begin(), res.end());
}
std::vector<Track::pointer>
TrackList::getTopTracks(std::size_t limit) const
{
assert(session());
assert(IdIsValid(self()->id()));
Wt::Dbo::collection<Track::pointer> res = session()->query<Track::pointer>("SELECT t from track t INNER JOIN tracklist_entry p_e ON p_e.track_id = t.id INNER JOIN tracklist p ON p.id = p_e.tracklist_id")
.where("p.id = ?").bind(self()->id())
.groupBy("t.id")
.orderBy("COUNT(t.id) DESC")
.limit(static_cast<int>(limit));
return std::vector<Track::pointer>(res.begin(), res.end());
}
TrackListEntry::TrackListEntry(Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<TrackList> tracklist)
: _track(track),
_tracklist(tracklist)
{
}
TrackListEntry::pointer
TrackListEntry::create(Session& session, Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<TrackList> tracklist)
{
session.checkUniqueLocked();
assert(track);
assert(tracklist);
auto res = session.getDboSession().add( std::make_unique<TrackListEntry>( track, tracklist) );
session.getDboSession().flush();
return res;
}
TrackListEntry::pointer
TrackListEntry::getById(Session& session, IdType id)
{
session.checkSharedLocked();
return session.getDboSession().find<TrackListEntry>().where("id = ?").bind(id);
}
} // namespace Database
+288
View File
@@ -0,0 +1,288 @@
/*
* Copyright (C) 2013 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/User.hpp"
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
#include "utils/Logger.hpp"
namespace Database {
AuthToken::AuthToken(const std::string& value, const Wt::WDateTime& expiry, Wt::Dbo::ptr<User> user)
: _value {value}
, _expiry {expiry}
, _user {user}
{
}
AuthToken::pointer
AuthToken::create(Session& session, const std::string& value, const Wt::WDateTime& expiry, Wt::Dbo::ptr<User> user)
{
session.checkUniqueLocked();
auto res {session.getDboSession().add(std::make_unique<AuthToken>(value, expiry, user))};
session.getDboSession().flush();
return res;
}
void
AuthToken::removeExpiredTokens(Session& session, const Wt::WDateTime& now)
{
session.checkUniqueLocked();
session.getDboSession().execute
("DELETE FROM auth_token WHERE expiry < ?").bind(now);
}
AuthToken::pointer
AuthToken::getByValue(Session& session, const std::string& value)
{
session.checkSharedLocked();
return session.getDboSession().find<AuthToken>()
.where("value = ?").bind(value);
}
static const std::string playedListName {"__played_tracks__"};
static const std::string queuedListName {"__queued_tracks__"};
const std::set<Bitrate>
User::audioTranscodeAllowedBitrates =
{
64000,
96000,
128000,
192000,
320000,
};
User::User()
: _maxAudioTranscodeBitrate {static_cast<int>(*audioTranscodeAllowedBitrates.rbegin())}
{
}
User::User(const std::string& loginName, const PasswordHash& passwordHash)
: User()
{
_loginName = loginName;
_passwordHash = passwordHash.hash;
_passwordSalt = passwordHash.salt;
}
std::vector<User::pointer>
User::getAll(Session& session)
{
session.checkSharedLocked();
Wt::Dbo::collection<pointer> res = session.getDboSession().find<User>();
return std::vector<pointer>(res.begin(), res.end());
}
User::pointer
User::getDemo(Session& session)
{
session.checkSharedLocked();
pointer res = session.getDboSession().find<User>().where("type = ?").bind(Type::DEMO);
return res;
}
User::pointer
User::create(Session& session, const std::string& loginName, const PasswordHash& passwordHash)
{
session.checkUniqueLocked();
User::pointer user {session.getDboSession().add(std::make_unique<User>(loginName, passwordHash))};
TrackList::create(session, playedListName, TrackList::Type::Internal, false, user);
TrackList::create(session, queuedListName, TrackList::Type::Internal, false, user);
session.getDboSession().flush();
return user;
}
User::pointer
User::getById(Session& session, IdType id)
{
return session.getDboSession().find<User>().where("id = ?").bind( id );
}
User::pointer
User::getByLoginName(Session& session, const std::string& name)
{
return session.getDboSession().find<User>()
.where("login_name = ?").bind(name);
}
void
User::setAudioTranscodeBitrate(Bitrate bitrate)
{
_audioTranscodeBitrate = std::min(bitrate, static_cast<Bitrate>(_maxAudioTranscodeBitrate));
}
void
User::setMaxAudioTranscodeBitrate(Bitrate requestedBitrate)
{
Bitrate bitrate {*audioTranscodeAllowedBitrates.begin()};
for (auto allowedBitrate : audioTranscodeAllowedBitrates)
{
if (requestedBitrate < allowedBitrate)
break;
bitrate = allowedBitrate;
}
_maxAudioTranscodeBitrate = bitrate;
if (_audioTranscodeBitrate > _maxAudioTranscodeBitrate)
_audioTranscodeBitrate = _maxAudioTranscodeBitrate;
}
void
User::clearAuthTokens()
{
_authTokens.clear();
}
Bitrate
User::getAudioTranscodeBitrate(void) const
{
return _audioTranscodeBitrate;
}
std::size_t
User::getMaxAudioTranscodeBitrate(void) const
{
return _maxAudioTranscodeBitrate;
}
Wt::Dbo::ptr<TrackList>
User::getPlayedTrackList(Session& session) const
{
assert(self());
session.checkSharedLocked();
return TrackList::get(session, playedListName, TrackList::Type::Internal, self());
}
Wt::Dbo::ptr<TrackList>
User::getQueuedTrackList(Session& session) const
{
assert(self());
session.checkSharedLocked();
return TrackList::get(session, queuedListName, TrackList::Type::Internal, self());
}
void
User::starArtist(Wt::Dbo::ptr<Artist> artist)
{
if (_starredArtists.count(artist) == 0)
_starredArtists.insert(artist);
}
void
User::unstarArtist(Wt::Dbo::ptr<Artist> artist)
{
if (_starredArtists.count(artist) != 0)
_starredArtists.erase(artist);
}
bool
User::hasStarredArtist(Wt::Dbo::ptr<Artist> artist) const
{
return _starredArtists.count(artist) != 0;
}
std::vector<Wt::Dbo::ptr<Artist>>
User::getStarredArtists() const
{
return std::vector<Wt::Dbo::ptr<Artist>>(_starredArtists.begin(), _starredArtists.end());
}
void
User::starRelease(Wt::Dbo::ptr<Release> release)
{
if (_starredReleases.count(release) == 0)
_starredReleases.insert(release);
}
void
User::unstarRelease(Wt::Dbo::ptr<Release> release)
{
if (_starredReleases.count(release) != 0)
_starredReleases.erase(release);
}
bool
User::hasStarredRelease(Wt::Dbo::ptr<Release> release) const
{
return _starredReleases.count(release) != 0;
}
std::vector<Wt::Dbo::ptr<Release>>
User::getStarredReleases(std::optional<std::size_t> offset, std::optional<std::size_t> limit) const
{
Wt::Dbo::collection<Wt::Dbo::ptr<Release>> res = _starredReleases.find()
.offset(offset ? static_cast<int>(*offset) : -1)
.limit(limit ? static_cast<int>(*limit) : -1);
return std::vector<Wt::Dbo::ptr<Release>>(res.begin(), res.end());
}
void
User::starTrack(Wt::Dbo::ptr<Track> track)
{
if (_starredTracks.count(track) == 0)
_starredTracks.insert(track);
}
void
User::unstarTrack(Wt::Dbo::ptr<Track> track)
{
if (_starredTracks.count(track) != 0)
_starredTracks.erase(track);
}
bool
User::hasStarredTrack(Wt::Dbo::ptr<Track> track) const
{
return _starredTracks.count(track) != 0;
}
std::vector<Wt::Dbo::ptr<Track>>
User::getStarredTracks() const
{
return std::vector<Wt::Dbo::ptr<Track>>(_starredTracks.begin(), _starredTracks.end());
}
} // namespace Database
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2015 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 <optional>
#include <string>
#include <vector>
#include <Wt/WDateTime.h>
#include <Wt/Dbo/Dbo.h>
#include "utils/UUID.hpp"
#include "TrackArtistLink.hpp"
#include "Types.hpp"
namespace Database
{
class Cluster;
class ClusterType;
class Release;
class Session;
class Track;
class User;
class Artist : public Wt::Dbo::Dbo<Artist>
{
public:
using pointer = Wt::Dbo::ptr<Artist>;
Artist() {}
Artist(const std::string& name, const std::optional<UUID>& MBID = {});
// Accessors
static pointer getByMBID(Session& session, const UUID& MBID);
static pointer getById(Session& session, IdType id);
static std::vector<pointer> getByName(Session& session, const std::string& name);
static std::vector<pointer> getByClusters(Session& session,
const std::set<IdType>& clusters); // at least one track that belongs to these clusters
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, at least one artist that belongs to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords
std::optional<TrackArtistLink::Type> linkType, // if set, only artists that have produced at least one track with this link type
std::optional<std::size_t> offset,
std::optional<std::size_t> size,
bool& moreExpected);
static std::vector<pointer> getAll(Session& session, std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {});
static std::vector<IdType> getAllIds(Session& session);
static std::vector<pointer> getAllOrphans(Session& session); // No track related
static std::vector<pointer> getLastAdded(Session& session, Wt::WDateTime after, std::optional<std::size_t> size = {});
// Accessors
const std::string& getName(void) const { return _name; }
std::optional<UUID> getMBID(void) const { return UUID::fromString(_MBID); }
std::vector<Wt::Dbo::ptr<Release>> getReleases(const std::set<IdType>& clusterIds = {}) const; // if non empty, get the releases that match all these clusters
std::size_t getReleaseCount() const;
std::vector<Wt::Dbo::ptr<Track>> getTracks(std::optional<TrackArtistLink::Type> linkType = {}) const;
std::vector<Wt::Dbo::ptr<Track>> getTracksWithRelease(std::optional<TrackArtistLink::Type> linkType = {}) const;
std::vector<Wt::Dbo::ptr<Track>> getRandomTracks(std::optional<std::size_t> count) const;
std::vector<pointer> getSimilarArtists(std::optional<std::size_t> offset = {}, std::optional<std::size_t> count = {}) const;
// Get the cluster of the tracks made by this artist
// Each clusters are grouped by cluster type, sorted by the number of occurence
// size is the max number of cluster per cluster type
std::vector<std::vector<Wt::Dbo::ptr<Cluster>>> getClusterGroups(std::vector<Wt::Dbo::ptr<ClusterType>> clusterTypes, std::size_t size) const;
void setMBID(const std::optional<UUID>& mbid) { _MBID = mbid ? mbid->getAsString() : ""; }
void setSortName(const std::string& sortName);
// Create
static pointer create(Session& session, const std::string& name, const std::optional<UUID>& UUID = {});
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _name, "sort_name");
Wt::Dbo::field(a, _MBID, "mbid");
Wt::Dbo::hasMany(a, _trackArtistLinks, Wt::Dbo::ManyToOne, "artist");
Wt::Dbo::hasMany(a, _starringUsers, Wt::Dbo::ManyToMany, "user_release_starred", "", Wt::Dbo::OnDeleteCascade);
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
std::string _sortName;
std::string _MBID; // Musicbrainz Identifier
Wt::Dbo::collection<Wt::Dbo::ptr<TrackArtistLink>> _trackArtistLinks; // Tracks involving this artist
Wt::Dbo::collection<Wt::Dbo::ptr<User>> _starringUsers; // Users that starred this artist
};
} // namespace Database
@@ -0,0 +1,124 @@
/*
* Copyright (C) 2018 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 <string>
#include <vector>
#include <Wt/Dbo/Dbo.h>
#include <Wt/WDateTime.h>
#include "Types.hpp"
namespace Database {
class Track;
class ClusterType;
class ScanSettings;
class Session;
class Cluster : public Wt::Dbo::Dbo<Cluster>
{
public:
using pointer = Wt::Dbo::ptr<Cluster>;
Cluster();
Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name);
// Find utility
static std::vector<pointer> getAll(Session& session);
static std::vector<pointer> getAllOrphans(Session& session);
static pointer getById(Session& session, IdType id);
// Create utility
static pointer create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name);
// Accessors
const std::string& getName() const { return _name; }
Wt::Dbo::ptr<ClusterType> getType() const { return _clusterType; }
std::size_t getTracksCount() const { return _tracks.size(); }
std::vector<Wt::Dbo::ptr<Track>> getTracks(std::optional<std::size_t> offset = {}, std::optional<std::size_t> limit = {}) const;
std::set<IdType> getTrackIds() const;
std::size_t getReleasesCount() const;
void addTrack(Wt::Dbo::ptr<Track> track);
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::belongsTo(a, _clusterType, "cluster_type", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToMany, "track_cluster", "", Wt::Dbo::OnDeleteCascade);
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
Wt::Dbo::ptr<ClusterType> _clusterType;
Wt::Dbo::collection< Wt::Dbo::ptr<Track> > _tracks;
};
class ClusterType : public Wt::Dbo::Dbo<ClusterType>
{
public:
using pointer = Wt::Dbo::ptr<ClusterType>;
ClusterType() {}
ClusterType(std::string name);
static std::vector<pointer> getAllOrphans(Session& session);
static pointer getByName(Session& session, const std::string& name);
static pointer getById(Session& session, IdType id);
static std::vector<pointer> getAll(Session& session);
static pointer create(Session& session, const std::string& name);
static void remove(Session& session, const std::string& name);
// Accessors
const std::string& getName(void) const { return _name; }
std::vector<Cluster::pointer> getClusters() const;
Cluster::pointer getCluster(const std::string& name) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::hasMany(a, _clusters, Wt::Dbo::ManyToOne, "cluster_type");
Wt::Dbo::belongsTo(a, _scanSettings, "scan_settings", Wt::Dbo::OnDeleteCascade);
}
private:
static const std::size_t _maxNameLength = 128;
std::string _name;
Wt::Dbo::collection< Wt::Dbo::ptr<Cluster> > _clusters;
Wt::Dbo::ptr<ScanSettings> _scanSettings;
};
} // namespace Database
+48
View File
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2019 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 <shared_mutex>
#include <Wt/Dbo/SqlConnectionPool.h>
namespace Database {
// Session living class handling the database and the login
class Db
{
public:
Db(const std::filesystem::path& dbPath);
private:
friend class Session;
std::shared_mutex& getMutex() { return _sharedMutex; }
Wt::Dbo::SqlConnectionPool& getConnectionPool() { return *_connectionPool; }
std::shared_mutex _sharedMutex;
std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool;
};
} // namespace Database
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2015 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 <optional>
#include <Wt/Dbo/WtSqlTraits.h>
#include "utils/UUID.hpp"
#include "TrackArtistLink.hpp"
#include "Types.hpp"
namespace Database
{
class Artist;
class Cluster;
class ClusterType;
class Release;
class Track;
class User;
class Release : public Wt::Dbo::Dbo<Release>
{
public:
using pointer = Wt::Dbo::ptr<Release>;
Release() {}
Release(const std::string& name, const std::optional<UUID>& MBID = {});
// Accessors
static std::size_t getCount(Session& session);
static pointer getByMBID(Session& session, const UUID& MBID);
static std::vector<pointer> getByName(Session& session, const std::string& name);
static pointer getById(Session& session, IdType id);
static std::vector<pointer> getAllOrphans(Session& session); // no track related
static std::vector<pointer> getAll(Session& session, std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {});
static std::vector<IdType> getAllIds(Session& session);
static std::vector<pointer> getAllOrderedByArtist(Session& session, std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {});
static std::vector<pointer> getAllRandom(Session& session, std::optional<std::size_t> size = {});
static std::vector<pointer> getLastAdded(Session& session, const Wt::WDateTime& after, std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {});
static std::vector<pointer> getByYear(Session& session, int yearFrom, int yearTo, std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {});
static std::vector<pointer> getByClusters(Session& session, const std::set<IdType>& clusters);
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, at least one release that belongs to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords
std::optional<std::size_t> offset,
std::optional<std::size_t> size,
bool& moreExpected);
std::vector<Wt::Dbo::ptr<Track>> getTracks(const std::set<IdType>& clusters = std::set<IdType>()) const;
std::size_t getTracksCount() const;
// Get the cluster of the tracks that belong to this release
// Each clusters are grouped by cluster type, sorted by the number of occurence (max to min)
// size is the max number of cluster per cluster type
std::vector<std::vector<Wt::Dbo::ptr<Cluster>>> getClusterGroups(std::vector<Wt::Dbo::ptr<ClusterType>> clusterTypes, std::size_t size) const;
// Create
static pointer create(Session& session, const std::string& name, const std::optional<UUID>& MBID = {});
// Utility functions
std::optional<int> getReleaseYear(bool originalDate = false) const; // 0 if unknown or various
std::optional<std::string> getCopyright() const;
std::optional<std::string> getCopyrightURL() const;
// Modifiers
void setTotalDiscNumber(std::size_t num) { _totalDiscNumber = static_cast<int>(num); }
void setTotalTrackNumber(std::size_t num) { _totalTrackNumber = static_cast<int>(num); }
// Accessors
std::string getName() const { return _name; }
std::optional<UUID> getMBID() const { return UUID::fromString(_MBID); }
std::optional<std::size_t> getTotalTrackNumber() const;
std::optional<std::size_t> getTotalDiscNumber() const;
std::chrono::milliseconds getDuration() const;
// Get the artists of this release
std::vector<Wt::Dbo::ptr<Artist> > getArtists(TrackArtistLink::Type type = TrackArtistLink::Type::Artist) const;
std::vector<Wt::Dbo::ptr<Artist> > getReleaseArtists() const { return getArtists(TrackArtistLink::Type::ReleaseArtist); }
bool hasVariousArtists() const;
std::vector<pointer> getSimilarReleases(std::optional<std::size_t> offset = {}, std::optional<std::size_t> count = {}) const;
void setMBID(const std::optional<UUID>& mbid) { _MBID = mbid ? mbid->getAsString() : ""; }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _MBID, "mbid");
Wt::Dbo::field(a, _totalDiscNumber, "total_disc_number");
Wt::Dbo::field(a, _totalTrackNumber, "total_track_number");
Wt::Dbo::hasMany(a, _tracks, Wt::Dbo::ManyToOne, "release");
Wt::Dbo::hasMany(a, _starringUsers, Wt::Dbo::ManyToMany, "user_release_starred", "", Wt::Dbo::OnDeleteCascade);
}
private:
static const std::size_t _maxNameLength {128};
std::string _name;
std::string _MBID;
int _totalDiscNumber {};
int _totalTrackNumber {};
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> _tracks; // Tracks in the release
Wt::Dbo::collection<Wt::Dbo::ptr<User>> _starringUsers; // Users that starred this release
};
} // namespace Database
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2018 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 <Wt/Dbo/Dbo.h>
#include <Wt/WTime.h>
namespace Database {
class ClusterType;
class Session;
class ScanSettings : public Wt::Dbo::Dbo<ScanSettings>
{
public:
using pointer = Wt::Dbo::ptr<ScanSettings>;
// Do not modify values (just add)
enum class UpdatePeriod {
Never = 0,
Daily,
Weekly,
Monthly
};
// Do not modify values (just add)
enum class SimilarityEngineType
{
Clusters = 0,
Features,
};
static void init(Session& session);
static pointer get(Session& session);
// Getters
std::size_t getScanVersion() const { return _scanVersion; }
std::filesystem::path getMediaDirectory() const { return _mediaDirectory; }
Wt::WTime getUpdateStartTime() const { return _startTime; }
UpdatePeriod getUpdatePeriod() const { return _updatePeriod; }
std::vector<Wt::Dbo::ptr<ClusterType>> getClusterTypes() const;
std::set<std::filesystem::path> getAudioFileExtensions() const;
SimilarityEngineType getSimilarityEngineType() const { return _similarityEngineType; }
// Setters
void addAudioFileExtension(const std::filesystem::path& ext);
void setMediaDirectory(const std::filesystem::path& p);
void setUpdateStartTime(Wt::WTime t) { _startTime = t; }
void setUpdatePeriod(UpdatePeriod p) { _updatePeriod = p; }
void setClusterTypes(Session& session, const std::set<std::string>& clusterTypeNames);
void setSimilarityEngineType(SimilarityEngineType type) { _similarityEngineType = type; }
void incScanVersion();
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _scanVersion, "scan_version");
Wt::Dbo::field(a, _mediaDirectory, "media_directory");
Wt::Dbo::field(a, _startTime, "start_time");
Wt::Dbo::field(a, _updatePeriod, "update_period");
Wt::Dbo::field(a, _audioFileExtensions, "audio_file_extensions");
Wt::Dbo::field(a, _similarityEngineType,"similarity_engine_type");
Wt::Dbo::hasMany(a, _clusterTypes, Wt::Dbo::ManyToOne, "scan_settings");
}
private:
int _scanVersion {};
std::string _mediaDirectory;
Wt::WTime _startTime = Wt::WTime {0,0,0};
UpdatePeriod _updatePeriod {UpdatePeriod::Never};
SimilarityEngineType _similarityEngineType {SimilarityEngineType::Clusters};
std::string _audioFileExtensions {".alac .mp3 .ogg .oga .aac .m4a .m4b .flac .wav .wma .aif .aiff .ape .mpc .shn .opus"};
Wt::Dbo::collection<Wt::Dbo::ptr<ClusterType>> _clusterTypes;
};
} // namespace Database
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2013 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 <mutex>
#include <map>
#include <memory>
#include <shared_mutex>
#include <vector>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/SqlConnectionPool.h>
namespace Database {
class UniqueTransaction
{
public:
~UniqueTransaction();
private:
friend class Session;
UniqueTransaction(std::shared_mutex& mutex, Wt::Dbo::Session& session);
std::unique_lock<std::shared_mutex> _lock;
Wt::Dbo::Transaction _transaction;
};
class SharedTransaction
{
public:
~SharedTransaction();
private:
friend class Session;
SharedTransaction(std::shared_mutex& mutex, Wt::Dbo::Session& session);
std::shared_lock<std::shared_mutex> _lock;
Wt::Dbo::Transaction _transaction;
};
class Db;
class Session
{
public:
Session (Db& database);
Session(const Session&) = delete;
Session(Session&&) = delete;
Session& operator=(const Session&) = delete;
Session& operator=(Session&&) = delete;
[[nodiscard]] UniqueTransaction createUniqueTransaction();
[[nodiscard]] SharedTransaction createSharedTransaction();
void checkUniqueLocked();
void checkSharedLocked();
void optimize();
void prepareTables(); // need to run only once at startup
Wt::Dbo::Session& getDboSession() { return _session; }
private:
Session(std::shared_mutex& mutex, Wt::Dbo::SqlConnectionPool& connectionPool);
void doDatabaseMigrationIfNeeded();
Db& _db;
Wt::Dbo::Session _session;
};
} // namespace Database
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2013 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 <memory>
#include <mutex>
#include <vector>
#include "Session.hpp"
namespace Database {
class SessionPool
{
public:
class ScopedSession
{
public:
ScopedSession(SessionPool& pool) : _pool {pool}, _session {_pool.acquireSession()} {}
~ScopedSession() { _pool.releaseSession(_session); }
ScopedSession(const ScopedSession&) = delete;
ScopedSession(ScopedSession&&) = delete;
ScopedSession& operator=(const ScopedSession&) = delete;
ScopedSession& operator=(ScopedSession&&) = delete;
Session& get() { return _session; }
private:
SessionPool& _pool;
Session& _session;
};
SessionPool(Db& database, std::size_t maxSessionCount = 30);
SessionPool(const SessionPool&) = delete;
SessionPool(SessionPool&&) = delete;
SessionPool& operator=(const SessionPool&) = delete;
SessionPool& operator=(SessionPool&&) = delete;
private:
friend class ScopedSession;
Session& acquireSession();
void releaseSession(Session& session);
std::mutex _mutex;
Db& _db;
std::size_t _maxSessionCount;
std::vector<std::unique_ptr<Session>> _freeSessions;
std::vector<std::unique_ptr<Session>> _acquiredSessions;
};
} // namespace Database
@@ -0,0 +1,189 @@
/*
* Copyright (C) 2013-2016 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 <chrono>
#include <filesystem>
#include <optional>
#include <vector>
#include <string>
#include <Wt/Dbo/Dbo.h>
#include <Wt/WDateTime.h>
#include "utils/UUID.hpp"
#include "TrackArtistLink.hpp"
#include "Types.hpp"
namespace Database {
class Artist;
class Cluster;
class ClusterType;
class Release;
class TrackFeatures;
class TrackListEntry;
class TrackStats;
class User;
class Track : public Wt::Dbo::Dbo<Track>
{
public:
using pointer = Wt::Dbo::ptr<Track>;
Track() {}
Track(const std::filesystem::path& p);
// Find utility functions
static pointer getByPath(Session& session, const std::filesystem::path& p);
static pointer getById(Session& session, IdType id);
static pointer getByMBID(Session& session, const UUID& MBID);
static std::vector<pointer> getSimilarTracks(Session& session,
const std::set<IdType>& trackIds,
std::optional<std::size_t> offset = {},
std::optional<std::size_t> size = {});
static std::vector<pointer> getByClusters(Session& session,
const std::set<IdType>& clusters); // tracks that belong to these clusters
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, tracks that belong to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords
std::optional<std::size_t> offset,
std::optional<std::size_t> size,
bool& moreExpected);
static std::vector<pointer> getAll(Session& session, std::optional<std::size_t> limit = {});
static std::vector<pointer> getAllRandom(Session& session, std::optional<std::size_t> limit = {});
static std::vector<IdType> getAllIds(Session& session);
static std::vector<std::filesystem::path> getAllPaths(Session& session);
static std::vector<pointer> getMBIDDuplicates(Session& session);
static std::vector<pointer> getLastAdded(Session& session, const Wt::WDateTime& after, std::optional<std::size_t> size = 1);
static std::vector<pointer> getAllWithMBIDAndMissingFeatures(Session& session);
static std::vector<IdType> getAllIdsWithFeatures(Session& session, std::optional<std::size_t> limit = {});
// Create utility
static pointer create(Session& session, const std::filesystem::path& p);
// Accessors
void setScanVersion(std::size_t version) { _scanVersion = version; }
void setTrackNumber(int num) { _trackNumber = num; }
void setDiscNumber(int num) { _discNumber = num; }
void setName(const std::string& name) { _name = std::string(name, 0, _maxNameLength); }
void setDuration(std::chrono::milliseconds duration) { _duration = duration; }
void setLastWriteTime(Wt::WDateTime time) { _fileLastWrite = time; }
void setAddedTime(Wt::WDateTime time) { _fileAdded = time; }
void setYear(int year) { _year = year; }
void setOriginalYear(int year) { _originalYear = year; }
void setHasCover(bool hasCover) { _hasCover = hasCover; }
void setMBID(const std::optional<UUID>& MBID) { _MBID = MBID ? MBID->getAsString() : ""; }
void setCopyright(const std::string& copyright) { _copyright = std::string(copyright, 0, _maxCopyrightLength); }
void setCopyrightURL(const std::string& copyrightURL) { _copyrightURL = std::string(copyrightURL, 0, _maxCopyrightURLLength); }
void clearArtistLinks();
void addArtistLink(const Wt::Dbo::ptr<TrackArtistLink>& artistLink);
void setRelease(Wt::Dbo::ptr<Release> release) { _release = release; }
void setClusters(const std::vector<Wt::Dbo::ptr<Cluster>>& clusters );
void setFeatures(const Wt::Dbo::ptr<TrackFeatures>& features);
std::size_t getScanVersion() const { return _scanVersion; }
std::optional<std::size_t> getTrackNumber() const;
std::optional<std::size_t> getDiscNumber() const;
std::string getName() const { return _name; }
std::filesystem::path getPath() const { return _filePath; }
std::chrono::milliseconds getDuration() const { return _duration; }
std::optional<int> getYear() const;
std::optional<int> getOriginalYear() const;
Wt::WDateTime getLastWriteTime() const { return _fileLastWrite; }
Wt::WDateTime getAddedTime() const { return _fileAdded; }
bool hasCover() const { return _hasCover; }
std::optional<UUID> getMBID() const { return UUID::fromString(_MBID); }
std::optional<std::string> getCopyright() const;
std::optional<std::string> getCopyrightURL() const;
std::vector<Wt::Dbo::ptr<Artist>> getArtists(TrackArtistLink::Type type = TrackArtistLink::Type::Artist) const;
std::vector<IdType> getArtistIds(TrackArtistLink::Type type = TrackArtistLink::Type::Artist) const;
std::vector<Wt::Dbo::ptr<TrackArtistLink>> getArtistLinks() const;
Wt::Dbo::ptr<Release> getRelease() const { return _release; }
std::vector<Wt::Dbo::ptr<Cluster>> getClusters() const;
std::vector<IdType> getClusterIds() const;
bool hasTrackFeatures() const;
Wt::Dbo::ptr<TrackFeatures> getTrackFeatures() const;
std::vector<std::vector<Wt::Dbo::ptr<Cluster>>> getClusterGroups(std::vector<Wt::Dbo::ptr<ClusterType>> clusterTypes, std::size_t size) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _scanVersion, "scan_version");
Wt::Dbo::field(a, _trackNumber, "track_number");
Wt::Dbo::field(a, _discNumber, "disc_number");
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _duration, "duration");
Wt::Dbo::field(a, _year, "year");
Wt::Dbo::field(a, _originalYear, "original_year");
Wt::Dbo::field(a, _filePath, "file_path");
Wt::Dbo::field(a, _fileLastWrite, "file_last_write");
Wt::Dbo::field(a, _fileAdded, "file_added");
Wt::Dbo::field(a, _hasCover, "has_cover");
Wt::Dbo::field(a, _MBID, "mbid");
Wt::Dbo::field(a, _copyright, "copyright");
Wt::Dbo::field(a, _copyrightURL, "copyright_url");
Wt::Dbo::belongsTo(a, _release, "release", 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);
Wt::Dbo::hasMany(a, _playlistEntries, Wt::Dbo::ManyToOne, "track");
Wt::Dbo::hasMany(a, _starringUsers, Wt::Dbo::ManyToMany, "user_track_starred", "", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasOne(a, _trackFeatures);
}
private:
static const std::size_t _maxNameLength = 128;
static const std::size_t _maxCopyrightLength = 128;
static const std::size_t _maxCopyrightURLLength = 128;
int _scanVersion = 0;
int _trackNumber = 0;
int _discNumber = 0;
std::string _name;
std::string _artistName;
std::string _releaseName;
std::chrono::duration<int, std::milli> _duration;
int _year = 0;
int _originalYear = 0;
std::string _filePath;
Wt::WDateTime _fileLastWrite;
Wt::WDateTime _fileAdded;
bool _hasCover = false;
std::string _MBID; // Musicbrainz Identifier
std::string _copyright;
std::string _copyrightURL;
Wt::Dbo::ptr<Release> _release;
Wt::Dbo::collection<Wt::Dbo::ptr<TrackArtistLink>> _trackArtistLinks;
Wt::Dbo::collection<Wt::Dbo::ptr<Cluster>> _clusters;
Wt::Dbo::collection<Wt::Dbo::ptr<TrackListEntry>> _playlistEntries;
Wt::Dbo::collection<Wt::Dbo::ptr<User>> _starringUsers;
Wt::Dbo::weak_ptr<TrackFeatures> _trackFeatures;
};
} // namespace database
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2013-2016 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 <Wt/Dbo/Dbo.h>
#include "Types.hpp"
namespace Database {
class Artist;
class Session;
class Track;
class TrackArtistLink
{
public:
enum class Type
{
Artist, // regular artist
Arranger,
Composer,
Conductor,
Lyricist,
Mixer,
Performer,
Producer,
ReleaseArtist,
Remixer,
Writer,
};
using pointer = Wt::Dbo::ptr<TrackArtistLink>;
TrackArtistLink() = default;
TrackArtistLink(Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<Artist> artist, Type type);
static pointer create(Session& session, Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<Artist> artist,Type type);
Wt::Dbo::ptr<Track> getTrack() const { return _track; }
Wt::Dbo::ptr<Artist> getArtist() const { return _artist; }
Type getType() const { return _type; }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _type, "type");
Wt::Dbo::field(a, _type, "name");
Wt::Dbo::belongsTo(a, _track, "track", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _artist, "artist", Wt::Dbo::OnDeleteCascade);
}
private:
Type _type;
std::string _name;
Wt::Dbo::ptr<Track> _track;
Wt::Dbo::ptr<Artist> _artist;
};
}
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2020 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 <chrono>
#include <Wt/Dbo/Dbo.h>
#include "Types.hpp"
namespace Database {
class Session;
class Track;
class User;
class TrackBookmark : public Wt::Dbo::Dbo<TrackBookmark>
{
public:
using pointer = Wt::Dbo::ptr<TrackBookmark>;
TrackBookmark () = default;
TrackBookmark(Wt::Dbo::ptr<User> user, Wt::Dbo::ptr<Track> track);
// utility
static pointer create(Session& session, Wt::Dbo::ptr<User> user, Wt::Dbo::ptr<Track> track);
// Find utility functions
static std::vector<pointer> getAll(Session& session);
static std::vector<pointer> getByUser(Session& session, Wt::Dbo::ptr<User> user);
static pointer getByUser(Session& session, Wt::Dbo::ptr<User> user, Wt::Dbo::ptr<Track> track);
static pointer getById(Session& session, IdType id);
// Setters
void setOffset(std::chrono::milliseconds offset) { _offset = offset; }
void setComment(std::string_view comment) { _comment = comment; }
// Getters
std::chrono::milliseconds getOffset() const { return _offset; }
std::string_view getComment() const { return _comment; }
Wt::Dbo::ptr<Track> getTrack() const { return _track; }
Wt::Dbo::ptr<User> getUser() const { return _user; }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _offset, "offset");
Wt::Dbo::field(a, _comment, "comment");
Wt::Dbo::belongsTo(a, _track, "track", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _user, "user", Wt::Dbo::OnDeleteCascade);
}
private:
static const std::size_t _maxCommentLength = 128;
std::chrono::duration<int, std::milli> _offset;
std::string _comment;
Wt::Dbo::ptr<User> _user;
Wt::Dbo::ptr<Track> _track;
};
} // namespace database
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2018 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 <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <Wt/Dbo/Dbo.h>
#include "Types.hpp"
namespace Database {
class Session;
class Track;
using FeatureName = std::string;
using FeatureValues = std::vector<double>;
using FeatureValuesMap = std::unordered_map<FeatureName, FeatureValues>;
class TrackFeatures : public Wt::Dbo::Dbo<TrackFeatures>
{
public:
using pointer = Wt::Dbo::ptr<TrackFeatures>;
TrackFeatures() = default;
TrackFeatures(Wt::Dbo::ptr<Track> track, const std::string& jsonEncodedFeatures);
// Create utility
static pointer create(Session& session, Wt::Dbo::ptr<Track> track, const std::string& jsonEncodedFeatures);
FeatureValues getFeatureValues(const FeatureName& feature) const;
FeatureValuesMap getFeatureValuesMap(const std::unordered_set<FeatureName>& featureNames) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _data, "data");
Wt::Dbo::belongsTo(a, _track, "track", Wt::Dbo::OnDeleteCascade);
}
private:
std::string _data;
Wt::Dbo::ptr<Track> _track;
};
} // namespace database
@@ -0,0 +1,150 @@
/*
* Copyright (C) 2014 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 <optional>
#include <string>
#include <Wt/Dbo/Dbo.h>
#include "Types.hpp"
namespace Database {
class Artist;
class Cluster;
class Release;
class Session;
class Track;
class TrackListEntry;
class User;
class TrackList : public Wt::Dbo::Dbo<TrackList>
{
public:
using pointer = Wt::Dbo::ptr<TrackList>;
enum class Type
{
Playlist, // user controlled playlists
Internal, // current playqueue, history
};
TrackList() = default;
TrackList(const std::string& name, Type type, bool isPublic, Wt::Dbo::ptr<User> user);
// Stats utility
std::vector<Wt::Dbo::ptr<Artist>> getTopArtists(std::size_t limit = 1) const;
std::vector<Wt::Dbo::ptr<Release>> getTopReleases(std::size_t limit = 1) const;
std::vector<Wt::Dbo::ptr<Track>> getTopTracks(std::size_t limit = 1) const;
// Search utility
static pointer get(Session& session, const std::string& name, Type type, Wt::Dbo::ptr<User> user);
static pointer getById(Session& session, IdType tracklistId);
static std::vector<pointer> getAll(Session& session);
static std::vector<pointer> getAll(Session& session, Wt::Dbo::ptr<User> user);
static std::vector<pointer> getAll(Session& session, Wt::Dbo::ptr<User> user, Type type);
// Create utility
static pointer create(Session& session, const std::string& name, Type type, bool isPublic, Wt::Dbo::ptr<User> user);
// Accessors
std::string getName() const { return _name; }
bool isPublic() const { return _isPublic; }
Type getType() const { return _type; }
Wt::Dbo::ptr<User> getUser() const { return _user; }
// Modifiers
void setName(const std::string& name) { _name = name; }
void setIsPublic(bool isPublic) { _isPublic = isPublic; }
void clear() { _entries.clear(); }
// Get tracks, ordered by position
std::size_t getCount() const;
Wt::Dbo::ptr<TrackListEntry> getEntry(std::size_t pos) const;
std::vector<Wt::Dbo::ptr<TrackListEntry>> getEntries(std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {}) const;
std::vector<Wt::Dbo::ptr<TrackListEntry>> getEntriesReverse(std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {}) const;
std::vector<IdType> getTrackIds() const;
std::chrono::milliseconds getDuration() const;
// Get clusters, order by occurence
std::vector<Wt::Dbo::ptr<Cluster>> getClusters() const;
bool hasTrack(IdType trackId) const;
// Ordered from most clusters in common
std::vector<Wt::Dbo::ptr<Track>> getSimilarTracks(std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {}) const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _name, "name");
Wt::Dbo::field(a, _type, "type");
Wt::Dbo::field(a, _isPublic, "public");
Wt::Dbo::belongsTo(a, _user, "user", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _entries, Wt::Dbo::ManyToOne, "tracklist");
}
private:
std::string _name;
Type _type {Type::Playlist};
bool _isPublic {false};
Wt::Dbo::ptr<User> _user;
Wt::Dbo::collection< Wt::Dbo::ptr<TrackListEntry> > _entries;
};
class TrackListEntry : public Wt::Dbo::Dbo<TrackListEntry>
{
public:
using pointer = Wt::Dbo::ptr<TrackListEntry>;
TrackListEntry() = default;
TrackListEntry(Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<TrackList> tracklist);
static pointer getById(Session& session, IdType id);
// Create utility
static pointer create(Session& session, Wt::Dbo::ptr<Track> track, Wt::Dbo::ptr<TrackList> tracklist);
// Accessors
Wt::Dbo::ptr<Track> getTrack() const { return _track; }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::belongsTo(a, _track, "track", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _tracklist, "tracklist", Wt::Dbo::OnDeleteCascade);
}
private:
Wt::Dbo::ptr<Track> _track;
Wt::Dbo::ptr<TrackList> _tracklist;
};
} // namespace Database
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2015 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 <Wt/Dbo/ptr.h>
namespace Database {
using IdType = Wt::Dbo::dbo_default_traits::IdType;
static inline bool IdIsValid(IdType id)
{
return id != Wt::Dbo::dbo_default_traits::invalidId();
}
}
+230
View File
@@ -0,0 +1,230 @@
/*
* Copyright (C) 2013 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 <optional>
#include <vector>
#include <Wt/Dbo/Dbo.h>
#include <Wt/WDateTime.h>
#include "Types.hpp"
namespace Database {
class Artist;
class Release;
class Session;
class TrackList;
class Track;
// User selectable audio formats
// Do not change values
enum class AudioFormat
{
MP3 = 1,
OGG_OPUS = 2,
OGG_VORBIS = 3,
WEBM_VORBIS = 4,
MATROSKA_OPUS = 5,
};
using Bitrate = std::size_t;
class User;
class AuthToken
{
public:
using pointer = Wt::Dbo::ptr<AuthToken>;
AuthToken() = default;
AuthToken(const std::string& value, const Wt::WDateTime& expiry, Wt::Dbo::ptr<User> user);
// Utility
static pointer create(Session& session, const std::string& value, const Wt::WDateTime&expiry, Wt::Dbo::ptr<User> user);
static void removeExpiredTokens(Session& session, const Wt::WDateTime& now);
static pointer getByValue(Session& session, const std::string& value);
static pointer getById(Session& session, IdType tokenId);
// Accessors
const Wt::WDateTime& getExpiry() const { return _expiry; }
Wt::Dbo::ptr<User> getUser() const { return _user; }
const std::string& getValue() const { return _value; }
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _value, "value");
Wt::Dbo::field(a, _expiry, "expiry");
Wt::Dbo::belongsTo(a, _user, "user", Wt::Dbo::OnDeleteCascade);
}
private:
std::string _value;
Wt::WDateTime _expiry;
Wt::Dbo::ptr<User> _user;
};
class User : public Wt::Dbo::Dbo<User>
{
public:
using pointer = Wt::Dbo::ptr<User>;
static const std::size_t MinNameLength = 3;
static const std::size_t MaxNameLength = 15;
enum class Type
{
REGULAR,
ADMIN,
DEMO
};
struct PasswordHash
{
std::string salt;
std::string hash;
};
// list of audio parameters
static const std::set<Bitrate> audioTranscodeAllowedBitrates;
User();
User(const std::string& loginName, const PasswordHash& passwordHash);
// utility
static pointer create(Session& session, const std::string& loginName, const PasswordHash& passwordHash);
static pointer getById(Session& session, IdType id);
static pointer getByLoginName(Session& session, const std::string& loginName);
static std::vector<pointer> getAll(Session& session);
static pointer getDemo(Session& session);
// accessors
const std::string& getLoginName() const { return _loginName; }
PasswordHash getPasswordHash() const { return PasswordHash {_passwordSalt, _passwordHash}; }
Wt::WDateTime getLastLogin() const { return _lastLogin; }
std::size_t getAuthTokensCount() const { return _authTokens.size(); }
// write
void setLastLogin(const Wt::WDateTime& dateTime) { _lastLogin = dateTime; }
void setPasswordHash(const PasswordHash& passwordHash) { _passwordSalt = passwordHash.salt; _passwordHash = passwordHash.hash; }
void setType(Type type) { _type = type; }
void setAudioTranscodeEnable(bool value) { _audioTranscodeEnable = value; }
void setAudioTranscodeFormat(AudioFormat format) { _audioTranscodeFormat = format; }
void setAudioTranscodeBitrate(Bitrate bitrate);
void setMaxAudioTranscodeBitrate(Bitrate bitrate);
void setCurPlayingTrackPos(std::size_t pos) { _curPlayingTrackPos = pos; }
void setRadio(bool val) { _radio = val; }
void setRepeatAll(bool val) { _repeatAll = val; }
void clearAuthTokens();
// read
bool isAdmin() const { return _type == Type::ADMIN; }
bool isDemo() const { return _type == Type::DEMO; }
bool getAudioTranscodeEnable() const { return _audioTranscodeEnable; }
Bitrate getAudioTranscodeBitrate() const;
AudioFormat getAudioTranscodeFormat() const { return _audioTranscodeFormat; }
Bitrate getMaxAudioTranscodeBitrate() const;
std::size_t getCurPlayingTrackPos() const { return _curPlayingTrackPos; }
bool isRepeatAllSet() const { return _repeatAll; }
bool isRadioSet() const { return _radio; }
Wt::Dbo::ptr<TrackList> getPlayedTrackList(Session& session) const;
Wt::Dbo::ptr<TrackList> getQueuedTrackList(Session& session) const;
void starArtist(Wt::Dbo::ptr<Artist> artist);
void unstarArtist(Wt::Dbo::ptr<Artist> artist);
bool hasStarredArtist(Wt::Dbo::ptr<Artist> artist) const;
std::vector<Wt::Dbo::ptr<Artist>> getStarredArtists() const;
void starRelease(Wt::Dbo::ptr<Release> release);
void unstarRelease(Wt::Dbo::ptr<Release> release);
bool hasStarredRelease(Wt::Dbo::ptr<Release> release) const;
std::vector<Wt::Dbo::ptr<Release>> getStarredReleases(std::optional<std::size_t> offset = {}, std::optional<std::size_t> size = {}) const;
// Stars
void starTrack(Wt::Dbo::ptr<Track> track);
void unstarTrack(Wt::Dbo::ptr<Track> track);
bool hasStarredTrack(Wt::Dbo::ptr<Track> track) const;
std::vector<Wt::Dbo::ptr<Track>> getStarredTracks() const;
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _type, "type");
Wt::Dbo::field(a, _loginName, "login_name");
Wt::Dbo::field(a, _passwordSalt, "password_salt");
Wt::Dbo::field(a, _passwordHash, "password_hash");
Wt::Dbo::field(a, _lastLogin, "last_login");
Wt::Dbo::field(a, _maxAudioTranscodeBitrate, "max_audio_bitrate");
Wt::Dbo::field(a, _audioTranscodeEnable, "audio_transcode_enable");
Wt::Dbo::field(a, _audioTranscodeBitrate, "audio_transcode_bitrate");
Wt::Dbo::field(a, _audioTranscodeFormat, "audio_transcode_format");
// User's dynamic data
Wt::Dbo::field(a, _curPlayingTrackPos, "cur_playing_track_pos");
Wt::Dbo::field(a, _repeatAll, "repeat_all");
Wt::Dbo::field(a, _radio, "radio");
Wt::Dbo::hasMany(a, _tracklists, Wt::Dbo::ManyToOne, "user");
Wt::Dbo::hasMany(a, _starredArtists, Wt::Dbo::ManyToMany, "user_artist_starred", "", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _starredReleases, Wt::Dbo::ManyToMany, "user_release_starred", "", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _starredTracks, Wt::Dbo::ManyToMany, "user_track_starred", "", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::hasMany(a, _authTokens, Wt::Dbo::ManyToOne, "user");
}
private:
static const bool defaultAudioTranscodeEnable {true};
static const AudioFormat defaultAudioTranscodeFormat {AudioFormat::OGG_OPUS};
static const Bitrate defaultAudioTranscodeBitrate {128000};
std::string _loginName;
std::string _passwordSalt;
std::string _passwordHash;
Wt::WDateTime _lastLogin;
// Admin defined settings
int _maxAudioTranscodeBitrate;
Type _type {Type::REGULAR};
// User defined settings
bool _audioTranscodeEnable {defaultAudioTranscodeEnable};
AudioFormat _audioTranscodeFormat {defaultAudioTranscodeFormat};
int _audioTranscodeBitrate {defaultAudioTranscodeBitrate};
// User's dynamic data (UI)
int _curPlayingTrackPos {}; // Current track position in queue
bool _repeatAll {};
bool _radio {};
Wt::Dbo::collection<Wt::Dbo::ptr<TrackList>> _tracklists;
Wt::Dbo::collection<Wt::Dbo::ptr<Artist>> _starredArtists;
Wt::Dbo::collection<Wt::Dbo::ptr<Release>> _starredReleases;
Wt::Dbo::collection<Wt::Dbo::ptr<Track>> _starredTracks;
Wt::Dbo::collection<Wt::Dbo::ptr<AuthToken>> _authTokens;
};
} // namespace Databas'
+25
View File
@@ -0,0 +1,25 @@
add_library(lmsrecommendation SHARED
impl/Engine.cpp
impl/ProviderCreator.cpp
impl/features/som/DataNormalizer.cpp
impl/features/som/Network.cpp
)
target_include_directories(lmsrecommendation INTERFACE
include
)
target_include_directories(lmsrecommendation PRIVATE
include
)
target_link_libraries(lmsrecommendation PRIVATE
lmsdatabase
)
target_link_libraries(lmsrecommendation PUBLIC
)
install(TARGETS lmsrecommendation DESTINATION lib)
+138
View File
@@ -0,0 +1,138 @@
/*
* Copyright (C) 2019 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 "Engine.hpp"
//#include "features/SimilarityFeaturesScannerAddon.hpp"
//#include "cluster/SimilarityClusterSearcher.hpp"
#include "database/ScanSettings.hpp"
#include "database/TrackList.hpp"
namespace Recommendation {
std::unique_ptr<IEngine>
createEngine()
{
return std::make_unique<Engine>();
}
void
Engine::clearProviders()
{
_providers.clear();
}
void
Engine::addProvider(std::unique_ptr<Provider> provider, unsigned priority)
{
_providers.emplace(priority, std::move(provider));
}
std::vector<Database::IdType>
Engine::getSimilarTracksFromTrackList(Database::Session& /*session*/, Database::IdType /*trackListId*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(session)};
auto somSearcher {_somAddon.getSearcher()};
std::set<Database::IdType> trackIds;
{
auto transaction {session.createSharedTransaction()};
Database::TrackList::pointer trackList {Database::TrackList::getById(session, trackListId)};
if (trackList)
{
const std::vector<Database::IdType> orderedTrackIds {trackList->getTrackIds()};
trackIds = std::set<Database::IdType> {std::cbegin(orderedTrackIds), std::cend(orderedTrackIds)};
}
}
if (trackIds.empty())
return {};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& std::any_of(std::cbegin(trackIds), std::cend(trackIds), [&](Database::IdType trackId) { return somSearcher->isTrackClassified(trackId); } ))
{
return somSearcher->getSimilarTracks(trackIds, maxCount);
}
else
return ClusterEngine::getSimilarTracksFromTrackList(session, trackListId, maxCount);
#endif
return {};
}
std::vector<Database::IdType>
Engine::getSimilarTracks(Database::Session& /*dbSession*/, const std::unordered_set<Database::IdType>& /*trackIds*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(dbSession)};
auto somSearcher {_somAddon.getSearcher()};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& std::any_of(std::cbegin(trackIds), std::cend(trackIds), [&](Database::IdType trackId) { return somSearcher->isTrackClassified(trackId); } ))
{
return somSearcher->getSimilarTracks(trackIds, maxCount);
}
else
return ClusterEngine::getSimilarTracks(dbSession, trackIds, maxCount);
#endif
return {};
}
std::vector<Database::IdType>
Engine::getSimilarReleases(Database::Session& /*dbSession*/, Database::IdType /*releaseId*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(dbSession)};
auto somSearcher {_somAddon.getSearcher()};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& somSearcher->isReleaseClassified(releaseId))
{
return somSearcher->getSimilarReleases(releaseId, maxCount);
}
else
return ClusterEngine::getSimilarReleases(dbSession, releaseId, maxCount);
#endif
return {};
}
std::vector<Database::IdType>
Engine::getSimilarArtists(Database::Session& /*dbSession*/, Database::IdType /*artistId*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(dbSession)};
auto somSearcher {_somAddon.getSearcher()};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& somSearcher->isArtistClassified(artistId))
{
return somSearcher->getSimilarArtists(artistId, maxCount);
}
else
return ClusterEngine::getSimilarArtists(dbSession, artistId, maxCount);
#endif
return {};
}
} // ns Similarity
+52
View File
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2019 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 <map>
#include "recommendation/IEngine.hpp"
#include "recommendation/Provider.hpp"
namespace Database
{
class Session;
}
namespace Recommendation
{
class Engine : public IEngine
{
public:
void clearProviders() override;
void addProvider(std::unique_ptr<Provider> provider, unsigned priority) override;
// Closest results first
std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount) override;
std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::unordered_set<Database::IdType>& tracksId, std::size_t maxCount) override;
std::vector<Database::IdType> getSimilarReleases(Database::Session& session, Database::IdType releaseId, std::size_t maxCount) override;
std::vector<Database::IdType> getSimilarArtists(Database::Session& session, Database::IdType artistId, std::size_t maxCount) override;
private:
std::map<unsigned, std::unique_ptr<Provider>> _providers;
};
} // ns Recommendation
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2020 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 "recommendation/ClustersRecommendationProviderCreator.hpp"
#include "recommendation/FeaturesRecommendationProviderCreator.hpp"
#include "recommendation/Provider.hpp"
namespace Recommendation
{
std::unique_ptr<Provider> createClustersRecommendationProvider()
{
return {};
}
std::unique_ptr<Provider> createFeaturesRecommendationProvider(Scanner::IMediaScanner&)
{
return {};
}
}
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2018 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 "SimilarityClusterSearcher.hpp"
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
namespace Similarity {
namespace ClusterSearcher {
std::vector<Database::IdType>
getSimilarTracks(Database::Session& dbSession, const std::set<Database::IdType>& trackIds, std::size_t maxCount)
{
auto transaction {dbSession.createSharedTransaction()};
auto tracks {Database::Track::getSimilarTracks(dbSession, trackIds, 0, maxCount)};
std::vector<Database::IdType> res;
res.reserve(tracks.size());
std::transform(std::cbegin(tracks), std::cend(tracks), std::back_inserter(res), [](const auto& track) { return track.id(); });
return res;
}
std::vector<Database::IdType>
getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
auto transaction {session.createSharedTransaction()};
const Database::TrackList::pointer trackList {Database::TrackList::getById(session, tracklistId)};
if (!trackList)
return res;
const std::vector<Database::Track::pointer> tracks {trackList->getSimilarTracks(0, maxCount)};
res.reserve(tracks.size());
std::transform(std::cbegin(tracks), std::cend(tracks), std::back_inserter(res),
[](const Database::Track::pointer& track) { return track.id(); });
return res;
}
std::vector<Database::IdType>
getSimilarReleases(Database::Session& dbSession, Database::IdType releaseId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
auto transaction {dbSession.createSharedTransaction()};
auto release {Database::Release::getById(dbSession, releaseId)};
if (!release)
return res;
const auto releases {release->getSimilarReleases(0, maxCount)};
res.reserve(releases.size());
std::transform(std::cbegin(releases), std::cend(releases), std::back_inserter(res), [](const auto& release) { return release.id(); });
return res;
}
std::vector<Database::IdType>
getSimilarArtists(Database::Session& dbSession, Database::IdType artistId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
auto transaction {dbSession.createSharedTransaction()};
auto artist {Database::Artist::getById(dbSession, artistId)};
if (!artist)
return res;
const auto artists {artist->getSimilarArtists(0, maxCount)};
res.reserve(artists.size());
std::transform(std::cbegin(artists), std::cend(artists), std::back_inserter(res), [](const auto& artist) { return artist.id(); });
return res;
}
} // namespace ClusterSearcher
} // namespace Similarity
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018 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 <set>
#include "database/Types.hpp"
namespace Database {
class Session;
}
namespace Similarity {
namespace ClusterSearcher
{
std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::set<Database::IdType>& tracksId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarReleases(Database::Session& session, Database::IdType releaseId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarArtists(Database::Session& session, Database::IdType artistId, std::size_t maxCount);
};
} // namespace Similarity
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2018 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 "AcousticBrainzUtils.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <Wt/WIOService.h>
#include <Wt/Http/Client.h>
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
namespace AcousticBrainz
{
static
std::string
getJsonData(const UUID& mbid)
{
static const std::string defaultAPIURL = "https://acousticbrainz.org/api/v1/";
const std::string url {ServiceProvider<IConfig>::get()->getString("acousticbrainz-api-url", defaultAPIURL) + std::string {mbid.getAsString()} + "/low-level"};
boost::asio::io_service ioService;
Wt::Http::Client client {ioService};
client.setFollowRedirect(true);
client.setSslCertificateVerificationEnabled(true);
client.setMaximumResponseSize(256*1024);
if (!client.get(url))
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot perform a GET request to url '" << url << "'";
return {};
}
std::string response;
client.done().connect([&](Wt::AsioWrapper::error_code ec, const Wt::Http::Message &msg)
{
if (ec)
{
LMS_LOG(SIMILARITY, ERROR) << "GET request to url '" << url << "' failed: " << ec.message();
return;
}
if (msg.status() != 200)
{
LMS_LOG(SIMILARITY, ERROR) << "GET request to url '" << url << "' failed: status = " << msg.status() << ", body = " << msg.body();
return;
}
response = msg.body();
});
ioService.run();
return response;
}
std::string
extractLowLevelFeatures(const UUID& mbid)
{
return getJsonData(mbid);
}
} // namespace Scanner::AcousticBrainz
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2018 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 <string>
#include "utils/UUID.hpp"
namespace AcousticBrainz
{
std::string extractLowLevelFeatures(const UUID& MBID);
}
@@ -0,0 +1,261 @@
/*
* Copyright (C) 2018 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 "SimilarityFeaturesCache.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
namespace Similarity {
static
std::filesystem::path getCacheDirectory()
{
return ServiceProvider<IConfig>::get()->getPath("working-dir") / "cache" / "features";
}
static std::filesystem::path getCacheNetworkFilePath()
{
return getCacheDirectory() / "network";
};
static std::filesystem::path getCacheTrackPositionsFilePath()
{
return getCacheDirectory() / "track_positions";
}
static
bool
networkToCacheFile(const SOM::Network& network, std::filesystem::path path)
{
try
{
boost::property_tree::ptree root;
root.put("width", network.getWidth());
root.put("height", network.getHeight());
root.put("dim_count", network.getInputDimCount());
for (SOM::InputVector::value_type weight : network.getDataWeights())
root.add("weights.weight", weight);
for (SOM::Coordinate x = 0; x < network.getWidth(); ++x)
{
for (SOM::Coordinate y = 0; y < network.getWidth(); ++y)
{
const auto& refVector = network.getRefVector({x, y});
boost::property_tree::ptree node;
for (auto value : refVector)
node.add("values.value", value);
node.put("coord_x", x);
node.put("coord_y", y);
root.add_child("ref_vectors.ref_vector", node);
}
}
boost::property_tree::write_xml(path.string(), root);
LMS_LOG(SIMILARITY, DEBUG) << "Created network cache";
return true;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot create network cache: " << error.what();
return false;
}
}
static
std::optional<SOM::Network>
createNetworkFromCacheFile(const std::filesystem::path& path)
{
if (!std::filesystem::exists(path))
return std::nullopt;
try
{
LMS_LOG(SIMILARITY, INFO) << "Reading network from cache...";
boost::property_tree::ptree root;
boost::property_tree::read_xml(path.string(), root);
SOM::Coordinate width {root.get<SOM::Coordinate>("width")};
SOM::Coordinate height {root.get<SOM::Coordinate>("height")};
std::size_t dimCount {root.get<std::size_t>("dim_count")};
SOM::Network res {width, height, dimCount};
{
SOM::InputVector weights {dimCount};
std::size_t i {};
for (const auto& val : root.get_child("weights"))
weights[i++] = val.second.get_value<double>();
res.setDataWeights(weights);
}
for (const auto& node : root.get_child("ref_vectors"))
{
SOM::Coordinate x {node.second.get<SOM::Coordinate>("coord_x")};
SOM::Coordinate y {node.second.get<SOM::Coordinate>("coord_y")};
SOM::InputVector refVector {dimCount};
std::size_t i {};
for (const auto& val : node.second.get_child("values"))
refVector[i++] = val.second.get_value<SOM::InputVector::value_type>();
res.setRefVector({x, y}, refVector);
}
LMS_LOG(SIMILARITY, INFO) << "Successfully read network from cache";
return res;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot read network cache: " << error.what();
return std::nullopt;
}
}
static
bool
objectPositionToCacheFile(const std::map<Database::IdType, std::set<SOM::Position>>& objectsPosition, std::filesystem::path path)
{
try
{
boost::property_tree::ptree root;
for (const auto& objectPosition : objectsPosition)
{
boost::property_tree::ptree node;
node.put("id", objectPosition.first);
for (const auto& position : objectPosition.second)
{
boost::property_tree::ptree positionNode;
positionNode.put("x", position.x);
positionNode.put("y", position.y);
node.add_child("position.position", positionNode);
}
root.add_child("objects.object", node);
}
boost::property_tree::write_xml(path.string(), root);
return true;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot cache object position: " << error.what();
return false;
}
}
static
std::optional<std::map<Database::IdType, std::set<SOM::Position>>>
createObjectPositionsFromCacheFile(std::filesystem::path path)
{
try
{
LMS_LOG(SIMILARITY, INFO) << "Reading object position from cache...";
boost::property_tree::ptree root;
boost::property_tree::read_xml(path.string(), root);
std::map<Database::IdType, std::set<SOM::Position>> res;
for (const auto& object : root.get_child("objects"))
{
auto id = object.second.get<Database::IdType>("id");
for (const auto& position : object.second.get_child("position"))
{
auto x = position.second.get<SOM::Coordinate>("x");
auto y = position.second.get<SOM::Coordinate>("y");
res[id].insert({x, y});
}
}
LMS_LOG(SIMILARITY, INFO) << "Successfully read object position from cache";
return res;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot create object position from cache file: " << error.what();
return std::nullopt;
}
}
void
FeaturesCache::invalidate()
{
std::filesystem::remove(getCacheNetworkFilePath());
std::filesystem::remove(getCacheTrackPositionsFilePath());
}
std::optional<FeaturesCache>
FeaturesCache::read()
{
std::optional<FeaturesCache> res;
auto network{createNetworkFromCacheFile(getCacheNetworkFilePath())};
if (!network)
return res;
auto trackPositions{createObjectPositionsFromCacheFile(getCacheTrackPositionsFilePath())};
if (!trackPositions)
return res;
return FeaturesCache{std::move(*network), std::move(*trackPositions)};
}
void
FeaturesCache::write()
{
std::filesystem::create_directories(ServiceProvider<IConfig>::get()->getPath("working-dir") / "cache" / "features");
if (!networkToCacheFile(_network, getCacheNetworkFilePath())
|| !objectPositionToCacheFile(_trackPositions, getCacheTrackPositionsFilePath()))
{
invalidate();
}
}
FeaturesCache::FeaturesCache(SOM::Network network, ObjectPositions trackPositions)
: _network {std::move(network)},
_trackPositions {std::move(trackPositions)}
{
}
} // namespace Similarity
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2018 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 <map>
#include <optional>
#include <set>
#include "database/Types.hpp"
#include "som/Network.hpp"
namespace Similarity {
class FeaturesCache
{
public:
static void invalidate();
static std::optional<FeaturesCache> read();
void write();
private:
using ObjectPositions = std::map<Database::IdType, std::set<SOM::Position>>;
FeaturesCache(SOM::Network network, ObjectPositions trackPositions);
friend class FeaturesSearcher;
SOM::Network _network;
ObjectPositions _trackPositions;
};
} // namespace Similarity
@@ -0,0 +1,402 @@
/*
* Copyright (C) 2019 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 "SimilarityFeaturesDefs.hpp"
#include <algorithm>
#include <iterator>
#include "utils/Exception.hpp"
namespace Similarity {
static const std::unordered_map<FeatureName, FeatureDef> featureDefinitions
{
{ "lowlevel.average_loudness", {1}},
{ "lowlevel.barkbands.dmean", {27}},
{ "lowlevel.barkbands.dmean2", {27}},
{ "lowlevel.barkbands.dvar", {27}},
{ "lowlevel.barkbands.dvar2", {27}},
{ "lowlevel.barkbands.max", {27}},
{ "lowlevel.barkbands.mean", {27}},
{ "lowlevel.barkbands.median", {27}},
{ "lowlevel.barkbands.min", {27}},
{ "lowlevel.barkbands.var", {27}},
{ "lowlevel.barkbands_crest.dmean", {1}},
{ "lowlevel.barkbands_crest.dmean2", {1}},
{ "lowlevel.barkbands_crest.dvar", {1}},
{ "lowlevel.barkbands_crest.dvar2", {1}},
{ "lowlevel.barkbands_crest.max", {1}},
{ "lowlevel.barkbands_crest.mean", {1}},
{ "lowlevel.barkbands_crest.median", {1}},
{ "lowlevel.barkbands_crest.min", {1}},
{ "lowlevel.barkbands_crest.var", {1}},
{ "lowlevel.barkbands_flatness_db.dmean", {1}},
{ "lowlevel.barkbands_flatness_db.dmean2", {1}},
{ "lowlevel.barkbands_flatness_db.dvar", {1}},
{ "lowlevel.barkbands_flatness_db.dvar2", {1}},
{ "lowlevel.barkbands_flatness_db.max", {1}},
{ "lowlevel.barkbands_flatness_db.mean", {1}},
{ "lowlevel.barkbands_flatness_db.median", {1}},
{ "lowlevel.barkbands_flatness_db.min", {1}},
{ "lowlevel.barkbands_flatness_db.var", {1}},
{ "lowlevel.barkbands_kurtosis.dmean", {1}},
{ "lowlevel.barkbands_kurtosis.dmean2", {1}},
{ "lowlevel.barkbands_kurtosis.dvar", {1}},
{ "lowlevel.barkbands_kurtosis.dvar2", {1}},
{ "lowlevel.barkbands_kurtosis.max", {1}},
{ "lowlevel.barkbands_kurtosis.mean", {1}},
{ "lowlevel.barkbands_kurtosis.median", {1}},
{ "lowlevel.barkbands_kurtosis.min", {1}},
{ "lowlevel.barkbands_kurtosis.var", {1}},
{ "lowlevel.barkbands_skewness.dmean", {1}},
{ "lowlevel.barkbands_skewness.dmean2", {1}},
{ "lowlevel.barkbands_skewness.dvar", {1}},
{ "lowlevel.barkbands_skewness.dvar2", {1}},
{ "lowlevel.barkbands_skewness.max", {1}},
{ "lowlevel.barkbands_skewness.mean", {1}},
{ "lowlevel.barkbands_skewness.median", {1}},
{ "lowlevel.barkbands_skewness.min", {1}},
{ "lowlevel.barkbands_skewness.var", {1}},
{ "lowlevel.barkbands_spread.dmean", {1}},
{ "lowlevel.barkbands_spread.dmean2", {1}},
{ "lowlevel.barkbands_spread.dvar", {1}},
{ "lowlevel.barkbands_spread.dvar2", {1}},
{ "lowlevel.barkbands_spread.max", {1}},
{ "lowlevel.barkbands_spread.mean", {1}},
{ "lowlevel.barkbands_spread.median", {1}},
{ "lowlevel.barkbands_spread.min", {1}},
{ "lowlevel.barkbands_spread.var", {1}},
{ "lowlevel.dissonance.dmean", {1}},
{ "lowlevel.dissonance.dmean2", {1}},
{ "lowlevel.dissonance.dvar", {1}},
{ "lowlevel.dissonance.dvar2", {1}},
{ "lowlevel.dissonance.max", {1}},
{ "lowlevel.dissonance.mean", {1}},
{ "lowlevel.dissonance.median", {1}},
{ "lowlevel.dissonance.min", {1}},
{ "lowlevel.dissonance.var", {1}},
{ "lowlevel.dynamic_complexity", {1}},
{ "lowlevel.spectral_contrast_coeffs.dmean", {6}},
{ "lowlevel.spectral_contrast_coeffs.dmean2", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar2", {6}},
{ "lowlevel.spectral_contrast_coeffs.max", {6}},
{ "lowlevel.spectral_contrast_coeffs.mean", {6}},
{ "lowlevel.spectral_contrast_coeffs.median", {6}},
{ "lowlevel.spectral_contrast_coeffs.min", {6}},
{ "lowlevel.spectral_contrast_coeffs.var", {6}},
{ "lowlevel.erbbands.dmean", {40}},
{ "lowlevel.erbbands.dmean2", {40}},
{ "lowlevel.erbbands.dvar", {40}},
{ "lowlevel.erbbands.dvar2", {40}},
{ "lowlevel.erbbands.max", {40}},
{ "lowlevel.erbbands.mean", {40}},
{ "lowlevel.erbbands.median", {40}},
{ "lowlevel.erbbands.min", {40}},
{ "lowlevel.erbbands.var", {40}},
{ "lowlevel.gfcc.mean", {13}},
{ "lowlevel.hfc.dmean", {1}},
{ "lowlevel.hfc.dmean2", {1}},
{ "lowlevel.hfc.dvar", {1}},
{ "lowlevel.hfc.dvar2", {1}},
{ "lowlevel.hfc.max", {1}},
{ "lowlevel.hfc.mean", {1}},
{ "lowlevel.hfc.median", {1}},
{ "lowlevel.hfc.min", {1}},
{ "lowlevel.hfc.var", {1}},
{ "tonal.hpcp.median", {36}},
{ "lowlevel.melbands.dmean", {40}},
{ "lowlevel.melbands.dmean2", {40}},
{ "lowlevel.melbands.dvar", {40}},
{ "lowlevel.melbands.dvar2", {40}},
{ "lowlevel.melbands.max", {40}},
{ "lowlevel.melbands.mean", {40}},
{ "lowlevel.melbands.median", {40}},
{ "lowlevel.melbands.min", {40}},
{ "lowlevel.melbands.var", {40}},
{ "lowlevel.melbands_crest.dmean", {1}},
{ "lowlevel.melbands_crest.dmean2", {1}},
{ "lowlevel.melbands_crest.dvar", {1}},
{ "lowlevel.melbands_crest.dvar2", {1}},
{ "lowlevel.melbands_crest.max", {1}},
{ "lowlevel.melbands_crest.mean", {1}},
{ "lowlevel.melbands_crest.median", {1}},
{ "lowlevel.melbands_crest.min", {1}},
{ "lowlevel.melbands_crest.var", {1}},
{ "lowlevel.melbands_flatness_db.dmean", {1}},
{ "lowlevel.melbands_flatness_db.dmean2", {1}},
{ "lowlevel.melbands_flatness_db.dvar", {1}},
{ "lowlevel.melbands_flatness_db.dvar2", {1}},
{ "lowlevel.melbands_flatness_db.max", {1}},
{ "lowlevel.melbands_flatness_db.mean", {1}},
{ "lowlevel.melbands_flatness_db.median", {1}},
{ "lowlevel.melbands_flatness_db.min", {1}},
{ "lowlevel.melbands_flatness_db.var", {1}},
{ "lowlevel.melbands_kurtosis.dmean", {1}},
{ "lowlevel.melbands_kurtosis.dmean2", {1}},
{ "lowlevel.melbands_kurtosis.dvar", {1}},
{ "lowlevel.melbands_kurtosis.dvar2", {1}},
{ "lowlevel.melbands_kurtosis.max", {1}},
{ "lowlevel.melbands_kurtosis.mean", {1}},
{ "lowlevel.melbands_kurtosis.median", {1}},
{ "lowlevel.melbands_kurtosis.min", {1}},
{ "lowlevel.melbands_kurtosis.var", {1}},
{ "lowlevel.melbands_skewness.dmean", {1}},
{ "lowlevel.melbands_skewness.dmean2", {1}},
{ "lowlevel.melbands_skewness.dvar", {1}},
{ "lowlevel.melbands_skewness.dvar2", {1}},
{ "lowlevel.melbands_skewness.max", {1}},
{ "lowlevel.melbands_skewness.mean", {1}},
{ "lowlevel.melbands_skewness.median", {1}},
{ "lowlevel.melbands_skewness.min", {1}},
{ "lowlevel.melbands_skewness.var", {1}},
{ "lowlevel.melbands_spread.dmean", {1}},
{ "lowlevel.melbands_spread.dmean2", {1}},
{ "lowlevel.melbands_spread.dvar", {1}},
{ "lowlevel.melbands_spread.dvar2", {1}},
{ "lowlevel.melbands_spread.max", {1}},
{ "lowlevel.melbands_spread.mean", {1}},
{ "lowlevel.melbands_spread.median", {1}},
{ "lowlevel.melbands_spread.min", {1}},
{ "lowlevel.melbands_spread.var", {1}},
{ "lowlevel.mfcc.mean", {13}},
{ "lowlevel.pitch_salience.dmean", {1}},
{ "lowlevel.pitch_salience.dmean2", {1}},
{ "lowlevel.pitch_salience.dvar", {1}},
{ "lowlevel.pitch_salience.dvar2", {1}},
{ "lowlevel.pitch_salience.max", {1}},
{ "lowlevel.pitch_salience.mean", {1}},
{ "lowlevel.pitch_salience.median", {1}},
{ "lowlevel.pitch_salience.min", {1}},
{ "lowlevel.pitch_salience.var", {1}},
{ "lowlevel.silence_rate_30dB.dmean", {1}},
{ "lowlevel.silence_rate_30dB.dmean2", {1}},
{ "lowlevel.silence_rate_30dB.dvar", {1}},
{ "lowlevel.silence_rate_30dB.dvar2", {1}},
{ "lowlevel.silence_rate_30dB.max", {1}},
{ "lowlevel.silence_rate_30dB.mean", {1}},
{ "lowlevel.silence_rate_30dB.median", {1}},
{ "lowlevel.silence_rate_30dB.min", {1}},
{ "lowlevel.silence_rate_30dB.var", {1}},
{ "lowlevel.silence_rate_60dB.dmean", {1}},
{ "lowlevel.silence_rate_60dB.dmean2", {1}},
{ "lowlevel.silence_rate_60dB.dvar", {1}},
{ "lowlevel.silence_rate_60dB.dvar2", {1}},
{ "lowlevel.silence_rate_60dB.max", {1}},
{ "lowlevel.silence_rate_60dB.mean", {1}},
{ "lowlevel.silence_rate_60dB.median", {1}},
{ "lowlevel.silence_rate_60dB.min", {1}},
{ "lowlevel.silence_rate_60dB.var", {1}},
{ "lowlevel.spectral_centroid.dmean", {1}},
{ "lowlevel.spectral_centroid.dmean2", {1}},
{ "lowlevel.spectral_centroid.dvar", {1}},
{ "lowlevel.spectral_centroid.dvar2", {1}},
{ "lowlevel.spectral_centroid.max", {1}},
{ "lowlevel.spectral_centroid.mean", {1}},
{ "lowlevel.spectral_centroid.median", {1}},
{ "lowlevel.spectral_centroid.min", {1}},
{ "lowlevel.spectral_centroid.var", {1}},
{ "lowlevel.spectral_complexity.dmean", {1}},
{ "lowlevel.spectral_complexity.dmean2", {1}},
{ "lowlevel.spectral_complexity.dvar", {1}},
{ "lowlevel.spectral_complexity.dvar2", {1}},
{ "lowlevel.spectral_complexity.max", {1}},
{ "lowlevel.spectral_complexity.mean", {1}},
{ "lowlevel.spectral_complexity.median", {1}},
{ "lowlevel.spectral_complexity.min", {1}},
{ "lowlevel.spectral_complexity.var", {1}},
{ "lowlevel.spectral_contrast_coeffs.dmean", {6}},
{ "lowlevel.spectral_contrast_coeffs.dmean2", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar2", {6}},
{ "lowlevel.spectral_contrast_coeffs.max", {6}},
{ "lowlevel.spectral_contrast_coeffs.mean", {6}},
{ "lowlevel.spectral_contrast_coeffs.median", {6}},
{ "lowlevel.spectral_contrast_coeffs.min", {6}},
{ "lowlevel.spectral_contrast_coeffs.var", {6}},
{ "lowlevel.spectral_contrast_valleys.dmean", {6}},
{ "lowlevel.spectral_contrast_valleys.dmean2", {6}},
{ "lowlevel.spectral_contrast_valleys.dvar", {6}},
{ "lowlevel.spectral_contrast_valleys.dvar2", {6}},
{ "lowlevel.spectral_contrast_valleys.max", {6}},
{ "lowlevel.spectral_contrast_valleys.mean", {6}},
{ "lowlevel.spectral_contrast_valleys.median", {6}},
{ "lowlevel.spectral_contrast_valleys.min", {6}},
{ "lowlevel.spectral_contrast_valleys.var", {6}},
{ "lowlevel.spectral_decrease.dmean", {1}},
{ "lowlevel.spectral_decrease.dmean2", {1}},
{ "lowlevel.spectral_decrease.dvar", {1}},
{ "lowlevel.spectral_decrease.dvar2", {1}},
{ "lowlevel.spectral_decrease.max", {1}},
{ "lowlevel.spectral_decrease.mean", {1}},
{ "lowlevel.spectral_decrease.median", {1}},
{ "lowlevel.spectral_decrease.min", {1}},
{ "lowlevel.spectral_decrease.var", {1}},
{ "lowlevel.spectral_energy.dmean", {1}},
{ "lowlevel.spectral_energy.dmean2", {1}},
{ "lowlevel.spectral_energy.dvar", {1}},
{ "lowlevel.spectral_energy.dvar2", {1}},
{ "lowlevel.spectral_energy.max", {1}},
{ "lowlevel.spectral_energy.mean", {1}},
{ "lowlevel.spectral_energy.median", {1}},
{ "lowlevel.spectral_energy.min", {1}},
{ "lowlevel.spectral_energy.var", {1}},
{ "lowlevel.spectral_energyband_high.dmean", {1}},
{ "lowlevel.spectral_energyband_high.dmean2", {1}},
{ "lowlevel.spectral_energyband_high.dvar", {1}},
{ "lowlevel.spectral_energyband_high.dvar2", {1}},
{ "lowlevel.spectral_energyband_high.max", {1}},
{ "lowlevel.spectral_energyband_high.mean", {1}},
{ "lowlevel.spectral_energyband_high.median", {1}},
{ "lowlevel.spectral_energyband_high.min", {1}},
{ "lowlevel.spectral_energyband_high.var", {1}},
{ "lowlevel.spectral_energyband_low.dmean", {1}},
{ "lowlevel.spectral_energyband_low.dmean2", {1}},
{ "lowlevel.spectral_energyband_low.dvar", {1}},
{ "lowlevel.spectral_energyband_low.dvar2", {1}},
{ "lowlevel.spectral_energyband_low.max", {1}},
{ "lowlevel.spectral_energyband_low.mean", {1}},
{ "lowlevel.spectral_energyband_low.median", {1}},
{ "lowlevel.spectral_energyband_low.min", {1}},
{ "lowlevel.spectral_energyband_low.var", {1}},
{ "lowlevel.spectral_energyband_middle_high.dmean", {1}},
{ "lowlevel.spectral_energyband_middle_high.dmean2", {1}},
{ "lowlevel.spectral_energyband_middle_high.dvar", {1}},
{ "lowlevel.spectral_energyband_middle_high.dvar2", {1}},
{ "lowlevel.spectral_energyband_middle_high.max", {1}},
{ "lowlevel.spectral_energyband_middle_high.mean", {1}},
{ "lowlevel.spectral_energyband_middle_high.median", {1}},
{ "lowlevel.spectral_energyband_middle_high.min", {1}},
{ "lowlevel.spectral_energyband_middle_high.var", {1}},
{ "lowlevel.spectral_energyband_middle_low.dmean", {1}},
{ "lowlevel.spectral_energyband_middle_low.dmean2", {1}},
{ "lowlevel.spectral_energyband_middle_low.dvar", {1}},
{ "lowlevel.spectral_energyband_middle_low.dvar2", {1}},
{ "lowlevel.spectral_energyband_middle_low.max", {1}},
{ "lowlevel.spectral_energyband_middle_low.mean", {1}},
{ "lowlevel.spectral_energyband_middle_low.median", {1}},
{ "lowlevel.spectral_energyband_middle_low.min", {1}},
{ "lowlevel.spectral_energyband_middle_low.var", {1}},
{ "lowlevel.spectral_entropy.dmean", {1}},
{ "lowlevel.spectral_entropy.dmean2", {1}},
{ "lowlevel.spectral_entropy.dvar", {1}},
{ "lowlevel.spectral_entropy.dvar2", {1}},
{ "lowlevel.spectral_entropy.max", {1}},
{ "lowlevel.spectral_entropy.mean", {1}},
{ "lowlevel.spectral_entropy.median", {1}},
{ "lowlevel.spectral_entropy.min", {1}},
{ "lowlevel.spectral_entropy.var", {1}},
{ "lowlevel.spectral_flux.dmean", {1}},
{ "lowlevel.spectral_flux.dmean2", {1}},
{ "lowlevel.spectral_flux.dvar", {1}},
{ "lowlevel.spectral_flux.dvar2", {1}},
{ "lowlevel.spectral_flux.max", {1}},
{ "lowlevel.spectral_flux.mean", {1}},
{ "lowlevel.spectral_flux.median", {1}},
{ "lowlevel.spectral_flux.min", {1}},
{ "lowlevel.spectral_flux.var", {1}},
{ "lowlevel.spectral_kurtosis.dmean", {1}},
{ "lowlevel.spectral_kurtosis.dmean2", {1}},
{ "lowlevel.spectral_kurtosis.dvar", {1}},
{ "lowlevel.spectral_kurtosis.dvar2", {1}},
{ "lowlevel.spectral_kurtosis.max", {1}},
{ "lowlevel.spectral_kurtosis.mean", {1}},
{ "lowlevel.spectral_kurtosis.median", {1}},
{ "lowlevel.spectral_kurtosis.min", {1}},
{ "lowlevel.spectral_kurtosis.var", {1}},
{ "lowlevel.spectral_rms.dmean", {1}},
{ "lowlevel.spectral_rms.dmean2", {1}},
{ "lowlevel.spectral_rms.dvar", {1}},
{ "lowlevel.spectral_rms.dvar2", {1}},
{ "lowlevel.spectral_rms.max", {1}},
{ "lowlevel.spectral_rms.mean", {1}},
{ "lowlevel.spectral_rms.median", {1}},
{ "lowlevel.spectral_rms.min", {1}},
{ "lowlevel.spectral_rms.var", {1}},
{ "lowlevel.spectral_rolloff.dmean", {1}},
{ "lowlevel.spectral_rolloff.dmean2", {1}},
{ "lowlevel.spectral_rolloff.dvar", {1}},
{ "lowlevel.spectral_rolloff.dvar2", {1}},
{ "lowlevel.spectral_rolloff.max", {1}},
{ "lowlevel.spectral_rolloff.mean", {1}},
{ "lowlevel.spectral_rolloff.median", {1}},
{ "lowlevel.spectral_rolloff.min", {1}},
{ "lowlevel.spectral_rolloff.var", {1}},
{ "lowlevel.spectral_skewness.dmean", {1}},
{ "lowlevel.spectral_skewness.dmean2", {1}},
{ "lowlevel.spectral_skewness.dvar", {1}},
{ "lowlevel.spectral_skewness.dvar2", {1}},
{ "lowlevel.spectral_skewness.max", {1}},
{ "lowlevel.spectral_skewness.mean", {1}},
{ "lowlevel.spectral_skewness.median", {1}},
{ "lowlevel.spectral_skewness.min", {1}},
{ "lowlevel.spectral_skewness.var", {1}},
{ "lowlevel.spectral_spread.dmean", {1}},
{ "lowlevel.spectral_spread.dmean2", {1}},
{ "lowlevel.spectral_spread.dvar", {1}},
{ "lowlevel.spectral_spread.dvar2", {1}},
{ "lowlevel.spectral_spread.max", {1}},
{ "lowlevel.spectral_spread.mean", {1}},
{ "lowlevel.spectral_spread.median", {1}},
{ "lowlevel.spectral_spread.min", {1}},
{ "lowlevel.spectral_spread.var", {1}},
{ "lowlevel.spectral_strongpeak.dmean", {1}},
{ "lowlevel.spectral_strongpeak.dmean2", {1}},
{ "lowlevel.spectral_strongpeak.dvar", {1}},
{ "lowlevel.spectral_strongpeak.dvar2", {1}},
{ "lowlevel.spectral_strongpeak.max", {1}},
{ "lowlevel.spectral_strongpeak.mean", {1}},
{ "lowlevel.spectral_strongpeak.median", {1}},
{ "lowlevel.spectral_strongpeak.min", {1}},
{ "lowlevel.spectral_strongpeak.var", {1}},
{ "lowlevel.zerocrossingrate.dmean", {1}},
{ "lowlevel.zerocrossingrate.dmean2", {1}},
{ "lowlevel.zerocrossingrate.dvar", {1}},
{ "lowlevel.zerocrossingrate.dvar2", {1}},
{ "lowlevel.zerocrossingrate.max", {1}},
{ "lowlevel.zerocrossingrate.mean", {1}},
{ "lowlevel.zerocrossingrate.median", {1}},
{ "lowlevel.zerocrossingrate.min", {1}},
{ "lowlevel.zerocrossingrate.var", {1}},
};
FeatureDef
getFeatureDef(const FeatureName& featureName)
{
auto it {featureDefinitions.find(featureName)};
if (it == std::cend(featureDefinitions))
throw LmsException {"Unhandled requested feature '" + featureName + "'"};
return it->second;
}
FeatureNames
getFeatureNames()
{
FeatureNames res;
std::transform(std::cbegin(featureDefinitions), std::cend(featureDefinitions),
std::inserter(res, std::begin(res)), [](auto itFeature) { return itFeature.first; });
return res;
}
} // namespace Similarity
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2019 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 <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace Similarity {
using FeatureName = std::string;
using FeatureNames = std::unordered_set<FeatureName>;
using FeatureValue = double;
using FeatureValues = std::vector<FeatureValue>;
using FeatureValuesMap = std::unordered_map<FeatureName, FeatureValues>;
struct FeatureDef
{
std::size_t nbDimensions {};
};
FeatureDef getFeatureDef(const FeatureName& featureName);
FeatureNames getFeatureNames();
struct FeatureSettings
{
double weight {};
};
using FeatureSettingsMap = std::unordered_map<FeatureName, FeatureSettings>;
} // namespace Similarity
@@ -0,0 +1,187 @@
/*
* Copyright (C) 2019 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 "similarity/features/SimilarityFeaturesScannerAddon.hpp"
#include "AcousticBrainzUtils.hpp"
#include "database/ScanSettings.hpp"
#include "database/Track.hpp"
#include "database/TrackFeatures.hpp"
#include "utils/Logger.hpp"
#include "SimilarityFeaturesCache.hpp"
namespace Similarity {
static
bool
hasAtLeastOneTrackWithFeatures(Database::Session& session)
{
auto transaction {session.createSharedTransaction()};
return !Database::Track::getAllIdsWithFeatures(session, 1).empty();
}
struct TrackInfo
{
Database::IdType id;
std::optional<UUID> mbid;
};
static
std::vector<TrackInfo>
getTracksWithMBIDAndMissingFeatures(Database::Session& dbSession)
{
std::vector<TrackInfo> res;
auto transaction {dbSession.createSharedTransaction()};
auto tracks {Database::Track::getAllWithMBIDAndMissingFeatures(dbSession)};
for (const Database::Track::pointer& track : tracks)
res.push_back({track.id(), track->getMBID()});
return res;
}
FeaturesScannerAddon::FeaturesScannerAddon(Database::Db& db)
: _dbSession {db}
{
std::optional<Similarity::FeaturesCache> cache {Similarity::FeaturesCache::read()};
if (cache)
{
auto searcher {std::make_shared<Similarity::FeaturesSearcher>(_dbSession, *cache, [&]() { return _stopRequested; })};
if (searcher->isValid())
std::atomic_store(&_searcher, searcher);
}
}
std::shared_ptr<Similarity::FeaturesSearcher>
FeaturesScannerAddon::getSearcher()
{
return std::atomic_load(&_searcher);
}
void
FeaturesScannerAddon::requestStop()
{
_stopRequested = true;
}
void
FeaturesScannerAddon::trackUpdated(Database::IdType trackId)
{
auto uniqueTransaction {_dbSession.createUniqueTransaction()};
auto track {Database::Track::getById(_dbSession, trackId)};
if (!track)
return;
track.modify()->setFeatures({});
}
void
FeaturesScannerAddon::preScanComplete()
{
{
auto transaction {_dbSession.createSharedTransaction()};
if (Database::ScanSettings::get(_dbSession)->getSimilarityEngineType() != Database::ScanSettings::SimilarityEngineType::Features)
{
LMS_LOG(DBUPDATER, INFO) << "Do not fetch features since the engine type does not make use of them";
return;
}
}
LMS_LOG(DBUPDATER, DEBUG) << "Getting tracks with missing Features...";
const std::vector<TrackInfo> tracksInfo {getTracksWithMBIDAndMissingFeatures(_dbSession)};
LMS_LOG(DBUPDATER, DEBUG) << "Getting tracks with missing Features DONE (found " << tracksInfo.size() << ")";
if (!tracksInfo.empty())
Similarity::FeaturesCache::invalidate();
for (const TrackInfo& trackInfo : tracksInfo)
{
if (_stopRequested)
return;
if (trackInfo.mbid)
fetchFeatures(trackInfo.id, *trackInfo.mbid);
}
updateSearcher();
}
void
FeaturesScannerAddon::updateSearcher()
{
LMS_LOG(SIMILARITY, INFO) << "Updating searcher...";
if (!hasAtLeastOneTrackWithFeatures(_dbSession))
{
LMS_LOG(DBUPDATER, INFO) << "No track found with features!";
std::atomic_store(&_searcher, std::shared_ptr<FeaturesSearcher>{});
return;
}
Similarity::FeaturesSearcher::TrainSettings trainSettings;
trainSettings.featureSettingsMap = FeaturesSearcher::getDefaultTrainFeatureSettings();
auto searcher {std::make_shared<FeaturesSearcher>(_dbSession, trainSettings, [&]() { return _stopRequested; })};
if (searcher->isValid())
{
std::atomic_store(&_searcher, searcher);
FeaturesCache cache{searcher->toCache()};
cache.write();
LMS_LOG(DBUPDATER, INFO) << "New features similarity searcher instanciated";
}
else
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot set up a valid features similarity searcher!";
std::atomic_store(&_searcher, std::shared_ptr<FeaturesSearcher>{});
}
}
bool
FeaturesScannerAddon::fetchFeatures(Database::IdType trackId, const UUID& MBID)
{
std::map<std::string, double> features;
LMS_LOG(DBUPDATER, DEBUG) << "Fetching low level features for track '" << MBID.getAsString() << "'";
const std::string data {AcousticBrainz::extractLowLevelFeatures(MBID)};
if (data.empty())
{
LMS_LOG(DBUPDATER, ERROR) << "Track " << trackId << ", MBID = '" << MBID.getAsString() << "': cannot extract features using AcousticBrainz";
return false;
}
{
auto uniqueTransaction {_dbSession.createUniqueTransaction()};
Wt::Dbo::ptr<Database::Track> track {Database::Track::getById(_dbSession, trackId)};
if (!track)
return false;
Database::TrackFeatures::create(_dbSession, track, data);
}
return true;
}
} // namespace Similarity
@@ -0,0 +1,487 @@
/*
* Copyright (C) 2018 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 "SimilarityFeaturesSearcher.hpp"
#include <random>
#include <unordered_map>
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackFeatures.hpp"
#include "som/DataNormalizer.hpp"
#include "utils/Logger.hpp"
namespace Similarity {
const FeatureSettingsMap&
FeaturesSearcher::getDefaultTrainFeatureSettings()
{
static FeatureSettingsMap defaultTrainFeatureSettings
{
{ "lowlevel.spectral_energyband_high.mean", {1}},
{ "lowlevel.spectral_rolloff.median", {1}},
{ "lowlevel.spectral_contrast_valleys.var", {1}},
{ "lowlevel.erbbands.mean", {1}},
{ "lowlevel.gfcc.mean", {1}},
};
return defaultTrainFeatureSettings;
}
static
std::optional<FeatureValuesMap>
getTrackFeatureValues(FeaturesSearcher::FeaturesFetchFunc func, Database::IdType trackId, const std::unordered_set<FeatureName>& featureNames)
{
return func(trackId, featureNames);
}
static
std::optional<FeatureValuesMap>
getTrackFeatureValuesFromDb(Database::Session& session, Database::IdType trackId, const std::unordered_set<FeatureName>& featureNames)
{
auto func = [&](Database::IdType trackId, const std::unordered_set<FeatureName>& featureNames)
{
std::optional<FeatureValuesMap> res;
auto transaction {session.createSharedTransaction()};
Database::Track::pointer track {Database::Track::getById(session, trackId)};
if (!track)
return res;
res = track->getTrackFeatures()->getFeatureValuesMap(featureNames);
if (res->empty())
res.reset();
return res;
};
return getTrackFeatureValues(func, trackId, featureNames);
}
static
std::optional<SOM::InputVector>
convertFeatureValuesMapToInputVector(const FeatureValuesMap& featureValuesMap, std::size_t nbDimensions)
{
std::size_t i {};
std::optional<SOM::InputVector> res {SOM::InputVector {nbDimensions}};
for (const auto& [featureName, values] : featureValuesMap)
{
if (values.size() != getFeatureDef(featureName).nbDimensions)
{
LMS_LOG(SIMILARITY, WARNING) << "Dimension mismatch for feature '" << featureName << "'. Expected " << getFeatureDef(featureName).nbDimensions << ", got " << values.size();
res.reset();
break;
}
for (double val : values)
(*res)[i++] = val;
}
return res;
}
static
SOM::InputVector
getInputVectorWeights(const FeatureSettingsMap& featureSettingsMap, std::size_t nbDimensions)
{
SOM::InputVector weights {nbDimensions};
std::size_t index {};
for (const auto& [featureName, featureSettings] : featureSettingsMap)
{
const std::size_t featureNbDimensions {getFeatureDef(featureName).nbDimensions};
for (std::size_t i {}; i < featureNbDimensions; ++i)
weights[index++] = (1. / featureNbDimensions * featureSettings.weight);
}
assert(index == nbDimensions);
return weights;
}
FeaturesSearcher::FeaturesSearcher(Database::Session& session,
const TrainSettings& trainSettings,
StopRequestedFunction stopRequested)
{
LMS_LOG(SIMILARITY, INFO) << "Constructing features searcher...";
std::unordered_set<FeatureName> featureNames;
std::transform(std::cbegin(trainSettings.featureSettingsMap), std::cend(trainSettings.featureSettingsMap), std::inserter(featureNames, std::begin(featureNames)),
[](const auto& itFeatureSetting) { return itFeatureSetting.first; });
const std::size_t nbDimensions {std::accumulate(std::cbegin(featureNames), std::cend(featureNames), std::size_t {0},
[](std::size_t sum, const FeatureName& featureName) { return sum + getFeatureDef(featureName).nbDimensions; })};
LMS_LOG(SIMILARITY, DEBUG) << "Features dimension = " << nbDimensions;
std::vector<Database::IdType> trackIds;
{
auto transaction {session.createSharedTransaction()};
LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features...";
trackIds = Database::Track::getAllIdsWithFeatures(session);
LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features DONE (found " << trackIds.size() << " tracks)";
}
std::vector<SOM::InputVector> samples;
std::vector<Database::IdType> samplesTrackIds;
samples.reserve(trackIds.size());
samplesTrackIds.reserve(trackIds.size());
LMS_LOG(SIMILARITY, DEBUG) << "Extracting features...";
for (Database::IdType trackId : trackIds)
{
if (stopRequested && stopRequested())
return;
std::optional<FeatureValuesMap> featureValuesMap;
if (_featuresFetchFunc)
featureValuesMap = getTrackFeatureValues(_featuresFetchFunc, trackId, featureNames);
else
featureValuesMap = getTrackFeatureValuesFromDb(session, trackId, featureNames);
if (!featureValuesMap)
continue;
std::optional<SOM::InputVector> inputVector {convertFeatureValuesMapToInputVector(*featureValuesMap, nbDimensions)};
if (!inputVector)
continue;
samples.emplace_back(std::move(*inputVector));
samplesTrackIds.emplace_back(trackId);
}
LMS_LOG(SIMILARITY, DEBUG) << "Extracting features DONE";
if (samples.empty())
{
LMS_LOG(SIMILARITY, INFO) << "Nothing to classify!";
return;
}
LMS_LOG(SIMILARITY, DEBUG) << "Normalizing data...";
SOM::DataNormalizer dataNormalizer {nbDimensions};
dataNormalizer.computeNormalizationFactors(samples);
for (auto& sample : samples)
dataNormalizer.normalizeData(sample);
SOM::Coordinate size {static_cast<SOM::Coordinate>(std::sqrt(samples.size() / trainSettings.sampleCountPerNeuron))};
LMS_LOG(SIMILARITY, INFO) << "Found " << samples.size() << " tracks, constructing a " << size << "*" << size << " network";
SOM::Network network {size, size, nbDimensions};
SOM::InputVector weights {getInputVectorWeights(trainSettings.featureSettingsMap, nbDimensions)};
network.setDataWeights(weights);
auto progressIndicator{[](const auto& iter)
{
LMS_LOG(SIMILARITY, DEBUG) << "Current pass = " << iter.idIteration << " / " << iter.iterationCount;
}};
LMS_LOG(SIMILARITY, DEBUG) << "Training network...";
network.train(samples, trainSettings.iterationCount, progressIndicator, stopRequested);
LMS_LOG(SIMILARITY, DEBUG) << "Training network DONE";
if (stopRequested && stopRequested())
return;
LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks...";
std::map<Database::IdType, std::set<SOM::Position>> trackPositions;
for (std::size_t i {}; i < samples.size(); ++i)
{
if (stopRequested && stopRequested())
return;
const SOM::Position position {network.getClosestRefVectorPosition(samples[i])};
trackPositions[samplesTrackIds[i]].insert(position);
}
LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks DONE";
init(session, std::move(network), std::move(trackPositions), stopRequested);
LMS_LOG(SIMILARITY, INFO) << "Successfully constructed features searcher";
}
FeaturesSearcher::FeaturesSearcher(Database::Session& session, FeaturesCache cache, StopRequestedFunction stopRequested)
{
LMS_LOG(SIMILARITY, INFO) << "Constructing features searcher from cache...";
init(session, std::move(cache._network), std::move(cache._trackPositions), stopRequested);
LMS_LOG(SIMILARITY, INFO) << "Successfully constructed features searcher from cache";
}
bool
FeaturesSearcher::isValid() const
{
return _network.get() != nullptr;
}
bool
FeaturesSearcher::isTrackClassified(Database::IdType trackId) const
{
return (_trackPositions.find(trackId) != _trackPositions.end());
}
bool
FeaturesSearcher::isReleaseClassified(Database::IdType releaseId) const
{
return (_releasePositions.find(releaseId) != _releasePositions.end());
}
bool
FeaturesSearcher::isArtistClassified(Database::IdType artistId) const
{
return (_artistPositions.find(artistId) != _artistPositions.end());
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarTracks(const std::set<Database::IdType>& tracksIds, std::size_t maxCount) const
{
return getSimilarObjects(tracksIds, _tracksMap, _trackPositions, maxCount);
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const
{
return getSimilarObjects({releaseId}, _releasesMap, _releasePositions, maxCount);
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const
{
return getSimilarObjects({artistId}, _artistsMap, _artistPositions, maxCount);
}
void
FeaturesSearcher::dump(Database::Session& session, std::ostream& os) const
{
if (!isValid())
{
os << "Invalid searcher" << std::endl;
return;
}
os << "Number of tracks classified: " << _trackPositions.size() << std::endl;
os << "Network size: " << _network->getWidth() << " * " << _network->getHeight() << std::endl;
os << "Ref vectors median distance = " << _networkRefVectorsDistanceMedian << std::endl;
auto transaction {session.createSharedTransaction()};
for (SOM::Coordinate y {}; y < _network->getHeight(); ++y)
{
for (SOM::Coordinate x {}; x < _network->getWidth(); ++x)
{
const auto& trackIds {_tracksMap[{x, y}]};
os << "{" << x << ", " << y << "}";
if (y > 0)
os << " - {" << x << ", " << y - 1 << "}: " << _network->getRefVectorsDistance({x, y}, {x, y - 1});
if (x > 0)
os << " - {" << x - 1 << ", " << y << "}: " << _network->getRefVectorsDistance({x, y}, {x - 1, y});
if (y != _network->getHeight() - 1)
os << " - {" << x << ", " << y + 1 << "}: " << _network->getRefVectorsDistance({x, y}, {x, y + 1});
if (x != _network->getWidth() - 1)
os << " - {" << x + 1 << ", " << y << "}: " << _network->getRefVectorsDistance({x, y}, {x + 1, y});
os << std::endl;
for (Database::IdType trackId : trackIds)
{
auto track {Database::Track::getById(session, trackId)};
if (!track)
continue;
os << "\t";
for (auto artist : track->getArtists())
os << artist->getName() << " - ";
if (track->getRelease())
os << track->getRelease()->getName() << " - ";
os << track->getName() << std::endl;
}
}
os << std::endl;
}
}
FeaturesCache
FeaturesSearcher::toCache() const
{
return FeaturesCache{*_network, _trackPositions};
}
void
FeaturesSearcher::init(Database::Session& session,
SOM::Network network,
std::map<Database::IdType,
std::set<SOM::Position>> tracksPosition,
std::function<bool()> stopRequested)
{
_networkRefVectorsDistanceMedian = network.computeRefVectorsDistanceMedian();
LMS_LOG(SIMILARITY, DEBUG) << "Median distance betweend ref vectors = " << _networkRefVectorsDistanceMedian;
SOM::Coordinate width {network.getWidth()};
SOM::Coordinate height {network.getHeight()};
_artistsMap = SOM::Matrix<std::set<Database::IdType>>{width, height};
_releasesMap = SOM::Matrix<std::set<Database::IdType>>{width, height};
_tracksMap = SOM::Matrix<std::set<Database::IdType>>{width, height};
LMS_LOG(SIMILARITY, DEBUG) << "Constructing maps...";
for (auto itTrackCoord : tracksPosition)
{
if (stopRequested && stopRequested())
return;
auto transaction {session.createSharedTransaction()};
Database::IdType trackId {itTrackCoord.first};
const std::set<SOM::Position>& positionSet {itTrackCoord.second};
const Database::Track::pointer track {Database::Track::getById(session, trackId)};
if (!track)
continue;
for (const SOM::Position& position : positionSet)
{
_tracksMap[position].insert(trackId);
_trackPositions[trackId].insert(position);
if (track->getRelease())
{
_releasePositions[track->getRelease().id()].insert(position);
_releasesMap[position].insert(track->getRelease().id());
}
for (const auto& artist : track->getArtists())
{
_artistPositions[artist.id()].insert(position);
_artistsMap[position].insert(artist.id());
}
}
}
_network = std::make_unique<SOM::Network>(std::move(network));
LMS_LOG(SIMILARITY, DEBUG) << "Constructing maps... DONE";
}
static
std::set<SOM::Position>
getMatchingRefVectorsPosition(const std::set<Database::IdType>& ids, const std::map<Database::IdType, std::set<SOM::Position>>& objectPosition)
{
std::set<SOM::Position> res;
if (ids.empty())
return res;
for (auto id : ids)
{
auto it = objectPosition.find(id);
if (it == objectPosition.end())
continue;
for (const auto& position : it->second)
res.insert(position);
}
return res;
}
static
std::set<Database::IdType>
getObjectsIds(const std::set<SOM::Position>& positionSet, const SOM::Matrix<std::set<Database::IdType>>& objectsMap )
{
std::set<Database::IdType> res;
for (const auto& position : positionSet)
{
for (auto id : objectsMap.get(position))
res.insert(id);
}
return res;
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarObjects(const std::set<Database::IdType>& ids,
const SOM::Matrix<std::set<Database::IdType>>& objectsMap,
const std::map<Database::IdType, std::set<SOM::Position>>& objectPosition,
std::size_t maxCount) const
{
std::vector<Database::IdType> res;
if (!isValid())
return res;
auto now {std::chrono::system_clock::now()};
std::mt19937 randGenerator{static_cast<std::mt19937::result_type>(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count())};
std::set<SOM::Position> searchedRefVectorsPosition {getMatchingRefVectorsPosition(ids, objectPosition)};
if (searchedRefVectorsPosition.empty())
return res;
while (1)
{
std::set<Database::IdType> closestObjectIds {getObjectsIds(searchedRefVectorsPosition, objectsMap)};
// Remove objects that are already in input or already reported
for (auto id : ids)
closestObjectIds.erase(id);
for (auto id : res)
closestObjectIds.erase(id);
{
std::vector<Database::IdType> objectIdsToAdd {closestObjectIds.begin(), closestObjectIds.end()};
std::shuffle(objectIdsToAdd.begin(), objectIdsToAdd.end(), randGenerator);
std::copy(objectIdsToAdd.begin(), objectIdsToAdd.end(), std::back_inserter(res));
}
if (res.size() > maxCount)
res.resize(maxCount);
if (res.size() == maxCount)
break;
// If there is not enough objects, try again with closest neighbour until there is too much distance
std::optional<SOM::Position> closestRefVectorPosition {_network->getClosestRefVectorPosition(searchedRefVectorsPosition, _networkRefVectorsDistanceMedian * 0.75)};
if (!closestRefVectorPosition)
break;
searchedRefVectorsPosition.insert(*closestRefVectorPosition);
}
return res;
}
} // ns Similarity
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2018 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 <map>
#include <optional>
#include <set>
#include <string>
#include "database/Types.hpp"
#include "som/DataNormalizer.hpp"
#include "som/Network.hpp"
#include "SimilarityFeaturesCache.hpp"
#include "SimilarityFeaturesDefs.hpp"
namespace Database
{
class Session;
}
namespace Similarity {
using FeatureWeight = double;
class FeaturesSearcher
{
public:
using StopRequestedFunction = std::function<bool()>; // return true if stop requested
// Use cache
FeaturesSearcher(Database::Session& session, FeaturesCache cache, StopRequestedFunction stopRequested);
// Use training (may be very slow)
struct TrainSettings
{
std::size_t iterationCount {10};
float sampleCountPerNeuron {4};
FeatureSettingsMap featureSettingsMap;
};
FeaturesSearcher(Database::Session& session, const TrainSettings& trainSettings, StopRequestedFunction stopRequested = {});
static const FeatureSettingsMap& getDefaultTrainFeatureSettings();
bool isValid() const;
bool isTrackClassified(Database::IdType trackId) const;
bool isReleaseClassified(Database::IdType releaseId) const;
bool isArtistClassified(Database::IdType artistId) const;
std::vector<Database::IdType> getSimilarTracks(const std::set<Database::IdType>& tracksId, std::size_t maxCount) const;
std::vector<Database::IdType> getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const;
std::vector<Database::IdType> getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const;
void dump(Database::Session& session, std::ostream& os) const;
FeaturesCache toCache() const;
using FeaturesFetchFunc = std::function<std::optional<std::unordered_map<std::string, std::vector<double>>>(Database::IdType /*trackId*/, const std::unordered_set<std::string>& /*features*/)>;
// Default is to retrieve the features from the database (may be slow).
// Use this only if you want to train different searchers with the same data
static void setFeaturesFetchFunc(FeaturesFetchFunc func) { _featuresFetchFunc = func; }
private:
using ObjectPositions = std::map<Database::IdType, std::set<SOM::Position>>;
void init(Database::Session& session,
SOM::Network network,
ObjectPositions tracksPosition,
StopRequestedFunction stopRequested);
std::vector<Database::IdType> getSimilarObjects(const std::set<Database::IdType>& ids,
const SOM::Matrix<std::set<Database::IdType>>& objectsMap,
const ObjectPositions& objectPosition,
std::size_t maxCount) const;
std::unique_ptr<SOM::Network> _network;
double _networkRefVectorsDistanceMedian {};
SOM::Matrix<std::set<Database::IdType>> _artistsMap;
ObjectPositions _artistPositions;
SOM::Matrix<std::set<Database::IdType>> _releasesMap;
ObjectPositions _releasePositions;
SOM::Matrix<std::set<Database::IdType>> _tracksMap;
ObjectPositions _trackPositions;
static inline FeaturesFetchFunc _featuresFetchFunc;
};
} // ns Similarity
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2018 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 "DataNormalizer.hpp"
#include <algorithm>
#include <numeric>
#include <sstream>
namespace SOM
{
template<typename T>
static
T
variance(const std::vector<T>& vec)
{
std::size_t size {vec.size()};
if (size == 1)
return T {};
const T mean {std::accumulate(vec.begin(), vec.end(), T{}) / size};
return std::accumulate(vec.begin(), vec.end(), T {},
[mean, size] (T accumulator, const T& val)
{
return accumulator + ((val - mean) * (val - mean) / (size - 1));
});
}
DataNormalizer::DataNormalizer(std::size_t inputDimCount)
: _inputDimCount{inputDimCount}
{
}
const DataNormalizer::MinMax&
DataNormalizer::getValue(std::size_t index) const
{
return _minmax[index];
}
void
DataNormalizer::setValue(std::size_t index, const MinMax& minMax)
{
_minmax[index] = minMax;
}
void
DataNormalizer::computeNormalizationFactors(const std::vector<InputVector>& inputVectors)
{
if (inputVectors.empty())
throw Exception("Empty input vectors");
// For each dimension of the input, compute the min/max
_minmax.clear();
_minmax.resize(_inputDimCount);
for (std::size_t dimId {}; dimId < _inputDimCount; ++dimId)
{
std::vector<InputVector::value_type> values;
for (const auto& inputVector: inputVectors)
{
checkSameDimensions(inputVector, _inputDimCount);
values.push_back(inputVector[dimId]);
}
auto result {std::minmax_element(values.begin(), values.end())};
_minmax[dimId] = {*result.first, *result.second};
}
}
InputVector::value_type
DataNormalizer::normalizeValue(InputVector::value_type value, std::size_t dimId) const
{
// clamp
if (value > _minmax[dimId].max)
value = _minmax[dimId].max;
else if (value < _minmax[dimId].min)
value = _minmax[dimId].min;
return (value - _minmax[dimId].min) / (_minmax[dimId].max - _minmax[dimId].min);
}
void
DataNormalizer::normalizeData(InputVector& a) const
{
checkSameDimensions(a, _inputDimCount);
for (std::size_t dimId {}; dimId < _inputDimCount; ++dimId)
{
a[dimId] = normalizeValue(a[dimId], dimId);
}
}
void
DataNormalizer::dump(std::ostream& os) const
{
for (std::size_t i {}; i < _inputDimCount; ++i)
os << "(" << _minmax[i].min << ", " << _minmax[i].max << ")";
}
} // namespace SOM
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2018 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 <vector>
#include <ostream>
#include "Network.hpp"
namespace SOM
{
class DataNormalizer
{
public:
struct MinMax
{
InputVector::value_type min;
InputVector::value_type max;
};
DataNormalizer(std::size_t inputDimCount);
std::size_t getInputDimCount() const { return _inputDimCount; }
const MinMax& getValue(std::size_t index) const;
void setValue(std::size_t index, const MinMax& minMax);
void computeNormalizationFactors(const std::vector<InputVector>& dataSamples);
void normalizeData(InputVector& data) const;
void dump(std::ostream& os) const;
private:
InputVector::value_type normalizeValue(InputVector::value_type value, std::size_t dimensionId) const;
const std::size_t _inputDimCount;
std::vector<MinMax> _minmax; // Indexed min/max used to normalize data
};
} // namespace SOM
@@ -0,0 +1,194 @@
/*
* Copyright (C) 2018 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 <vector>
#include <cmath>
namespace SOM
{
class Exception : public LmsException
{
public:
Exception(const std::string& msg) : LmsException(msg) {}
};
class InputVector
{
public:
using value_type = double;
using Norm = double;
using Distance = double;
InputVector(std::size_t nbDimensions, value_type defaultValue = value_type {}) : _values(nbDimensions, defaultValue) {}
bool hasSameDimension(const InputVector& other) const
{
return _values.size() == other._values.size();
}
std::size_t getNbDimensions() const
{
return _values.size();
}
value_type& operator[](std::size_t index)
{
if (index >= getNbDimensions())
throw Exception("Bad range");
return _values[index];
}
value_type operator[](std::size_t index) const
{
if (index >= getNbDimensions())
throw Exception("Bad range");
return _values[index];
}
InputVector& operator+=(const InputVector& other)
{
if (!hasSameDimension(other.getNbDimensions()))
throw Exception {"Not the same dimension count"};
for (std::size_t i {}; i < _values.size(); ++i)
{
_values[i] += other[i];
}
return *this;
}
InputVector& operator-=(const InputVector& other)
{
if (!hasSameDimension(other.getNbDimensions()))
throw Exception {"Not the same dimension count"};
for (std::size_t i {}; i < _values.size(); ++i)
{
_values[i] -= other[i];
}
return *this;
}
InputVector& operator*=(value_type factor)
{
for (std::size_t i {}; i < _values.size(); ++i)
{
_values[i] *= factor;
}
return *this;
}
Norm computeNorm() const
{
Norm res {};
for (value_type val : _values)
res += val * val;
return std::sqrt(res);
}
Distance computeEuclidianSquareDistance(const InputVector& other, const InputVector& weights) const
{
if (!hasSameDimension(other.getNbDimensions())
|| !hasSameDimension(weights.getNbDimensions()))
{
throw Exception {"Not the same dimension count"};
}
Distance res {};
for (std::size_t i {}; i < getNbDimensions(); ++i)
{
const InputVector::value_type diff {_values[i] - other._values[i]};
res += diff * diff * weights._values[i];
}
return res;
}
std::vector<value_type>::iterator begin()
{
return _values.begin();
}
std::vector<value_type>::const_iterator begin() const
{
return _values.cbegin();
}
std::vector<value_type>::const_iterator cbegin() const
{
return _values.cbegin();
}
std::vector<value_type>::iterator end()
{
return _values.end();
}
std::vector<value_type>::const_iterator end() const
{
return _values.cend();
}
std::vector<value_type>::const_iterator cend() const
{
return _values.cend();
}
private:
friend class InputVector operator-(const InputVector& a, const InputVector& b)
{
if (!a.hasSameDimension(b.getNbDimensions()))
throw Exception {"Not the same dimension count"};
InputVector res {a.getNbDimensions()};
for (std::size_t i {}; i < res._values.size(); ++i)
res._values[i] = a._values[i] - b._values[i];
return res;
}
friend std::ostream&
operator<<(std::ostream& os, const InputVector& a)
{
os << "[";
for (value_type val : a._values)
{
os << val << " ";
}
os << "]";
return os;
}
std::vector<value_type> _values;
};
}
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2018 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 <algorithm>
#include <cassert>
#include <sstream>
#include <vector>
namespace SOM
{
using Coordinate = unsigned;
using Norm = InputVector::value_type;
struct Position
{
Coordinate x;
Coordinate y;
bool operator<(const Position& other) const
{
if (x == other.x)
return y < other.y;
else
return x < other.x;
}
bool operator==(const Position& other) const
{
return x == other.x && y == other.y;
}
};
template <typename T>
class Matrix
{
public:
Matrix() = default;
Matrix(Coordinate width, Coordinate height)
: _width{width},
_height{height}
{
_values.resize(_width*_height);
}
template<typename... CtArgs>
Matrix(Coordinate width, Coordinate height, CtArgs... args)
: _width{width},
_height{height}
{
_values.resize(_width*_height, T{args...});
}
void clear()
{
std::vector<T> values(_width*_height);
_values.swap(values);
}
Coordinate getHeight() const { return _height; }
Coordinate getWidth() const { return _width; }
T& get(const Position& position)
{
assert(position.x < _width);
assert(position.y < _height);
return _values[position.x + _width*position.y];
}
const T& get(const Position& position) const
{
assert(position.x < _width);
assert(position.y < _height);
return _values[position.x + _width*position.y];
}
T& operator[](const Position& position) { return get(position); }
const T& operator[](const Position& position) const { return get(position); }
template <typename Func>
Position getPositionMinElement(Func func) const
{
assert(!_values.empty());
auto it {std::min_element(_values.begin(), _values.end(), std::move(func))};
auto index {static_cast<Coordinate>(std::distance(_values.begin(), it))};
return {index % _height, index / _height};
}
private:
Coordinate _width {};
Coordinate _height {};
std::vector<T> _values;
};
} // ns SOM
@@ -0,0 +1,336 @@
/*
* Copyright (C) 2018 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 "Network.hpp"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <random>
#include <sstream>
#include "utils/Logger.hpp"
namespace SOM
{
void
checkSameDimensions(const InputVector& a, const InputVector& b)
{
if (!a.hasSameDimension(b))
throw Exception("Bad data dimension count");
}
void
checkSameDimensions(const InputVector& a, std::size_t inputDimCount)
{
if (a.getNbDimensions() != inputDimCount)
throw Exception("Bad data dimension count");
}
static LearningFactor
defaultLearningFactor(Network::CurrentIteration iteration)
{
static const LearningFactor initialValue{1};
return initialValue * exp(-((iteration.idIteration + 1) / static_cast<LearningFactor>(iteration.iterationCount)));
}
static InputVector::Distance
euclidianSquareDistance(const InputVector& a, const InputVector& b, const InputVector& weights)
{
return a.computeEuclidianSquareDistance(b, weights);
}
static
InputVector::value_type
sigmaFunc(Network::CurrentIteration iteration)
{
constexpr InputVector::value_type sigma0 {1};
return sigma0 * std::exp(- ((iteration.idIteration + 1) / static_cast<InputVector::value_type>(iteration.iterationCount)));
}
static
InputVector::value_type
defaultNeighbourhoodFunc(Norm norm, const Network::CurrentIteration& iteration)
{
InputVector::value_type sigma {sigmaFunc(iteration)};
return exp(-norm / (2 * sigma * sigma));
}
Network::Network(Coordinate width, Coordinate height, std::size_t inputDimCount)
:
_inputDimCount(inputDimCount),
_weights(inputDimCount, static_cast<InputVector::value_type>(1)),
_refVectors(width, height, _inputDimCount),
_distanceFunc(euclidianSquareDistance),
_learningFactorFunc(defaultLearningFactor),
_neighbourhoodFunc(defaultNeighbourhoodFunc)
{
auto now {std::chrono::system_clock::now()};
std::mt19937 randGenerator {static_cast<std::mt19937::result_type>(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count())};
// init each vector with a random normalized value
std::uniform_real_distribution<InputVector::value_type> dist{0, 1};
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
for (InputVector::value_type& val : _refVectors.get({x,y}))
val = dist(randGenerator);
}
}
}
void
Network::setDataWeights(const InputVector& weights)
{
checkSameDimensions(weights, _inputDimCount);
_weights = weights;
}
void
Network::setRefVector(const Position& position, const InputVector& data)
{
checkSameDimensions(data, _inputDimCount);
_refVectors[position] = data;
}
InputVector::Distance
Network::getRefVectorsDistance(const Position& position1, const Position& position2) const
{
return _distanceFunc(_refVectors.get(position1), _refVectors.get(position2), _weights);
}
InputVector::Distance
Network::computeRefVectorsDistanceMean() const
{
std::vector<InputVector::Distance> values;
values.reserve(2 * _refVectors.getHeight()*_refVectors.getWidth() - _refVectors.getWidth() - _refVectors.getHeight());
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
if (x != _refVectors.getWidth() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x + 1, y}));
if (y != _refVectors.getHeight() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x, y + 1}));
}
}
return std::accumulate(values.begin(), values.end(), 0.) / values.size();
}
double
Network::computeRefVectorsDistanceMedian() const
{
std::vector<InputVector::Distance> values;
values.reserve(2*_refVectors.getHeight()*_refVectors.getWidth() - _refVectors.getWidth() - _refVectors.getHeight());
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
if (x != _refVectors.getWidth() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x + 1, y}));
if (y != _refVectors.getHeight() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x, y + 1}));
}
}
std::sort(values.begin(), values.end());
return values[values.size() > 1 ? values.size()/2 - 1 : 0];
}
void
Network::dump(std::ostream& os) const
{
os << "Width: " << _refVectors.getWidth() << ", Height: " << _refVectors.getHeight() << std::endl;;
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
os << _refVectors.get({x, y}) << " ";
}
os << std::endl;
}
os << std::endl;
}
Position
Network::getClosestRefVectorPosition(const InputVector& data) const
{
return _refVectors.getPositionMinElement([&](const auto& a, const auto& b)
{
return (_distanceFunc(a, data, _weights) < _distanceFunc(b, data, _weights));
});
}
std::optional<Position>
Network::getClosestRefVectorPosition(const InputVector& data, InputVector::Distance maxDistance) const
{
std::optional<Position> position {getClosestRefVectorPosition(data)};
if (_distanceFunc(data, _refVectors.get(*position), _weights) > maxDistance)
position.reset();
return position;
}
std::optional<Position>
Network::getClosestRefVectorPosition(const std::set<Position>& refVectorsPosition, InputVector::Distance maxDistance) const
{
std::set<Position> neighboursPosition;
for (const Position& refVectorPosition : refVectorsPosition)
{
if (refVectorPosition.y > 0)
neighboursPosition.insert({ refVectorPosition.x, refVectorPosition.y - 1 });
if (refVectorPosition.y < _refVectors.getHeight() - 1)
neighboursPosition.insert({ refVectorPosition.x, refVectorPosition.y + 1 });
if (refVectorPosition.x > 0)
neighboursPosition.insert({ refVectorPosition.x - 1, refVectorPosition.y });
if (refVectorPosition.x < _refVectors.getWidth() - 1)
neighboursPosition.insert({ refVectorPosition.x + 1, refVectorPosition.y });
}
// remove position that are in the input position
for (const auto& refVectorPosition : refVectorsPosition)
neighboursPosition.erase(refVectorPosition);
if (neighboursPosition.empty())
return std::nullopt;
// Now compute the distance for each neighbour
struct NeighbourInfo
{
Position position;
double distance;
};
std::vector<NeighbourInfo> neighboursInfo;
for (const Position& neighbourPosition : neighboursPosition)
{
auto min = std::min_element(refVectorsPosition.begin(), refVectorsPosition.end(),
[this, neighbourPosition](const auto& a, const auto& b)
{
return (this->getRefVectorsDistance(a, neighbourPosition) < this->getRefVectorsDistance(b, neighbourPosition));
});
InputVector::Distance distance {getRefVectorsDistance(neighbourPosition, *min)};
if (distance > maxDistance)
continue;
neighboursInfo.emplace_back(NeighbourInfo {neighbourPosition, distance});
}
if (neighboursInfo.empty())
return std::nullopt;
auto min {std::min_element(std::cbegin(neighboursInfo), std::cend(neighboursInfo),
[&](const auto& a, const auto& b)
{
return a.distance < b.distance;
})};
return min->position;
}
static Norm
computePositionNorm(const Position& c1, const Position& c2)
{
return std::sqrt((c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y));
}
void
Network::updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration)
{
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
InputVector& refVector {_refVectors.get({x, y})};
const Norm norm {computePositionNorm({x, y}, closestRefVectorPosition)};
InputVector delta {input - refVector};
delta *= (learningFactor * _neighbourhoodFunc(norm, iteration));
refVector += delta;
}
}
}
void
Network::train(const std::vector<InputVector>& inputData, std::size_t nbIterations, ProgressCallback progressCallback, RequestStopCallback requestStopCallback)
{
bool stopRequested {false};
std::vector<const InputVector*> inputDataShuffled;
inputDataShuffled.reserve(inputData.size());
for (const auto& input : inputData)
inputDataShuffled.push_back(&input);
auto now {std::chrono::system_clock::now()};
std::mt19937 randGenerator{static_cast<std::mt19937::result_type>(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count())};
for (std::size_t i {}; i < nbIterations; ++i)
{
CurrentIteration curIter {i, nbIterations};
if (progressCallback)
progressCallback(curIter);
std::shuffle(inputDataShuffled.begin(), inputDataShuffled.end(), randGenerator);
const LearningFactor learningFactor {_learningFactorFunc(curIter)};
for (const InputVector* input : inputDataShuffled)
{
if (requestStopCallback)
stopRequested = requestStopCallback();
if (stopRequested)
return;
updateRefVectors(getClosestRefVectorPosition(*input), *input, learningFactor, curIter);
}
if (stopRequested)
return;
}
}
const InputVector&
Network::getRefVector(const Position& position) const
{
return _refVectors[position];
}
} // namespace SOM
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2018 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 <vector>
#include <set>
#include <optional>
#include <ostream>
#include <functional>
#include "utils/Exception.hpp"
#include "InputVector.hpp"
#include "Matrix.hpp"
namespace SOM
{
using LearningFactor = InputVector::value_type;
void checkSameDimensions(const InputVector& a, const InputVector& b);
void checkSameDimensions(const InputVector& a, std::size_t inputDimCount);
std::ostream& operator<<(std::ostream& os, const InputVector& a);
class Network
{
public:
// Init a network with random values
Network(Coordinate width, Coordinate height, std::size_t inputDimCount);
Coordinate getWidth() const { return _refVectors.getWidth(); }
Coordinate getHeight() const { return _refVectors.getHeight(); }
std::size_t getInputDimCount() const { return _inputDimCount; }
const InputVector& getDataWeights() const { return _weights; }
// Set weight for each dimension (default is 1 for each weight)
void setDataWeights(const InputVector& weights);
// use this to manually construct a network without training
void setRefVector(const Position& position, const InputVector& data);
// <!> data must be normalized
struct CurrentIteration
{
std::size_t idIteration;
std::size_t iterationCount;
};
using ProgressCallback = std::function<void(const CurrentIteration&)>;
using RequestStopCallback = std::function<bool()>;
void train(const std::vector<InputVector>& dataSamples, std::size_t nbIterations, ProgressCallback = ProgressCallback{}, RequestStopCallback = RequestStopCallback{});
const InputVector& getRefVector(const Position& position) const;
Position getClosestRefVectorPosition(const InputVector& data) const;
std::optional<Position> getClosestRefVectorPosition(const InputVector& data, InputVector::Distance maxDistance) const;
std::optional<Position> getClosestRefVectorPosition(const std::set<Position>& refVectorsPosition, InputVector::Distance maxDistance) const;
InputVector::Distance getRefVectorsDistance(const Position& position1, const Position& position2) const;
InputVector::Distance computeRefVectorsDistanceMean() const;
InputVector::Distance computeRefVectorsDistanceMedian() const;
void dump(std::ostream& os) const;
// For each ref vector, update formula is:
// i is the current iteration
// refVector(i+1) = refVector(i) + LearningFactor(i) * NeighbourhoodFunc(i) * (MatchingRefVector - refVector)
using DistanceFunc = std::function<InputVector::Distance(const InputVector& /* a */, const InputVector& /* b */, const InputVector& /* weights */)>;
void setDistanceFunc(DistanceFunc distanceFunc);
DistanceFunc getDistanceFunc() { return _distanceFunc; }
using LearningFactorFunc = std::function<LearningFactor(const CurrentIteration&)>;
void setLearningFactorFunc(LearningFactorFunc learningFactorFunc);
using NeighbourhoodFunc = std::function<InputVector::value_type(Norm /* norm(Position - CoordMatchingRefVector) */, const CurrentIteration&)>;
void setNeighbourhoodFunc(NeighbourhoodFunc neighbourhoodFunc);
private:
void updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration);
std::size_t _inputDimCount {};
InputVector _weights; // weight for each dimension
Matrix<InputVector> _refVectors;
DistanceFunc _distanceFunc;
LearningFactorFunc _learningFactorFunc;
NeighbourhoodFunc _neighbourhoodFunc;
};
} // namespace SOM
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2020 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 <memory>
namespace Database
{
class Session;
}
namespace Recommendation
{
class Provider;
std::unique_ptr<Provider> createClustersRecommendationProvider();
}
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 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 <memory>
namespace Database
{
class Session;
}
namespace Scanner
{
class IMediaScanner;
}
namespace Recommendation
{
class Provider;
std::unique_ptr<Provider> createFeaturesRecommendationProvider(Scanner::IMediaScanner& scanner);
}
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2019 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 <vector>
#include <unordered_set>
#include "database/Types.hpp"
#include "Provider.hpp"
namespace Database
{
class Session;
}
namespace Recommendation
{
class Provider;
class IEngine
{
public:
virtual ~IEngine() = default;
virtual void clearProviders() = 0;
virtual void addProvider(std::unique_ptr<Provider> provider, unsigned priority) = 0;
// Closest results first
virtual std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::unordered_set<Database::IdType>& tracksId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarReleases(Database::Session& session, Database::IdType releaseId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarArtists(Database::Session& session, Database::IdType artistId, std::size_t maxCount) = 0;
};
std::unique_ptr<IEngine> createEngine();
} // ns Recommendation
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2020 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 <unordered_set>
#include <vector>
#include "database/Types.hpp"
namespace Database
{
class Session;
}
namespace Recommendation
{
class Provider
{
public:
virtual ~Provider() = default;
virtual bool isTrackClassified(Database::IdType trackId) const = 0;
virtual bool isReleaseClassified(Database::IdType releaseId) const = 0;
virtual bool isArtistClassified(Database::IdType artistId) const = 0;
virtual std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::unordered_set<Database::IdType>& tracksId, std::size_t maxCount) const = 0;
virtual std::vector<Database::IdType> getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const = 0;
virtual std::vector<Database::IdType> getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const = 0;
};
} // ns Recommendation
+29
View File
@@ -0,0 +1,29 @@
add_library(lmsscanner SHARED
impl/metadata/AvFormat.cpp
impl/metadata/TagLibParser.cpp
impl/MediaScanner.cpp
impl/MediaScannerStats.cpp
)
target_include_directories(lmsscanner INTERFACE
include
)
target_include_directories(lmsscanner PRIVATE
include
)
target_link_libraries(lmsscanner PRIVATE
lmsav
lmsdatabase
lmsutils
tag
)
target_link_libraries(lmsscanner PUBLIC
wt
)
install(TARGETS lmsscanner DESTINATION lib)
+829
View File
@@ -0,0 +1,829 @@
/*
* Copyright (C) 2013 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 "MediaScanner.hpp"
#include <boost/asio/placeholders.hpp>
#include <Wt/WLocalDateTime.h>
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Release.hpp"
#include "database/ScanSettings.hpp"
#include "database/Track.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
#include "utils/Path.hpp"
using namespace Database;
namespace {
Wt::WDate
getNextMonday(Wt::WDate current)
{
do
{
current = current.addDays(1);
} while (current.dayOfWeek() != 1);
return current;
}
Wt::WDate
getNextFirstOfMonth(Wt::WDate current)
{
do
{
current = current.addDays(1);
} while (current.day() != 1);
return current;
}
bool
isFileSupported(const std::filesystem::path& file, const std::set<std::filesystem::path>& extensions)
{
return (extensions.find(file.extension()) != extensions.end());
}
bool
isPathInParentPath(const std::filesystem::path& path, const std::filesystem::path& parentPath)
{
std::filesystem::path curPath = path;
while (curPath.parent_path() != curPath)
{
curPath = curPath.parent_path();
if (curPath == parentPath)
return true;
}
return false;
}
std::vector<Artist::pointer>
getOrCreateArtists(Session& session, const std::vector<MetaData::Artist>& artistsInfo)
{
std::vector<Artist::pointer> artists;
for (const MetaData::Artist& artistInfo : artistsInfo)
{
Artist::pointer artist;
// First try to get by MBID
if (artistInfo.musicBrainzArtistID)
{
artist = Artist::getByMBID(session, *artistInfo.musicBrainzArtistID);
if (!artist)
artist = Artist::create(session, artistInfo.name, artistInfo.musicBrainzArtistID);
artists.emplace_back(std::move(artist));
continue;
}
// Fall back on artist name (collisions may occur)
if (!artistInfo.name.empty())
{
for (const Artist::pointer& sameNamedArtist : Artist::getByName(session, artistInfo.name))
{
// Do not fallback on artist that is correctly tagged
if (!sameNamedArtist->getMBID())
{
artist = sameNamedArtist;
break;
}
}
// No Artist found with the same name and without MBID -> creating
if (!artist)
artist = Artist::create(session, artistInfo.name);
artists.emplace_back(std::move(artist));
continue;
}
}
return artists;
}
Release::pointer
getOrCreateRelease(Session& session, const MetaData::Album& album)
{
Release::pointer release;
// First try to get by MBID
if (album.musicBrainzAlbumID)
{
release = Release::getByMBID(session, *album.musicBrainzAlbumID);
if (!release)
release = Release::create(session, album.name, album.musicBrainzAlbumID);
return release;
}
// Fall back on release name (collisions may occur)
if (!album.name.empty())
{
for (const Release::pointer& sameNamedRelease : Release::getByName(session, album.name))
{
// do not fallback on properly tagged releases
if (!sameNamedRelease->getMBID())
{
release = sameNamedRelease;
break;
}
}
// No release found with the same name and without MBID -> creating
if (!release)
release = Release::create(session, album.name);
return release;
}
return Release::pointer{};
}
std::vector<Cluster::pointer>
getOrCreateClusters(Session& session, const MetaData::Clusters& clustersNames)
{
std::vector< Cluster::pointer > clusters;
for (auto clusterNames : clustersNames)
{
auto clusterType = ClusterType::getByName(session, clusterNames.first);
if (!clusterType)
continue;
for (auto clusterName : clusterNames.second)
{
auto cluster = clusterType->getCluster(clusterName);
if (!cluster)
cluster = Cluster::create(session, clusterType, clusterName);
clusters.push_back(cluster);
}
}
return clusters;
}
} // namespace
namespace Scanner {
std::unique_ptr<IMediaScanner>
createMediaScanner(Database::Db& db)
{
return std::make_unique<MediaScanner>(db);
}
MediaScanner::MediaScanner(Database::Db& db)
: _dbSession {db}
{
_ioService.setThreadCount(1);
refreshScanSettings();
}
void
MediaScanner::setAddon(MediaScannerAddon& addon)
{
_addons.push_back(&addon);
}
void
MediaScanner::restart(void)
{
stop();
start();
}
void
MediaScanner::start(void)
{
_running = true;
scheduleNextScan();
_ioService.start();
}
void
MediaScanner::stop(void)
{
_running = false;
for (auto& addon : _addons)
addon->requestStop();
_scheduleTimer.cancel();
_ioService.stop();
}
void
MediaScanner::requestImmediateScan()
{
_ioService.post([=]()
{
scheduleScan();
});
}
void
MediaScanner::requestReschedule()
{
_ioService.post([=]()
{
scheduleNextScan();
});
}
MediaScanner::Status
MediaScanner::getStatus()
{
Status res;
std::unique_lock<std::mutex> lock {_statusMutex};
res.currentState = _curState;
res.nextScheduledScan = _nextScheduledScan;
res.lastCompleteScanStats = _lastCompleteScanStats;
res.inProgressScanStats = _inProgressScanStats;
return res;
}
void
MediaScanner::scheduleNextScan()
{
LMS_LOG(DBUPDATER, INFO) << "Scheduling next scan";
refreshScanSettings();
Wt::WDateTime now {Wt::WLocalDateTime::currentServerDateTime().toUTC()};
Wt::WDate nextScanDate;
switch (_updatePeriod)
{
case ScanSettings::UpdatePeriod::Daily:
if (now.time() < _startTime)
nextScanDate = now.date();
else
nextScanDate = now.date().addDays(1);
break;
case ScanSettings::UpdatePeriod::Weekly:
if (now.time() < _startTime && now.date().dayOfWeek() == 1)
nextScanDate = now.date();
else
nextScanDate = getNextMonday(now.date());
break;
case ScanSettings::UpdatePeriod::Monthly:
if (now.time() < _startTime && now.date().day() == 1)
nextScanDate = now.date();
else
nextScanDate = getNextFirstOfMonth(now.date());
break;
case ScanSettings::UpdatePeriod::Never:
LMS_LOG(DBUPDATER, INFO) << "Auto scan disabled!";
break;
}
Wt::WDateTime nextScanDateTime;
if (nextScanDate.isValid())
{
nextScanDateTime = Wt::WDateTime {nextScanDate, _startTime};
scheduleScan(nextScanDateTime);
}
{
std::unique_lock<std::mutex> lock {_statusMutex};
_curState = nextScanDateTime.isValid() ? State::Scheduled : State::NotScheduled;
_nextScheduledScan = nextScanDateTime;
}
_sigScheduled.emit(_nextScheduledScan);
}
void
MediaScanner::countAllFiles(ScanStats& stats)
{
std::error_code ec;
stats.filesToScan = 0;
std::filesystem::recursive_directory_iterator itPath {_mediaDirectory, std::filesystem::directory_options::follow_directory_symlink, ec};
if (ec)
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot iterate over '" << _mediaDirectory.string() << "': " << ec.message();
return;
}
std::filesystem::recursive_directory_iterator itEnd;
while (_running && itPath != itEnd)
{
const std::filesystem::path& path {*itPath};
if (!ec)
{
if (std::filesystem::is_regular_file(path) && isFileSupported(path, _fileExtensions))
stats.filesToScan ++;
if (stats.filesToScan % 250 == 0)
notifyInProgressIfNeeded(stats);
}
itPath.increment(ec);
}
}
void
MediaScanner::scheduleScan(const Wt::WDateTime& dateTime)
{
if (dateTime.isNull())
{
LMS_LOG(DBUPDATER, INFO) << "Scheduling next scan right now";
_scheduleTimer.expires_from_now(std::chrono::seconds(0));
_scheduleTimer.async_wait(std::bind(&MediaScanner::scan, this, std::placeholders::_1));
}
else
{
std::chrono::system_clock::time_point timePoint {dateTime.toTimePoint()};
std::time_t t {std::chrono::system_clock::to_time_t(timePoint)};
LMS_LOG(DBUPDATER, INFO) << "Scheduling next scan at " << std::string(std::ctime(&t));
_scheduleTimer.expires_at(timePoint);
_scheduleTimer.async_wait(std::bind(&MediaScanner::scan, this, std::placeholders::_1));
}
}
void
MediaScanner::scan(boost::system::error_code err)
{
if (err)
return;
{
std::unique_lock<std::mutex> lock {_statusMutex};
_curState = State::InProgress;
_nextScheduledScan = {};
}
ScanStats stats;
stats.startTime = Wt::WLocalDateTime::currentDateTime().toUTC();
LMS_LOG(UI, INFO) << "New scan started!";
refreshScanSettings();
bool forceScan {false};
LMS_LOG(DBUPDATER, DEBUG) << "Counting files in media directory '" << _mediaDirectory.string() << "'...";
countAllFiles(stats);
LMS_LOG(DBUPDATER, DEBUG) << "-> Nb files = " << stats.filesToScan;
removeMissingTracks(stats);
LMS_LOG(UI, INFO) << "Checks complete, force scan = " << forceScan;
LMS_LOG(DBUPDATER, INFO) << "scaning media directory '" << _mediaDirectory.string() << "'...";
scanMediaDirectory(_mediaDirectory, forceScan, stats);
LMS_LOG(DBUPDATER, INFO) << "scaning media directory '" << _mediaDirectory.string() << "' DONE";
removeOrphanEntries();
if (_running)
checkDuplicatedAudioFiles(stats);
LMS_LOG(DBUPDATER, INFO) << "Scan " << (_running ? "complete" : "aborted") << ". Changes = " << stats.nbChanges() << " (added = " << stats.additions << ", removed = " << stats.deletions << ", updated = " << stats.updates << "), Not changed = " << stats.skips << ", Scanned = " << stats.scans << " (errors = " << stats.errors.size() << "), duplicates = " << stats.duplicates.size();
if (_running)
{
for (auto& addon : _addons)
addon->preScanComplete();
}
if (_running)
{
stats.stopTime = Wt::WLocalDateTime::currentDateTime().toUTC();
{
std::unique_lock<std::mutex> lock {_statusMutex};
_lastCompleteScanStats = std::move(stats);
_inProgressScanStats.reset();
}
scheduleNextScan();
scanComplete().emit();
}
else
{
std::unique_lock<std::mutex> lock {_statusMutex};
_curState = State::NotScheduled;
_inProgressScanStats.reset();
}
LMS_LOG(DBUPDATER, INFO) << "Optimizing db...";
_dbSession.optimize();
LMS_LOG(DBUPDATER, INFO) << "Optimize db done!";
}
void
MediaScanner::refreshScanSettings()
{
{
auto transaction {_dbSession.createSharedTransaction()};
ScanSettings::pointer scanSettings {ScanSettings::get(_dbSession)};
LMS_LOG(DBUPDATER, INFO) << "Using scan settings version " << scanSettings->getScanVersion();
_scanVersion = scanSettings->getScanVersion();
_startTime = scanSettings->getUpdateStartTime();
_updatePeriod = scanSettings->getUpdatePeriod();
_fileExtensions = scanSettings->getAudioFileExtensions();
_mediaDirectory = scanSettings->getMediaDirectory();
auto clusterTypes = scanSettings->getClusterTypes();
std::set<std::string> clusterTypeNames;
std::transform(std::cbegin(clusterTypes), std::cend(clusterTypes),
std::inserter(clusterTypeNames, clusterTypeNames.begin()),
[](ClusterType::pointer clusterType) { return clusterType->getName(); });
_metadataParser.setClusterTypeNames(clusterTypeNames);
}
for (auto& addon : _addons)
addon->refreshSettings();
}
void
MediaScanner::notifyInProgress(const ScanStats& stats)
{
{
std::unique_lock<std::mutex> lock {_statusMutex};
_inProgressScanStats = stats.toProgressStats();
}
std::chrono::system_clock::time_point now {std::chrono::system_clock::now()};
_sigScanInProgress(*_inProgressScanStats);
_lastScanInProgressEmit = now;
}
void
MediaScanner::notifyInProgressIfNeeded(const ScanStats& stats)
{
std::chrono::system_clock::time_point now {std::chrono::system_clock::now()};
if (std::chrono::duration_cast<std::chrono::seconds>(now - _lastScanInProgressEmit).count() > 2)
notifyInProgress(stats);
}
void
MediaScanner::scanAudioFile(const std::filesystem::path& file, bool forceScan, ScanStats& stats)
{
notifyInProgressIfNeeded(stats);
Wt::WDateTime lastWriteTime;
try
{
lastWriteTime = getLastWriteTime(file);
}
catch (LmsException& e)
{
LMS_LOG(DBUPDATER, ERROR) << e.what();
stats.skips++;
return;
}
if (!forceScan)
{
// Skip file if last write is the same
auto transaction {_dbSession.createSharedTransaction()};
const Track::pointer track {Track::getByPath(_dbSession, file)};
if (track && track->getLastWriteTime().toTime_t() == lastWriteTime.toTime_t()
&& track->getScanVersion() == _scanVersion)
{
stats.skips++;
return;
}
}
std::optional<MetaData::Track> trackInfo {_metadataParser.parse(file)};
if (!trackInfo)
{
stats.errors.emplace_back(file, ScanErrorType::CannotParseFile);
return;
}
stats.scans++;
auto uniqueTransaction {_dbSession.createUniqueTransaction()};
Track::pointer track {Track::getByPath(_dbSession, file) };
// We estimate this is an audio file if:
// - we found a least one audio stream
// - the duration is not null
if (trackInfo->audioStreams.empty())
{
LMS_LOG(DBUPDATER, INFO) << "Skipped '" << file.string() << "' (no audio stream found)";
// If Track exists here, delete it!
if (track)
{
track.remove();
stats.deletions++;
}
stats.errors.emplace_back(ScanError {file, ScanErrorType::NoAudioTrack});
return;
}
if (trackInfo->duration == std::chrono::milliseconds::zero())
{
LMS_LOG(DBUPDATER, INFO) << "Skipped '" << file.string() << "' (duration is 0)";
// If Track exists here, delete it!
if (track)
{
track.remove();
stats.deletions++;
}
stats.errors.emplace_back(ScanError {file, ScanErrorType::BadDuration});
return;
}
// ***** Title
std::string title;
if (!trackInfo->title.empty())
title = trackInfo->title;
else
{
// TODO parse file name guess track etc.
// For now juste use file name as title
title = file.filename().string();
}
// ***** Clusters
std::vector<Cluster::pointer> clusters {getOrCreateClusters(_dbSession, trackInfo->clusters)};
// ***** Artists
std::vector<Artist::pointer> artists {getOrCreateArtists(_dbSession, trackInfo->artists)};
// ***** Release artists
std::vector<Artist::pointer> releaseArtists {getOrCreateArtists(_dbSession, trackInfo->albumArtists)};
// ***** Release
Release::pointer release;
if (trackInfo->album)
release = getOrCreateRelease(_dbSession, *trackInfo->album);
// If file already exist, update data
// Otherwise, create it
if (!track)
{
// Create a new song
track = Track::create(_dbSession, file);
LMS_LOG(DBUPDATER, INFO) << "Adding '" << file.string() << "'";
stats.additions++;
}
else
{
LMS_LOG(DBUPDATER, INFO) << "Updating '" << file.string() << "'";
stats.updates++;
}
// Release related data
if (release)
{
release.modify()->setTotalTrackNumber(trackInfo->totalTrack ? *trackInfo->totalTrack : 0);
release.modify()->setTotalDiscNumber(trackInfo->totalDisc ? *trackInfo->totalDisc : 0);
}
// Track related data
assert(track);
track.modify()->clearArtistLinks();
for (const auto& artist : artists)
track.modify()->addArtistLink(Database::TrackArtistLink::create(_dbSession, track, artist, Database::TrackArtistLink::Type::Artist));
for (const auto& releaseArtist : releaseArtists)
track.modify()->addArtistLink(Database::TrackArtistLink::create(_dbSession, track, releaseArtist, Database::TrackArtistLink::Type::ReleaseArtist));
track.modify()->setScanVersion(_scanVersion);
track.modify()->setRelease(release);
track.modify()->setClusters(clusters);
track.modify()->setLastWriteTime(lastWriteTime);
track.modify()->setName(title);
track.modify()->setDuration(trackInfo->duration);
track.modify()->setAddedTime(Wt::WLocalDateTime::currentServerDateTime().toUTC());
track.modify()->setTrackNumber(trackInfo->trackNumber ? *trackInfo->trackNumber : 0);
track.modify()->setDiscNumber(trackInfo->discNumber ? *trackInfo->discNumber : 0);
track.modify()->setYear(trackInfo->year ? *trackInfo->year : 0);
track.modify()->setOriginalYear(trackInfo->originalYear ? *trackInfo->originalYear : 0);
// If a file has an OriginalYear but no Year, set it to ease filtering
if (!trackInfo->year && trackInfo->originalYear)
track.modify()->setYear(*trackInfo->originalYear);
track.modify()->setMBID(trackInfo->musicBrainzRecordID);
track.modify()->setHasCover(trackInfo->hasCover);
track.modify()->setCopyright(trackInfo->copyright);
track.modify()->setCopyrightURL(trackInfo->copyrightURL);
}
void
MediaScanner::scanMediaDirectory(const std::filesystem::path& mediaDirectory, bool forceScan, ScanStats& stats)
{
std::error_code ec;
std::filesystem::recursive_directory_iterator itPath {_mediaDirectory, std::filesystem::directory_options::follow_directory_symlink, ec};
if (ec)
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot iterate over '" << mediaDirectory.string() << "': " << ec.message();
stats.errors.emplace_back(ScanError {mediaDirectory, ScanErrorType::CannotReadFile, ec.message()});
return;
}
std::filesystem::recursive_directory_iterator itEnd;
while (_running && itPath != itEnd)
{
const std::filesystem::path& path {*itPath};
if (ec)
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot process entry '" << path.string() << "': " << ec.message();
stats.errors.emplace_back(ScanError {path, ScanErrorType::CannotReadFile, ec.message()});
}
else if (std::filesystem::is_regular_file(path))
{
if (isFileSupported(path, _fileExtensions))
scanAudioFile(path, forceScan, stats );
}
itPath.increment(ec);
}
notifyInProgress(stats);
}
// Check if a file exists and is still in a media directory
static bool
checkFile(const std::filesystem::path& p, const std::filesystem::path& mediaDirectory, const std::set<std::filesystem::path>& extensions)
{
try
{
// For each track, make sure the the file still exists
// and still belongs to a media directory
if (!std::filesystem::exists( p )
|| !std::filesystem::is_regular_file( p ) )
{
LMS_LOG(DBUPDATER, INFO) << "Removing '" << p.string() << "': missing";
return false;
}
if (!isPathInParentPath(p, mediaDirectory))
{
LMS_LOG(DBUPDATER, INFO) << "Removing '" << p.string() << "': out of media directory";
return false;
}
if (!isFileSupported(p, extensions))
{
LMS_LOG(DBUPDATER, INFO) << "Removing '" << p.string() << "': file format no longer handled";
return false;
}
return true;
}
catch (std::filesystem::filesystem_error& e)
{
LMS_LOG(DBUPDATER, ERROR) << "Caught exception while checking file '" << p.string() << "': " << e.what();
return false;
}
}
void
MediaScanner::removeMissingTracks(ScanStats& stats)
{
std::vector<std::filesystem::path> trackPaths;
{
auto transaction {_dbSession.createSharedTransaction()};
trackPaths = Track::getAllPaths(_dbSession);;
}
LMS_LOG(DBUPDATER, DEBUG) << "Checking tracks...";
for (const auto& trackPath : trackPaths)
{
if (!_running)
return;
if (!checkFile(trackPath, _mediaDirectory, _fileExtensions))
{
auto transaction {_dbSession.createUniqueTransaction()};
Track::pointer track {Track::getByPath(_dbSession, trackPath)};
if (track)
{
track.remove();
stats.deletions++;
}
}
}
}
void
MediaScanner::removeOrphanEntries()
{
LMS_LOG(DBUPDATER, DEBUG) << "Checking orphan clusters...";
{
auto transaction {_dbSession.createUniqueTransaction()};
// Now process orphan Cluster (no track)
auto clusters {Cluster::getAllOrphans(_dbSession)};
for (auto& cluster : clusters)
{
LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan cluster '" << cluster->getName() << "'";
cluster.remove();
}
}
LMS_LOG(DBUPDATER, DEBUG) << "Checking orphan artists...";
{
auto transaction {_dbSession.createUniqueTransaction()};
auto artists {Artist::getAllOrphans(_dbSession)};
for (auto& artist : artists)
{
LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan artist '" << artist->getName() << "'";
artist.remove();
}
}
LMS_LOG(DBUPDATER, DEBUG) << "Checking orphan releases...";
{
auto transaction {_dbSession.createUniqueTransaction()};
auto releases {Release::getAllOrphans(_dbSession)};
for (auto& release : releases)
{
LMS_LOG(DBUPDATER, DEBUG) << "Removing orphan release '" << release->getName() << "'";
release.remove();
}
}
LMS_LOG(DBUPDATER, INFO) << "Check audio files done!";
}
void
MediaScanner::checkDuplicatedAudioFiles(ScanStats& stats)
{
LMS_LOG(DBUPDATER, INFO) << "Checking duplicated audio files";
auto transaction {_dbSession.createSharedTransaction()};
const std::vector<Track::pointer> tracks = Database::Track::getMBIDDuplicates(_dbSession);
for (const Track::pointer& track : tracks)
{
if (track->getMBID())
{
LMS_LOG(DBUPDATER, INFO) << "Found duplicated MBID [" << track->getMBID()->getAsString() << "], file: " << track->getPath().string() << " - " << track->getName();
stats.duplicates.emplace_back(ScanDuplicate {track->getPath(), DuplicateReason::SameMBID});
}
}
LMS_LOG(DBUPDATER, INFO) << "Checking duplicated audio files done!";
}
} // namespace Scanner
+111
View File
@@ -0,0 +1,111 @@
/*
* Copyright (C) 2013 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 <chrono>
#include <mutex>
#include <optional>
#include <Wt/WDateTime.h>
#include <Wt/WIOService.h>
#include <Wt/WSignal.h>
#include <boost/asio/system_timer.hpp>
#include "scanner/IMediaScanner.hpp"
#include "database/ScanSettings.hpp"
#include "database/Session.hpp"
#include "metadata/TagLibParser.hpp"
namespace Scanner {
class MediaScanner : public IMediaScanner
{
public:
MediaScanner(Database::Db& db);
void setAddon(MediaScannerAddon& addon) override;
void start() override;
void stop() override;
void restart() override;
void requestImmediateScan() override;
void requestReschedule() override ;
Status getStatus() override;
Wt::Signal<>& scanComplete() override { return _sigScanComplete; }
Wt::Signal<ScanProgressStats>& scanInProgress() override { return _sigScanInProgress; }
Wt::Signal<Wt::WDateTime>& scheduled() override { return _sigScheduled; }
private:
// Job handling
void scheduleNextScan();
void scheduleScan(const Wt::WDateTime& dateTime = {});
// Update database (scheduled callback)
void scan(boost::system::error_code ec);
void scanMediaDirectory( const std::filesystem::path& mediaDirectory, bool forceScan, ScanStats& stats);
// Helpers
void refreshScanSettings();
void countAllFiles(ScanStats& stats);
void removeMissingTracks(ScanStats& stats);
void removeOrphanEntries();
void checkDuplicatedAudioFiles(ScanStats& stats);
void scanAudioFile(const std::filesystem::path& file, bool forceScan, ScanStats& stats);
Database::IdType doScanAudioFile(const std::filesystem::path& file, ScanStats& stats);
void notifyInProgressIfNeeded(const ScanStats& stats);
void notifyInProgress(const ScanStats& stats);
bool _running {false};
Wt::WIOService _ioService;
boost::asio::system_timer _scheduleTimer {_ioService};
Wt::Signal<> _sigScanComplete;
Wt::Signal<ScanProgressStats> _sigScanInProgress;
std::chrono::system_clock::time_point _lastScanInProgressEmit {};
Wt::Signal<Wt::WDateTime> _sigScheduled;
Database::Session _dbSession;
MetaData::TagLibParser _metadataParser;
std::vector<MediaScannerAddon*> _addons;
std::mutex _statusMutex;
State _curState {State::NotScheduled};
std::optional<ScanStats> _lastCompleteScanStats;
std::optional<ScanProgressStats> _inProgressScanStats;
Wt::WDateTime _nextScheduledScan;
// Current scan settings
std::size_t _scanVersion {};
Wt::WTime _startTime;
Database::ScanSettings::UpdatePeriod _updatePeriod {Database::ScanSettings::UpdatePeriod::Never};
std::set<std::filesystem::path> _fileExtensions;
std::filesystem::path _mediaDirectory;
}; // class MediaScanner
} // Scanner
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2019 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 "scanner/MediaScannerStats.hpp"
namespace Scanner {
ScanError::ScanError(const std::filesystem::path& _file, ScanErrorType _error, const std::string& _systemError)
: file {_file},
error {_error},
systemError {_systemError}
{
}
std::size_t
ScanStats::nbFiles() const
{
return skips + additions + updates;
}
std::size_t
ScanStats::nbChanges() const
{
return additions + deletions + updates;
}
ScanProgressStats
ScanStats::toProgressStats() const
{
return ScanProgressStats {startTime, filesToScan, nbFiles()};
}
unsigned
ScanProgressStats::progress() const
{
return (processedFiles / static_cast<float>(filesToScan ? filesToScan : 1)) * 100;
}
} // namespace Scanner
+238
View File
@@ -0,0 +1,238 @@
/*
* Copyright (C) 2013 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 "AvFormat.hpp"
#include <algorithm>
#include <iostream>
#include "av/AvInfo.hpp"
#include "utils/Logger.hpp"
#include "utils/String.hpp"
namespace MetaData
{
using MetadataMap = std::map<std::string, std::string>;
template <typename T>
std::optional<T>
findFirstValueOfAs(const MetadataMap& metadataMap, std::initializer_list<std::string> tags)
{
auto it = std::find_first_of(std::cbegin(metadataMap), std::cend(metadataMap), std::cbegin(tags), std::cend(tags), [](const auto& it, const auto& str) { return it.first == str; });
if (it == std::cend(metadataMap))
return std::nullopt;
return StringUtils::readAs<T>(StringUtils::stringTrim(it->second));
}
template <>
std::optional<std::vector<UUID>>
findFirstValueOfAs(const MetadataMap& metadataMap, std::initializer_list<std::string> tags)
{
std::optional<std::string> str {findFirstValueOfAs<std::string>(metadataMap, tags)};
if (!str)
return std::nullopt;
std::vector<std::string> strUuids = StringUtils::splitString(*str, "/");
std::vector<UUID> res;
for (const std::string strUuid : strUuids)
{
std::optional<UUID> uuid {UUID::fromString(strUuid)};
if (!uuid)
return std::nullopt;
res.push_back(std::move(*uuid));
}
return res;
}
static
std::optional<Album>
getAlbum(const MetadataMap& metadataMap)
{
std::optional<Album> res;
auto album {findFirstValueOfAs<std::string>(metadataMap, {"ALBUM"})};
if (!album)
return res;
auto albumMBID {findFirstValueOfAs<UUID>(metadataMap, {"MUSICBRAINZ ALBUM ID", "MUSICBRAINZ_ALBUMID", "MUSICBRAINZ/ALBUM ID"})};
return Album{*album, albumMBID};
}
static
std::vector<Artist>
getAlbumArtists(const MetadataMap& metadataMap)
{
std::vector<Artist> res;
auto name {findFirstValueOfAs<std::string>(metadataMap, {"ALBUM_ARTIST"})};
if (!name)
return res;
auto mbid {findFirstValueOfAs<UUID>(metadataMap, {"MUSICBRAINZ ALBUM ARTIST ID", "MUSICBRAINZ/ALBUM ARTIST ID"})};
return {Artist {*name, mbid} };
}
static
std::vector<Artist>
getArtists(const MetadataMap& metadataMap)
{
std::vector<Artist> artists;
std::vector<std::string> artistNames;
if (metadataMap.find("ARTISTS") != metadataMap.end())
{
artistNames = StringUtils::splitString(metadataMap.find("ARTISTS")->second, "/;");
}
else if (metadataMap.find("ARTIST") != metadataMap.end())
{
artistNames = {metadataMap.find("ARTIST")->second};
}
auto artistMBIDs {findFirstValueOfAs<std::vector<UUID>>(metadataMap, {"MUSICBRAINZ ARTIST ID", "MUSICBRAINZ_ARTISTID", "MUSICBRAINZ/ARTIST ID"})};
for (std::size_t i {}; i < artistNames.size(); ++i)
{
if (artistMBIDs && artistNames.size() == artistMBIDs->size())
artists.emplace_back(Artist {artistNames[i], (*artistMBIDs)[i]});
else
artists.emplace_back(Artist {artistNames[i], {}});
}
return artists;
}
std::optional<Track>
AvFormat::parse(const std::filesystem::path& p, bool debug)
{
Track track;
try
{
Av::MediaFile mediaFile {p};
// Stream info
{
std::vector<AudioStream> audioStreams;
for (auto stream : mediaFile.getStreamInfo())
{
MetaData::AudioStream audioStream {static_cast<unsigned>(stream.bitrate)};
track.audioStreams.emplace_back(audioStream);
}
}
track.duration = mediaFile.getDuration();
track.hasCover = mediaFile.hasAttachedPictures();
MetaData::Clusters clusters;
const std::map<std::string, std::string> metadataMap {mediaFile.getMetaData()};
for (const auto& metadata : metadataMap)
{
const std::string& tag {metadata.first};
const std::string& value {metadata.second};
if (debug)
std::cout << "TAG = " << tag << ", VAL = " << value << std::endl;
if (tag == "TITLE")
track.title = value;
else if (tag == "TRACK")
{
// Expecting 'Number/Total'
std::vector<std::string> strings {StringUtils::splitString(value, "/") };
if (strings.size() > 0)
{
track.trackNumber = StringUtils::readAs<std::size_t>(strings[0]);
if (strings.size() > 1)
track.totalTrack = StringUtils::readAs<std::size_t>(strings[1]);
}
}
else if (tag == "DISC")
{
// Expecting 'Number/Total'
std::vector<std::string> strings {StringUtils::splitString(value, "/")};
if (strings.size() > 0)
{
track.discNumber = StringUtils::readAs<std::size_t>(strings[0]);
if (strings.size() > 1)
track.totalDisc = StringUtils::readAs<std::size_t>(strings[1]);
}
}
else if (tag == "DATE"
|| tag == "YEAR"
|| tag == "WM/Year")
{
track.year = StringUtils::readAs<int>(value);
}
else if (tag == "TDOR" // Original release time (ID3v2 2.4)
|| tag == "TORY") // Original release year
{
track.originalYear = StringUtils::readAs<int>(value);
}
else if (tag == "ACOUSTID ID")
{
track.acoustID = UUID::fromString(value);
}
else if (tag == "MUSICBRAINZ RELEASE TRACK ID"
|| tag == "MUSICBRAINZ_RELEASETRACKID"
|| tag == "MUSICBRAINZ_TRACKID"
|| tag == "MUSICBRAINZ/TRACK ID")
{
track.musicBrainzTrackID = UUID::fromString(value);
}
else if (_clusterTypeNames.find(tag) != _clusterTypeNames.end())
{
std::vector<std::string> clusterNames {StringUtils::splitString(value, "/,;")};
if (!clusterNames.empty())
track.clusters[tag] = std::set<std::string>{clusterNames.begin(), clusterNames.end()};
}
}
track.artists = getArtists(metadataMap);
track.album = getAlbum(metadataMap);
track.albumArtists = getAlbumArtists(metadataMap);
}
catch(Av::MediaFileException& e)
{
return std::nullopt;
}
return track;
}
} // namespace MetaData
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2018 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 "MetaData.hpp"
namespace MetaData
{
// Parse that makes use of AvFormat
class AvFormat : public Parser
{
public:
std::optional<Track> parse(const std::filesystem::path& p, bool debug = false) override;
};
} // namespace MetaData
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2018 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 <chrono>
#include <filesystem>
#include <map>
#include <optional>
#include <set>
#include <vector>
//#include "utils/Utils.hpp"
#include "utils/UUID.hpp"
namespace MetaData
{
using Clusters = std::map<std::string /* type */, std::set<std::string> /* names */>;
struct Artist
{
std::string name;
std::optional<UUID> musicBrainzArtistID;
};
struct Album
{
std::string name;
std::optional<UUID> musicBrainzAlbumID;
};
struct AudioStream
{
unsigned bitRate;
};
struct Track
{
std::vector<Artist> artists;
std::vector<Artist> albumArtists;
std::string title;
std::optional<UUID> musicBrainzTrackID;
std::optional<UUID> musicBrainzRecordID;
std::optional<Album> album;
Clusters clusters;
std::chrono::milliseconds duration {};
std::optional<std::size_t> trackNumber;
std::optional<std::size_t> totalTrack;
std::optional<std::size_t> discNumber;
std::optional<std::size_t> totalDisc;
std::optional<int> year;
std::optional<int> originalYear;
bool hasCover {false};
std::vector<AudioStream> audioStreams;
std::optional<UUID> acoustID;
std::string copyright;
std::string copyrightURL;
};
class Parser
{
public:
virtual std::optional<Track> parse(const std::filesystem::path& p, bool debug = false) = 0;
void setClusterTypeNames(const std::set<std::string>& clusterTypeNames) { _clusterTypeNames = clusterTypeNames; }
protected:
std::set<std::string> _clusterTypeNames;
};
} // namespace MetaData
@@ -0,0 +1,334 @@
/*
* Copyright (C) 2016 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 "TagLibParser.hpp"
#include <taglib/asffile.h>
#include <taglib/id3v2tag.h>
#include <taglib/fileref.h>
#include <taglib/flacfile.h>
#include <taglib/mpegfile.h>
#include <taglib/tag.h>
#include <taglib/tpropertymap.h>
#include "utils/Logger.hpp"
#include "utils/String.hpp"
namespace MetaData
{
template<typename T>
std::vector<T>
getPropertyValuesFirstMatchAs(const TagLib::PropertyMap& properties, const std::set<std::string>& keys)
{
std::vector<T> res;
for (const std::string& key : keys)
{
const TagLib::StringList& values {properties[key]};
if (values.isEmpty())
continue;
res.reserve(values.size());
for (const auto& value : values)
{
auto val {StringUtils::readAs<T>(StringUtils::stringTrim(value.to8Bit(true)))};
if (!val)
continue;
res.emplace_back(std::move(*val));
}
break;
}
return res;
}
template <typename T>
std::vector<T>
getPropertyValuesAs(const TagLib::PropertyMap& properties, const std::string& key)
{
return getPropertyValuesFirstMatchAs<T>(properties, {std::move(key)});
}
static
std::vector<std::string>
splitAndTrimString(const std::string& str, const std::string& delimiters)
{
std::vector<std::string> res;
std::vector<std::string> strings {StringUtils::splitString(str, delimiters)};
for (const std::string& s : strings)
res.emplace_back(StringUtils::stringTrim(s));
return res;
}
static
std::vector<Artist>
getArtists(const TagLib::PropertyMap& properties)
{
std::vector<Artist> res;
std::vector<std::string> artistNames {getPropertyValuesAs<std::string>(properties, "ARTISTS")};
if (artistNames.empty())
artistNames = getPropertyValuesAs<std::string>(properties, "ARTIST");
if (artistNames.empty())
return res;
const std::vector<UUID> artistsMBID {getPropertyValuesFirstMatchAs<UUID>(properties, {"MUSICBRAINZ_ARTISTID", "MUSICBRAINZ ARTIST ID"})};
if (artistNames.size() == artistsMBID.size())
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::cbegin(artistsMBID), std::back_inserter(res),
[&](const std::string& name, const UUID& mbid) { return Artist {name, mbid}; });
}
else
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::back_inserter(res),
[&](const std::string& name) { return Artist{name, {}}; });
}
return res;
}
static
std::vector<Artist>
getAlbumArtists(const TagLib::PropertyMap& properties)
{
std::vector<Artist> res;
std::vector<std::string> artistNames {getPropertyValuesAs<std::string>(properties, "ALBUMARTIST")};
if (artistNames.empty())
return res;
const std::vector<UUID> artistsMBID {getPropertyValuesFirstMatchAs<UUID>(properties, {"MUSICBRAINZ_ALBUMARTISTID", "MUSICBRAINZ ALBUM ARTIST ID"})};
if (artistNames.size() == artistsMBID.size())
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::cbegin(artistsMBID), std::back_inserter(res),
[&](const std::string& name, const UUID& mbid) { return Artist{name, mbid}; });
}
else
{
std::transform(std::cbegin(artistNames), std::cend(artistNames), std::back_inserter(res),
[&](const std::string& name) { return Artist{name, {}}; });
}
return res;
}
static
std::optional<Album>
getAlbum(const TagLib::PropertyMap& properties)
{
std::vector<std::string> albumName {getPropertyValuesAs<std::string>(properties, "ALBUM")};
if (albumName.empty())
return std::nullopt;
const std::vector<UUID> albumMBID {getPropertyValuesFirstMatchAs<UUID>(properties, {"MUSICBRAINZ_ALBUMID", "MUSICBRAINZ ALBUM ID"})};
if (albumMBID.empty())
return Album {std::move(albumName.front()), {}};
else
return Album {std::move(albumName.front()), albumMBID.front()};
}
std::optional<Track>
TagLibParser::parse(const std::filesystem::path& p, bool debug)
{
TagLib::FileRef f {p.string().c_str(),
true, // read audio properties
TagLib::AudioProperties::Fast};
if (f.isNull())
{
LMS_LOG(METADATA, ERROR) << "File '" << p.string() << "': parsing failed";
return std::nullopt;
}
if (!f.audioProperties())
{
LMS_LOG(METADATA, INFO) << "File '" << p.string() << "': no audio properties";
return std::nullopt;
}
Track track;
{
const TagLib::AudioProperties *properties {f.audioProperties() };
track.duration = std::chrono::milliseconds {properties->length() * 1000};
MetaData::AudioStream audioStream {static_cast<unsigned>(properties->bitrate() * 1000)};
track.audioStreams = {std::move(audioStream)};
}
// Not that good embedded pictures handling
// WMA
if (TagLib::ASF::File* asfFile {dynamic_cast<TagLib::ASF::File*>(f.file())})
{
const TagLib::ASF::Tag* tag {asfFile->tag()};
if (tag && tag->attributeListMap().contains("WM/Picture"))
track.hasCover = true;
}
// MP3
else if (TagLib::MPEG::File* mp3File {dynamic_cast<TagLib::MPEG::File*>(f.file())})
{
if (mp3File->ID3v2Tag())
{
if (!mp3File->ID3v2Tag()->frameListMap()["APIC"].isEmpty())
track.hasCover = true;
}
}
// FLAC
else if (TagLib::FLAC::File* flacFile {dynamic_cast<TagLib::FLAC::File*>(f.file())})
{
if (!flacFile->pictureList().isEmpty())
track.hasCover = true;
}
if (f.tag())
{
MetaData::Clusters clusters;
const TagLib::PropertyMap& properties {f.file()->properties()};
for(const auto& property : properties)
{
const std::string tag {property.first.upper().to8Bit(true)};
const TagLib::StringList& values {property.second};
// TODO validate MBID format
if (debug)
{
std::vector<std::string> strs;
std::transform(values.begin(), values.end(), std::back_inserter(strs), [](const auto& value) { return value.to8Bit(true); });
std::cout << "[" << tag << "] = " << StringUtils::joinStrings(strs, "*SEP*") << std::endl;
}
if (tag.empty() || values.isEmpty() || values.front().isEmpty())
continue;
std::string value {StringUtils::stringTrim(values.front().to8Bit(true))};
if (tag == "TITLE")
track.title = value;
else if (tag == "MUSICBRAINZ_RELEASETRACKID"
|| tag == "MUSICBRAINZ RELEASE TRACK ID")
{
track.musicBrainzTrackID = UUID::fromString(value);
}
else if (tag == "MUSICBRAINZ_TRACKID"
|| tag == "MUSICBRAINZ TRACK ID")
track.musicBrainzRecordID = UUID::fromString(value);
else if (tag == "ACOUSTID_ID")
track.acoustID = UUID::fromString(value);
else if (tag == "TRACKTOTAL")
{
auto totalTrack = StringUtils::readAs<std::size_t>(value);
if (totalTrack)
track.totalTrack = totalTrack;
}
else if (tag == "TRACKNUMBER")
{
// Expecting 'Number/Total'
std::vector<std::string> strings {splitAndTrimString(value, "/")};
if (!strings.empty())
{
track.trackNumber = StringUtils::readAs<std::size_t>(strings[0]);
// Lower priority than TRACKTOTAL
if (strings.size() > 1 && !track.totalTrack)
track.totalTrack = StringUtils::readAs<std::size_t>(strings[1]);
}
}
else if (tag == "DISCTOTAL")
{
auto totalDisc = StringUtils::readAs<std::size_t>(value);
if (totalDisc)
track.totalDisc = totalDisc;
}
else if (tag == "DISCNUMBER")
{
// Expecting 'Number/Total'
std::vector<std::string> strings {StringUtils::splitString(value, "/")};
if (!strings.empty())
{
track.discNumber = StringUtils::readAs<std::size_t>(strings[0]);
// Lower priority than DISCTOTAL
if (strings.size() > 1 && !track.totalDisc)
track.totalDisc = StringUtils::readAs<std::size_t>(strings[1]);
}
}
else if (tag == "DATE")
track.year = StringUtils::readAs<int>(value);
else if (tag == "ORIGINALDATE" && !track.originalYear)
{
// Lower priority than ORIGINALYEAR
track.originalYear = StringUtils::readAs<int>(value);
}
else if (tag == "ORIGINALYEAR")
{
// Higher priority than ORIGINALDATE
auto originalYear = StringUtils::readAs<int>(value);
if (originalYear)
track.originalYear = originalYear;
}
else if (tag == "METADATA_BLOCK_PICTURE")
track.hasCover = true;
else if (tag == "COPYRIGHT")
track.copyright = value;
else if (tag == "COPYRIGHTURL")
track.copyrightURL = value;
else if (_clusterTypeNames.find(tag) != _clusterTypeNames.end())
{
std::set<std::string> clusterNames;
for (const auto& valueList : values)
{
auto values = splitAndTrimString(valueList.to8Bit(true), "/,;");
for (const auto& value : values)
clusterNames.insert(value);
}
if (!clusterNames.empty())
track.clusters[tag] = clusterNames;
}
}
track.artists = getArtists(properties);
track.albumArtists = getAlbumArtists(properties);
track.album = getAlbum(properties);
}
return track;
}
} // namespace MetaData
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2018 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 "MetaData.hpp"
namespace MetaData
{
// Parse that makes use of AvFormat
class TagLibParser : public Parser
{
public:
std::optional<Track> parse(const std::filesystem::path& p, bool debug = false) override;
};
} // namespace MetaData
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2013 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 <optional>
#include <Wt/WDateTime.h>
#include <Wt/WSignal.h>
#include "MediaScannerAddon.hpp"
#include "MediaScannerStats.hpp"
namespace Database
{
class Db;
}
namespace Scanner {
class IMediaScanner
{
public:
virtual ~IMediaScanner() = default;
virtual void setAddon(MediaScannerAddon& addon) = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual void restart() = 0;
// Async requests
virtual void requestImmediateScan() = 0;
virtual void requestReschedule() = 0;
enum class State
{
NotScheduled,
Scheduled,
InProgress,
};
struct Status
{
State currentState {State::NotScheduled};
Wt::WDateTime nextScheduledScan;
std::optional<ScanStats> lastCompleteScanStats;
std::optional<ScanProgressStats> inProgressScanStats;
};
virtual Status getStatus() = 0;
// Called just after scan complete
virtual Wt::Signal<>& scanComplete() = 0;
// Called during scan in progress
virtual Wt::Signal<ScanProgressStats>& scanInProgress() = 0;
// Called after a schedule
virtual Wt::Signal<Wt::WDateTime>& scheduled() = 0;
};
std::unique_ptr<IMediaScanner> createMediaScanner(Database::Db& db);
} // Scanner
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2018 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/Types.hpp"
namespace Scanner {
class MediaScannerAddon
{
public:
virtual void refreshSettings() = 0;
virtual void requestStop() = 0;
virtual void preScanComplete() = 0;
virtual void trackAdded(Database::IdType trackId) = 0;
virtual void trackToRemove(Database::IdType trackId) = 0;
virtual void trackUpdated(Database::IdType trackId) = 0;
};
} // ns Scanner
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2019 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 <Wt/WDateTime.h>
#include <filesystem>
#include <vector>
namespace Scanner {
enum class ScanErrorType
{
CannotReadFile, // cannot read file
CannotParseFile, // cannot parse file
NoAudioTrack, // no audio track found
BadDuration, // bad duration
};
enum class DuplicateReason
{
SameHash,
SameMBID,
};
struct ScanError
{
std::filesystem::path file;
ScanErrorType error;
std::string systemError;
ScanError(const std::filesystem::path& file, ScanErrorType error, const std::string& systemError = "");
};
struct ScanDuplicate
{
std::filesystem::path file;
DuplicateReason reason;
};
// reduced scan stats
struct ScanProgressStats
{
Wt::WDateTime startTime;
std::size_t filesToScan {};
std::size_t processedFiles {};
unsigned progress() const;
};
struct ScanStats
{
Wt::WDateTime startTime;
Wt::WDateTime stopTime;
std::size_t filesToScan {}; // Total number of files to be scanned (estimated)
std::size_t skips {}; // no change since last scan
std::size_t scans {}; // actually scanned filed
std::size_t additions {}; // Added in DB
std::size_t deletions {}; // removed from DB
std::size_t updates {}; // updated file in DB
std::vector<ScanError> errors;
std::vector<ScanDuplicate> duplicates;
std::size_t nbFiles() const;
std::size_t nbChanges() const;
ScanProgressStats toProgressStats() const;
};
}
+30
View File
@@ -0,0 +1,30 @@
add_library(lmssubsonic SHARED
impl/SubsonicId.cpp
impl/SubsonicResource.cpp
impl/SubsonicResponse.cpp
)
target_include_directories(lmssubsonic INTERFACE
include
)
target_include_directories(lmssubsonic PRIVATE
include
)
target_link_libraries(lmssubsonic PRIVATE
lmsauth
lmsav
lmscover
lmsrecommendation
lmsutils
)
target_link_libraries(lmssubsonic PUBLIC
lmsdatabase
wt
)
install(TARGETS lmssubsonic DESTINATION lib)
+89
View File
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2019 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 "SubsonicId.hpp"
#include "SubsonicResponse.hpp"
#include "utils/Logger.hpp"
#include "utils/String.hpp"
namespace API::Subsonic
{
std::optional<Id>
IdFromString(const std::string& id)
{
if (id == "root")
return Id {Id::Type::Root};
std::vector<std::string> values {StringUtils::splitString(id, "-")};
if (values.size() != 2)
return std::nullopt;
Id res;
const std::string type {std::move(values[0])};
if (type == "ar")
res.type = Id::Type::Artist;
else if (type == "al")
res.type = Id::Type::Release;
else if (type == "tr")
res.type = Id::Type::Track;
else if (type == "pl")
res.type = Id::Type::Playlist;
else
return std::nullopt;
auto optId {StringUtils::readAs<Database::IdType>(values[1])};
if (!optId)
return std::nullopt;
res.value = *optId;
return res;
}
std::string
IdToString(const Id& id)
{
std::string res;
switch (id.type)
{
case Id::Type::Root:
return "root";
case Id::Type::Artist:
res = "ar-";
break;
case Id::Type::Release:
res = "al-";
break;
case Id::Type::Track:
res = "tr-";
break;
case Id::Type::Playlist:
res = "pl-";
break;
}
return res + std::to_string(id.value);
}
} // namespace API::Subsonic
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2019 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 <optional>
#include "database/Types.hpp"
namespace API::Subsonic
{
struct Id
{
enum class Type
{
Root, // Where all artists artistless albums reside
Track,
Release,
Artist,
Playlist,
};
Type type;
Database::IdType value {};
};
std::optional<Id> IdFromString(const std::string& id);
std::string IdToString(const Id& id);
} // namespace API::Subsonic
File diff suppressed because it is too large Load Diff
+237
View File
@@ -0,0 +1,237 @@
/*
* Copyright (C) 2019 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 "SubsonicResponse.hpp"
#include <Wt/Json/Array.h>
#include <Wt/Json/Object.h>
#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 "utils/Exception.hpp"
namespace API::Subsonic
{
std::string
ResponseFormatToMimeType(ResponseFormat format)
{
switch (format)
{
case ResponseFormat::xml: return "text/xml";
case ResponseFormat::json: return "application/json";
}
return "";
}
void
Response::Node::setValue(std::string_view value)
{
if (!_children.empty() || !_childrenArrays.empty())
throw LmsException {"Node already has children"};
_value = value;
}
void
Response::Node::setAttribute(std::string_view key, std::string_view value)
{
_attributes[std::string {key}] = value;
}
void
Response::Node::addChild(const std::string& key, Node node)
{
if (!_value.empty())
throw LmsException {"Node already has a value"};
_children[key].emplace_back(std::move(node));
}
void
Response::Node::addArrayChild(const std::string& key, Node node)
{
if (!_value.empty())
throw LmsException {"Node already has a value"};
_childrenArrays[key].emplace_back(std::move(node));
}
Response::Node&
Response::Node::createChild(const std::string& key)
{
_children[key].emplace_back();
return _children[key].back();
}
Response::Node&
Response::Node::createArrayChild(const std::string& key)
{
_childrenArrays[key].emplace_back();
return _childrenArrays[key].back();
}
Response
Response::createOkResponse()
{
Response response;
Node& responseNode {response._root.createChild("subsonic-response")};
responseNode.setAttribute("status", "ok");
responseNode.setAttribute("version", API_VERSION_STR);
return response;
}
Response
Response::createFailedResponse(const Error& error)
{
Response response;
Node& responseNode {response._root.createChild("subsonic-response")};
responseNode.setAttribute("status", "failed");
responseNode.setAttribute("version", API_VERSION_STR);
Node& errorNode {responseNode.createChild("error")};
errorNode.setAttribute("code", std::to_string(static_cast<int>(error.getCode())));
errorNode.setAttribute("message", error.getMessage());
return response;
}
void
Response::addNode(const std::string& key, Node node)
{
return _root._children["subsonic-response"].front().addChild(key, std::move(node));
}
Response::Node&
Response::createNode(const std::string& key)
{
return _root._children["subsonic-response"].front().createChild(key);
}
Response::Node&
Response::createArrayNode(const std::string& key)
{
return _root._children["subsonic-response"].front().createArrayChild(key);
}
void
Response::write(std::ostream& os, ResponseFormat format)
{
switch (format)
{
case ResponseFormat::xml:
writeXML(os);
break;
case ResponseFormat::json:
writeJSON(os);
break;
}
}
void
Response::writeXML(std::ostream& os)
{
std::function<boost::property_tree::ptree(const Response::Node&)> nodeToPropertyTree = [&] (const Response::Node& node)
{
boost::property_tree::ptree res;
for (auto itAttribute : node._attributes)
res.put("<xmlattr>." + itAttribute.first, itAttribute.second);
if (!node._value.empty())
{
res.put_value(node._value);
}
else
{
for (auto itChildNode : node._children)
{
for (const Response::Node& childNode : itChildNode.second)
res.add_child(itChildNode.first, nodeToPropertyTree(childNode));
}
for (auto itChildArrayNode : node._childrenArrays)
{
const std::vector<Response::Node>& childArrayNodes {itChildArrayNode .second};
for (const Response::Node& childNode : childArrayNodes )
res.add_child(itChildArrayNode.first, nodeToPropertyTree(childNode));
}
}
return res;
};
boost::property_tree::ptree root {nodeToPropertyTree(_root)};
boost::property_tree::write_xml(os, root);
}
void
Response::writeJSON(std::ostream& os)
{
namespace Json = Wt::Json;
std::function<Json::Object(const Response::Node&)> nodeToJsonObject = [&] (const Response::Node& node)
{
Json::Object res;
for (auto itAttribute : node._attributes)
res[itAttribute.first] = Json::Value {itAttribute.second};
if (!node._value.empty())
{
res["value"] = Json::Value {node._value};
}
else
{
for (auto itChildNode : node._children)
{
for (const Response::Node& childNode : itChildNode.second)
res[itChildNode.first] = nodeToJsonObject(childNode);
}
for (auto itChildArrayNode : node._childrenArrays)
{
const std::vector<Response::Node>& childArrayNodes {itChildArrayNode .second};
Json::Array array;
for (const Response::Node& childNode : childArrayNodes )
array.emplace_back(nodeToJsonObject(childNode));
res[itChildArrayNode.first] = std::move(array);
}
}
return res;
};
Json::Object root {nodeToJsonObject(_root)};
os << Json::serialize(root);
}
} // namespace
+224
View File
@@ -0,0 +1,224 @@
/*
* Copyright (C) 2019 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 <map>
#include <string>
#include <string_view>
#include <vector>
namespace API::Subsonic
{
#define API_VERSION_MAJOR 1
#define API_VERSION_MINOR 12
#define API_VERSION_PATCH 0
#define API_VERSION_STR "1.12.0"
enum class ResponseFormat
{
xml,
json,
};
std::string ResponseFormatToMimeType(ResponseFormat format);
class Error
{
public:
enum class Code
{
Generic = 0,
RequiredParameterMissing = 10,
ClientMustUpgrade = 20,
ServerMustUpgrade = 30,
WrongUsernameOrPassword = 40,
UserNotAuthorized = 50,
RequestedDataNotFound = 70,
};
Error(Code code) : _code {code} {}
virtual std::string getMessage() const = 0;
Code getCode() const { return _code; }
private:
const Code _code;
};
class GenericError : public Error
{
public:
GenericError() : Error {Code::Generic} {}
};
class RequiredParameterMissingError : public Error
{
public:
RequiredParameterMissingError() : Error {Code::RequiredParameterMissing} {}
private:
std::string getMessage() const override { return "Required parameter is missing."; }
};
class ClientMustUpgradeError : public Error
{
public:
ClientMustUpgradeError() : Error {Code::ClientMustUpgrade} {}
private:
std::string getMessage() const override { return "Incompatible Subsonic REST protocol version. Client must upgrade."; }
};
class ServerMustUpgradeError : public Error
{
public:
ServerMustUpgradeError() : Error {Code::ServerMustUpgrade} {}
private:
std::string getMessage() const override { return "Incompatible Subsonic REST protocol version. Server must upgrade."; }
};
class WrongUsernameOrPasswordError : public Error
{
public:
WrongUsernameOrPasswordError() : Error {Code::WrongUsernameOrPassword} {}
private:
std::string getMessage() const override { return "Wrong username or password."; }
};
class UserNotAuthorizedError : public Error
{
public:
UserNotAuthorizedError () : Error {Code::UserNotAuthorized} {}
private:
std::string getMessage() const override { return "User is not authorized for the given operation."; }
};
class RequestedDataNotFoundError : public Error
{
public:
RequestedDataNotFoundError() : Error {Code::RequestedDataNotFound} {}
private:
std::string getMessage() const override { return "The requested data was not found."; }
};
class InternalErrorGenericError : public GenericError
{
public:
InternalErrorGenericError(const std::string& message) : _message {message} {}
private:
std::string getMessage() const override { return "Internal error: " + _message; }
const std::string _message;
};
class LoginThrottledGenericError : public GenericError
{
std::string getMessage() const override { return "Login throttled, too many attempts"; }
};
class NotImplementedGenericError : public GenericError
{
std::string getMessage() const override { return "Not implemented"; }
};
class UnknownEntryPointGenericError : public GenericError
{
std::string getMessage() const override { return "Unknown API method"; }
};
class PasswordTooWeakGenericError : public GenericError
{
std::string getMessage() const override { return "Password too weak"; }
};
class UserAlreadyExistsGenericError : public GenericError
{
std::string getMessage() const override { return "User already exists"; }
};
class BadParameterGenericError : public GenericError
{
public:
BadParameterGenericError(const std::string& parameterName) : _parameterName {parameterName} {}
private:
std::string getMessage() const override { return "Parameter '" + _parameterName + "': bad value"; }
const std::string _parameterName;
};
class BadParameterFormatGenericError : public GenericError
{
public:
BadParameterFormatGenericError(const std::string& parameterName) : _parameterName {parameterName} {}
private:
std::string getMessage() const override { return "Parameter '" + _parameterName + "': bad format"; }
const std::string _parameterName;
};
class Response
{
public:
class Node
{
public:
void setAttribute(std::string_view key, std::string_view value);
// A Node has either a value or some children
void setValue(std::string_view value);
Node& createChild(const std::string& key);
Node& createArrayChild(const std::string& key);
void addChild(const std::string& key, Node node);
void addArrayChild(const std::string& key, Node node);
private:
friend class Response;
std::map<std::string, std::string> _attributes;
std::string _value;
std::map<std::string, std::vector<Node>> _children;
std::map<std::string, std::vector<Node>> _childrenArrays;
};
static Response createOkResponse();
static Response createFailedResponse(const Error& error);
virtual ~Response() {}
Response(const Response&) = delete;
Response& operator=(const Response&) = delete;
Response(Response&&) = default;
Response& operator=(Response&&) = default;
void addNode(const std::string& key, Node node);
Node& createNode(const std::string& key);
Node& createArrayNode(const std::string& key);
void write(std::ostream& os, ResponseFormat format);
private:
void writeJSON(std::ostream& os);
void writeXML(std::ostream& os);
Response() = default;
Node _root;
};
} // namespace
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019 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 <Wt/WResource.h>
#include <Wt/Http/Response.h>
#include "database/SessionPool.hpp"
namespace Database
{
class Db;
}
namespace API::Subsonic
{
class SubsonicResource final : public Wt::WResource
{
public:
SubsonicResource(Database::Db& db);
static std::string getPath() { return "/rest/"; }
private:
void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) override;
Database::SessionPool _sessionPool;
};
} // namespace
+31
View File
@@ -0,0 +1,31 @@
add_library(lmsutils SHARED
impl/Config.cpp
impl/Logger.cpp
impl/NetAddress.cpp
impl/Path.cpp
impl/Random.cpp
impl/StreamLogger.cpp
impl/String.cpp
impl/UUID.cpp
impl/WtLogger.cpp
)
target_include_directories(lmsutils INTERFACE
include
)
target_include_directories(lmsutils PRIVATE
include
)
target_link_libraries(lmsutils PRIVATE
config++
)
target_link_libraries(lmsutils PUBLIC
stdc++fs
)
install(TARGETS lmsutils DESTINATION lib)
+119
View File
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2016 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 "Config.hpp"
#include "utils/Exception.hpp"
#include "utils/Logger.hpp"
std::unique_ptr<IConfig> createConfig(const std::filesystem::path& p)
{
return std::make_unique<Config>(p);
}
Config::Config(const std::filesystem::path& p)
{
try
{
_config.readFile(p.string().c_str());
}
catch( libconfig::FileIOException& e)
{
throw LmsException {"Cannot open config file '" + p.string() + "'"};
}
catch( libconfig::ParseException& e)
{
throw LmsException {"Cannot parse config file '" + p.string() + "', line = " + std::to_string(e.getLine()) + ", error = '" + e.getError() + "'"};
}
catch (libconfig::ConfigException& e)
{
throw LmsException {"Cannot open config file '" + p.string() + "': " + e.what()};
}
}
std::string
Config::getString(const std::string& setting, const std::string& def, const std::unordered_set<std::string>& allowedValues)
{
try {
std::string res {(const char*)_config.lookup(setting)};
if (!allowedValues.empty() && allowedValues.find(res) == std::cend(allowedValues))
{
LMS_LOG(MAIN, ERROR) << "Invalid setting for '" << setting << "', using default value '" << def << "'";
return def;
}
return res;
}
catch (std::exception &e)
{
return def;
}
}
std::filesystem::path
Config::getPath(const std::string& setting, const std::filesystem::path& path)
{
try {
const char* res = _config.lookup(setting);
return std::filesystem::path {std::string(res)};
}
catch (std::exception &e)
{
return path;
}
}
unsigned long
Config::getULong(const std::string& setting, unsigned long def)
{
try {
return static_cast<unsigned int>(_config.lookup(setting));
}
catch (...)
{
return def;
}
}
long
Config::getLong(const std::string& setting, long def)
{
try {
return _config.lookup(setting);
}
catch (...)
{
return def;
}
}
bool
Config::getBool(const std::string& setting, bool def)
{
try {
return _config.lookup(setting);
}
catch (...)
{
return def;
}
}

Some files were not shown because too many files have changed in this diff Show More