TrinityCore
DatabaseWorkerPool< T > Class Template Reference

#include <DatabaseWorkerPool.h>

Classes

struct  QueueSizeTracker
 

Public Types

typedef T::Statements PreparedStatementIndex
 

Public Member Functions

 DatabaseWorkerPool ()
 
 ~DatabaseWorkerPool ()
 
void SetConnectionInfo (std::string const &infoString, uint8 const asyncThreads, uint8 const synchThreads)
 
uint32 Open ()
 
void Close ()
 
bool PrepareStatements ()
 Prepares all prepared statements. More...
 
MySQLConnectionInfo const * GetConnectionInfo () const
 
void Execute (char const *sql)
 
template<typename... Args>
void PExecute (Trinity::FormatString< Args... > sql, Args &&... args)
 
void Execute (PreparedStatement< T > *stmt)
 
void DirectExecute (char const *sql)
 
template<typename... Args>
void DirectPExecute (Trinity::FormatString< Args... > sql, Args &&... args)
 
void DirectExecute (PreparedStatement< T > *stmt)
 
QueryResult Query (char const *sql, T *connection=nullptr)
 
template<typename... Args>
QueryResult PQuery (Trinity::FormatString< Args... > sql, T *conn, Args &&... args)
 
template<typename... Args>
QueryResult PQuery (Trinity::FormatString< Args... > sql, Args &&... args)
 
PreparedQueryResult Query (PreparedStatement< T > *stmt)
 
QueryCallback AsyncQuery (char const *sql)
 
QueryCallback AsyncQuery (PreparedStatement< T > *stmt)
 
SQLQueryHolderCallback DelayQueryHolder (std::shared_ptr< SQLQueryHolder< T > > holder)
 
SQLTransaction< T > BeginTransaction ()
 Begins an automanaged transaction pointer that will automatically rollback if not commited. (Autocommit=0) More...
 
void CommitTransaction (SQLTransaction< T > transaction)
 
TransactionCallback AsyncCommitTransaction (SQLTransaction< T > transaction)
 
void DirectCommitTransaction (SQLTransaction< T > &transaction)
 
void ExecuteOrAppend (SQLTransaction< T > &trans, char const *sql)
 
void ExecuteOrAppend (SQLTransaction< T > &trans, PreparedStatement< T > *stmt)
 
PreparedStatement< T > * GetPreparedStatement (PreparedStatementIndex index)
 
void EscapeString (std::string &str)
 Apply escape string'ing for current collation. (utf8) More...
 
void KeepAlive ()
 Keeps all our MySQL connections alive, prevent the server from disconnecting us. More...
 
void WarnAboutSyncQueries (bool warn)
 
size_t QueueSize () const
 

Private Types

enum  InternalIndex { IDX_ASYNC , IDX_SYNCH , IDX_SIZE }
 

Private Member Functions

uint32 OpenConnections (InternalIndex type, uint8 numConnections)
 
unsigned long EscapeString (char *to, char const *from, unsigned long length)
 
T * GetFreeConnection ()
 
T * GetAsyncConnectionForCurrentThread () const
 
char const * GetDatabaseName () const
 

Private Attributes

friend QueueSizeTracker
 
std::unique_ptr< Trinity::Asio::IoContext_ioContext
 Queue shared by async worker threads. More...
 
std::atomic< size_t > _queueSize
 
std::array< std::vector< std::unique_ptr< T > >, IDX_SIZE_connections
 
std::unique_ptr< MySQLConnectionInfo_connectionInfo
 
std::vector< uint8_preparedStatementSize
 
uint8 _async_threads
 
uint8 _synch_threads
 

Detailed Description

template<class T>
class DatabaseWorkerPool< T >

Definition at line 32 of file DatabaseWorkerPool.h.

Member Typedef Documentation

◆ PreparedStatementIndex

template<class T >
typedef T::Statements DatabaseWorkerPool< T >::PreparedStatementIndex

Other

Definition at line 193 of file DatabaseWorkerPool.h.

Member Enumeration Documentation

◆ InternalIndex

template<class T >
enum DatabaseWorkerPool::InternalIndex
private
Enumerator
IDX_ASYNC 
IDX_SYNCH 
IDX_SIZE 

Definition at line 35 of file DatabaseWorkerPool.h.

Constructor & Destructor Documentation

◆ DatabaseWorkerPool()

template<class T >
DatabaseWorkerPool< T >::DatabaseWorkerPool

Definition at line 105 of file DatabaseWorkerPool.cpp.

◆ ~DatabaseWorkerPool()

template<class T >
DatabaseWorkerPool< T >::~DatabaseWorkerPool

Definition at line 120 of file DatabaseWorkerPool.cpp.

Member Function Documentation

◆ AsyncCommitTransaction()

template<class T >
TransactionCallback DatabaseWorkerPool< T >::AsyncCommitTransaction ( SQLTransaction< T >  transaction)

Enqueues a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations were appended to the transaction will be respected during execution.

Definition at line 327 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ AsyncQuery() [1/2]

template<class T >
QueryCallback DatabaseWorkerPool< T >::AsyncQuery ( char const *  sql)

Asynchronous query (with resultset) methods. Enqueues a query in string format that will set the value of the QueryResultFuture return object as soon as the query is executed. The return value is then processed in ProcessQueryCallback methods.

Definition at line 261 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ AsyncQuery() [2/2]

template<class T >
QueryCallback DatabaseWorkerPool< T >::AsyncQuery ( PreparedStatement< T > *  stmt)

Enqueues a query in prepared format that will set the value of the PreparedQueryResultFuture return object as soon as the query is executed. The return value is then processed in ProcessQueryCallback methods. Statement must be prepared with CONNECTION_ASYNC flag.

Definition at line 272 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ BeginTransaction()

template<class T >
SQLTransaction< T > DatabaseWorkerPool< T >::BeginTransaction

Begins an automanaged transaction pointer that will automatically rollback if not commited. (Autocommit=0)

Transaction context methods.

Definition at line 294 of file DatabaseWorkerPool.cpp.

◆ Close()

template<class T >
void DatabaseWorkerPool< T >::Close

Closes the actualy MySQL connection.

Shut down the synchronous connections There's no need for locking the connection, because DatabaseWorkerPool<>::Close should only be called after any other thread tasks in the core have exited, meaning there can be no concurrent access at this point.

Definition at line 166 of file DatabaseWorkerPool.cpp.

+ Here is the caller graph for this function:

◆ CommitTransaction()

template<class T >
void DatabaseWorkerPool< T >::CommitTransaction ( SQLTransaction< T >  transaction)

Enqueues a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations were appended to the transaction will be respected during execution.

Definition at line 300 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ DelayQueryHolder()

template<class T >
SQLQueryHolderCallback DatabaseWorkerPool< T >::DelayQueryHolder ( std::shared_ptr< SQLQueryHolder< T > >  holder)

Enqueues a vector of SQL operations (can be both adhoc and prepared) that will set the value of the QueryResultHolderFuture return object as soon as the query is executed. The return value is then processed in ProcessQueryCallback methods. Any prepared statements added to this holder need to be prepared with the CONNECTION_ASYNC flag.

Definition at line 283 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ DirectCommitTransaction()

template<class T >
void DatabaseWorkerPool< T >::DirectCommitTransaction ( SQLTransaction< T > &  transaction)

Directly executes a collection of one-way SQL operations (can be both adhoc and prepared). The order in which these operations were appended to the transaction will be respected during execution.

Handle MySQL Errno 1213 without extending deadlock to the core itself

Todo:
More elegant way

Clean up now.

Definition at line 355 of file DatabaseWorkerPool.cpp.

◆ DirectExecute() [1/2]

template<class T >
void DatabaseWorkerPool< T >::DirectExecute ( char const *  sql)

Direct synchronous one-way statement methods. Directly executes a one-way SQL operation in string format, that will block the calling thread until finished. This method should only be used for queries that are only executed once, e.g during startup.

Definition at line 552 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ DirectExecute() [2/2]

template<class T >
void DatabaseWorkerPool< T >::DirectExecute ( PreparedStatement< T > *  stmt)

Directly executes a one-way SQL operation in prepared statement format, that will block the calling thread until finished. Statement must be prepared with the CONNECTION_SYNCH flag.

Delete proxy-class. Not needed anymore

Definition at line 563 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ DirectPExecute()

template<class T >
template<typename... Args>
void DatabaseWorkerPool< T >::DirectPExecute ( Trinity::FormatString< Args... >  sql,
Args &&...  args 
)
inline

Directly executes a one-way SQL operation in string format -with variable args-, that will block the calling thread until finished. This method should only be used for queries that are only executed once, e.g during startup.

Definition at line 96 of file DatabaseWorkerPool.h.

+ Here is the call graph for this function:

◆ EscapeString() [1/2]

template<class T >
unsigned long DatabaseWorkerPool< T >::EscapeString ( char *  to,
char const *  from,
unsigned long  length 
)
private

Definition at line 470 of file DatabaseWorkerPool.cpp.

◆ EscapeString() [2/2]

template<class T >
void DatabaseWorkerPool< T >::EscapeString ( std::string &  str)

Apply escape string'ing for current collation. (utf8)

Definition at line 391 of file DatabaseWorkerPool.cpp.

◆ Execute() [1/2]

template<class T >
void DatabaseWorkerPool< T >::Execute ( char const *  sql)

Delayed one-way statement methods. Enqueues a one-way SQL operation in string format that will be executed asynchronously. This method should only be used for queries that are only executed once, e.g during startup.

Definition at line 529 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Execute() [2/2]

template<class T >
void DatabaseWorkerPool< T >::Execute ( PreparedStatement< T > *  stmt)

Enqueues a one-way SQL operation in prepared statement format that will be executed asynchronously. Statement must be prepared with CONNECTION_ASYNC flag.

Definition at line 542 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ ExecuteOrAppend() [1/2]

template<class T >
void DatabaseWorkerPool< T >::ExecuteOrAppend ( SQLTransaction< T > &  trans,
char const *  sql 
)

Method used to execute ad-hoc statements in a diverse context. Will be wrapped in a transaction if valid object is present, otherwise executed standalone.

Definition at line 574 of file DatabaseWorkerPool.cpp.

◆ ExecuteOrAppend() [2/2]

template<class T >
void DatabaseWorkerPool< T >::ExecuteOrAppend ( SQLTransaction< T > &  trans,
PreparedStatement< T > *  stmt 
)

Method used to execute prepared statements in a diverse context. Will be wrapped in a transaction if valid object is present, otherwise executed standalone.

Definition at line 583 of file DatabaseWorkerPool.cpp.

◆ GetAsyncConnectionForCurrentThread()

template<class T >
T * DatabaseWorkerPool< T >::GetAsyncConnectionForCurrentThread
private

Definition at line 512 of file DatabaseWorkerPool.cpp.

◆ GetConnectionInfo()

template<class T >
MySQLConnectionInfo const * DatabaseWorkerPool< T >::GetConnectionInfo ( ) const
inline

Definition at line 57 of file DatabaseWorkerPool.h.

+ Here is the caller graph for this function:

◆ GetDatabaseName()

template<class T >
char const * DatabaseWorkerPool< T >::GetDatabaseName
private

Definition at line 523 of file DatabaseWorkerPool.cpp.

◆ GetFreeConnection()

template<class T >
T * DatabaseWorkerPool< T >::GetFreeConnection
private

Gets a free connection in the synchronous connection pool. Caller MUST call t->Unlock() after touching the MySQL context to prevent deadlocks.

Block forever until a connection is free

Must be matched with t->Unlock() or you will get deadlocks

Definition at line 485 of file DatabaseWorkerPool.cpp.

◆ GetPreparedStatement()

template<class T >
PreparedStatement< T > * DatabaseWorkerPool< T >::GetPreparedStatement ( PreparedStatementIndex  index)

Automanaged (internally) pointer to a prepared statement object for usage in upper level code. Pointer is deleted in this->DirectExecute(PreparedStatement*), this->Query(PreparedStatement*) or PreparedStatementTask::~PreparedStatementTask. This object is not tied to the prepared statement on the MySQL context yet until execution.

Definition at line 385 of file DatabaseWorkerPool.cpp.

◆ KeepAlive()

template<class T >
void DatabaseWorkerPool< T >::KeepAlive

Keeps all our MySQL connections alive, prevent the server from disconnecting us.

Ping synchronous connections

Assuming all worker threads are free, every worker thread will receive 1 ping operation request If one or more worker threads are busy, the ping operations will not be split evenly, but this doesn't matter as the sole purpose is to prevent connections from idling.

Definition at line 403 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ Open()

template<class T >
uint32 DatabaseWorkerPool< T >::Open

Definition at line 135 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OpenConnections()

template<class T >
uint32 DatabaseWorkerPool< T >::OpenConnections ( InternalIndex  type,
uint8  numConnections 
)
private

Definition at line 430 of file DatabaseWorkerPool.cpp.

◆ PExecute()

template<class T >
template<typename... Args>
void DatabaseWorkerPool< T >::PExecute ( Trinity::FormatString< Args... >  sql,
Args &&...  args 
)
inline

Enqueues a one-way SQL operation in string format -with variable args- that will be executed asynchronously. This method should only be used for queries that are only executed once, e.g during startup.

Definition at line 73 of file DatabaseWorkerPool.h.

+ Here is the call graph for this function:

◆ PQuery() [1/2]

template<class T >
template<typename... Args>
QueryResult DatabaseWorkerPool< T >::PQuery ( Trinity::FormatString< Args... >  sql,
Args &&...  args 
)
inline

Directly executes an SQL query in string format -with variable args- that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code.

Definition at line 130 of file DatabaseWorkerPool.h.

+ Here is the call graph for this function:

◆ PQuery() [2/2]

template<class T >
template<typename... Args>
QueryResult DatabaseWorkerPool< T >::PQuery ( Trinity::FormatString< Args... >  sql,
T *  conn,
Args &&...  args 
)
inline

Directly executes an SQL query in string format -with variable args- that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code.

Definition at line 119 of file DatabaseWorkerPool.h.

+ Here is the call graph for this function:

◆ PrepareStatements()

template<class T >
bool DatabaseWorkerPool< T >::PrepareStatements

Prepares all prepared statements.

Definition at line 192 of file DatabaseWorkerPool.cpp.

+ Here is the caller graph for this function:

◆ Query() [1/2]

template<class T >
QueryResult DatabaseWorkerPool< T >::Query ( char const *  sql,
T *  connection = nullptr 
)

Synchronous query (with resultset) methods. Directly executes an SQL query in string format that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code.

Definition at line 236 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Query() [2/2]

template<class T >
PreparedQueryResult DatabaseWorkerPool< T >::Query ( PreparedStatement< T > *  stmt)

Directly executes an SQL query in prepared format that will block the calling thread until finished. Returns reference counted auto pointer, no need for manual memory management in upper level code. Statement must be prepared with CONNECTION_SYNCH flag.

Delete proxy-class. Not needed anymore

Definition at line 248 of file DatabaseWorkerPool.cpp.

+ Here is the call graph for this function:

◆ QueueSize()

template<class T >
size_t DatabaseWorkerPool< T >::QueueSize

Definition at line 479 of file DatabaseWorkerPool.cpp.

◆ SetConnectionInfo()

template<class T >
void DatabaseWorkerPool< T >::SetConnectionInfo ( std::string const &  infoString,
uint8 const  asyncThreads,
uint8 const  synchThreads 
)

Definition at line 125 of file DatabaseWorkerPool.cpp.

+ Here is the caller graph for this function:

◆ WarnAboutSyncQueries()

template<class T >
void DatabaseWorkerPool< T >::WarnAboutSyncQueries ( bool  warn)
inline

Definition at line 206 of file DatabaseWorkerPool.h.

Member Data Documentation

◆ _async_threads

template<class T >
uint8 DatabaseWorkerPool< T >::_async_threads
private

Definition at line 237 of file DatabaseWorkerPool.h.

◆ _connectionInfo

template<class T >
std::unique_ptr<MySQLConnectionInfo> DatabaseWorkerPool< T >::_connectionInfo
private

Definition at line 235 of file DatabaseWorkerPool.h.

◆ _connections

template<class T >
std::array<std::vector<std::unique_ptr<T> >, IDX_SIZE> DatabaseWorkerPool< T >::_connections
private

Definition at line 234 of file DatabaseWorkerPool.h.

◆ _ioContext

template<class T >
std::unique_ptr<Trinity::Asio::IoContext> DatabaseWorkerPool< T >::_ioContext
private

Queue shared by async worker threads.

Definition at line 232 of file DatabaseWorkerPool.h.

◆ _preparedStatementSize

template<class T >
std::vector<uint8> DatabaseWorkerPool< T >::_preparedStatementSize
private

Definition at line 236 of file DatabaseWorkerPool.h.

◆ _queueSize

template<class T >
std::atomic<size_t> DatabaseWorkerPool< T >::_queueSize
private

Definition at line 233 of file DatabaseWorkerPool.h.

◆ _synch_threads

template<class T >
uint8 DatabaseWorkerPool< T >::_synch_threads
private

Definition at line 237 of file DatabaseWorkerPool.h.

◆ QueueSizeTracker

template<class T >
friend DatabaseWorkerPool< T >::QueueSizeTracker
private

Definition at line 229 of file DatabaseWorkerPool.h.


The documentation for this class was generated from the following files: