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
=== modified file 'src/database.cpp'
--- src/database.cpp 2014-11-07 14:30:31 +0000
+++ src/database.cpp 2015-01-06 10:46:20 +0000
@@ -377,55 +377,39 @@
377377
378QString Database::getNextDocRevisionNumber(QString doc_id)378QString Database::getNextDocRevisionNumber(QString doc_id)
379{379{
380
381 QString revision_number = getReplicaUid()+":1";380 QString revision_number = getReplicaUid()+":1";
382
383 QString current_revision_number = getCurrentDocRevisionNumber(doc_id);381 QString current_revision_number = getCurrentDocRevisionNumber(doc_id);
384382
385 /*!383 /*!
386 Some revisions contain information from previous384 Some revisions contain information from previous
387conflicts/syncs. Revisions are delimited by '|'.385conflicts/syncs. Revisions are delimited by '|'.
388
389 */386 */
390387
391 QStringList current_revision_list = current_revision_number.split("|");388 QStringList current_revision_list = current_revision_number.split("|");
392
393 Q_FOREACH (QString current_revision, current_revision_list) {389 Q_FOREACH (QString current_revision, current_revision_list) {
394390
395 /*!391 /*!
396 Each revision contains two pieces of information,392 Each revision contains two pieces of information,
397the uid of the database that made the revsion, and a counter393the uid of the database that made the revsion, and a counter
398for the revsion. This information is delimited by ':'.394for the revsion. This information is delimited by ':'.
399
400 */395 */
401396
402 QStringList current_revision_number_list = current_revision.split(":");397 QStringList current_revision_number_list = current_revision.split(":");
403
404 if(current_revision_number_list[0]==getReplicaUid()) {398 if(current_revision_number_list[0]==getReplicaUid()) {
405399
406 /*!400 /*!
407 If the current revision uid is the same as this Database's uid the counter portion is increased by one.401 If the current revision uid is the same as this Database's uid the counter portion is increased by one.
408
409 */402 */
410403
411 int revision_generation_number = current_revision_number_list[1].toInt()+1;404 int revision_generation_number = current_revision_number_list[1].toInt()+1;
412
413 revision_number = getReplicaUid()+":"+QString::number(revision_generation_number);405 revision_number = getReplicaUid()+":"+QString::number(revision_generation_number);
414
415 }406 }
416 else {407 else {
417408
418 /*!409 /*!
419 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.410 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.
420411 */
421 */412 // FIXME: revision_number+="|"+current_revision;
422 //revision_number+="|"+current_revision;
423
424 /* Not sure if the above is necessary,
425 *and did not appear to be working as intended either.
426 *
427 * Commented out, but maybe OK to delete.
428 */
429 }413 }
430414
431 }415 }
@@ -436,9 +420,7 @@
436 */420 */
437421
438 revision_number = revision_number.replace("{","");422 revision_number = revision_number.replace("{","");
439
440 revision_number = revision_number.replace("}","");423 revision_number = revision_number.replace("}","");
441
442 return revision_number;424 return revision_number;
443425
444}426}
@@ -618,10 +600,10 @@
618 there's no error.600 there's no error.
619 If no \a docId is given or \a docId is an empty string the \a contents will be601 If no \a docId is given or \a docId is an empty string the \a contents will be
620 stored under an autogenerated name.602 stored under an autogenerated name.
621 Returns the new revision of the document, or -1 on failure.603 Returns the new \a revision of the document, or -1 on failure.
622 */604 */
623QString605QString
624Database::putDoc(QVariant contents, const QString& docId)606Database::putDoc(QVariant contents, const QString& docId, const QString& revision)
625{607{
626 if (!initializeIfNeeded())608 if (!initializeIfNeeded())
627 return "";609 return "";
@@ -629,16 +611,15 @@
629 ScopedTransaction t(m_db);611 ScopedTransaction t(m_db);
630612
631 QString newOrEmptyDocId(docId);613 QString newOrEmptyDocId(docId);
632 QVariant oldDoc = newOrEmptyDocId.isEmpty() ? QVariant() : getDocUnchecked(newOrEmptyDocId);614 QVariant oldDoc = newOrEmptyDocId.isEmpty() || !revision.isEmpty() ? QVariant() : getDocUnchecked(newOrEmptyDocId);
633615 QString newRevisionNumber = revision.isEmpty() ? getNextDocRevisionNumber(newOrEmptyDocId) : revision;
634 QString revision_number = getNextDocRevisionNumber(newOrEmptyDocId);
635616
636 QSqlQuery query(m_db.exec());617 QSqlQuery query(m_db.exec());
637 if (oldDoc.isValid())618 if (oldDoc.isValid())
638 {619 {
639 query.prepare("UPDATE document SET doc_rev=:docRev, content=:docJson WHERE doc_id = :docId");620 query.prepare("UPDATE document SET doc_rev=:docRev, content=:docJson WHERE doc_id = :docId");
640 query.bindValue(":docId", newOrEmptyDocId);621 query.bindValue(":docId", newOrEmptyDocId);
641 query.bindValue(":docRev", revision_number);622 query.bindValue(":docRev", newRevisionNumber);
642 // Parse Variant from QML as JsonDocument, fallback to string623 // Parse Variant from QML as JsonDocument, fallback to string
643 QString json(QJsonDocument::fromVariant(contents).toJson());624 QString json(QJsonDocument::fromVariant(contents).toJson());
644 query.bindValue(":docJson", json.isEmpty() ? contents : json);625 query.bindValue(":docJson", json.isEmpty() ? contents : json);
@@ -650,7 +631,6 @@
650 return setError(QString("Failed to delete document field %1: %2\n%3").arg(newOrEmptyDocId).arg(query.lastError().text()).arg(query.lastQuery())) ? "" : "";631 return setError(QString("Failed to delete document field %1: %2\n%3").arg(newOrEmptyDocId).arg(query.lastError().text()).arg(query.lastQuery())) ? "" : "";
651632
652 createNewTransaction(newOrEmptyDocId);633 createNewTransaction(newOrEmptyDocId);
653
654 }634 }
655 else635 else
656 {636 {
@@ -661,7 +641,7 @@
661641
662 query.prepare("INSERT INTO document (doc_id, doc_rev, content) VALUES (:docId, :docRev, :docJson)");642 query.prepare("INSERT INTO document (doc_id, doc_rev, content) VALUES (:docId, :docRev, :docJson)");
663 query.bindValue(":docId", newOrEmptyDocId);643 query.bindValue(":docId", newOrEmptyDocId);
664 query.bindValue(":docRev", revision_number);644 query.bindValue(":docRev", newRevisionNumber);
665 // Parse Variant from QML as JsonDocument, fallback to string645 // Parse Variant from QML as JsonDocument, fallback to string
666 QJsonDocument json(QJsonDocument::fromVariant(contents));646 QJsonDocument json(QJsonDocument::fromVariant(contents));
667 query.bindValue(":docJson", json.isEmpty() ? contents : json.toJson());647 query.bindValue(":docJson", json.isEmpty() ? contents : json.toJson());
@@ -680,7 +660,7 @@
680660
681 Q_EMIT docChanged(newOrEmptyDocId, contents);661 Q_EMIT docChanged(newOrEmptyDocId, contents);
682662
683 return revision_number;663 return newRevisionNumber;
684}664}
685665
686/*!666/*!
687667
=== modified file 'src/database.h'
--- src/database.h 2014-01-24 11:13:35 +0000
+++ src/database.h 2015-01-06 10:46:20 +0000
@@ -48,7 +48,7 @@
48 Q_INVOKABLE QVariant getDoc(const QString& docId);48 Q_INVOKABLE QVariant getDoc(const QString& docId);
49 QString getDocumentContents(const QString& docId);49 QString getDocumentContents(const QString& docId);
50 QVariant getDocUnchecked(const QString& docId) const;50 QVariant getDocUnchecked(const QString& docId) const;
51 Q_INVOKABLE QString putDoc(QVariant newDoc, const QString& docID=QString());51 Q_INVOKABLE QString putDoc(QVariant newDoc, const QString& docID=QString(), const QString& rev=QString());
52 Q_INVOKABLE void deleteDoc(const QString& docID);52 Q_INVOKABLE void deleteDoc(const QString& docID);
53 Q_INVOKABLE QList<QString> listDocs();53 Q_INVOKABLE QList<QString> listDocs();
54 Q_INVOKABLE QString lastError();54 Q_INVOKABLE QString lastError();
@@ -57,6 +57,7 @@
57 Q_INVOKABLE QStringList getIndexKeys(const QString& indexName);57 Q_INVOKABLE QStringList getIndexKeys(const QString& indexName);
5858
59 /* Functions handy for Synchronization */59 /* Functions handy for Synchronization */
60 QString getReplicaUid();
60 QString getNextDocRevisionNumber(QString doc_id);61 QString getNextDocRevisionNumber(QString doc_id);
61 QString getCurrentDocRevisionNumber(QString doc_id);62 QString getCurrentDocRevisionNumber(QString doc_id);
62 void updateDocRevisionNumber(QString doc_id,QString revision);63 void updateDocRevisionNumber(QString doc_id,QString revision);
@@ -81,7 +82,6 @@
81 QSqlDatabase m_db;82 QSqlDatabase m_db;
82 QString m_error;83 QString m_error;
8384
84 QString getReplicaUid();
85 bool isInitialized();85 bool isInitialized();
86 bool initializeIfNeeded(const QString& path=":memory:");86 bool initializeIfNeeded(const QString& path=":memory:");
87 bool setError(const QString& error);87 bool setError(const QString& error);
8888
=== modified file 'src/document.cpp'
--- src/document.cpp 2013-04-23 15:17:24 +0000
+++ src/document.cpp 2015-01-06 10:46:20 +0000
@@ -152,8 +152,8 @@
152 m_create = create;152 m_create = create;
153 Q_EMIT createChanged(create);153 Q_EMIT createChanged(create);
154154
155 if (m_create && m_database && m_defaults.isValid() && !m_database->getDocUnchecked(m_docId).isValid())155 if (m_create && m_database && m_defaults.isValid() && !m_contents.isValid())
156 m_database->putDoc(m_defaults, m_docId);156 m_database->putDoc(m_defaults, m_docId, m_database->getReplicaUid() + ":1");
157}157}
158158
159QVariant159QVariant
@@ -178,8 +178,8 @@
178 m_defaults = defaults;178 m_defaults = defaults;
179 Q_EMIT defaultsChanged(defaults);179 Q_EMIT defaultsChanged(defaults);
180180
181 if (m_create && m_database && m_defaults.isValid() && !m_database->getDocUnchecked(m_docId).isValid())181 if (m_create && m_database && m_defaults.isValid() && !m_contents.isValid())
182 m_database->putDoc(m_defaults, m_docId);182 m_database->putDoc(m_defaults, m_docId, m_database->getReplicaUid() + ":1");
183}183}
184184
185QVariant185QVariant

Subscribers

People subscribed via source and target branches

to all changes: