From da4b74e593f1b1262e83824cc499bd855f1b4e3e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 26 Jun 2018 10:24:58 +0200 Subject: Avoid overwriting local changes with remote modifications. The case we ran into is the following: * Fetching the full payload and marking all messages of a thread as read happens simultaneously. * The local modification to mark as read gets immediately overwritten when the full payload arrives. * Eventually the modification gets replayed to the server though (and the reversal isn't because coming from the source), so on next sync the situation fixes itself. To be able to improve this we try to protect local modifications in that properties that have been modified since baseRevision (which currently isn't, but should be equal to the last to the server replayed revision) are not overwritten. This conflict resolution strategy thus always prefers local modifications. baseRevision is currently set to the current maximum revision of the store at the time when the resource creates the modification. --- common/storage/entitystore.cpp | 32 ++++++++++++++++++++++++++------ common/storage/entitystore.h | 4 +++- 2 files changed, 29 insertions(+), 7 deletions(-) (limited to 'common/storage') diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index c5b5ffc..dd6bbf0 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp @@ -247,22 +247,26 @@ bool EntityStore::add(const QByteArray &type, ApplicationDomainType entity, bool return true; } -ApplicationDomain::ApplicationDomainType EntityStore::applyDiff(const QByteArray &type, const ApplicationDomainType ¤t, const ApplicationDomainType &diff, const QByteArrayList &deletions) const +ApplicationDomain::ApplicationDomainType EntityStore::applyDiff(const QByteArray &type, const ApplicationDomainType ¤t, const ApplicationDomainType &diff, const QByteArrayList &deletions, const QSet &excludeProperties) const { - SinkTraceCtx(d->logCtx) << "Applying diff: " << current.availableProperties() << "Deletions: " << deletions << "Changeset: " << diff.changedProperties(); + SinkTraceCtx(d->logCtx) << "Applying diff: " << current.availableProperties() << "Deletions: " << deletions << "Changeset: " << diff.changedProperties() << "Excluded: " << excludeProperties; auto newEntity = *ApplicationDomainType::getInMemoryRepresentation(current, current.availableProperties()); // Apply diff for (const auto &property : diff.changedProperties()) { - const auto value = diff.getProperty(property); - if (value.isValid()) { - newEntity.setProperty(property, value); + if (!excludeProperties.contains(property)) { + const auto value = diff.getProperty(property); + if (value.isValid()) { + newEntity.setProperty(property, value); + } } } // Remove deletions for (const auto &property : deletions) { - newEntity.setProperty(property, QVariant()); + if (!excludeProperties.contains(property)) { + newEntity.setProperty(property, QVariant()); + } } return newEntity; } @@ -639,6 +643,22 @@ bool EntityStore::exists(const QByteArray &type, const QByteArray &uid) return true; } +void EntityStore::readRevisions(const QByteArray &type, const QByteArray &uid, qint64 startingRevision, const std::function callback) +{ + Q_ASSERT(d); + Q_ASSERT(!uid.isEmpty()); + DataStore::mainDatabase(d->transaction, type) + .scan(uid, + [&](const QByteArray &key, const QByteArray &value) -> bool { + const auto revision = DataStore::revisionFromKey(key); + if (revision >= startingRevision) { + callback(DataStore::uidFromKey(key), revision, Sink::EntityBuffer(value.data(), value.size())); + } + return true; + }, + [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error while reading: " << error.message; }, true); + +} qint64 EntityStore::maxRevision() { diff --git a/common/storage/entitystore.h b/common/storage/entitystore.h index ffa70b9..69de76c 100644 --- a/common/storage/entitystore.h +++ b/common/storage/entitystore.h @@ -49,7 +49,7 @@ public: bool modify(const QByteArray &type, const ApplicationDomainType ¤t, ApplicationDomainType newEntity, bool replayToSource); bool remove(const QByteArray &type, const ApplicationDomainType ¤t, bool replayToSource); bool cleanupRevisions(qint64 revision); - ApplicationDomainType applyDiff(const QByteArray &type, const ApplicationDomainType ¤t, const ApplicationDomainType &diff, const QByteArrayList &deletions) const; + ApplicationDomainType applyDiff(const QByteArray &type, const ApplicationDomainType ¤t, const ApplicationDomainType &diff, const QByteArrayList &deletions, const QSet &excludeProperties = {}) const; void startTransaction(Sink::Storage::DataStore::AccessMode); void commitTransaction(); @@ -122,6 +122,8 @@ public: ///Db contains entity and entity is not yet removed bool exists(const QByteArray &type, const QByteArray &uid); + void readRevisions(const QByteArray &type, const QByteArray &uid, qint64 baseRevision, const std::function callback); + qint64 maxRevision(); Sink::Log::Context logContext() const; -- cgit v1.2.3