From 9e3bcbdd45ec05d0a1fd423e6219ac6443feed1c Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 25 May 2015 17:22:09 +0200 Subject: Use an iterator for the result set. --- examples/dummyresource/facade.cpp | 43 ++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'examples/dummyresource') diff --git a/examples/dummyresource/facade.cpp b/examples/dummyresource/facade.cpp index 5e0bada..9722335 100644 --- a/examples/dummyresource/facade.cpp +++ b/examples/dummyresource/facade.cpp @@ -126,8 +126,41 @@ void DummyResourceFacade::readValue(const QSharedPointer &sto }); } -//TODO this should return an iterator to the result set so we can lazy load -static QVector getResultSet(const Akonadi2::Query &query, const QSharedPointer &storage) +/* + * An iterator to a result set. + * + * We'll eventually want to lazy load results in next(). + */ +class ResultSet { + public: + ResultSet(const QVector &resultSet) + : mResultSet(resultSet), + mIt(nullptr) + { + + } + + bool next() + { + if (!mIt) { + mIt = mResultSet.constBegin(); + } else { + mIt++; + } + return mIt != mResultSet.constEnd(); + } + + QByteArray id() + { + return *mIt; + } + + private: + QVector mResultSet; + QVector::ConstIterator mIt; +}; + +static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer &storage) { //Now that the sync is complete we can execute the query const auto preparedQuery = prepareQuery(query); @@ -156,7 +189,7 @@ static QVector getResultSet(const Akonadi2::Query &query, const QSha }); } - return keys; + return ResultSet(keys); } KAsync::Job DummyResourceFacade::load(const Akonadi2::Query &query, const QSharedPointer > &resultProvider, qint64 oldRevision, qint64 newRevision) @@ -176,8 +209,8 @@ KAsync::Job DummyResourceFacade::load(const Akonadi2::Query &query, cons // TODO only emit changes and don't replace everything resultProvider->clear(); auto resultCallback = std::bind(&Akonadi2::ResultProvider::add, resultProvider, std::placeholders::_1); - for (const auto &key : resultSet) { - readValue(storage, key, [resultCallback](const Akonadi2::ApplicationDomain::Event::Ptr &event) { + while (resultSet.next()) { + readValue(storage, resultSet.id(), [resultCallback](const Akonadi2::ApplicationDomain::Event::Ptr &event) { //We create an in-memory copy because the result provider will store the value, and the result we get back is only valid during the callback resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation(event)); }); -- cgit v1.2.3