]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Refactor the MySQL query and result queue classes.
authorPeter Powell <petpow@saberuk.com>
Wed, 13 Nov 2019 15:55:18 +0000 (15:55 +0000)
committerPeter Powell <petpow@saberuk.com>
Wed, 13 Nov 2019 16:20:34 +0000 (16:20 +0000)
src/modules/extra/m_mysql.cpp

index 3b9bde4de60fe8022ee3b30a125039e5404db934..d94c75917b548fbe7176a95d2c96817f4f9b5626 100644 (file)
@@ -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();