Merge lp:~uonedb-qt/u1db-qt/cacheReplicaSql into lp:u1db-qt

Proposed by Christian Dywan on 2015-01-06
Status: Needs review
Proposed branch: lp:~uonedb-qt/u1db-qt/cacheReplicaSql
Merge into: lp:u1db-qt
Diff against target: 89 lines (+16/-13)
2 files modified
src/database.cpp (+15/-13)
src/database.h (+1/-0)
To merge this branch: bzr merge lp:~uonedb-qt/u1db-qt/cacheReplicaSql
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve on 2015-01-06
U1DB Qt developers 2015-01-06 Pending
Review via email: mp+245637@code.launchpad.net

Commit message

Cache replicaUid and avoid unused SQL queries

To post a comment you must log in.
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Unmerged revisions

122. By Christian Dywan on 2015-01-06

Cache replicaUid and avoid unused SQL queries

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/database.cpp'
2--- src/database.cpp 2014-11-07 14:30:31 +0000
3+++ src/database.cpp 2015-01-06 10:43:51 +0000
4@@ -78,9 +78,14 @@
5 QString
6 Database::getReplicaUid()
7 {
8+ if (!m_replicaUid.isEmpty())
9+ return m_replicaUid;
10+
11 QSqlQuery query (m_db.exec("SELECT value FROM u1db_config WHERE name = 'replica_uid'"));
12- if (!query.lastError().isValid() && query.next())
13- return query.value(0).toString();
14+ if (!query.lastError().isValid() && query.next()) {
15+ m_replicaUid = query.value(0).toString();
16+ return m_replicaUid;
17+ }
18 return setError(QString("Failed to get replica UID: %1\n%2").arg(query.lastError().text()).arg(query.lastQuery())) ? QString() : QString();
19 }
20
21@@ -137,6 +142,9 @@
22 if (!m_db.isValid())
23 m_db = QSqlDatabase::addDatabase("QSQLITE",QUuid::createUuid().toString());
24
25+ /* Reset cache */
26+ m_replicaUid = QString();
27+
28 if (!m_db.isValid())
29 return setError("QSqlDatabase error");
30
31@@ -160,8 +168,6 @@
32 return setError(QString("Failed to open %1: %2").arg(path).arg(m_db.lastError().text()));
33 if (!isInitialized())
34 {
35- if (!isInitialized())
36- {
37 QFile file(":/dbschema.sql");
38 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
39 {
40@@ -181,13 +187,9 @@
41 query.bindValue(":uuid", QUuid::createUuid().toString());
42 if (!query.exec())
43 return setError(QString("Failed to apply internal schema: %1\n%2").arg(m_db.lastError().text()).arg(query.lastQuery()));
44- // Double-check
45- if (query.boundValue(0).toString() != getReplicaUid())
46- return setError(QString("Invalid replica uid: %1").arg(query.boundValue(0).toString()));
47 }
48 else
49 return setError(QString("Failed to read internal schema: FileError %1").arg(file.error()));
50- }
51 }
52 return true;
53 }
54@@ -286,7 +288,7 @@
55 return QVariant();
56
57 QSqlQuery query(m_db.exec());
58- query.prepare("SELECT doc_rev, content FROM document WHERE doc_id = :docId");
59+ query.prepare("SELECT content FROM document WHERE doc_id = :docId");
60 query.bindValue(":docId", docId);
61 if (query.exec() && query.next())
62 {
63@@ -717,10 +719,10 @@
64 return list;
65
66 QSqlQuery query(m_db.exec());
67- query.prepare("SELECT document.doc_id, document.doc_rev, document.content, "
68- "count(conflicts.doc_rev) FROM document LEFT OUTER JOIN conflicts "
69- "ON conflicts.doc_id = document.doc_id GROUP BY document.doc_id, "
70- "document.doc_rev, document.content");
71+ query.prepare("SELECT document.doc_id, document.doc_rev "
72+ "FROM document LEFT OUTER JOIN conflicts "
73+ "ON conflicts.doc_id = document.doc_id "
74+ "GROUP BY document.doc_id, document.doc_rev");
75 if (query.exec())
76 {
77 while (query.next())
78
79=== modified file 'src/database.h'
80--- src/database.h 2014-01-24 11:13:35 +0000
81+++ src/database.h 2015-01-06 10:43:51 +0000
82@@ -80,6 +80,7 @@
83 QString m_path;
84 QSqlDatabase m_db;
85 QString m_error;
86+ QString m_replicaUid;
87
88 QString getReplicaUid();
89 bool isInitialized();

Subscribers

People subscribed via source and target branches

to all changes: