diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-18 10:26:47 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-18 12:15:24 +0200 |
commit | e2857def8e67c3a95656f9d4737beba93a38c53a (patch) | |
tree | 2e824f5e185b5273728edaa314ebbbbe7f97f68e | |
parent | bbb7c128488d0bc1dbedd4fe9f8a7a4de778705b (diff) | |
download | sink-e2857def8e67c3a95656f9d4737beba93a38c53a.tar.gz sink-e2857def8e67c3a95656f9d4737beba93a38c53a.zip |
Moved generic part of load to GenericFacade
-rw-r--r-- | common/clientapi.h | 2 | ||||
-rw-r--r-- | common/facade.h | 45 | ||||
-rw-r--r-- | dummyresource/facade.cpp | 42 | ||||
-rw-r--r-- | dummyresource/facade.h | 1 |
4 files changed, 47 insertions, 43 deletions
diff --git a/common/clientapi.h b/common/clientapi.h index c2b9493..88bc38c 100644 --- a/common/clientapi.h +++ b/common/clientapi.h | |||
@@ -365,7 +365,7 @@ public: | |||
365 | virtual Async::Job<void> modify(const DomainType &domainObject) = 0; | 365 | virtual Async::Job<void> modify(const DomainType &domainObject) = 0; |
366 | virtual Async::Job<void> remove(const DomainType &domainObject) = 0; | 366 | virtual Async::Job<void> remove(const DomainType &domainObject) = 0; |
367 | //TODO remove from public API | 367 | //TODO remove from public API |
368 | virtual Async::Job<qint64> load(const Query &query, const std::function<void(const typename DomainType::Ptr &)> &resultCallback) = 0; | 368 | // virtual Async::Job<qint64> load(const Query &query, const std::function<void(const typename DomainType::Ptr &)> &resultCallback) = 0; |
369 | virtual Async::Job<void> load(const Query &query, const QSharedPointer<ResultProvider<typename DomainType::Ptr> > &resultProvider) { return Async::null<void>(); }; | 369 | virtual Async::Job<void> load(const Query &query, const QSharedPointer<ResultProvider<typename DomainType::Ptr> > &resultProvider) { return Async::null<void>(); }; |
370 | }; | 370 | }; |
371 | 371 | ||
diff --git a/common/facade.h b/common/facade.h index f9b5a83..6c1ad67 100644 --- a/common/facade.h +++ b/common/facade.h | |||
@@ -29,6 +29,7 @@ | |||
29 | #include "createentity_generated.h" | 29 | #include "createentity_generated.h" |
30 | #include "domainadaptor.h" | 30 | #include "domainadaptor.h" |
31 | #include "entitybuffer.h" | 31 | #include "entitybuffer.h" |
32 | #include "log.h" | ||
32 | 33 | ||
33 | /** | 34 | /** |
34 | * A QueryRunner runs a query and updates the corresponding result set. | 35 | * A QueryRunner runs a query and updates the corresponding result set. |
@@ -98,6 +99,48 @@ public: | |||
98 | { | 99 | { |
99 | } | 100 | } |
100 | 101 | ||
102 | //TODO JOBAPI return job from sync continuation to execute it as subjob? | ||
103 | Async::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) Q_DECL_OVERRIDE | ||
104 | { | ||
105 | auto runner = QSharedPointer<QueryRunner>::create(query); | ||
106 | QWeakPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > weakResultProvider = resultProvider; | ||
107 | runner->setQuery([this, weakResultProvider, query] (qint64 oldRevision, qint64 newRevision) -> Async::Job<qint64> { | ||
108 | return Async::start<qint64>([this, weakResultProvider, query](Async::Future<qint64> &future) { | ||
109 | auto resultProvider = weakResultProvider.toStrongRef(); | ||
110 | if (!resultProvider) { | ||
111 | Warning() << "Tried executing query after result provider is already gone"; | ||
112 | future.setError(0, QString()); | ||
113 | future.setFinished(); | ||
114 | return; | ||
115 | } | ||
116 | //TODO only emit changes and don't replace everything | ||
117 | resultProvider->clear(); | ||
118 | //rerun query | ||
119 | auto addCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1); | ||
120 | load(query, addCallback).template then<void, qint64>([resultProvider, &future](qint64 queriedRevision) { | ||
121 | //TODO set revision in result provider? | ||
122 | //TODO update all existing results with new revision | ||
123 | resultProvider->complete(); | ||
124 | future.setValue(queriedRevision); | ||
125 | future.setFinished(); | ||
126 | }).exec(); | ||
127 | }); | ||
128 | }); | ||
129 | |||
130 | //In case of a live query we keep the runner for as long alive as the result provider exists | ||
131 | if (query.liveQuery) { | ||
132 | resultProvider->setQueryRunner(runner); | ||
133 | QObject::connect(mResourceAccess.data(), &Akonadi2::ResourceAccess::revisionChanged, runner.data(), &QueryRunner::revisionChanged); | ||
134 | } | ||
135 | |||
136 | //We have to capture the runner to keep it alive | ||
137 | return synchronizeResource(query.syncOnDemand, query.processAll).template then<void>([runner](Async::Future<void> &future) { | ||
138 | runner->run().then<void>([&future]() { | ||
139 | future.setFinished(); | ||
140 | }).exec(); | ||
141 | }); | ||
142 | } | ||
143 | |||
101 | protected: | 144 | protected: |
102 | Async::Job<void> sendCreateCommand(const QByteArray &t, const QByteArray &buffer) | 145 | Async::Job<void> sendCreateCommand(const QByteArray &t, const QByteArray &buffer) |
103 | { | 146 | { |
@@ -129,6 +172,8 @@ protected: | |||
129 | return Async::null<void>(); | 172 | return Async::null<void>(); |
130 | } | 173 | } |
131 | 174 | ||
175 | virtual Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const typename DomainType::Ptr &)> &resultCallback) { return Async::null<qint64>(); }; | ||
176 | |||
132 | protected: | 177 | protected: |
133 | //TODO use one resource access instance per application => make static | 178 | //TODO use one resource access instance per application => make static |
134 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; | 179 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; |
diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp index da2a27a..e50e4f3 100644 --- a/dummyresource/facade.cpp +++ b/dummyresource/facade.cpp | |||
@@ -147,49 +147,9 @@ void DummyResourceFacade::readValue(QSharedPointer<Akonadi2::Storage> storage, c | |||
147 | }); | 147 | }); |
148 | } | 148 | } |
149 | 149 | ||
150 | Async::Job<void> DummyResourceFacade::load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) | ||
151 | { | ||
152 | auto runner = QSharedPointer<QueryRunner>::create(query); | ||
153 | QWeakPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > weakResultProvider = resultProvider; | ||
154 | runner->setQuery([this, weakResultProvider, query] (qint64 oldRevision, qint64 newRevision) -> Async::Job<qint64> { | ||
155 | return Async::start<qint64>([this, weakResultProvider, query](Async::Future<qint64> &future) { | ||
156 | auto resultProvider = weakResultProvider.toStrongRef(); | ||
157 | if (!resultProvider) { | ||
158 | Warning() << "Tried executing query after result provider is already gone"; | ||
159 | future.setError(0, QString()); | ||
160 | future.setFinished(); | ||
161 | return; | ||
162 | } | ||
163 | //TODO only emit changes and don't replace everything | ||
164 | resultProvider->clear(); | ||
165 | //rerun query | ||
166 | auto addCallback = std::bind(&Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr>::add, resultProvider, std::placeholders::_1); | ||
167 | load(query, addCallback).then<void, qint64>([resultProvider, &future](qint64 queriedRevision) { | ||
168 | //TODO set revision in result provider? | ||
169 | //TODO update all existing results with new revision | ||
170 | resultProvider->complete(); | ||
171 | future.setValue(queriedRevision); | ||
172 | future.setFinished(); | ||
173 | }).exec(); | ||
174 | }); | ||
175 | }); | ||
176 | |||
177 | //In case of a live query we keep the runner for as long alive as the result provider exists | ||
178 | if (query.liveQuery) { | ||
179 | resultProvider->setQueryRunner(runner); | ||
180 | QObject::connect(mResourceAccess.data(), &Akonadi2::ResourceAccess::revisionChanged, runner.data(), &QueryRunner::revisionChanged); | ||
181 | } | ||
182 | |||
183 | return Async::start<void>([runner](Async::Future<void> &future) { | ||
184 | runner->run().then<void>([&future]() { | ||
185 | future.setFinished(); | ||
186 | }).exec(); | ||
187 | }); | ||
188 | } | ||
189 | |||
190 | Async::Job<qint64> DummyResourceFacade::load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) | 150 | Async::Job<qint64> DummyResourceFacade::load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) |
191 | { | 151 | { |
192 | return synchronizeResource(query.syncOnDemand, query.processAll).then<qint64>([=](Async::Future<qint64> &future) { | 152 | return Async::start<qint64>([=](Async::Future<qint64> &future) { |
193 | //Now that the sync is complete we can execute the query | 153 | //Now that the sync is complete we can execute the query |
194 | const auto preparedQuery = prepareQuery(query); | 154 | const auto preparedQuery = prepareQuery(query); |
195 | 155 | ||
diff --git a/dummyresource/facade.h b/dummyresource/facade.h index 3ddfe15..91ae351 100644 --- a/dummyresource/facade.h +++ b/dummyresource/facade.h | |||
@@ -38,7 +38,6 @@ public: | |||
38 | Async::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE; | 38 | Async::Job<void> modify(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE; |
39 | Async::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE; | 39 | Async::Job<void> remove(const Akonadi2::ApplicationDomain::Event &domainObject) Q_DECL_OVERRIDE; |
40 | Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) Q_DECL_OVERRIDE; | 40 | Async::Job<qint64> load(const Akonadi2::Query &query, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback) Q_DECL_OVERRIDE; |
41 | Async::Job<void> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) Q_DECL_OVERRIDE; | ||
42 | 41 | ||
43 | private: | 42 | private: |
44 | void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)>); | 43 | void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)>); |