From 6389a5714d46fc7444678e4fc469e8b04d536cf0 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 6 Jul 2022 10:11:17 +0200 Subject: [PATCH] Added multi threaded and single threaded Database base classes. --- modules/database/database.cpp | 1 + modules/database/database_multi_threaded.cpp | 60 +++++++++++++++++++ modules/database/database_multi_threaded.h | 33 ++++++++++ modules/database/database_single_threaded.cpp | 36 +++++++++++ modules/database/database_single_threaded.h | 33 ++++++++++ 5 files changed, 163 insertions(+) create mode 100644 modules/database/database_multi_threaded.cpp create mode 100644 modules/database/database_multi_threaded.h create mode 100644 modules/database/database_single_threaded.cpp create mode 100644 modules/database/database_single_threaded.h diff --git a/modules/database/database.cpp b/modules/database/database.cpp index 4c86f73ff..4f1833918 100644 --- a/modules/database/database.cpp +++ b/modules/database/database.cpp @@ -20,6 +20,7 @@ Ref Database::_allocate_connection() { //Ref dbc; //dbc.instance(); //dbc->set_owner(this); //if needed + //dbc->connect(_connection_string); //return dbc; return Ref(); diff --git a/modules/database/database_multi_threaded.cpp b/modules/database/database_multi_threaded.cpp new file mode 100644 index 000000000..4b6c93866 --- /dev/null +++ b/modules/database/database_multi_threaded.cpp @@ -0,0 +1,60 @@ +#include "database_multi_threaded.h" + +#include "database_connection.h" +#include "query_builder.h" +#include "query_result.h" +#include "table_builder.h" + +Ref DatabaseMultiThreaded::get_connection() { + _connection_map_lock.read_lock(); + + Thread::ID tid = Thread::get_caller_id(); + Map>::Element *e; + e = _connections.find(tid); + + if (!e) { + _connection_map_lock.read_unlock(); + + _connection_map_lock.write_lock(); + Ref dbc = _allocate_connection(); + _connections.insert(tid, dbc); + _connection_map_lock.write_unlock(); + + return dbc; + } + + Ref dbc = e->get(); + _connection_map_lock.read_unlock(); + + return dbc; +} + +Ref DatabaseMultiThreaded::_allocate_connection() { + Ref dbc; + dbc.instance(); + dbc->set_owner(this); + dbc->connect(_connection_string); + return dbc; +} + +DatabaseMultiThreaded::DatabaseMultiThreaded() { +} + +DatabaseMultiThreaded::~DatabaseMultiThreaded() { + _connection_map_lock.write_lock(); + + for (Map>::Element *e = _connections.front(); e; e = e->next()) { + Ref dbc = e->get(); + + if (dbc.is_valid()) { + dbc->set_owner(nullptr); + } + } + + _connections.clear(); + + _connection_map_lock.write_unlock(); +} + +void DatabaseMultiThreaded::_bind_methods() { +} diff --git a/modules/database/database_multi_threaded.h b/modules/database/database_multi_threaded.h new file mode 100644 index 000000000..a7f6690bc --- /dev/null +++ b/modules/database/database_multi_threaded.h @@ -0,0 +1,33 @@ +#ifndef DATABASE_MULTI_THREADED_H +#define DATABASE_MULTI_THREADED_H + +#include "core/map.h" +#include "core/os/rw_lock.h" +#include "core/os/thread.h" + +#include "database.h" + +class QueryBuilder; +class TableBuilder; +class QueryResult; +class DatabaseConnection; + +class DatabaseMultiThreaded : public Database { + GDCLASS(DatabaseMultiThreaded, Database); + +public: + Ref get_connection(); + + DatabaseMultiThreaded(); + ~DatabaseMultiThreaded(); + +protected: + Ref _allocate_connection(); + + static void _bind_methods(); + + RWLock _connection_map_lock; + Map> _connections; +}; + +#endif diff --git a/modules/database/database_single_threaded.cpp b/modules/database/database_single_threaded.cpp new file mode 100644 index 000000000..e6888af4a --- /dev/null +++ b/modules/database/database_single_threaded.cpp @@ -0,0 +1,36 @@ +#include "database_single_threaded.h" + +#include "database_connection.h" +#include "query_builder.h" +#include "query_result.h" +#include "table_builder.h" + +Ref DatabaseSingleThreaded::get_connection() { + if (!_connection.is_valid()) { + _connection = _allocate_connection(); + } + + return _connection; +} + +Ref DatabaseSingleThreaded::_allocate_connection() { + Ref dbc; + dbc.instance(); + dbc->set_owner(this); + dbc->connect(_connection_string); + return dbc; +} + +DatabaseSingleThreaded::DatabaseSingleThreaded() { +} + +DatabaseSingleThreaded::~DatabaseSingleThreaded() { + if (_connection.is_valid()) { + _connection->set_owner(nullptr); + } + + _connection.unref(); +} + +void DatabaseSingleThreaded::_bind_methods() { +} diff --git a/modules/database/database_single_threaded.h b/modules/database/database_single_threaded.h new file mode 100644 index 000000000..5eb757f7d --- /dev/null +++ b/modules/database/database_single_threaded.h @@ -0,0 +1,33 @@ +#ifndef DATABASE_SINGLE_THREADED_H +#define DATABASE_SINGLE_THREADED_H + +#include "core/ustring.h" + +#include "database.h" + +class QueryBuilder; +class TableBuilder; +class QueryResult; +class DatabaseConnection; + +class DatabaseSingleThreaded : public Database { + GDCLASS(DatabaseSingleThreaded, Database); + +public: + String get_connection_string(); + void set_connection_string(const String &val); + + Ref get_connection(); + + DatabaseSingleThreaded(); + ~DatabaseSingleThreaded(); + +protected: + Ref _allocate_connection(); + + static void _bind_methods(); + + Ref _connection; +}; + +#endif