summaryrefslogtreecommitdiffstats
path: root/abs/core/mythtv/stable-29/mythtv/0259-0117-UI-Provide-dbase-cache-for-RegisterKey-and-RegisterJ.patch
blob: 79169e4439ec0199745274fa9af03226936492fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
From 53ab06dc745938d2ea74214fc10a2b5446bac01a Mon Sep 17 00:00:00 2001
From: Lawrence Rust <lvr@softsystem.co.uk>
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 <lvr@softsystem.co.uk>
---
 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 <QKeyEvent>
 #include <QKeySequence>
 #include <QSize>
+#include <QPair>
+#include <QMap>
+#include <QMutexLocker>
 
 // 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