Made STB the default image manipulation library to reduce memory usage (GraphicsMagick can still be selected). closes #93

This commit is contained in:
emeric
2020-10-17 14:58:41 +02:00
parent e79dae48d1
commit c04f57ceb5
63 changed files with 866 additions and 545 deletions
-1
View File
@@ -9,7 +9,6 @@ add_library(lmsdatabase SHARED
impl/Release.cpp
impl/ScanSettings.cpp
impl/Session.cpp
impl/SessionPool.cpp
impl/SqlQuery.cpp
impl/Track.cpp
impl/TrackBookmark.cpp
+4 -4
View File
@@ -32,14 +32,14 @@ Cluster::Cluster()
{
}
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name)
: _name(std::string(name, 0, _maxNameLength)),
_clusterType(type)
Cluster::Cluster(Wt::Dbo::ptr<ClusterType> type, std::string_view name)
: _name(std::string {name, 0, _maxNameLength}),
_clusterType {type}
{
}
Cluster::pointer
Cluster::create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name)
Cluster::create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string_view name)
{
session.checkUniqueLocked();
+19
View File
@@ -22,6 +22,7 @@
#include <Wt/Dbo/FixedSqlConnectionPool.h>
#include <Wt/Dbo/backend/Sqlite3.h>
#include "database/Session.hpp"
#include "database/User.hpp"
#include "utils/Logger.hpp"
@@ -57,6 +58,24 @@ Db::executeSql(const std::string& sql)
connection->executeSql(sql);
}
Session&
Db::getTLSSession()
{
static thread_local Session* tlsSession {};
if (!tlsSession)
{
auto newSession {std::make_unique<Session>(*this)};
tlsSession = newSession.get();
{
std::scoped_lock lock {_tlsSessionsMutex};
_tlsSessions.push_back(std::move(newSession));
}
}
return *tlsSession;
}
Db::ScopedConnection::ScopedConnection(Wt::Dbo::SqlConnectionPool& pool)
: _connectionPool {pool}
+2 -2
View File
@@ -362,13 +362,13 @@ Session::checkSharedLocked()
UniqueTransaction
Session::createUniqueTransaction()
{
return UniqueTransaction{_db.getMutex(), _session};
return UniqueTransaction {_db.getMutex(), _session};
}
SharedTransaction
Session::createSharedTransaction()
{
return SharedTransaction{_db.getMutex(), _session};
return SharedTransaction {_db.getMutex(), _session};
}
void
-69
View File
@@ -1,69 +0,0 @@
/*
* 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
@@ -20,6 +20,7 @@
#pragma once
#include <string>
#include <string_view>
#include <vector>
#include <Wt/Dbo/Dbo.h>
@@ -41,7 +42,7 @@ class Cluster : public Wt::Dbo::Dbo<Cluster>
using pointer = Wt::Dbo::ptr<Cluster>;
Cluster();
Cluster(Wt::Dbo::ptr<ClusterType> type, std::string name);
Cluster(Wt::Dbo::ptr<ClusterType> type, std::string_view name);
// Find utility
static std::vector<pointer> getAll(Session& session);
@@ -49,7 +50,7 @@ class Cluster : public Wt::Dbo::Dbo<Cluster>
static pointer getById(Session& session, IdType id);
// Create utility
static pointer create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string name);
static pointer create(Session& session, Wt::Dbo::ptr<ClusterType> type, std::string_view name);
// Accessors
const std::string& getName() const { return _name; }
+6 -1
View File
@@ -26,7 +26,7 @@
namespace Database {
// Session living class handling the database and the login
class Session;
class Db
{
public:
@@ -39,6 +39,8 @@ class Db
Db& operator=(const Db&) = delete;
Db& operator=(Db&&) = delete;
Session& getTLSSession();
private:
friend class Session;
@@ -88,6 +90,9 @@ class Db
std::shared_mutex _sharedMutex;
std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool;
std::mutex _tlsSessionsMutex;
std::vector<std::unique_ptr<Session>> _tlsSessions;
};
} // namespace Database
@@ -84,7 +84,7 @@ class Session
void doDatabaseMigrationIfNeeded();
Db& _db;
Db& _db;
Wt::Dbo::Session _session;
};
@@ -1,72 +0,0 @@
/*
* 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