diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-08-30 00:48:01 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-09-15 16:14:19 +0200 |
commit | 6b09016f5d9119729265211db6cc91f991fc3e28 (patch) | |
tree | 887e7f8e5db3213cedc2cd4ddf8165212220be00 /common/synchronizer.cpp | |
parent | 4cd598035dcc297ad3a3af16fb5eda218c018a16 (diff) | |
download | sink-6b09016f5d9119729265211db6cc91f991fc3e28.tar.gz sink-6b09016f5d9119729265211db6cc91f991fc3e28.zip |
Don' create new entities on flag change.
Diffstat (limited to 'common/synchronizer.cpp')
-rw-r--r-- | common/synchronizer.cpp | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/common/synchronizer.cpp b/common/synchronizer.cpp index 15a06e7..8de45a9 100644 --- a/common/synchronizer.cpp +++ b/common/synchronizer.cpp | |||
@@ -143,36 +143,45 @@ void Synchronizer::scanForRemovals(const QByteArray &bufferType, const std::func | |||
143 | }); | 143 | }); |
144 | } | 144 | } |
145 | 145 | ||
146 | void Synchronizer::modify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity) | ||
147 | { | ||
148 | auto mainDatabase = Storage::mainDatabase(transaction(), bufferType); | ||
149 | const auto sinkId = syncStore().resolveRemoteId(bufferType, remoteId); | ||
150 | auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType); | ||
151 | Q_ASSERT(adaptorFactory); | ||
152 | qint64 retrievedRevision = 0; | ||
153 | if (auto current = EntityReaderUtils::getLatest(mainDatabase, sinkId, *adaptorFactory, retrievedRevision)) { | ||
154 | bool changed = false; | ||
155 | for (const auto &property : entity.changedProperties()) { | ||
156 | if (entity.getProperty(property) != current->getProperty(property)) { | ||
157 | SinkTrace() << "Property changed " << sinkId << property; | ||
158 | changed = true; | ||
159 | } | ||
160 | } | ||
161 | if (changed) { | ||
162 | SinkTrace() << "Found a modified entity: " << remoteId; | ||
163 | modifyEntity(sinkId, Sink::Storage::maxRevision(transaction()), bufferType, entity, *adaptorFactory, | ||
164 | [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::ModifyEntityCommand, buffer); }); | ||
165 | } | ||
166 | } else { | ||
167 | SinkWarning() << "Failed to get current entity"; | ||
168 | } | ||
169 | } | ||
170 | |||
146 | void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity) | 171 | void Synchronizer::createOrModify(const QByteArray &bufferType, const QByteArray &remoteId, const Sink::ApplicationDomain::ApplicationDomainType &entity) |
147 | { | 172 | { |
148 | SinkTrace() << "Create or modify" << bufferType << remoteId; | 173 | SinkTrace() << "Create or modify" << bufferType << remoteId; |
149 | auto mainDatabase = Storage::mainDatabase(transaction(), bufferType); | 174 | auto mainDatabase = Storage::mainDatabase(transaction(), bufferType); |
150 | const auto sinkId = syncStore().resolveRemoteId(bufferType, remoteId); | 175 | const auto sinkId = syncStore().resolveRemoteId(bufferType, remoteId); |
151 | const auto found = mainDatabase.contains(sinkId); | 176 | const auto found = mainDatabase.contains(sinkId); |
152 | auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType); | ||
153 | Q_ASSERT(adaptorFactory); | ||
154 | if (!found) { | 177 | if (!found) { |
155 | SinkTrace() << "Found a new entity: " << remoteId; | 178 | SinkTrace() << "Found a new entity: " << remoteId; |
179 | auto adaptorFactory = Sink::AdaptorFactoryRegistry::instance().getFactory(mResourceType, bufferType); | ||
180 | Q_ASSERT(adaptorFactory); | ||
156 | createEntity( | 181 | createEntity( |
157 | sinkId, bufferType, entity, *adaptorFactory, [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::CreateEntityCommand, buffer); }); | 182 | sinkId, bufferType, entity, *adaptorFactory, [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::CreateEntityCommand, buffer); }); |
158 | } else { // modification | 183 | } else { // modification |
159 | qint64 retrievedRevision = 0; | 184 | modify(bufferType, remoteId, entity); |
160 | if (auto current = EntityReaderUtils::getLatest(mainDatabase, sinkId, *adaptorFactory, retrievedRevision)) { | ||
161 | bool changed = false; | ||
162 | for (const auto &property : entity.changedProperties()) { | ||
163 | if (entity.getProperty(property) != current->getProperty(property)) { | ||
164 | SinkTrace() << "Property changed " << sinkId << property; | ||
165 | changed = true; | ||
166 | } | ||
167 | } | ||
168 | if (changed) { | ||
169 | SinkTrace() << "Found a modified entity: " << remoteId; | ||
170 | modifyEntity(sinkId, Sink::Storage::maxRevision(transaction()), bufferType, entity, *adaptorFactory, | ||
171 | [this](const QByteArray &buffer) { enqueueCommand(Sink::Commands::ModifyEntityCommand, buffer); }); | ||
172 | } | ||
173 | } else { | ||
174 | SinkWarning() << "Failed to get current entity"; | ||
175 | } | ||
176 | } | 185 | } |
177 | } | 186 | } |
178 | 187 | ||