From c55054e899660f2d667af2c2e573a1267d47358e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 13 Apr 2015 20:15:14 +0200 Subject: Use a queryrunner to execute queries. The queryrunner is responsible for running queries and keeping them up to date. This is required for self-updating queries. To get this to work properly the ResultProvider/emitter had to be fixed. The emitter now only lives as long as the client holds a reference to it, allowing the provider to detect when it is no longer necessary to keep the query alive (because noone is listening). In the process various lifetime issues have been fixed, that we're caused by lambdas capturing smartpointers, that then extended the lifetime of the associated objects unpredictably. --- dummyresource/facade.cpp | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'dummyresource/facade.cpp') diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp index 1477fcf..f603c56 100644 --- a/dummyresource/facade.cpp +++ b/dummyresource/facade.cpp @@ -147,15 +147,51 @@ void DummyResourceFacade::readValue(QSharedPointer storage, c }); } -Async::Job DummyResourceFacade::load(const Akonadi2::Query &query, const std::function &resultCallback) +Async::Job DummyResourceFacade::load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider) { - return synchronizeResource(query.syncOnDemand, query.processAll).then([=](Async::Future &future) { + auto runner = QSharedPointer::create(query); + //The runner only lives as long as the resultProvider + resultProvider->setQueryRunner(runner); + runner->setQuery([this, resultProvider, query](qint64 oldRevision, qint64 newRevision) -> Async::Job { + return Async::start([this, resultProvider, query](Async::Future &future) { + //TODO only emit changes and don't replace everything + resultProvider->clear(); + //rerun query + std::function addCallback = std::bind(&Akonadi2::ResultProvider::add, resultProvider, std::placeholders::_1); + load(query, addCallback).then([resultProvider, &future](qint64 queriedRevision) { + //TODO set revision in result provider? + //TODO update all existing results with new revision + resultProvider->complete(); + future.setValue(queriedRevision); + future.setFinished(); + }).exec(); + }); + }); + + auto resourceAccess = mResourceAccess; + //TODO we need to somehow disconnect this + QObject::connect(resourceAccess.data(), &Akonadi2::ResourceAccess::revisionChanged, [runner](qint64 newRevision) { + runner->revisionChanged(newRevision); + }); + + return Async::start([runner](Async::Future &future) { + runner->run().then([&future]() { + //TODO if not live query, destroy runner. + future.setFinished(); + }).exec(); + }); +} + +Async::Job DummyResourceFacade::load(const Akonadi2::Query &query, const std::function &resultCallback) +{ + return synchronizeResource(query.syncOnDemand, query.processAll).then([=](Async::Future &future) { //Now that the sync is complete we can execute the query const auto preparedQuery = prepareQuery(query); auto storage = QSharedPointer::create(Akonadi2::Store::storageLocation(), "org.kde.dummy"); - //TODO use transaction over full query and record store revision. We'll need it to update the query. + storage->startTransaction(Akonadi2::Storage::ReadOnly); + const qint64 revision = storage->maxRevision(); //Index lookups QVector keys; @@ -177,6 +213,8 @@ Async::Job DummyResourceFacade::load(const Akonadi2::Query &query, const s readValue(storage, key, resultCallback, preparedQuery); } } + storage->abortTransaction(); + future.setValue(revision); future.setFinished(); }); } -- cgit v1.2.3