diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-09-13 12:42:31 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-09-13 12:42:31 +0200 |
commit | c12a9a09da59b9e418316dba02e6215cb55e47ee (patch) | |
tree | 05498d9a42e399bcca787f40c1fc473fb09e680e /common/store.cpp | |
parent | 55fe06979ceebe67553135b43aa47e70d931304b (diff) | |
parent | ebdb89b8bb482bbb5ecd544c3d38bef35fc7d820 (diff) | |
download | sink-0.4.0.tar.gz sink-0.4.0.zip |
Merge commit 'ebdb89b8bb482bbb5ecd544c3d38bef35fc7d820'v0.4.0
Diffstat (limited to 'common/store.cpp')
-rw-r--r-- | common/store.cpp | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/common/store.cpp b/common/store.cpp index 4735113..1701a43 100644 --- a/common/store.cpp +++ b/common/store.cpp | |||
@@ -36,10 +36,34 @@ | |||
36 | #include "storage.h" | 36 | #include "storage.h" |
37 | #include "log.h" | 37 | #include "log.h" |
38 | 38 | ||
39 | #define ASSERT_ENUMS_MATCH(A, B) Q_STATIC_ASSERT_X(static_cast<int>(A) == static_cast<int>(B), "The enum values must match"); | ||
40 | |||
41 | //Ensure the copied enum matches | ||
42 | typedef ModelResult<Sink::ApplicationDomain::Mail, Sink::ApplicationDomain::Mail::Ptr> MailModelResult; | ||
43 | ASSERT_ENUMS_MATCH(Sink::Store::DomainObjectBaseRole, MailModelResult::DomainObjectBaseRole) | ||
44 | ASSERT_ENUMS_MATCH(Sink::Store::ChildrenFetchedRole, MailModelResult::ChildrenFetchedRole) | ||
45 | ASSERT_ENUMS_MATCH(Sink::Store::DomainObjectRole, MailModelResult::DomainObjectRole) | ||
46 | ASSERT_ENUMS_MATCH(Sink::Store::StatusRole, MailModelResult::StatusRole) | ||
47 | ASSERT_ENUMS_MATCH(Sink::Store::WarningRole, MailModelResult::WarningRole) | ||
48 | ASSERT_ENUMS_MATCH(Sink::Store::ProgressRole, MailModelResult::ProgressRole) | ||
49 | |||
39 | Q_DECLARE_METATYPE(QSharedPointer<Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>>) | 50 | Q_DECLARE_METATYPE(QSharedPointer<Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>>) |
40 | Q_DECLARE_METATYPE(QSharedPointer<Sink::ResourceAccessInterface>); | 51 | Q_DECLARE_METATYPE(QSharedPointer<Sink::ResourceAccessInterface>); |
41 | Q_DECLARE_METATYPE(std::shared_ptr<void>); | 52 | Q_DECLARE_METATYPE(std::shared_ptr<void>); |
42 | 53 | ||
54 | |||
55 | static bool sanityCheckQuery(const Sink::Query &query) | ||
56 | { | ||
57 | for (const auto &id : query.ids()) { | ||
58 | if (id.isEmpty()) { | ||
59 | SinkError() << "Empty id in query."; | ||
60 | return false; | ||
61 | } | ||
62 | } | ||
63 | return true; | ||
64 | } | ||
65 | |||
66 | |||
43 | namespace Sink { | 67 | namespace Sink { |
44 | 68 | ||
45 | QString Store::storageLocation() | 69 | QString Store::storageLocation() |
@@ -138,6 +162,7 @@ static Log::Context getQueryContext(const Sink::Query &query, const QByteArray & | |||
138 | template <class DomainType> | 162 | template <class DomainType> |
139 | QSharedPointer<QAbstractItemModel> Store::loadModel(const Query &query) | 163 | QSharedPointer<QAbstractItemModel> Store::loadModel(const Query &query) |
140 | { | 164 | { |
165 | Q_ASSERT(sanityCheckQuery(query)); | ||
141 | auto ctx = getQueryContext(query, ApplicationDomain::getTypeName<DomainType>()); | 166 | auto ctx = getQueryContext(query, ApplicationDomain::getTypeName<DomainType>()); |
142 | auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties, ctx); | 167 | auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties, ctx); |
143 | 168 | ||
@@ -189,6 +214,10 @@ KAsync::Job<void> Store::create(const DomainType &domainObject) | |||
189 | template <class DomainType> | 214 | template <class DomainType> |
190 | KAsync::Job<void> Store::modify(const DomainType &domainObject) | 215 | KAsync::Job<void> Store::modify(const DomainType &domainObject) |
191 | { | 216 | { |
217 | if (domainObject.changedProperties().isEmpty()) { | ||
218 | SinkLog() << "Nothing to modify: " << domainObject.identifier(); | ||
219 | return KAsync::null(); | ||
220 | } | ||
192 | SinkLog() << "Modify: " << domainObject; | 221 | SinkLog() << "Modify: " << domainObject; |
193 | // Potentially move to separate thread as well | 222 | // Potentially move to separate thread as well |
194 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); | 223 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); |
@@ -198,6 +227,10 @@ KAsync::Job<void> Store::modify(const DomainType &domainObject) | |||
198 | template <class DomainType> | 227 | template <class DomainType> |
199 | KAsync::Job<void> Store::modify(const Query &query, const DomainType &domainObject) | 228 | KAsync::Job<void> Store::modify(const Query &query, const DomainType &domainObject) |
200 | { | 229 | { |
230 | if (domainObject.changedProperties().isEmpty()) { | ||
231 | SinkLog() << "Nothing to modify: " << domainObject.identifier(); | ||
232 | return KAsync::null(); | ||
233 | } | ||
201 | SinkLog() << "Modify: " << query << domainObject; | 234 | SinkLog() << "Modify: " << query << domainObject; |
202 | return fetchAll<DomainType>(query) | 235 | return fetchAll<DomainType>(query) |
203 | .each([=] (const typename DomainType::Ptr &entity) { | 236 | .each([=] (const typename DomainType::Ptr &entity) { |
@@ -311,9 +344,14 @@ KAsync::Job<void> Store::synchronize(const Sink::Query &query) | |||
311 | 344 | ||
312 | KAsync::Job<void> Store::synchronize(const Sink::SyncScope &scope) | 345 | KAsync::Job<void> Store::synchronize(const Sink::SyncScope &scope) |
313 | { | 346 | { |
347 | auto resourceFilter = scope.getResourceFilter(); | ||
348 | //Filter resources by type by default | ||
349 | if (!resourceFilter.propertyFilter.contains(ApplicationDomain::SinkResource::Capabilities::name) && !scope.type().isEmpty()) { | ||
350 | resourceFilter.propertyFilter.insert(ApplicationDomain::SinkResource::Capabilities::name, Query::Comparator{scope.type(), Query::Comparator::Contains}); | ||
351 | } | ||
314 | Sink::Query query; | 352 | Sink::Query query; |
315 | query.setFilter(scope.getResourceFilter()); | 353 | query.setFilter(resourceFilter); |
316 | SinkLog() << "Synchronizing: " << query; | 354 | SinkLog() << "Synchronizing all resource matching: " << query; |
317 | return fetchAll<ApplicationDomain::SinkResource>(query) | 355 | return fetchAll<ApplicationDomain::SinkResource>(query) |
318 | .template each([scope](const ApplicationDomain::SinkResource::Ptr &resource) -> KAsync::Job<void> { | 356 | .template each([scope](const ApplicationDomain::SinkResource::Ptr &resource) -> KAsync::Job<void> { |
319 | return synchronize(resource->identifier(), scope); | 357 | return synchronize(resource->identifier(), scope); |
@@ -337,6 +375,7 @@ KAsync::Job<QList<typename DomainType::Ptr>> Store::fetchAll(const Sink::Query & | |||
337 | template <class DomainType> | 375 | template <class DomainType> |
338 | KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &query, int minimumAmount) | 376 | KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &query, int minimumAmount) |
339 | { | 377 | { |
378 | Q_ASSERT(sanityCheckQuery(query)); | ||
340 | auto model = loadModel<DomainType>(query); | 379 | auto model = loadModel<DomainType>(query); |
341 | auto list = QSharedPointer<QList<typename DomainType::Ptr>>::create(); | 380 | auto list = QSharedPointer<QList<typename DomainType::Ptr>>::create(); |
342 | auto context = QSharedPointer<QObject>::create(); | 381 | auto context = QSharedPointer<QObject>::create(); |
@@ -388,6 +427,7 @@ DomainType Store::readOne(const Sink::Query &query) | |||
388 | template <class DomainType> | 427 | template <class DomainType> |
389 | QList<DomainType> Store::read(const Sink::Query &query_) | 428 | QList<DomainType> Store::read(const Sink::Query &query_) |
390 | { | 429 | { |
430 | Q_ASSERT(sanityCheckQuery(query_)); | ||
391 | auto query = query_; | 431 | auto query = query_; |
392 | query.setFlags(Query::SynchronousQuery); | 432 | query.setFlags(Query::SynchronousQuery); |
393 | 433 | ||