From 8f01eb530262d1442fc4fa0782a41e052412d43b Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 25 May 2016 17:31:17 +0200 Subject: Handle all the remoteId updating and entity reading in the base-class. --- common/genericresource.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++- common/genericresource.h | 8 ++++++ 2 files changed, 74 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/genericresource.cpp b/common/genericresource.cpp index 637e371..eae6ead 100644 --- a/common/genericresource.cpp +++ b/common/genericresource.cpp @@ -19,6 +19,10 @@ #include #include +//This is the resources entity type, and not the domain type +#define ENTITY_TYPE_MAIL "mail" +#define ENTITY_TYPE_FOLDER "folder" + static int sBatchSize = 100; // This interval directly affects the roundtrip time of single commands static int sCommitInterval = 10; @@ -392,11 +396,72 @@ void GenericResource::addType(const QByteArray &type, DomainTypeAdaptorFactoryIn { mPipeline->setPreprocessors(type, preprocessors); mPipeline->setAdaptorFactory(type, factory); + mAdaptorFactories.insert(type, factory); } KAsync::Job GenericResource::replay(Sink::Storage &synchronizationStore, const QByteArray &type, const QByteArray &key, const QByteArray &value) { - return KAsync::null(); + Sink::EntityBuffer buffer(value); + const Sink::Entity &entity = buffer.entity(); + const auto metadataBuffer = Sink::EntityBuffer::readBuffer(entity.metadata()); + Q_ASSERT(metadataBuffer); + if (!metadataBuffer->replayToSource()) { + Trace() << "Change is coming from the source"; + return KAsync::null(); + } + const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; + const auto operation = metadataBuffer ? metadataBuffer->operation() : Sink::Operation_Creation; + const auto uid = Sink::Storage::uidFromKey(key); + QByteArray oldRemoteId; + + if (operation != Sink::Operation_Creation) { + auto synchronizationTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadOnly); + oldRemoteId = resolveLocalId(type, uid, synchronizationTransaction); + } + Trace() << "Replaying " << key << type; + + KAsync::Job job = KAsync::null(); + if (type == ENTITY_TYPE_FOLDER) { + const Sink::ApplicationDomain::Folder folder(mResourceInstanceIdentifier, uid, revision, mAdaptorFactories.value(type)->createAdaptor(entity)); + job = replay(folder, operation, oldRemoteId); + } else if (type == ENTITY_TYPE_MAIL) { + const Sink::ApplicationDomain::Mail mail(mResourceInstanceIdentifier, uid, revision, mAdaptorFactories.value(type)->createAdaptor(entity)); + job = replay(mail, operation, oldRemoteId); + } + + return job.then([=, &synchronizationStore](const QByteArray &remoteId) { + auto synchronizationTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadWrite); + Trace() << "Replayed change with remote id: " << remoteId; + if (operation == Sink::Operation_Creation) { + if (remoteId.isEmpty()) { + Warning() << "Returned an empty remoteId from the creation"; + } else { + recordRemoteId(type, uid, remoteId, synchronizationTransaction); + } + } else if (operation == Sink::Operation_Modification) { + if (remoteId.isEmpty()) { + Warning() << "Returned an empty remoteId from the creation"; + } else { + updateRemoteId(type, uid, remoteId, synchronizationTransaction); + } + } else if (operation == Sink::Operation_Removal) { + removeRemoteId(type, uid, remoteId, synchronizationTransaction); + } else { + Warning() << "Unkown operation" << operation; + } + }, [](int errorCode, const QString &errorMessage) { + Warning() << "Failed to replay change: " << errorMessage; + }); +} + +KAsync::Job GenericResource::replay(const ApplicationDomain::Mail &, Sink::Operation, const QByteArray &) +{ + return KAsync::null(); +} + +KAsync::Job GenericResource::replay(const ApplicationDomain::Folder &, Sink::Operation, const QByteArray &) +{ + return KAsync::null(); } void GenericResource::removeDataFromDisk() diff --git a/common/genericresource.h b/common/genericresource.h index 9582f06..c551e29 100644 --- a/common/genericresource.h +++ b/common/genericresource.h @@ -61,8 +61,15 @@ private slots: protected: void enableChangeReplay(bool); + void addType(const QByteArray &type, DomainTypeAdaptorFactoryInterface::Ptr factory, const QVector &preprocessors); + + ///Base implementation call the replay$Type calls virtual KAsync::Job replay(Sink::Storage &synchronizationStore, const QByteArray &type, const QByteArray &key, const QByteArray &value); + ///Implement to write back changes to the server + virtual KAsync::Job replay(const Sink::ApplicationDomain::Mail &, Sink::Operation, const QByteArray &oldRemoteId); + virtual KAsync::Job replay(const Sink::ApplicationDomain::Folder &, Sink::Operation, const QByteArray &oldRemoteId); + void onProcessorError(int errorCode, const QString &errorMessage); void enqueueCommand(MessageQueue &mq, int commandId, const QByteArray &data); @@ -127,5 +134,6 @@ private: int mError; QTimer mCommitQueueTimer; qint64 mClientLowerBoundRevision; + QHash mAdaptorFactories; }; } -- cgit v1.2.3