diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-25 17:31:17 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-25 17:31:17 +0200 |
commit | 8f01eb530262d1442fc4fa0782a41e052412d43b (patch) | |
tree | c322ad9ddb30da51eccf71e774787c05fc3226f6 /common/genericresource.cpp | |
parent | 7ed3ecd282516296bbc7e4a2a24d1e3266bd8601 (diff) | |
download | sink-8f01eb530262d1442fc4fa0782a41e052412d43b.tar.gz sink-8f01eb530262d1442fc4fa0782a41e052412d43b.zip |
Handle all the remoteId updating and entity reading in the base-class.
Diffstat (limited to 'common/genericresource.cpp')
-rw-r--r-- | common/genericresource.cpp | 67 |
1 files changed, 66 insertions, 1 deletions
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 @@ | |||
19 | #include <QDataStream> | 19 | #include <QDataStream> |
20 | #include <QTime> | 20 | #include <QTime> |
21 | 21 | ||
22 | //This is the resources entity type, and not the domain type | ||
23 | #define ENTITY_TYPE_MAIL "mail" | ||
24 | #define ENTITY_TYPE_FOLDER "folder" | ||
25 | |||
22 | static int sBatchSize = 100; | 26 | static int sBatchSize = 100; |
23 | // This interval directly affects the roundtrip time of single commands | 27 | // This interval directly affects the roundtrip time of single commands |
24 | static int sCommitInterval = 10; | 28 | static int sCommitInterval = 10; |
@@ -392,11 +396,72 @@ void GenericResource::addType(const QByteArray &type, DomainTypeAdaptorFactoryIn | |||
392 | { | 396 | { |
393 | mPipeline->setPreprocessors(type, preprocessors); | 397 | mPipeline->setPreprocessors(type, preprocessors); |
394 | mPipeline->setAdaptorFactory(type, factory); | 398 | mPipeline->setAdaptorFactory(type, factory); |
399 | mAdaptorFactories.insert(type, factory); | ||
395 | } | 400 | } |
396 | 401 | ||
397 | KAsync::Job<void> GenericResource::replay(Sink::Storage &synchronizationStore, const QByteArray &type, const QByteArray &key, const QByteArray &value) | 402 | KAsync::Job<void> GenericResource::replay(Sink::Storage &synchronizationStore, const QByteArray &type, const QByteArray &key, const QByteArray &value) |
398 | { | 403 | { |
399 | return KAsync::null<void>(); | 404 | Sink::EntityBuffer buffer(value); |
405 | const Sink::Entity &entity = buffer.entity(); | ||
406 | const auto metadataBuffer = Sink::EntityBuffer::readBuffer<Sink::Metadata>(entity.metadata()); | ||
407 | Q_ASSERT(metadataBuffer); | ||
408 | if (!metadataBuffer->replayToSource()) { | ||
409 | Trace() << "Change is coming from the source"; | ||
410 | return KAsync::null<void>(); | ||
411 | } | ||
412 | const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
413 | const auto operation = metadataBuffer ? metadataBuffer->operation() : Sink::Operation_Creation; | ||
414 | const auto uid = Sink::Storage::uidFromKey(key); | ||
415 | QByteArray oldRemoteId; | ||
416 | |||
417 | if (operation != Sink::Operation_Creation) { | ||
418 | auto synchronizationTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadOnly); | ||
419 | oldRemoteId = resolveLocalId(type, uid, synchronizationTransaction); | ||
420 | } | ||
421 | Trace() << "Replaying " << key << type; | ||
422 | |||
423 | KAsync::Job<QByteArray> job = KAsync::null<QByteArray>(); | ||
424 | if (type == ENTITY_TYPE_FOLDER) { | ||
425 | const Sink::ApplicationDomain::Folder folder(mResourceInstanceIdentifier, uid, revision, mAdaptorFactories.value(type)->createAdaptor(entity)); | ||
426 | job = replay(folder, operation, oldRemoteId); | ||
427 | } else if (type == ENTITY_TYPE_MAIL) { | ||
428 | const Sink::ApplicationDomain::Mail mail(mResourceInstanceIdentifier, uid, revision, mAdaptorFactories.value(type)->createAdaptor(entity)); | ||
429 | job = replay(mail, operation, oldRemoteId); | ||
430 | } | ||
431 | |||
432 | return job.then<void, QByteArray>([=, &synchronizationStore](const QByteArray &remoteId) { | ||
433 | auto synchronizationTransaction = synchronizationStore.createTransaction(Sink::Storage::ReadWrite); | ||
434 | Trace() << "Replayed change with remote id: " << remoteId; | ||
435 | if (operation == Sink::Operation_Creation) { | ||
436 | if (remoteId.isEmpty()) { | ||
437 | Warning() << "Returned an empty remoteId from the creation"; | ||
438 | } else { | ||
439 | recordRemoteId(type, uid, remoteId, synchronizationTransaction); | ||
440 | } | ||
441 | } else if (operation == Sink::Operation_Modification) { | ||
442 | if (remoteId.isEmpty()) { | ||
443 | Warning() << "Returned an empty remoteId from the creation"; | ||
444 | } else { | ||
445 | updateRemoteId(type, uid, remoteId, synchronizationTransaction); | ||
446 | } | ||
447 | } else if (operation == Sink::Operation_Removal) { | ||
448 | removeRemoteId(type, uid, remoteId, synchronizationTransaction); | ||
449 | } else { | ||
450 | Warning() << "Unkown operation" << operation; | ||
451 | } | ||
452 | }, [](int errorCode, const QString &errorMessage) { | ||
453 | Warning() << "Failed to replay change: " << errorMessage; | ||
454 | }); | ||
455 | } | ||
456 | |||
457 | KAsync::Job<QByteArray> GenericResource::replay(const ApplicationDomain::Mail &, Sink::Operation, const QByteArray &) | ||
458 | { | ||
459 | return KAsync::null<QByteArray>(); | ||
460 | } | ||
461 | |||
462 | KAsync::Job<QByteArray> GenericResource::replay(const ApplicationDomain::Folder &, Sink::Operation, const QByteArray &) | ||
463 | { | ||
464 | return KAsync::null<QByteArray>(); | ||
400 | } | 465 | } |
401 | 466 | ||
402 | void GenericResource::removeDataFromDisk() | 467 | void GenericResource::removeDataFromDisk() |