0
0
mirror of https://github.com/rbock/sqlpp11.git synced 2024-11-16 21:04:15 +08:00

Untab everything and improve formatting

This commit is contained in:
Frank Park 2017-04-11 14:15:38 -04:00
parent 1987614bfe
commit 628b0bbaff
2 changed files with 224 additions and 220 deletions

View File

@ -42,196 +42,196 @@
namespace sqlpp namespace sqlpp
{ {
namespace connection_validator namespace connection_validator
{ {
struct automatic struct automatic
{ {
template<typename Connection> template<typename Connection>
void validate(Connection* connection) void validate(Connection* connection)
{ {
if (!connection->is_valid()) if (!connection->is_valid())
{ {
try try
{ {
connection->reconnect(); connection->reconnect();
} }
catch (const sqlpp::exception&) catch (const sqlpp::exception&)
{ {
throw sqlpp::exception("Failed to reconnect to database."); throw sqlpp::exception("Failed to reconnect to database.");
} }
} }
} }
template<typename Connection> template<typename Connection>
void deregister(Connection* connection) {} void deregister(Connection* connection) {}
}; };
using namespace std::chrono_literals; using namespace std::chrono_literals;
class periodic class periodic
{ {
private: private:
std::chrono::seconds revalidate_interval; std::chrono::seconds revalidate_interval;
std::unordered_map<void*,std::chrono::time_point<std::chrono::system_clock>> last_checked; std::unordered_map<void*, std::chrono::time_point<std::chrono::system_clock>> last_checked;
public: public:
periodic(const std::chrono::seconds r = 28800s) //default wait_timeout in MySQL periodic(const std::chrono::seconds r = 28800s) //default wait_timeout in MySQL
: revalidate_interval(r), last_checked() {} : revalidate_interval(r), last_checked() {}
template<typename Connection> template<typename Connection>
void validate(Connection* connection) void validate(Connection* connection)
{ {
auto last = last_checked.find(connection); auto last = last_checked.find(connection);
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
if (last == last_checked.end()) if (last == last_checked.end())
{ {
last_checked.emplace_hint(last, connection, now); last_checked.emplace_hint(last, connection, now);
} }
if (now - last->second < revalidate_interval) if (now - last->second < revalidate_interval)
{ {
return; return;
} }
if (!connection->is_valid()) if (!connection->is_valid())
{ {
try try
{ {
connection->reconnect(); connection->reconnect();
} }
catch (const sqlpp::exception& e) catch (const sqlpp::exception& e)
{ {
throw sqlpp::exception("Failed to reconnect to database."); throw sqlpp::exception("Failed to reconnect to database.");
} }
} }
last = now; last = now;
} }
template<typename Connection> template<typename Connection>
void deregister(Connection* con) void deregister(Connection* con)
{ {
auto itr = last_checked.find(con); auto itr = last_checked.find(con);
if(itr != last_checked.end()) if (itr != last_checked.end())
{ {
last_checked.erase(itr); last_checked.erase(itr);
} }
} }
}; };
struct none struct none
{ {
template<typename Connection> template<typename Connection>
void validate(Connection*) {} void validate(Connection*) {}
template<typename Connection> template<typename Connection>
void deregister(Connection*) {} void deregister(Connection*) {}
}; };
} }
template <typename Connection_config, template <typename Connection_config,
typename Connection_validator = connection_validator::automatic, typename Connection_validator = connection_validator::automatic,
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type> typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
class connection_pool class connection_pool
{ {
friend pool_connection<Connection_config, Connection_validator, Connection>; friend pool_connection<Connection_config, Connection_validator, Connection>;
private: private:
std::mutex connection_pool_mutex; std::mutex connection_pool_mutex;
const std::shared_ptr<Connection_config> config; const std::shared_ptr<Connection_config> config;
size_t maximum_pool_size = 0; size_t maximum_pool_size = 0;
std::stack<std::unique_ptr<Connection>> free_connections; std::stack<std::unique_ptr<Connection>> free_connections;
Connection_validator connection_validator; Connection_validator connection_validator;
void free_connection(std::unique_ptr<Connection>& connection) void free_connection(std::unique_ptr<Connection>& connection)
{ {
std::lock_guard<std::mutex> lock(connection_pool_mutex); std::lock_guard<std::mutex> lock(connection_pool_mutex);
if (free_connections.size() >= maximum_pool_size) if (free_connections.size() >= maximum_pool_size)
{ {
// Exceeds default size, deregister left over info in the connection_validator and let connection self destroy. // Exceeds default size, deregister left over info in the connection_validator and let connection self destroy.
connection_validator.deregister(connection.get()); connection_validator.deregister(connection.get());
} }
else else
{ {
if (connection.get()) if (connection.get())
{ {
free_connections.push(std::move(connection)); free_connections.push(std::move(connection));
} }
else else
{ {
throw sqlpp::exception("Trying to free an empty connection."); throw sqlpp::exception("Trying to free an empty connection.");
} }
} }
} }
public: public:
connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size) connection_pool(const std::shared_ptr<Connection_config>& config, size_t pool_size)
: config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {} : config(config), maximum_pool_size(pool_size), connection_validator(Connection_validator()) {}
~connection_pool() = default; ~connection_pool() = default;
connection_pool(const connection_pool&) = delete; connection_pool(const connection_pool&) = delete;
connection_pool(connection_pool&& other) connection_pool(connection_pool&& other)
: config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)), : config(std::move(other.config)), maximum_pool_size(std::move(other.maximum_pool_size)),
connection_validator(std::move(other.connection_validator)) {} connection_validator(std::move(other.connection_validator)) {}
connection_pool& operator=(const connection_pool&) = delete; connection_pool& operator=(const connection_pool&) = delete;
connection_pool& operator=(connection_pool&&) = delete; connection_pool& operator=(connection_pool&&) = delete;
pool_connection<Connection_config, Connection_validator, Connection> get_connection() auto get_connection()
{ -> pool_connection<Connection_config, Connection_validator, Connection>
std::lock_guard<std::mutex> lock(connection_pool_mutex); {
while (true) std::lock_guard<std::mutex> lock(connection_pool_mutex);
{ while (true)
try {
{ try
if (!free_connections.empty()) {
{ if (!free_connections.empty())
auto connection = std::move(free_connections.top()); {
free_connections.pop(); auto connection = std::move(free_connections.top());
connection_validator.validate(connection.get()); free_connections.pop();
connection_validator.validate(connection.get());
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(connection), this); return pool_connection<Connection_config, Connection_validator, Connection>(std::move(connection), this);
} }
else else
{ {
break; break;
} }
} }
catch (const sqlpp::exception&) catch (const sqlpp::exception&)
{ {
throw sqlpp::exception("Failed to retrieve a valid connection."); throw sqlpp::exception("Failed to retrieve a valid connection.");
} }
} }
try try
{ {
return pool_connection<Connection_config, Connection_validator, Connection>(std::move(std::make_unique<Connection>(config)), this); return pool_connection<Connection_config, Connection_validator, Connection>(std::move(std::make_unique<Connection>(config)), this);
} }
catch (const sqlpp::exception&) catch (const sqlpp::exception&)
{ {
throw sqlpp::exception("Failed to spawn a new connection."); throw sqlpp::exception("Failed to spawn a new connection.");
} }
} }
template<typename Query, typename Lambda> template<typename Query, typename Lambda>
void operator()(Query query, Lambda callback) void operator()(Query query, Lambda callback)
{ {
query_task<connection_pool, Query, Lambda>(*this, query, callback)(); query_task<connection_pool, Query, Lambda>(*this, query, callback)();
} }
template<typename Query> template<typename Query>
void operator()(Query query) void operator()(Query query)
{ {
operator()(query, [](){}); operator()(query, []() {});
} }
}; };
template<typename Connection_config, template<typename Connection_config,
typename Connection_validator = connection_validator::automatic, typename Connection_validator = connection_validator::automatic,
typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value,Connection_config::connection>::type> typename Connection = typename std::enable_if<std::is_class<Connection_config::connection>::value, Connection_config::connection>::type>
connection_pool<Connection_config, Connection_validator, Connection> make_connection_pool( auto make_connection_pool(const std::shared_ptr<Connection_config>& config, size_t max_pool_size)
const std::shared_ptr<Connection_config>& config, -> connection_pool<Connection_config, Connection_validator, Connection>
size_t max_pool_size) {
{ return connection_pool<Connection_config, Connection_validator, Connection>(config, max_pool_size);
return connection_pool<Connection_config, Connection_validator, Connection>(config, max_pool_size); }
}
} }
#endif #endif

View File

@ -32,63 +32,67 @@
namespace sqlpp namespace sqlpp
{ {
template <typename Connection_config, typename Connection_validator, typename Connection, template <typename Connection_config, typename Connection_validator, typename Connection,
typename Connection_pool = connection_pool<Connection_config, Connection_validator, Connection>> typename Connection_pool = connection_pool<Connection_config, Connection_validator, Connection>>
struct pool_connection : public sqlpp::connection struct pool_connection : public sqlpp::connection
{ {
private: private:
std::unique_ptr<Connection> _impl; std::unique_ptr<Connection> _impl;
Connection_pool* origin; Connection_pool* origin;
public: public:
pool_connection() : _impl(nullptr), origin(nullptr) {} pool_connection() : _impl(nullptr), origin(nullptr)
{
}
pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin) pool_connection(std::unique_ptr<Connection>& connection, Connection_pool* origin) : _impl(std::move(connection)), origin(origin)
: _impl(std::move(connection)), origin(origin) {} {
}
~pool_connection() ~pool_connection()
{ {
if (_impl.get()) if (_impl.get())
{ {
origin->free_connection(_impl); origin->free_connection(_impl);
} }
} }
template<typename... Args> template<typename... Args>
auto operator()(Args&&... args) -> decltype(_impl->args(std::forward<Args>(args)...)) auto operator()(Args&&... args) -> decltype(_impl->args(std::forward<Args>(args)...))
{ {
return _impl->args(std::forward<Args>(args)...); return _impl->args(std::forward<Args>(args)...);
} }
template <typename T> template <typename T>
auto operator()(const T& t) -> decltype(_impl->run(t)) auto operator()(const T& t) -> decltype(_impl->run(t))
{ {
return _impl->run(t); return _impl->run(t);
} }
template <typename T> template <typename T>
auto execute(const T& t) -> decltype(_impl->execute(t)) auto execute(const T& t) -> decltype(_impl->execute(t))
{ {
return _impl->execute(t); return _impl->execute(t);
} }
template <typename T> template <typename T>
auto prepare(const T& t) -> decltype(_impl->prepare(t)) auto prepare(const T& t) -> decltype(_impl->prepare(t))
{ {
return _impl->prepare(t); return _impl->prepare(t);
} }
pool_connection(const pool_connection&) = delete; pool_connection(const pool_connection&) = delete;
pool_connection(pool_connection&& other) pool_connection(pool_connection&& other) : _impl(std::move(other._impl)), origin(other.origin)
: _impl(std::move(other._impl)), origin(other.origin) {} {
pool_connection& operator=(const pool_connection&) = delete; }
pool_connection& operator=(pool_connection&& other) pool_connection& operator=(const pool_connection&) = delete;
{ pool_connection& operator=(pool_connection&& other)
_impl = std::move(other._impl); {
origin = other.origin; _impl = std::move(other._impl);
return *this; origin = other.origin;
} return *this;
}; }
};
} }
#endif #endif