summaryrefslogtreecommitdiffstats
path: root/examples/maildirresource/maildirresource.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-29 10:01:42 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-29 10:01:42 +0100
commit664fb0fbfd21a25d1f86938a186f6ec9cea6d882 (patch)
treed819fb4eba44da46fd9c9e9d5bd051e0124f68ee /examples/maildirresource/maildirresource.cpp
parent1998b8d35478205118cea5cc215c682b235434f1 (diff)
downloadsink-664fb0fbfd21a25d1f86938a186f6ec9cea6d882.tar.gz
sink-664fb0fbfd21a25d1f86938a186f6ec9cea6d882.zip
Mark commands with whether they need to be replayed
This way we don't have to try to figure out whether a change is coming from the source already.
Diffstat (limited to 'examples/maildirresource/maildirresource.cpp')
-rw-r--r--examples/maildirresource/maildirresource.cpp24
1 files changed, 13 insertions, 11 deletions
diff --git a/examples/maildirresource/maildirresource.cpp b/examples/maildirresource/maildirresource.cpp
index 8333f76..7fcf12e 100644
--- a/examples/maildirresource/maildirresource.cpp
+++ b/examples/maildirresource/maildirresource.cpp
@@ -132,6 +132,8 @@ QStringList MaildirResource::listAvailableFolders()
132 132
133static void createEntity(const QByteArray &akonadiId, const QByteArray &bufferType, const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<void(const QByteArray &)> callback) 133static void createEntity(const QByteArray &akonadiId, const QByteArray &bufferType, const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<void(const QByteArray &)> callback)
134{ 134{
135 //These changes are coming from the source
136 const auto replayToSource = false;
135 flatbuffers::FlatBufferBuilder entityFbb; 137 flatbuffers::FlatBufferBuilder entityFbb;
136 adaptorFactory.createBuffer(domainObject, entityFbb); 138 adaptorFactory.createBuffer(domainObject, entityFbb);
137 flatbuffers::FlatBufferBuilder fbb; 139 flatbuffers::FlatBufferBuilder fbb;
@@ -139,13 +141,15 @@ static void createEntity(const QByteArray &akonadiId, const QByteArray &bufferTy
139 auto entityId = fbb.CreateString(akonadiId.toStdString()); 141 auto entityId = fbb.CreateString(akonadiId.toStdString());
140 auto type = fbb.CreateString(bufferType.toStdString()); 142 auto type = fbb.CreateString(bufferType.toStdString());
141 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); 143 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize());
142 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, entityId, type, delta); 144 auto location = Akonadi2::Commands::CreateCreateEntity(fbb, entityId, type, delta, replayToSource);
143 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location); 145 Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);
144 callback(QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); 146 callback(QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize()));
145} 147}
146 148
147static void modifyEntity(const QByteArray &akonadiId, qint64 revision, const QByteArray &bufferType, const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<void(const QByteArray &)> callback) 149static void modifyEntity(const QByteArray &akonadiId, qint64 revision, const QByteArray &bufferType, const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, DomainTypeAdaptorFactoryInterface &adaptorFactory, std::function<void(const QByteArray &)> callback)
148{ 150{
151 //These changes are coming from the source
152 const auto replayToSource = false;
149 flatbuffers::FlatBufferBuilder entityFbb; 153 flatbuffers::FlatBufferBuilder entityFbb;
150 adaptorFactory.createBuffer(domainObject, entityFbb); 154 adaptorFactory.createBuffer(domainObject, entityFbb);
151 flatbuffers::FlatBufferBuilder fbb; 155 flatbuffers::FlatBufferBuilder fbb;
@@ -154,18 +158,20 @@ static void modifyEntity(const QByteArray &akonadiId, qint64 revision, const QBy
154 auto type = fbb.CreateString(bufferType.toStdString()); 158 auto type = fbb.CreateString(bufferType.toStdString());
155 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize()); 159 auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize());
156 //TODO removals 160 //TODO removals
157 auto location = Akonadi2::Commands::CreateModifyEntity(fbb, revision, entityId, 0, type, delta); 161 auto location = Akonadi2::Commands::CreateModifyEntity(fbb, revision, entityId, 0, type, delta, replayToSource);
158 Akonadi2::Commands::FinishModifyEntityBuffer(fbb, location); 162 Akonadi2::Commands::FinishModifyEntityBuffer(fbb, location);
159 callback(QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); 163 callback(QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize()));
160} 164}
161 165
162static void deleteEntity(const QByteArray &akonadiId, qint64 revision, const QByteArray &bufferType, std::function<void(const QByteArray &)> callback) 166static void deleteEntity(const QByteArray &akonadiId, qint64 revision, const QByteArray &bufferType, std::function<void(const QByteArray &)> callback)
163{ 167{
168 //These changes are coming from the source
169 const auto replayToSource = false;
164 flatbuffers::FlatBufferBuilder fbb; 170 flatbuffers::FlatBufferBuilder fbb;
165 auto entityId = fbb.CreateString(akonadiId.toStdString()); 171 auto entityId = fbb.CreateString(akonadiId.toStdString());
166 //This is the resource type and not the domain type 172 //This is the resource type and not the domain type
167 auto type = fbb.CreateString(bufferType.toStdString()); 173 auto type = fbb.CreateString(bufferType.toStdString());
168 auto location = Akonadi2::Commands::CreateDeleteEntity(fbb, revision, entityId, type); 174 auto location = Akonadi2::Commands::CreateDeleteEntity(fbb, revision, entityId, type, replayToSource);
169 Akonadi2::Commands::FinishDeleteEntityBuffer(fbb, location); 175 Akonadi2::Commands::FinishDeleteEntityBuffer(fbb, location);
170 callback(QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize())); 176 callback(QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize()));
171} 177}
@@ -365,23 +371,19 @@ KAsync::Job<void> MaildirResource::replay(const QByteArray &type, const QByteArr
365 //This results in a deadlock during sync 371 //This results in a deadlock during sync
366 Akonadi2::Storage store(Akonadi2::storageLocation(), mResourceInstanceIdentifier + ".synchronization", Akonadi2::Storage::ReadWrite); 372 Akonadi2::Storage store(Akonadi2::storageLocation(), mResourceInstanceIdentifier + ".synchronization", Akonadi2::Storage::ReadWrite);
367 auto synchronizationTransaction = store.createTransaction(Akonadi2::Storage::ReadWrite); 373 auto synchronizationTransaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
368 const auto uid = Akonadi2::Storage::uidFromKey(key);
369 const auto remoteId = resolveLocalId(type, uid, synchronizationTransaction);
370 374
371 Trace() << "Replaying " << key << type; 375 Trace() << "Replaying " << key << type;
372 if (type == ENTITY_TYPE_FOLDER) { 376 if (type == ENTITY_TYPE_FOLDER) {
373 Akonadi2::EntityBuffer buffer(value.data(), value.size()); 377 Akonadi2::EntityBuffer buffer(value.data(), value.size());
374 const Akonadi2::Entity &entity = buffer.entity(); 378 const Akonadi2::Entity &entity = buffer.entity();
375 const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata()); 379 const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata());
380 if (metadataBuffer && !metadataBuffer->replayToSource()) {
381 Trace() << "Change is coming from the source";
382 return KAsync::null<void>();
383 }
376 const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; 384 const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1;
377 const auto operation = metadataBuffer ? metadataBuffer->operation() : Akonadi2::Operation_Creation; 385 const auto operation = metadataBuffer ? metadataBuffer->operation() : Akonadi2::Operation_Creation;
378 if (operation == Akonadi2::Operation_Creation) { 386 if (operation == Akonadi2::Operation_Creation) {
379 //FIXME: This check only works for new entities
380 //Figure out wether we have replayed that revision already to the source
381 if (!remoteId.isEmpty()) {
382 Trace() << "Change is coming from the source";
383 return KAsync::null<void>();
384 }
385 const Akonadi2::ApplicationDomain::Folder folder(mResourceInstanceIdentifier, Akonadi2::Storage::uidFromKey(key), revision, mFolderAdaptorFactory->createAdaptor(entity)); 387 const Akonadi2::ApplicationDomain::Folder folder(mResourceInstanceIdentifier, Akonadi2::Storage::uidFromKey(key), revision, mFolderAdaptorFactory->createAdaptor(entity));
386 auto folderName = folder.getProperty("name").toString(); 388 auto folderName = folder.getProperty("name").toString();
387 //TODO handle non toplevel folders 389 //TODO handle non toplevel folders