This repository was archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathchangecontroller.cpp
260 lines (223 loc) · 8.1 KB
/
changecontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "changecontroller_p.h"
#include "datastore.h"
#include "setup_p.h"
#include "exchangeengine_p.h"
#include "synchelper_p.h"
#include "changeemitter_p.h"
using namespace QtDataSync;
#define QTDATASYNC_LOG QTDATASYNC_LOG_CONTROLLER
ChangeController::ChangeController(const Defaults &defaults, QObject *parent) :
Controller{"change", defaults, parent}
{}
void ChangeController::initialize(const QVariantHash ¶ms)
{
_store = params.value(QStringLiteral("store")).value<LocalStore*>();
Q_ASSERT_X(_store, Q_FUNC_INFO, "Missing parameter: store (LocalStore)");
_emitter = params.value(QStringLiteral("emitter")).value<ChangeEmitter*>();
Q_ASSERT_X(_emitter, Q_FUNC_INFO, "Missing parameter: emitter (ChangeEmitter)");
connect(_emitter, &ChangeEmitter::uploadNeeded,
this, &ChangeController::changeTriggered);
}
void ChangeController::setUploadingEnabled(bool uploading)
{
_uploadingEnabled = uploading;
logDebug() << "Change uploading enabled to" << uploading;
if(uploading)
uploadNext(true);
else {
endOp(); //stop timeouts
emit uploadingChanged(false);
}
}
void ChangeController::clearUploads()
{
setUploadingEnabled(false);
if(!_activeUploads.isEmpty())
logDebug() << "Finished uploading changes";
_activeUploads.clear();
_changeEstimate = 0;
}
void ChangeController::updateUploadLimit(quint32 limit)
{
logDebug() << "Updated update limit to:" << limit;
_uploadLimit = static_cast<int>(limit);
}
void ChangeController::uploadDone(const QByteArray &key)
{
if(!_activeUploads.contains(key)) {
logWarning() << "Unknown key completed:" << key.toHex();
return;
}
try {
auto info = _activeUploads.take(key);
_store->markUnchanged(info.key, info.version, info.isDelete);
_changeEstimate--;
emit progressIncrement();
logDebug() << "Completed upload. Marked"
<< info.key << "as unchanged ( Active uploads:"
<< _activeUploads.size() << ")";
if(_uploadingEnabled && _activeUploads.size() < _uploadLimit) //queued, so we may have the luck to complete a few more before uploading again
QMetaObject::invokeMethod(this, "uploadNext", Qt::QueuedConnection,
Q_ARG(bool, false));
} catch(Exception &e) {
logCritical() << "Failed to complete upload with error:" << e.what();
emit controllerError(tr("Failed to upload changes to server."));
}
}
void ChangeController::deviceUploadDone(const QByteArray &key, QUuid deviceId)
{
if(!_activeUploads.contains({key, deviceId})) {
logWarning() << "Unknown device key completed:" << key.toHex() << deviceId;
return;
}
try {
auto info = _activeUploads.take({key, deviceId});
_store->removeDeviceChange(info.key, deviceId);
_changeEstimate--;
emit progressIncrement();
logDebug() << "Completed device upload. Marked"
<< info.key << "for device" << deviceId << "as unchanged ( Active uploads:"
<< _activeUploads.size() << ")";
if(_uploadingEnabled && _activeUploads.size() < _uploadLimit) //queued, so we may have the luck to complete a few more before uploading again
QMetaObject::invokeMethod(this, "uploadNext", Qt::QueuedConnection,
Q_ARG(bool, false));
} catch(Exception &e) {
logCritical() << "Failed to complete upload with error:" << e.what();
emit controllerError(tr("Failed to upload changes to server."));
}
}
void ChangeController::changeTriggered()
{
if(_uploadingEnabled)
uploadNext(_activeUploads.isEmpty());
}
void ChangeController::uploadNext(bool emitStarted)
{
//uploads already exists: emit started no matter whether any are actually started from this call
if(emitStarted && !_activeUploads.isEmpty()) {
emitStarted = false;
logDebug() << "Beginning uploading changes";
emit uploadingChanged(true);
}
if(_activeUploads.size() >= _uploadLimit)
return;
try {
//update change estimate, if neccessary
auto emitProgress = false;
if(_changeEstimate == 0) {
_changeEstimate = _store->changeCount();
if(_changeEstimate > 0) {
if(emitStarted)
emitProgress = true;
else
emit progressAdded(_changeEstimate);
}
}
_store->loadChanges(_uploadLimit, [this, emitProgress, &emitStarted](const ObjectKey &objKey, quint64 version, const QString &file, QUuid deviceId) {
CachedObjectKey key(objKey, deviceId);
//skip stuff already beeing uploaded (could still have changed, but to prevent errors)
// auto skip = false;
if(_activeUploads.contains(key))
return true;
// for(const auto &mKey : _activeUploads.keys()) {
// if(key == mKey) {
// skip = true;
// break;
// }
// }
// if(skip)
// return true;
//signale that uploading has started
if(emitStarted) {
emitStarted = false;
logDebug() << "Beginning uploading changes";
emit uploadingChanged(true);
if(emitProgress)
emit progressAdded(_changeEstimate);
}
auto keyHash = key.hashed();
auto isDelete = file.isNull();
_activeUploads.insert(key, {key, version, isDelete});
beginOp(); //start the default timeout
if(isDelete) {//deleted
if(deviceId.isNull()) {
emit uploadChange(keyHash, SyncHelper::combine(key, version));
logDebug() << "Started upload of deleted" << key
<< "( Active uploads:" << _activeUploads.size() << ")";
} else {
emit uploadDeviceChange(keyHash, deviceId, SyncHelper::combine(key, version));
logDebug() << "Started device upload of deleted"
<< key << "for device" << deviceId
<< "( Active uploads:" << _activeUploads.size() << ")";
}
} else { //changed
try {
auto json = _store->readJson(key, file);
if(deviceId.isNull()) {
emit uploadChange(keyHash, SyncHelper::combine(key, version, json));
logDebug() << "Started upload of changed" << key
<< "( Active uploads:" << _activeUploads.size() << ")";
} else {
emit uploadDeviceChange(keyHash, deviceId, SyncHelper::combine(key, version, json));
logDebug() << "Started device upload of changed"
<< key << "for device" << deviceId
<< "( Active uploads:" << _activeUploads.size() << ")";
}
} catch (Exception &e) {
logWarning() << "Failed to read json for upload. Assuming unchanged. Error:" << e.what();
QMetaObject::invokeMethod(this, "uploadDone", Qt::QueuedConnection,
Q_ARG(QByteArray, keyHash));
}
}
return _activeUploads.size() < _uploadLimit; //only continue as long as there is free space
});
if(_activeUploads.isEmpty()) {
endOp(); //stop any timeouts
logDebug() << "Finished uploading changes";
emit uploadingChanged(false);
}
} catch(Exception &e) {
logCritical() << "Error when trying to upload change:" << e.what();
emit controllerError(tr("Failed to upload changes to server."));
}
}
ChangeController::ChangeInfo::ChangeInfo() = default;
ChangeController::ChangeInfo::ChangeInfo(ObjectKey key, quint64 version, QByteArray checksum) :
key{std::move(key)},
version{version},
checksum{std::move(checksum)}
{}
ChangeController::CachedObjectKey::CachedObjectKey() = default;
ChangeController::CachedObjectKey::CachedObjectKey(ObjectKey other, QUuid deviceId) :
ObjectKey{std::move(other)},
optionalDevice{deviceId}
{}
ChangeController::CachedObjectKey::CachedObjectKey(QByteArray hash, QUuid deviceId) :
ObjectKey{},
optionalDevice{deviceId},
_hash{std::move(hash)}
{}
QByteArray ChangeController::CachedObjectKey::hashed() const
{
if(_hash.isEmpty())
_hash = ObjectKey::hashed();
return _hash;
}
bool ChangeController::CachedObjectKey::operator==(const CachedObjectKey &other) const
{
if(!_hash.isEmpty() && !other._hash.isEmpty())
return _hash == other._hash && optionalDevice == other.optionalDevice;
else
return (static_cast<ObjectKey>(*this) == static_cast<ObjectKey>(other)) && optionalDevice == other.optionalDevice;
}
bool ChangeController::CachedObjectKey::operator!=(const CachedObjectKey &other) const
{
if(!_hash.isEmpty() && !other._hash.isEmpty())
return _hash != other._hash || optionalDevice != other.optionalDevice;
else
return (static_cast<ObjectKey>(*this) != static_cast<ObjectKey>(other)) || optionalDevice != other.optionalDevice;
}
uint QtDataSync::qHash(const ChangeController::CachedObjectKey &key, uint seed)
{
return qHash(key.hashed(), seed) ^ qHash(key.optionalDevice, seed);
}