summaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorPeter Powell <petpow@saberuk.com>2019-11-13 15:55:18 +0000
committerPeter Powell <petpow@saberuk.com>2019-11-13 16:20:34 +0000
commitfe87ef196773cc9adf7aacc01e3efe094ed387a5 (patch)
treeb72a939474e74d129a9d73f54638fe4888b44921 /src/modules
parentb2c876c5779f044b110786b9860b9c8a6ea8464a (diff)
Refactor the MySQL query and result queue classes.
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/extra/m_mysql.cpp85
1 files changed, 52 insertions, 33 deletions
diff --git a/src/modules/extra/m_mysql.cpp b/src/modules/extra/m_mysql.cpp
index 3b9bde4de..d94c75917 100644
--- a/src/modules/extra/m_mysql.cpp
+++ b/src/modules/extra/m_mysql.cpp
@@ -90,24 +90,43 @@ class SQLConnection;
class MySQLresult;
class DispatcherThread;
-struct QQueueItem
+struct QueryQueueItem
{
- SQL::Query* q;
- std::string query;
- SQLConnection* c;
- QQueueItem(SQL::Query* Q, const std::string& S, SQLConnection* C) : q(Q), query(S), c(C) {}
+ // An SQL database which this query is executed on.
+ SQLConnection* connection;
+
+ // An object which handles the result of the query.
+ SQL::Query* query;
+
+ // The SQL query which is to be executed.
+ std::string querystr;
+
+ QueryQueueItem(SQL::Query* q, const std::string& s, SQLConnection* c)
+ : connection(c)
+ , query(q)
+ , querystr(s)
+ {
+ }
};
-struct RQueueItem
+struct ResultQueueItem
{
- SQL::Query* q;
- MySQLresult* r;
- RQueueItem(SQL::Query* Q, MySQLresult* R) : q(Q), r(R) {}
+ // An object which handles the result of the query.
+ SQL::Query* query;
+
+ // The result returned from executing the MySQL query.
+ MySQLresult* result;
+
+ ResultQueueItem(SQL::Query* q, MySQLresult* r)
+ : query(q)
+ , result(r)
+ {
+ }
};
typedef insp::flat_map<std::string, SQLConnection*> ConnMap;
-typedef std::deque<QQueueItem> QueryQueue;
-typedef std::deque<RQueueItem> ResultQueue;
+typedef std::deque<QueryQueueItem> QueryQueue;
+typedef std::deque<ResultQueueItem> ResultQueue;
/** MySQL module
* */
@@ -377,7 +396,7 @@ class SQLConnection : public SQL::Provider
{
ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Executing MySQL query: " + qs);
Parent()->Dispatcher->LockQueue();
- Parent()->qq.push_back(QQueueItem(q, qs, this));
+ Parent()->qq.push_back(QueryQueueItem(q, qs, this));
Parent()->Dispatcher->UnlockQueueWakeup();
}
@@ -486,10 +505,10 @@ void ModuleSQL::ReadConfig(ConfigStatus& status)
for (size_t j = qq.size(); j > 0; j--)
{
size_t k = j - 1;
- if (qq[k].c == i->second)
+ if (qq[k].connection == i->second)
{
- qq[k].q->OnError(err);
- delete qq[k].q;
+ qq[k].query->OnError(err);
+ delete qq[k].query;
qq.erase(qq.begin() + k);
}
}
@@ -508,17 +527,17 @@ void ModuleSQL::OnUnloadModule(Module* mod)
while (i > 0)
{
i--;
- if (qq[i].q->creator == mod)
+ if (qq[i].query->creator == mod)
{
if (i == 0)
{
// need to wait until the query is done
// (the result will be discarded)
- qq[i].c->lock.Lock();
- qq[i].c->lock.Unlock();
+ qq[i].connection->lock.Lock();
+ qq[i].connection->lock.Unlock();
}
- qq[i].q->OnError(err);
- delete qq[i].q;
+ qq[i].query->OnError(err);
+ delete qq[i].query;
qq.erase(qq.begin() + i);
}
}
@@ -539,23 +558,23 @@ void DispatcherThread::Run()
{
if (!Parent->qq.empty())
{
- QQueueItem i = Parent->qq.front();
- i.c->lock.Lock();
+ QueryQueueItem i = Parent->qq.front();
+ i.connection->lock.Lock();
this->UnlockQueue();
- MySQLresult* res = i.c->DoBlockingQuery(i.query);
- i.c->lock.Unlock();
+ MySQLresult* res = i.connection->DoBlockingQuery(i.querystr);
+ i.connection->lock.Unlock();
/*
* At this point, the main thread could be working on:
- * Rehash - delete i.c out from under us. We don't care about that.
- * UnloadModule - delete i.q and the qq item. Need to avoid reporting results.
+ * Rehash - delete i.connection out from under us. We don't care about that.
+ * UnloadModule - delete i.query and the qq item. Need to avoid reporting results.
*/
this->LockQueue();
- if (!Parent->qq.empty() && Parent->qq.front().q == i.q)
+ if (!Parent->qq.empty() && Parent->qq.front().query == i.query)
{
Parent->qq.pop_front();
- Parent->rq.push_back(RQueueItem(i.q, res));
+ Parent->rq.push_back(ResultQueueItem(i.query, res));
NotifyParent();
}
else
@@ -581,13 +600,13 @@ void DispatcherThread::OnNotify()
this->LockQueue();
for(ResultQueue::iterator i = Parent->rq.begin(); i != Parent->rq.end(); i++)
{
- MySQLresult* res = i->r;
+ MySQLresult* res = i->result;
if (res->err.code == SQL::SUCCESS)
- i->q->OnResult(*res);
+ i->query->OnResult(*res);
else
- i->q->OnError(res->err);
- delete i->q;
- delete i->r;
+ i->query->OnError(res->err);
+ delete i->query;
+ delete i->result;
}
Parent->rq.clear();
this->UnlockQueue();