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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
/*
* Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "store.h"
#include <QTime>
#include <QAbstractItemModel>
#include <functional>
#include <memory>
#include "resourceaccess.h"
#include "commands.h"
#include "resourcefacade.h"
#include "definitions.h"
#include "resourceconfig.h"
#include "facadefactory.h"
#include "modelresult.h"
#include "storage.h"
#include "log.h"
#include "utils.h"
#define ASSERT_ENUMS_MATCH(A, B) Q_STATIC_ASSERT_X(static_cast<int>(A) == static_cast<int>(B), "The enum values must match");
//Ensure the copied enum matches
typedef ModelResult<Sink::ApplicationDomain::Mail, Sink::ApplicationDomain::Mail::Ptr> MailModelResult;
ASSERT_ENUMS_MATCH(Sink::Store::DomainObjectBaseRole, MailModelResult::DomainObjectBaseRole)
ASSERT_ENUMS_MATCH(Sink::Store::ChildrenFetchedRole, MailModelResult::ChildrenFetchedRole)
ASSERT_ENUMS_MATCH(Sink::Store::DomainObjectRole, MailModelResult::DomainObjectRole)
ASSERT_ENUMS_MATCH(Sink::Store::StatusRole, MailModelResult::StatusRole)
ASSERT_ENUMS_MATCH(Sink::Store::WarningRole, MailModelResult::WarningRole)
ASSERT_ENUMS_MATCH(Sink::Store::ProgressRole, MailModelResult::ProgressRole)
Q_DECLARE_METATYPE(QSharedPointer<Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>>)
Q_DECLARE_METATYPE(QSharedPointer<Sink::ResourceAccessInterface>);
Q_DECLARE_METATYPE(std::shared_ptr<void>);
static bool sanityCheckQuery(const Sink::Query &query)
{
for (const auto &id : query.ids()) {
if (id.isEmpty()) {
SinkError() << "Empty id in query.";
return false;
}
}
return true;
}
namespace Sink {
QString Store::storageLocation()
{
return Sink::storageLocation();
}
template <class DomainType>
KAsync::Job<void> queryResource(const QByteArray resourceType, const QByteArray &resourceInstanceIdentifier, const Query &query, typename AggregatingResultEmitter<typename DomainType::Ptr>::Ptr aggregatingEmitter, const Sink::Log::Context &ctx_)
{
auto ctx = ctx_.subContext(resourceInstanceIdentifier);
auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier);
if (facade) {
SinkTraceCtx(ctx) << "Trying to fetch from resource " << resourceInstanceIdentifier;
auto result = facade->load(query, ctx);
if (result.second) {
aggregatingEmitter->addEmitter(result.second);
} else {
SinkWarningCtx(ctx) << "Null emitter for resource " << resourceInstanceIdentifier;
}
return result.first;
} else {
SinkTraceCtx(ctx) << "Couldn' find a facade for " << resourceInstanceIdentifier;
// Ignore the error and carry on
return KAsync::null<void>();
}
}
template <class DomainType>
QPair<typename AggregatingResultEmitter<typename DomainType::Ptr>::Ptr, typename ResultEmitter<typename ApplicationDomain::SinkResource::Ptr>::Ptr> getEmitter(Query query, const Log::Context &ctx)
{
query.setType(ApplicationDomain::getTypeName<DomainType>());
SinkTraceCtx(ctx) << "Query: " << query;
// Query all resources and aggregate results
auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create();
if (ApplicationDomain::isGlobalType(ApplicationDomain::getTypeName<DomainType>())) {
//For global types we don't need to query for the resources first.
queryResource<DomainType>("", "", query, aggregatingEmitter, ctx).exec();
} else {
auto resourceCtx = ctx.subContext("resourceQuery");
auto facade = FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>();
Q_ASSERT(facade);
Sink::Query resourceQuery;
resourceQuery.request<ApplicationDomain::SinkResource::Capabilities>();
if (query.liveQuery()) {
SinkTraceCtx(ctx) << "Listening for new resources.";
resourceQuery.setFlags(Query::LiveQuery);
}
//Filter resources by available content types (unless the query already specifies a capability filter)
auto resourceFilter = query.getResourceFilter();
if (!resourceFilter.propertyFilter.contains(ApplicationDomain::SinkResource::Capabilities::name)) {
resourceFilter.propertyFilter.insert(ApplicationDomain::SinkResource::Capabilities::name, Query::Comparator{ApplicationDomain::getTypeName<DomainType>(), Query::Comparator::Contains});
}
resourceQuery.setFilter(resourceFilter);
resourceQuery.requestedProperties << resourceFilter.propertyFilter.keys();
auto result = facade->load(resourceQuery, resourceCtx);
auto emitter = result.second;
emitter->onAdded([=](const ApplicationDomain::SinkResource::Ptr &resource) {
SinkTraceCtx(resourceCtx) << "Found new resources: " << resource->identifier();
const auto resourceType = ResourceConfig::getResourceType(resource->identifier());
Q_ASSERT(!resourceType.isEmpty());
queryResource<DomainType>(resourceType, resource->identifier(), query, aggregatingEmitter, ctx).exec();
});
emitter->onComplete([query, aggregatingEmitter, resourceCtx]() {
SinkTraceCtx(resourceCtx) << "Resource query complete";
});
return qMakePair(aggregatingEmitter, emitter);
}
return qMakePair(aggregatingEmitter, ResultEmitter<typename ApplicationDomain::SinkResource::Ptr>::Ptr{});
}
static Log::Context getQueryContext(const Sink::Query &query, const QByteArray &type)
{
if (!query.id().isEmpty()) {
return Log::Context{"query." + type + "." + query.id()};
}
return Log::Context{"query." + type};
}
template <class DomainType>
QSharedPointer<QAbstractItemModel> Store::loadModel(const Query &query)
{
Q_ASSERT(sanityCheckQuery(query));
auto ctx = getQueryContext(query, ApplicationDomain::getTypeName<DomainType>());
auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties, ctx);
//* Client defines lifetime of model
//* The model lifetime defines the duration of live-queries
//* The facade needs to life for the duration of any calls being made (assuming we get rid of any internal callbacks
//* The emitter needs to live or the duration of query (respectively, the model)
//* The result provider needs to live for as long as results are provided (until the last thread exits).
auto result = getEmitter<DomainType>(query, ctx);
model->setEmitter(result.first);
//Keep the emitter alive
if (auto resourceEmitter = result.second) {
model->setProperty("resourceEmitter", QVariant::fromValue(resourceEmitter)); //TODO only neceesary for live queries
resourceEmitter->fetch();
}
//Automatically populate the top-level
model->fetchMore(QModelIndex());
return model;
}
template <class DomainType>
static std::shared_ptr<StoreFacade<DomainType>> getFacade(const QByteArray &resourceInstanceIdentifier)
{
if (ApplicationDomain::isGlobalType(ApplicationDomain::getTypeName<DomainType>())) {
if (auto facade = FacadeFactory::instance().getFacade<DomainType>()) {
return facade;
}
}
if (auto facade = FacadeFactory::instance().getFacade<DomainType>(ResourceConfig::getResourceType(resourceInstanceIdentifier), resourceInstanceIdentifier)) {
return facade;
}
return std::make_shared<NullFacade<DomainType>>();
}
template <class DomainType>
KAsync::Job<void> Store::create(const DomainType &domainObject)
{
SinkLog() << "Create: " << domainObject;
auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
return facade->create(domainObject).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to create " << error; });
}
template <class DomainType>
KAsync::Job<void> Store::modify(const DomainType &domainObject)
{
if (domainObject.changedProperties().isEmpty()) {
SinkLog() << "Nothing to modify: " << domainObject.identifier();
return KAsync::null();
}
SinkLog() << "Modify: " << domainObject;
auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
if (domainObject.isAggregate()) {
return KAsync::value(domainObject.aggregatedIds())
.addToContext(std::shared_ptr<void>(facade))
.each([=] (const QByteArray &id) {
auto object = Sink::ApplicationDomain::ApplicationDomainType::createCopy(id, domainObject);
return facade->modify(object).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to modify " << error; });
});
}
return facade->modify(domainObject).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to modify"; });
}
template <class DomainType>
KAsync::Job<void> Store::modify(const Query &query, const DomainType &domainObject)
{
if (domainObject.changedProperties().isEmpty()) {
SinkLog() << "Nothing to modify: " << domainObject.identifier();
return KAsync::null();
}
SinkLog() << "Modify: " << query << domainObject;
return fetchAll<DomainType>(query)
.each([=] (const typename DomainType::Ptr &entity) {
auto copy = *entity;
for (const auto &p : domainObject.changedProperties()) {
copy.setProperty(p, domainObject.getProperty(p));
}
return modify(copy);
});
}
template <class DomainType>
KAsync::Job<void> Store::move(const DomainType &domainObject, const QByteArray &newResource)
{
SinkLog() << "Move: " << domainObject << newResource;
auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
if (domainObject.isAggregate()) {
return KAsync::value(domainObject.aggregatedIds())
.addToContext(std::shared_ptr<void>(facade))
.each([=] (const QByteArray &id) {
auto object = Sink::ApplicationDomain::ApplicationDomainType::createCopy(id, domainObject);
return facade->move(object, newResource).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to move " << error; });
});
}
return facade->move(domainObject, newResource).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to move " << error; });
}
template <class DomainType>
KAsync::Job<void> Store::copy(const DomainType &domainObject, const QByteArray &newResource)
{
SinkLog() << "Copy: " << domainObject << newResource;
auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
if (domainObject.isAggregate()) {
return KAsync::value(domainObject.aggregatedIds())
.addToContext(std::shared_ptr<void>(facade))
.each([=] (const QByteArray &id) {
auto object = Sink::ApplicationDomain::ApplicationDomainType::createCopy(id, domainObject);
return facade->copy(object, newResource).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to copy " << error; });
});
}
return facade->copy(domainObject, newResource).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to copy " << error; });
}
template <class DomainType>
KAsync::Job<void> Store::remove(const DomainType &domainObject)
{
SinkLog() << "Remove: " << domainObject;
auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
if (domainObject.isAggregate()) {
return KAsync::value(domainObject.aggregatedIds())
.addToContext(std::shared_ptr<void>(facade))
.each([=] (const QByteArray &id) {
auto object = Sink::ApplicationDomain::ApplicationDomainType::createCopy(id, domainObject);
return facade->remove(object).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to remove " << error; });
});
}
return facade->remove(domainObject).addToContext(std::shared_ptr<void>(facade)).onError([](const KAsync::Error &error) { SinkWarning() << "Failed to remove " << error; });
}
template <class DomainType>
KAsync::Job<void> Store::remove(const Sink::Query &query)
{
SinkLog() << "Remove: " << query;
return fetchAll<DomainType>(query)
.each([] (const typename DomainType::Ptr &entity) {
return remove(*entity);
});
}
KAsync::Job<void> Store::removeDataFromDisk(const QByteArray &identifier)
{
// All databases are going to become invalid, nuke the environments
// TODO: all clients should react to a notification from the resource
Sink::Storage::DataStore::clearEnv();
SinkTrace() << "Remove data from disk " << identifier;
auto time = QSharedPointer<QTime>::create();
time->start();
auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier));
resourceAccess->open();
return resourceAccess->sendCommand(Sink::Commands::RemoveFromDiskCommand)
.addToContext(resourceAccess)
.then<void>([resourceAccess](KAsync::Future<void> &future) {
if (resourceAccess->isReady()) {
//Wait for the resource shutdown
QObject::connect(resourceAccess.data(), &ResourceAccess::ready, [&future](bool ready) {
if (!ready) {
future.setFinished();
}
});
} else {
future.setFinished();
}
})
.then([time]() {
SinkTrace() << "Remove from disk complete." << Log::TraceTime(time->elapsed());
});
}
static KAsync::Job<Store::UpgradeResult> upgrade(const QByteArray &resource)
{
auto store = Sink::Storage::DataStore(Sink::storageLocation(), resource, Sink::Storage::DataStore::ReadOnly);
if (!store.exists() || Storage::DataStore::databaseVersion(store.createTransaction(Storage::DataStore::ReadOnly)) == Sink::latestDatabaseVersion()) {
return KAsync::value(Store::UpgradeResult{false});
}
SinkLog() << "Upgrading " << resource;
auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource));
return resourceAccess->sendCommand(Sink::Commands::UpgradeCommand)
.addToContext(resourceAccess)
.then([=](const KAsync::Error &error) {
if (error) {
SinkWarning() << "Error during upgrade.";
return KAsync::error(error);
}
SinkTrace() << "Upgrade of resource " << resource << " complete.";
return KAsync::null();
})
.then(KAsync::value(Store::UpgradeResult{true}));
}
KAsync::Job<Store::UpgradeResult> Store::upgrade()
{
SinkLog() << "Upgrading...";
auto ret = QSharedPointer<bool>::create(false);
return fetchAll<ApplicationDomain::SinkResource>({})
.template each([ret](const ApplicationDomain::SinkResource::Ptr &resource) -> KAsync::Job<void> {
return Sink::upgrade(resource->identifier())
.then([ret](UpgradeResult returnValue) {
if (returnValue.upgradeExecuted) {
SinkLog() << "Upgrade executed.";
*ret = true;
}
});
})
.then([ret] {
if (*ret) {
SinkLog() << "Upgrade complete.";
}
return Store::UpgradeResult{*ret};
});
}
static KAsync::Job<void> synchronize(const QByteArray &resource, const Sink::SyncScope &scope)
{
SinkLog() << "Synchronizing " << resource << scope;
auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource));
return resourceAccess->synchronizeResource(scope)
.addToContext(resourceAccess)
.then([=](const KAsync::Error &error) {
if (error) {
SinkWarning() << "Error during sync.";
return KAsync::error(error);
}
SinkTrace() << "Synchronization of resource " << resource << " complete.";
return KAsync::null();
});
}
KAsync::Job<void> Store::synchronize(const Sink::Query &query)
{
return synchronize(Sink::SyncScope{query});
}
KAsync::Job<void> Store::synchronize(const Sink::SyncScope &scope)
{
auto resourceFilter = scope.getResourceFilter();
//Filter resources by type by default
if (!resourceFilter.propertyFilter.contains(ApplicationDomain::SinkResource::Capabilities::name) && !scope.type().isEmpty()) {
resourceFilter.propertyFilter.insert(ApplicationDomain::SinkResource::Capabilities::name, Query::Comparator{scope.type(), Query::Comparator::Contains});
}
Sink::Query query;
query.setFilter(resourceFilter);
SinkLog() << "Synchronizing all resource matching: " << query;
return fetchAll<ApplicationDomain::SinkResource>(query)
.template each([scope](const ApplicationDomain::SinkResource::Ptr &resource) -> KAsync::Job<void> {
return synchronize(resource->identifier(), scope);
});
}
template <class DomainType>
KAsync::Job<DomainType> Store::fetchOne(const Sink::Query &query)
{
return fetch<DomainType>(query, 1).template then<DomainType, QList<typename DomainType::Ptr>>([](const QList<typename DomainType::Ptr> &list) {
return KAsync::value(*list.first());
});
}
template <class DomainType>
KAsync::Job<QList<typename DomainType::Ptr>> Store::fetchAll(const Sink::Query &query)
{
return fetch<DomainType>(query);
}
template <class DomainType>
KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &query, int minimumAmount)
{
Q_ASSERT(sanityCheckQuery(query));
auto model = loadModel<DomainType>(query);
auto list = QSharedPointer<QList<typename DomainType::Ptr>>::create();
auto context = QSharedPointer<QObject>::create();
return KAsync::start<QList<typename DomainType::Ptr>>([model, list, context, minimumAmount](KAsync::Future<QList<typename DomainType::Ptr>> &future) {
if (model->rowCount() >= 1) {
for (int i = 0; i < model->rowCount(); i++) {
list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value<typename DomainType::Ptr>());
}
} else {
QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, context.data(), [model, &future, list](const QModelIndex &index, int start, int end) {
for (int i = start; i <= end; i++) {
list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value<typename DomainType::Ptr>());
}
});
QObject::connect(model.data(), &QAbstractItemModel::dataChanged, context.data(),
[model, &future, list, minimumAmount](const QModelIndex &, const QModelIndex &, const QVector<int> &roles) {
if (roles.contains(ModelResult<DomainType, typename DomainType::Ptr>::ChildrenFetchedRole)) {
if (list->size() < minimumAmount) {
future.setError(1, "Not enough values.");
} else {
future.setValue(*list);
future.setFinished();
}
}
});
}
if (model->data(QModelIndex(), ModelResult<DomainType, typename DomainType::Ptr>::ChildrenFetchedRole).toBool()) {
if (list->size() < minimumAmount) {
future.setError(1, "Not enough values.");
} else {
future.setValue(*list);
}
future.setFinished();
}
});
}
template <class DomainType>
DomainType Store::readOne(const Sink::Query &query)
{
const auto list = read<DomainType>(query);
if (!list.isEmpty()) {
return list.first();
}
SinkWarning() << "Tried to read value but no values are available.";
return DomainType();
}
template <class DomainType>
QList<DomainType> Store::read(const Sink::Query &query_)
{
Q_ASSERT(sanityCheckQuery(query_));
auto query = query_;
query.setFlags(Query::SynchronousQuery);
auto ctx = getQueryContext(query, ApplicationDomain::getTypeName<DomainType>());
QList<DomainType> list;
auto result = getEmitter<DomainType>(query, ctx);
auto aggregatingEmitter = result.first;
aggregatingEmitter->onAdded([&list, ctx](const typename DomainType::Ptr &value){
SinkTraceCtx(ctx) << "Found value: " << value->identifier();
list << *value;
});
if (auto resourceEmitter = result.second) {
resourceEmitter->fetch();
}
aggregatingEmitter->fetch();
return list;
}
#define REGISTER_TYPE(T) \
template KAsync::Job<void> Store::remove<T>(const T &domainObject); \
template KAsync::Job<void> Store::remove<T>(const Query &); \
template KAsync::Job<void> Store::create<T>(const T &domainObject); \
template KAsync::Job<void> Store::modify<T>(const T &domainObject); \
template KAsync::Job<void> Store::modify<T>(const Query &, const T &); \
template KAsync::Job<void> Store::move<T>(const T &domainObject, const QByteArray &newResource); \
template KAsync::Job<void> Store::copy<T>(const T &domainObject, const QByteArray &newResource); \
template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(const Query &query); \
template KAsync::Job<T> Store::fetchOne<T>(const Query &); \
template KAsync::Job<QList<T::Ptr>> Store::fetchAll<T>(const Query &); \
template KAsync::Job<QList<T::Ptr>> Store::fetch<T>(const Query &, int); \
template T Store::readOne<T>(const Query &); \
template QList<T> Store::read<T>(const Query &);
SINK_REGISTER_TYPES()
} // namespace Sink
|