diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-11-21 23:13:38 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-11-21 23:51:24 +0100 |
commit | 1d713d9e2dbaf27de9da087f9270d260dfc40c31 (patch) | |
tree | 666d8edd42e44df3eaa674a35b6e938b99c2f4b4 /common/sourcewriteback.cpp | |
parent | 0adba61a00491b96dadaa6d4719cb46831356222 (diff) | |
download | sink-1d713d9e2dbaf27de9da087f9270d260dfc40c31.tar.gz sink-1d713d9e2dbaf27de9da087f9270d260dfc40c31.zip |
Folded the SourceWriteback into the Synchronizer.
By concentrating all communication to the source in one place we get rid
of several oddities.
* Quite a bit of duplication since both need access to the
synchronizationStore and the source.
* We currently have an akward locking in place because both classes
access the ync store. This is not easier to resolve cleanly.
* The live of resource implementers becomes easier.
* An implementation could elect to not use changereplay and always do a
full sync... (maybe?)
Diffstat (limited to 'common/sourcewriteback.cpp')
-rw-r--r-- | common/sourcewriteback.cpp | 146 |
1 files changed, 0 insertions, 146 deletions
diff --git a/common/sourcewriteback.cpp b/common/sourcewriteback.cpp deleted file mode 100644 index e2994d1..0000000 --- a/common/sourcewriteback.cpp +++ /dev/null | |||
@@ -1,146 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #include "sourcewriteback.h" | ||
21 | |||
22 | #include "definitions.h" | ||
23 | #include "log.h" | ||
24 | #include "bufferutils.h" | ||
25 | #include "entitybuffer.h" | ||
26 | #include "entity_generated.h" | ||
27 | |||
28 | #define ENTITY_TYPE_MAIL "mail" | ||
29 | #define ENTITY_TYPE_FOLDER "folder" | ||
30 | |||
31 | SINK_DEBUG_AREA("sourcewriteback") | ||
32 | |||
33 | using namespace Sink; | ||
34 | |||
35 | SourceWriteBack::SourceWriteBack(const ResourceContext &context) | ||
36 | : ChangeReplay(context), | ||
37 | mResourceContext(context), | ||
38 | mSyncStorage(Sink::storageLocation(), context.instanceId() + ".synchronization", Sink::Storage::DataStore::ReadWrite), | ||
39 | mEntityStore(QSharedPointer<Storage::EntityStore>::create(mResourceContext)) | ||
40 | { | ||
41 | |||
42 | } | ||
43 | |||
44 | Storage::EntityStore &SourceWriteBack::store() | ||
45 | { | ||
46 | return *mEntityStore; | ||
47 | } | ||
48 | |||
49 | RemoteIdMap &SourceWriteBack::syncStore() | ||
50 | { | ||
51 | if (!mSyncStore) { | ||
52 | mSyncStore = QSharedPointer<RemoteIdMap>::create(mSyncTransaction); | ||
53 | } | ||
54 | return *mSyncStore; | ||
55 | } | ||
56 | |||
57 | bool SourceWriteBack::canReplay(const QByteArray &type, const QByteArray &key, const QByteArray &value) | ||
58 | { | ||
59 | Sink::EntityBuffer buffer(value); | ||
60 | const Sink::Entity &entity = buffer.entity(); | ||
61 | const auto metadataBuffer = Sink::EntityBuffer::readBuffer<Sink::Metadata>(entity.metadata()); | ||
62 | Q_ASSERT(metadataBuffer); | ||
63 | if (!metadataBuffer->replayToSource()) { | ||
64 | SinkTrace() << "Change is coming from the source"; | ||
65 | } | ||
66 | return metadataBuffer->replayToSource(); | ||
67 | } | ||
68 | |||
69 | KAsync::Job<void> SourceWriteBack::replay(const QByteArray &type, const QByteArray &key, const QByteArray &value) | ||
70 | { | ||
71 | SinkTrace() << "Replaying" << type << key; | ||
72 | |||
73 | Sink::EntityBuffer buffer(value); | ||
74 | const Sink::Entity &entity = buffer.entity(); | ||
75 | const auto metadataBuffer = Sink::EntityBuffer::readBuffer<Sink::Metadata>(entity.metadata()); | ||
76 | Q_ASSERT(metadataBuffer); | ||
77 | Q_ASSERT(!mSyncStore); | ||
78 | Q_ASSERT(!mSyncTransaction); | ||
79 | mEntityStore->startTransaction(Storage::DataStore::ReadOnly); | ||
80 | mSyncTransaction = mSyncStorage.createTransaction(Sink::Storage::DataStore::ReadWrite); | ||
81 | |||
82 | // const qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
83 | const auto operation = metadataBuffer ? metadataBuffer->operation() : Sink::Operation_Creation; | ||
84 | const auto uid = Sink::Storage::DataStore::uidFromKey(key); | ||
85 | const auto modifiedProperties = metadataBuffer->modifiedProperties() ? BufferUtils::fromVector(*metadataBuffer->modifiedProperties()) : QByteArrayList(); | ||
86 | QByteArray oldRemoteId; | ||
87 | |||
88 | if (operation != Sink::Operation_Creation) { | ||
89 | oldRemoteId = syncStore().resolveLocalId(type, uid); | ||
90 | if (oldRemoteId.isEmpty()) { | ||
91 | SinkWarning() << "Couldn't find the remote id for: " << type << uid; | ||
92 | return KAsync::error<void>(1, "Couldn't find the remote id."); | ||
93 | } | ||
94 | } | ||
95 | SinkTrace() << "Replaying " << key << type << uid << oldRemoteId; | ||
96 | |||
97 | KAsync::Job<QByteArray> job = KAsync::null<QByteArray>(); | ||
98 | if (type == ENTITY_TYPE_FOLDER) { | ||
99 | auto folder = store().readEntity<ApplicationDomain::Folder>(key); | ||
100 | job = replay(folder, operation, oldRemoteId, modifiedProperties); | ||
101 | } else if (type == ENTITY_TYPE_MAIL) { | ||
102 | auto mail = store().readEntity<ApplicationDomain::Mail>(key); | ||
103 | job = replay(mail, operation, oldRemoteId, modifiedProperties); | ||
104 | } | ||
105 | |||
106 | return job.syncThen<void, QByteArray>([this, operation, type, uid, oldRemoteId](const QByteArray &remoteId) { | ||
107 | if (operation == Sink::Operation_Creation) { | ||
108 | SinkTrace() << "Replayed creation with remote id: " << remoteId; | ||
109 | if (remoteId.isEmpty()) { | ||
110 | SinkWarning() << "Returned an empty remoteId from the creation"; | ||
111 | } else { | ||
112 | syncStore().recordRemoteId(type, uid, remoteId); | ||
113 | } | ||
114 | } else if (operation == Sink::Operation_Modification) { | ||
115 | SinkTrace() << "Replayed modification with remote id: " << remoteId; | ||
116 | if (remoteId.isEmpty()) { | ||
117 | SinkWarning() << "Returned an empty remoteId from the creation"; | ||
118 | } else { | ||
119 | syncStore().updateRemoteId(type, uid, remoteId); | ||
120 | } | ||
121 | } else if (operation == Sink::Operation_Removal) { | ||
122 | SinkTrace() << "Replayed removal with remote id: " << oldRemoteId; | ||
123 | syncStore().removeRemoteId(type, uid, oldRemoteId); | ||
124 | } else { | ||
125 | SinkError() << "Unkown operation" << operation; | ||
126 | } | ||
127 | }) | ||
128 | .syncThen<void>([this](const KAsync::Error &error) { | ||
129 | if (error) { | ||
130 | SinkWarning() << "Failed to replay change: " << error.errorMessage; | ||
131 | } | ||
132 | mSyncStore.clear(); | ||
133 | mSyncTransaction.commit(); | ||
134 | mEntityStore->abortTransaction(); | ||
135 | }); | ||
136 | } | ||
137 | |||
138 | KAsync::Job<QByteArray> SourceWriteBack::replay(const ApplicationDomain::Mail &, Sink::Operation, const QByteArray &, const QList<QByteArray> &) | ||
139 | { | ||
140 | return KAsync::null<QByteArray>(); | ||
141 | } | ||
142 | |||
143 | KAsync::Job<QByteArray> SourceWriteBack::replay(const ApplicationDomain::Folder &, Sink::Operation, const QByteArray &, const QList<QByteArray> &) | ||
144 | { | ||
145 | return KAsync::null<QByteArray>(); | ||
146 | } | ||