From fe87ef196773cc9adf7aacc01e3efe094ed387a5 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Wed, 13 Nov 2019 15:55:18 +0000 Subject: Refactor the MySQL query and result queue classes. --- src/modules/extra/m_mysql.cpp | 85 ++++++++++++++++++++++++++----------------- 1 file 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 ConnMap; -typedef std::deque QueryQueue; -typedef std::deque ResultQueue; +typedef std::deque QueryQueue; +typedef std::deque 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(); -- cgit v1.2.3