Files
TCPlayerbotCore/src/server/shared/Database/DatabaseWorkerPool.h
T

359 lines
10 KiB
C++
Raw Normal View History

2010-10-07 15:54:07 +02:00
/*
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
*
* This program 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 2 of the License, or (at your
* option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
2010-08-18 02:25:52 +02:00
*/
#ifndef _DATABASEWORKERPOOL_H
#define _DATABASEWORKERPOOL_H
#include <ace/Atomic_Op_T.h>
#include <ace/Thread_Mutex.h>
2010-09-03 00:04:14 +02:00
#include "Common.h"
2010-08-18 19:48:51 +02:00
#include "Callback.h"
2010-08-18 02:25:52 +02:00
#include "MySQLConnection.h"
2010-09-03 00:04:14 +02:00
#include "Transaction.h"
2010-09-02 17:47:50 +02:00
#include "DatabaseWorker.h"
2010-09-03 00:08:20 +02:00
#include "PreparedStatement.h"
#include "Log.h"
2010-09-11 21:10:54 +02:00
#include "QueryResult.h"
#include "QueryHolder.h"
#include "AdhocStatement.h"
2010-08-18 02:25:52 +02:00
2010-09-25 01:05:24 +02:00
class PingOperation : public SQLOperation
{
/// Operation for idle delaythreads
bool Execute()
{
2010-10-31 10:55:29 +01:00
if (m_conn->LockIfReady())
{
m_conn->Ping();
m_conn->Unlock();
return true;
}
2010-09-27 00:20:56 +02:00
return false;
2010-09-25 01:05:24 +02:00
}
};
2010-09-02 17:47:50 +02:00
template <class T>
2010-08-18 02:25:52 +02:00
class DatabaseWorkerPool
{
2010-12-04 21:50:36 +01:00
template <class Y> friend class DatabaseWorker;
2010-09-03 00:04:14 +02:00
private:
typedef ACE_Atomic_Op<ACE_SYNCH_MUTEX, uint32> AtomicUInt;
2010-08-18 02:25:52 +02:00
public:
2010-09-02 17:47:50 +02:00
DatabaseWorkerPool() :
m_queue(new ACE_Activation_Queue(new ACE_Message_Queue<ACE_MT_SYNCH>)),
m_connections(0)
{
mysql_library_init(-1, NULL, NULL);
WPFatal (mysql_thread_safe(), "Used MySQL library isn't thread-safe.");
}
~DatabaseWorkerPool()
{
sLog.outSQLDriver("~DatabaseWorkerPool for '%s'.", m_connectionInfo.database.c_str());
2010-09-02 17:47:50 +02:00
mysql_library_end();
}
2010-12-04 21:50:36 +01:00
bool Open(const std::string& infoString, uint8 connections, uint8 delaythreads)
2010-09-02 17:47:50 +02:00
{
m_connectionInfo = MySQLConnectionInfo(infoString);
2010-12-04 21:50:36 +01:00
sLog.outSQLDriver("Opening databasepool '%s'. Delaythreads: %u, connections: %u", m_connectionInfo.database.c_str(), delaythreads, connections);
2010-09-03 10:52:32 +02:00
2010-12-04 21:50:36 +01:00
m_connections.resize(connections);
for (uint8 i = 0; i < connections; ++i)
2010-09-02 17:47:50 +02:00
{
T* t = new T(m_connectionInfo);
t->Open();
2010-12-04 21:50:36 +01:00
m_connections[i] = t;
2010-09-27 00:20:56 +02:00
++m_connectionCount;
2010-09-02 17:47:50 +02:00
}
2010-12-04 21:50:36 +01:00
m_delaythreads.resize(delaythreads);
for (uint8 i = 0; i < delaythreads; ++i)
m_delaythreads[i] = new DatabaseWorker<ThisClass>(m_queue, this);
2010-09-27 00:20:56 +02:00
sLog.outSQLDriver("Databasepool opened succesfuly. %u connections running.", (uint32)m_connectionCount.value());
2010-09-02 17:47:50 +02:00
return true;
}
void Close()
{
sLog.outSQLDriver("Closing down databasepool '%s'.", m_connectionInfo.database.c_str());
2010-09-02 17:47:50 +02:00
2010-09-27 00:20:56 +02:00
/// Shuts down delaythreads for this connection pool.
m_queue->queue()->deactivate();
2010-09-02 17:47:50 +02:00
2010-12-04 21:50:36 +01:00
for (uint8 i = 0; i < m_delaythreads.size(); ++i)
m_delaythreads[i]->wait();
2010-09-02 17:47:50 +02:00
2010-12-04 21:50:36 +01:00
/// Shut down the connections
for (uint8 i = 0; i < m_connections.size(); ++i)
2010-09-02 17:47:50 +02:00
{
2010-12-04 21:50:36 +01:00
T* t = m_connections[i];
while (1)
if (t->LockIfReady())
{
t->Close();
--m_connectionCount;
break;
}
2010-09-02 17:47:50 +02:00
}
2010-09-27 00:20:56 +02:00
sLog.outSQLDriver("All connections on databasepool %s closed.", m_connectionInfo.database.c_str());
2010-09-02 17:47:50 +02:00
}
void Execute(const char* sql)
{
if (!sql)
return;
BasicStatementTask* task = new BasicStatementTask(sql);
Enqueue(task);
}
void PExecute(const char* sql, ...)
{
if (!sql)
return;
va_list ap;
char szQuery[MAX_QUERY_LEN];
va_start(ap, sql);
vsnprintf(szQuery, MAX_QUERY_LEN, sql, ap);
va_end(ap);
Execute(szQuery);
}
void DirectExecute(const char* sql)
{
2010-09-27 00:20:56 +02:00
if (!sql)
return;
T* t = GetFreeConnection();
t->Execute(sql);
t->Unlock();
2010-09-02 17:47:50 +02:00
}
void DirectPExecute(const char* sql, ...)
{
if (!sql)
return;
va_list ap;
char szQuery[MAX_QUERY_LEN];
va_start(ap, sql);
vsnprintf(szQuery, MAX_QUERY_LEN, sql, ap);
va_end(ap);
return DirectExecute(szQuery);
}
2010-09-27 00:20:56 +02:00
QueryResult Query(const char* sql, MySQLConnection* conn = NULL)
2010-09-02 17:47:50 +02:00
{
2010-09-27 00:20:56 +02:00
if (!conn)
conn = GetFreeConnection();
ResultSet* result = conn->Query(sql);
conn->Unlock();
2010-09-19 12:16:29 +02:00
if (!result || !result->GetRowCount())
return QueryResult(NULL);
result->NextRow();
return QueryResult(result);
2010-09-02 17:47:50 +02:00
}
2010-09-27 00:20:56 +02:00
QueryResult PQuery(const char* sql, MySQLConnection* conn, ...)
{
if (!sql)
return QueryResult(NULL);
va_list ap;
char szQuery[MAX_QUERY_LEN];
va_start(ap, sql);
vsnprintf(szQuery, MAX_QUERY_LEN, sql, ap);
va_end(ap);
return Query(szQuery, conn);
}
2010-09-11 21:10:54 +02:00
QueryResult PQuery(const char* sql, ...)
2010-09-02 17:47:50 +02:00
{
if (!sql)
2010-09-11 21:10:54 +02:00
return QueryResult(NULL);
2010-09-02 17:47:50 +02:00
va_list ap;
char szQuery[MAX_QUERY_LEN];
va_start(ap, sql);
vsnprintf(szQuery, MAX_QUERY_LEN, sql, ap);
va_end(ap);
return Query(szQuery);
}
2010-09-11 21:10:54 +02:00
ACE_Future<QueryResult> AsyncQuery(const char* sql)
2010-09-02 17:47:50 +02:00
{
QueryResultFuture res;
BasicStatementTask* task = new BasicStatementTask(sql, res);
Enqueue(task);
return res; //! Actual return value has no use yet
}
2010-09-11 21:10:54 +02:00
ACE_Future<QueryResult> AsyncPQuery(const char* sql, ...)
2010-09-02 17:47:50 +02:00
{
va_list ap;
char szQuery[MAX_QUERY_LEN];
va_start(ap, sql);
vsnprintf(szQuery, MAX_QUERY_LEN, sql, ap);
va_end(ap);
return AsyncQuery(szQuery);
}
QueryResultHolderFuture DelayQueryHolder(SQLQueryHolder* holder)
{
QueryResultHolderFuture res;
SQLQueryHolderTask* task = new SQLQueryHolderTask(holder, res);
Enqueue(task);
return res; //! Fool compiler, has no use yet
}
2010-09-12 01:40:27 +02:00
2010-09-02 17:47:50 +02:00
SQLTransaction BeginTransaction()
{
return SQLTransaction(new Transaction);
}
void CommitTransaction(SQLTransaction transaction)
{
if (sLog.GetSQLDriverQueryLogging())
2010-09-02 17:47:50 +02:00
{
if (transaction->GetSize() == 0)
{
sLog.outSQLDriver("Transaction contains 0 queries. Not executing.");
return;
}
if (transaction->GetSize() == 1)
{
sLog.outSQLDriver("Warning: Transaction only holds 1 query, consider removing Transaction context in code.");
}
2010-09-02 17:47:50 +02:00
}
Enqueue(new TransactionTask(transaction));
}
2010-08-18 02:25:52 +02:00
2010-09-02 20:54:43 +02:00
PreparedStatement* GetPreparedStatement(uint32 index)
{
return new PreparedStatement(index);
}
void Execute(PreparedStatement* stmt)
{
PreparedStatementTask* task = new PreparedStatementTask(stmt);
Enqueue(task);
}
void DirectExecute(PreparedStatement* stmt)
{
T* t = GetFreeConnection();
t->Execute(stmt);
t->Unlock();
}
2010-09-02 20:54:43 +02:00
2010-08-18 02:25:52 +02:00
void escape_string(std::string& str)
{
if (str.empty())
return;
char* buf = new char[str.size()*2+1];
escape_string(buf,str.c_str(),str.size());
str = buf;
delete[] buf;
}
2010-09-11 21:22:15 +02:00
PreparedQueryResult Query(PreparedStatement* stmt)
{
2010-09-27 00:20:56 +02:00
T* t = GetFreeConnection();
PreparedResultSet* ret = t->Query(stmt);
t->Unlock();
2010-09-19 12:16:29 +02:00
if (!ret || !ret->GetRowCount())
2010-09-11 21:22:15 +02:00
return PreparedQueryResult(NULL);
2010-09-11 21:22:15 +02:00
return PreparedQueryResult(ret);
}
2010-09-25 01:05:24 +02:00
void KeepAlive()
{
2010-12-04 21:50:36 +01:00
/// Ping connections
for (uint8 i = 0; i < m_connections.size(); ++i)
2010-10-31 10:55:29 +01:00
{
2010-12-04 21:50:36 +01:00
T* t = m_connections[i];
2010-10-31 10:55:29 +01:00
if (t->LockIfReady())
{
t->Ping();
t->Unlock();
}
}
2010-12-04 21:50:36 +01:00
}
protected:
T* GetFreeConnection()
{
uint8 i = 0;
for (;;) /// Block forever until a connection is free
{
T* t = m_connections[++i % m_connectionCount.value()];
if (t->LockIfReady()) /// Must be matched with t->Unlock() or you will get deadlocks
return t;
}
// This will be called when Celine Dion learns to sing
return NULL;
}
2010-09-25 01:05:24 +02:00
2010-09-03 10:52:32 +02:00
private:
2010-08-18 02:25:52 +02:00
unsigned long escape_string(char *to, const char *from, unsigned long length)
{
if (!to || !from || !length)
return 0;
2010-09-27 00:20:56 +02:00
T* t = GetFreeConnection();
unsigned long ret = mysql_real_escape_string(t->GetHandle(), to, from, length);
t->Unlock();
return ret;
2010-08-18 02:25:52 +02:00
}
2010-09-12 01:40:27 +02:00
2010-08-18 02:25:52 +02:00
void Enqueue(SQLOperation* op)
{
m_queue->enqueue(op);
}
private:
2010-12-04 21:50:36 +01:00
typedef DatabaseWorkerPool<T> ThisClass;
ACE_Activation_Queue* m_queue; //! Queue shared by async worker threads.
std::vector<T*> m_connections;
AtomicUInt m_connectionCount; //! Counter of MySQL connections;
MySQLConnectionInfo m_connectionInfo;
2010-12-04 21:50:36 +01:00
std::vector<DatabaseWorker<ThisClass>*> m_delaythreads; //! Delaythreads (templatized)
2010-08-18 02:25:52 +02:00
};
#endif