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

Proposed by Christian Dywan on 2015-01-06
Status: Needs review
Proposed branch: lp:~uonedb-qt/u1db-qt/smartDefaultDocs
Merge into: lp:u1db-qt
Diff against target: 185 lines (+15/-35)
3 files modified
src/database.cpp (+9/-29)
src/database.h (+2/-2)
src/document.cpp (+4/-4)
To merge this branch: bzr merge lp:~uonedb-qt/u1db-qt/smartDefaultDocs
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+245638@code.launchpad.net

Commit message

Avoid loading contents and revision for default documents

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

Avoid loading contents and revision for default documents

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:46:20 +0000
4@@ -377,55 +377,39 @@
5
6 QString Database::getNextDocRevisionNumber(QString doc_id)
7 {
8-
9 QString revision_number = getReplicaUid()+":1";
10-
11 QString current_revision_number = getCurrentDocRevisionNumber(doc_id);
12
13 /*!
14 Some revisions contain information from previous
15 conflicts/syncs. Revisions are delimited by '|'.
16-
17 */
18
19 QStringList current_revision_list = current_revision_number.split("|");
20-
21 Q_FOREACH (QString current_revision, current_revision_list) {
22
23 /*!
24 Each revision contains two pieces of information,
25 the uid of the database that made the revsion, and a counter
26 for the revsion. This information is delimited by ':'.
27-
28 */
29
30 QStringList current_revision_number_list = current_revision.split(":");
31-
32 if(current_revision_number_list[0]==getReplicaUid()) {
33
34 /*!
35 If the current revision uid is the same as this Database's uid the counter portion is increased by one.
36-
37 */
38
39 int revision_generation_number = current_revision_number_list[1].toInt()+1;
40-
41 revision_number = getReplicaUid()+":"+QString::number(revision_generation_number);
42-
43 }
44 else {
45
46 /*!
47 If the current revision uid is not the same as this Database's uid then the revision represents a change that originated in another database.
48-
49- */
50- //revision_number+="|"+current_revision;
51-
52- /* Not sure if the above is necessary,
53- *and did not appear to be working as intended either.
54- *
55- * Commented out, but maybe OK to delete.
56- */
57+ */
58+ // FIXME: revision_number+="|"+current_revision;
59 }
60
61 }
62@@ -436,9 +420,7 @@
63 */
64
65 revision_number = revision_number.replace("{","");
66-
67 revision_number = revision_number.replace("}","");
68-
69 return revision_number;
70
71 }
72@@ -618,10 +600,10 @@
73 there's no error.
74 If no \a docId is given or \a docId is an empty string the \a contents will be
75 stored under an autogenerated name.
76- Returns the new revision of the document, or -1 on failure.
77+ Returns the new \a revision of the document, or -1 on failure.
78 */
79 QString
80-Database::putDoc(QVariant contents, const QString& docId)
81+Database::putDoc(QVariant contents, const QString& docId, const QString& revision)
82 {
83 if (!initializeIfNeeded())
84 return "";
85@@ -629,16 +611,15 @@
86 ScopedTransaction t(m_db);
87
88 QString newOrEmptyDocId(docId);
89- QVariant oldDoc = newOrEmptyDocId.isEmpty() ? QVariant() : getDocUnchecked(newOrEmptyDocId);
90-
91- QString revision_number = getNextDocRevisionNumber(newOrEmptyDocId);
92+ QVariant oldDoc = newOrEmptyDocId.isEmpty() || !revision.isEmpty() ? QVariant() : getDocUnchecked(newOrEmptyDocId);
93+ QString newRevisionNumber = revision.isEmpty() ? getNextDocRevisionNumber(newOrEmptyDocId) : revision;
94
95 QSqlQuery query(m_db.exec());
96 if (oldDoc.isValid())
97 {
98 query.prepare("UPDATE document SET doc_rev=:docRev, content=:docJson WHERE doc_id = :docId");
99 query.bindValue(":docId", newOrEmptyDocId);
100- query.bindValue(":docRev", revision_number);
101+ query.bindValue(":docRev", newRevisionNumber);
102 // Parse Variant from QML as JsonDocument, fallback to string
103 QString json(QJsonDocument::fromVariant(contents).toJson());
104 query.bindValue(":docJson", json.isEmpty() ? contents : json);
105@@ -650,7 +631,6 @@
106 return setError(QString("Failed to delete document field %1: %2\n%3").arg(newOrEmptyDocId).arg(query.lastError().text()).arg(query.lastQuery())) ? "" : "";
107
108 createNewTransaction(newOrEmptyDocId);
109-
110 }
111 else
112 {
113@@ -661,7 +641,7 @@
114
115 query.prepare("INSERT INTO document (doc_id, doc_rev, content) VALUES (:docId, :docRev, :docJson)");
116 query.bindValue(":docId", newOrEmptyDocId);
117- query.bindValue(":docRev", revision_number);
118+ query.bindValue(":docRev", newRevisionNumber);
119 // Parse Variant from QML as JsonDocument, fallback to string
120 QJsonDocument json(QJsonDocument::fromVariant(contents));
121 query.bindValue(":docJson", json.isEmpty() ? contents : json.toJson());
122@@ -680,7 +660,7 @@
123
124 Q_EMIT docChanged(newOrEmptyDocId, contents);
125
126- return revision_number;
127+ return newRevisionNumber;
128 }
129
130 /*!
131
132=== modified file 'src/database.h'
133--- src/database.h 2014-01-24 11:13:35 +0000
134+++ src/database.h 2015-01-06 10:46:20 +0000
135@@ -48,7 +48,7 @@
136 Q_INVOKABLE QVariant getDoc(const QString& docId);
137 QString getDocumentContents(const QString& docId);
138 QVariant getDocUnchecked(const QString& docId) const;
139- Q_INVOKABLE QString putDoc(QVariant newDoc, const QString& docID=QString());
140+ Q_INVOKABLE QString putDoc(QVariant newDoc, const QString& docID=QString(), const QString& rev=QString());
141 Q_INVOKABLE void deleteDoc(const QString& docID);
142 Q_INVOKABLE QList<QString> listDocs();
143 Q_INVOKABLE QString lastError();
144@@ -57,6 +57,7 @@
145 Q_INVOKABLE QStringList getIndexKeys(const QString& indexName);
146
147 /* Functions handy for Synchronization */
148+ QString getReplicaUid();
149 QString getNextDocRevisionNumber(QString doc_id);
150 QString getCurrentDocRevisionNumber(QString doc_id);
151 void updateDocRevisionNumber(QString doc_id,QString revision);
152@@ -81,7 +82,6 @@
153 QSqlDatabase m_db;
154 QString m_error;
155
156- QString getReplicaUid();
157 bool isInitialized();
158 bool initializeIfNeeded(const QString& path=":memory:");
159 bool setError(const QString& error);
160
161=== modified file 'src/document.cpp'
162--- src/document.cpp 2013-04-23 15:17:24 +0000
163+++ src/document.cpp 2015-01-06 10:46:20 +0000
164@@ -152,8 +152,8 @@
165 m_create = create;
166 Q_EMIT createChanged(create);
167
168- if (m_create && m_database && m_defaults.isValid() && !m_database->getDocUnchecked(m_docId).isValid())
169- m_database->putDoc(m_defaults, m_docId);
170+ if (m_create && m_database && m_defaults.isValid() && !m_contents.isValid())
171+ m_database->putDoc(m_defaults, m_docId, m_database->getReplicaUid() + ":1");
172 }
173
174 QVariant
175@@ -178,8 +178,8 @@
176 m_defaults = defaults;
177 Q_EMIT defaultsChanged(defaults);
178
179- if (m_create && m_database && m_defaults.isValid() && !m_database->getDocUnchecked(m_docId).isValid())
180- m_database->putDoc(m_defaults, m_docId);
181+ if (m_create && m_database && m_defaults.isValid() && !m_contents.isValid())
182+ m_database->putDoc(m_defaults, m_docId, m_database->getReplicaUid() + ":1");
183 }
184
185 QVariant

Subscribers

People subscribed via source and target branches

to all changes: