From 53ab06dc745938d2ea74214fc10a2b5446bac01a Mon Sep 17 00:00:00 2001 From: Lawrence Rust Date: Wed, 5 Jun 2013 16:07:56 +0100 Subject: [PATCH 117/333] UI: Provide dbase cache for RegisterKey and RegisterJump to speed startup Signed-off-by: Lawrence Rust --- mythtv/libs/libmythui/mythmainwindow.cpp | 211 +++++++++++++++++++++--------- 1 file changed, 147 insertions(+), 64 deletions(-) diff --git a/mythtv/libs/libmythui/mythmainwindow.cpp b/mythtv/libs/libmythui/mythmainwindow.cpp index c765279..226454a 100644 --- a/mythtv/libs/libmythui/mythmainwindow.cpp +++ b/mythtv/libs/libmythui/mythmainwindow.cpp @@ -25,6 +25,9 @@ using namespace std; #include #include #include +#include +#include +#include // Platform headers #include "unistd.h" @@ -1765,67 +1768,107 @@ void MythMainWindow::BindKey(const QString &context, const QString &action, void MythMainWindow::RegisterKey(const QString &context, const QString &action, const QString &description, const QString &key) { - QString keybind = key; - - MSqlQuery query(MSqlQuery::InitCon()); + typedef QPair< QString,QString > key_t; // context, action + typedef QPair< QString,QString > val_t; // keybind, description + typedef QMap< key_t,val_t > cache_t; + static cache_t s_cache; + static QMutex s_mutex; - if (d->m_useDB && query.isConnected()) + if (s_cache.empty() && d->m_useDB) { - query.prepare("SELECT keylist, description FROM keybindings WHERE " - "context = :CONTEXT AND action = :ACTION AND " - "hostname = :HOSTNAME ;"); - query.bindValue(":CONTEXT", context); - query.bindValue(":ACTION", action); - query.bindValue(":HOSTNAME", GetMythDB()->GetHostName()); - - if (query.exec() && query.next()) + MSqlQuery query(MSqlQuery::InitCon()); + if (query.isConnected()) { - keybind = query.value(0).toString(); - QString db_description = query.value(1).toString(); - - // Update keybinding description if changed - if (db_description != description) + query.prepare("SELECT context, action, keylist, description " + "FROM keybindings WHERE hostname = :HOSTNAME ;"); + query.bindValue(":HOSTNAME", GetMythDB()->GetHostName()); + if (query.exec()) { - LOG(VB_GENERAL, LOG_NOTICE, - "Updating keybinding description..."); - query.prepare( - "UPDATE keybindings " - "SET description = :DESCRIPTION " - "WHERE context = :CONTEXT AND " - " action = :ACTION AND " - " hostname = :HOSTNAME"); - - query.bindValue(":DESCRIPTION", description); - query.bindValue(":CONTEXT", context); - query.bindValue(":ACTION", action); - query.bindValue(":HOSTNAME", GetMythDB()->GetHostName()); - - if (!query.exec() && !(GetMythDB()->SuppressDBMessages())) + QMutexLocker locker(&s_mutex); + while (query.next()) { - MythDB::DBError("Update Keybinding", query); + key_t k(query.value(0).toString(), query.value(1).toString()); + val_t v(query.value(2).toString(), query.value(3).toString()); + s_cache[k] = v; } } + else if (!GetMythDB()->SuppressDBMessages()) + MythDB::DBError("RegisterKey", query); } - else + } + + QString keybind = key; + QString db_description; + bool bFound = false; + { + QMutexLocker locker(&s_mutex); + cache_t::const_iterator it = s_cache.find(key_t(context, action)); + if (it != s_cache.end()) { - QString inskey = keybind; - - query.prepare("INSERT INTO keybindings (context, action, " - "description, keylist, hostname) VALUES " - "( :CONTEXT, :ACTION, :DESCRIPTION, :KEYLIST, " - ":HOSTNAME );"); - query.bindValue(":CONTEXT", context); - query.bindValue(":ACTION", action); + keybind = it->first; + db_description = it->second; + bFound = true; + } + } + + if (bFound) + { + // Update keybinding description if changed + if (db_description != description && d->m_useDB) + { + LOG(VB_GENERAL, LOG_NOTICE, "Updating keybinding description..."); + + MSqlQuery query(MSqlQuery::InitCon()); + + query.prepare( + "UPDATE keybindings " + "SET description = :DESCRIPTION " + "WHERE context = :CONTEXT AND " + " action = :ACTION AND " + " hostname = :HOSTNAME"); + query.bindValue(":DESCRIPTION", description); - query.bindValue(":KEYLIST", inskey); - query.bindValue(":HOSTNAME", GetMythDB()->GetHostName()); + query.bindValue(":CONTEXT", context); + query.bindValue(":ACTION", action); + query.bindValue(":HOSTNAME", GetMythDB()->GetHostName()); if (!query.exec() && !(GetMythDB()->SuppressDBMessages())) { - MythDB::DBError("Insert Keybinding", query); + MythDB::DBError("Update Keybinding", query); } } } + else if (d->m_useDB) + { + LOG(VB_GENERAL, LOG_NOTICE, QString("Add keybinding %1::%2 = %3") + .arg(context).arg(action).arg(keybind) ); + + MSqlQuery query(MSqlQuery::InitCon()); + + QString inskey = keybind; + + query.prepare("INSERT INTO keybindings (context, action, " + "description, keylist, hostname) VALUES " + "( :CONTEXT, :ACTION, :DESCRIPTION, :KEYLIST, " + ":HOSTNAME );"); + query.bindValue(":CONTEXT", context); + query.bindValue(":ACTION", action); + query.bindValue(":DESCRIPTION", description); + query.bindValue(":KEYLIST", inskey); + query.bindValue(":HOSTNAME", GetMythDB()->GetHostName()); + + if (!query.exec() && !(GetMythDB()->SuppressDBMessages())) + { + MythDB::DBError("Insert Keybinding", query); + } + else + { + QMutexLocker locker(&s_mutex); + key_t k(context, action); + val_t v(keybind, description); + s_cache[k] = v; + } + } BindKey(context, action, keybind); d->actionText[context][action] = description; @@ -1930,35 +1973,75 @@ void MythMainWindow::RegisterJump(const QString &destination, const QString &key, void (*callback)(void), bool exittomain, QString localAction) { - QString keybind = key; + typedef QPair< QString,QString > val_t; // keylist, description + typedef QMap< QString,val_t > cache_t; // destination -> keylist, description + static cache_t s_cache; + static QMutex s_mutex; - MSqlQuery query(MSqlQuery::InitCon()); - if (query.isConnected()) + if (s_cache.empty() && d->m_useDB) { - query.prepare("SELECT keylist FROM jumppoints WHERE " - "destination = :DEST and hostname = :HOST ;"); - query.bindValue(":DEST", destination); - query.bindValue(":HOST", GetMythDB()->GetHostName()); - - if (query.exec() && query.next()) + MSqlQuery query(MSqlQuery::InitCon()); + if (query.isConnected()) { - keybind = query.value(0).toString(); + query.prepare("SELECT destination, keylist, description " + "FROM jumppoints WHERE hostname = :HOSTNAME ;"); + query.bindValue(":HOSTNAME", GetMythDB()->GetHostName()); + if (query.exec()) + { + QMutexLocker locker(&s_mutex); + while (query.next()) + { + val_t v(query.value(1).toString(), query.value(2).toString()); + s_cache.insert(query.value(0).toString(), v); + } + } + else if (!GetMythDB()->SuppressDBMessages()) + MythDB::DBError("RegisterJump", query); } - else + } + + QString keybind = key; + bool bFound = false; + { + QMutexLocker locker(&s_mutex); + cache_t::const_iterator it = s_cache.find(destination); + if (it != s_cache.end()) { - QString inskey = keybind; + keybind = it->first; + bFound = true; + } + } - query.prepare("INSERT INTO jumppoints (destination, description, " - "keylist, hostname) VALUES ( :DEST, :DESC, :KEYLIST, " - ":HOST );"); + if (!bFound) + { + MSqlQuery query(MSqlQuery::InitCon()); + if (query.isConnected()) + { + query.prepare("SELECT keylist FROM jumppoints WHERE " + "destination = :DEST and hostname = :HOST ;"); query.bindValue(":DEST", destination); - query.bindValue(":DESC", description); - query.bindValue(":KEYLIST", inskey); query.bindValue(":HOST", GetMythDB()->GetHostName()); - if (!query.exec() || !query.isActive()) + if (query.exec() && query.next()) { - MythDB::DBError("Insert Jump Point", query); + keybind = query.value(0).toString(); + } + else + { + QString inskey = keybind; + + query.prepare("INSERT INTO jumppoints (destination, description, " + "keylist, hostname) VALUES ( :DEST, :DESC, :KEYLIST, " + ":HOST );"); + query.bindValue(":DEST", destination); + query.bindValue(":DESC", description); + query.bindValue(":KEYLIST", inskey); + query.bindValue(":HOST", GetMythDB()->GetHostName()); + + if (!query.exec() || !query.isActive()) + { + MythDB::DBError("Insert Jump Point", query); + } } } } -- 1.7.9.5