From 7189433c0a55cee16ec54e8af767a4f6b1d614e9 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 15 Feb 2016 13:42:36 +0100 Subject: Ensure we correctly sort by date. --- common/query.h | 6 ++--- common/queryrunner.cpp | 13 ++++++++--- tests/querytest.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/common/query.h b/common/query.h index bb826aa..29cff78 100644 --- a/common/query.h +++ b/common/query.h @@ -103,6 +103,7 @@ public: } Query(Flags flags = Flags()) + : limit(0) {} Query& operator+=(const Query& rhs) @@ -115,8 +116,6 @@ public: requestedProperties += rhs.requestedProperties; parentProperty = rhs.parentProperty; liveQuery = rhs.liveQuery; - syncOnDemand = rhs.syncOnDemand; - processAll = rhs.processAll; return *this; } @@ -133,8 +132,7 @@ public: QByteArray parentProperty; QByteArray sortProperty; bool liveQuery; - bool syncOnDemand; - bool processAll; + int limit; }; } diff --git a/common/queryrunner.cpp b/common/queryrunner.cpp index 1f645e8..2d188ae 100644 --- a/common/queryrunner.cpp +++ b/common/queryrunner.cpp @@ -18,7 +18,9 @@ */ #include "queryrunner.h" +#include #include + #include "commands.h" #include "log.h" #include "storage.h" @@ -74,7 +76,7 @@ QueryRunner::QueryRunner(const Sink::Query &query, const Sink::Resou mResourceAccess(resourceAccess), mResultProvider(new ResultProvider), mOffset(0), - mBatchSize(0) + mBatchSize(query.limit) { Trace() << "Starting query"; //We delegate loading of initial data to the result provider, os it can decide for itself what it needs to load. @@ -280,7 +282,7 @@ ResultSet QueryWorker::loadIncrementalResultSet(qint64 baseRevision, template ResultSet QueryWorker::filterAndSortSet(ResultSet &resultSet, const std::function &filter, const Sink::Storage::NamedDatabase &db, bool initialQuery, const QByteArray &sortProperty) { - bool sortingRequired = false; + const bool sortingRequired = !sortProperty.isEmpty(); if (initialQuery && sortingRequired) { //Sort the complete set by reading the sort property and filling into a sorted map auto sortedMap = QSharedPointer>::create(); @@ -290,7 +292,12 @@ ResultSet QueryWorker::filterAndSortSet(ResultSet &resultSet, const //We're not interested in removals during the initial query if ((operation != Sink::Operation_Removal) && filter(domainObject)) { if (!sortProperty.isEmpty()) { - sortedMap->insert(domainObject->getProperty(sortProperty).toString().toLatin1(), domainObject->identifier()); + const auto sortValue = domainObject->getProperty(sortProperty); + if (sortValue.canConvert()) { + sortedMap->insert(QByteArray::number(std::numeric_limits::max() - sortValue.toDateTime().toTime_t()), domainObject->identifier()); + } else { + sortedMap->insert(sortValue.toString().toLatin1(), domainObject->identifier()); + } } else { sortedMap->insert(domainObject->identifier(), domainObject->identifier()); } diff --git a/tests/querytest.cpp b/tests/querytest.cpp index 16376b9..50fc67f 100644 --- a/tests/querytest.cpp +++ b/tests/querytest.cpp @@ -259,6 +259,69 @@ private Q_SLOTS: QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()); QCOMPARE(model->rowCount(), 1); } + + void testMailByFolderSortedByDate() + { + //Setup + Sink::ApplicationDomain::Folder::Ptr folderEntity; + { + Sink::ApplicationDomain::Folder folder("org.kde.dummy.instance1"); + Sink::Store::create(folder).exec().waitForFinished(); + + Sink::Query query; + query.resources << "org.kde.dummy.instance1"; + + //Ensure all local data is processed + Sink::ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto model = Sink::Store::loadModel(query); + QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()); + QCOMPARE(model->rowCount(), 1); + + folderEntity = model->index(0, 0).data(Sink::Store::DomainObjectRole).value(); + QVERIFY(!folderEntity->identifier().isEmpty()); + + const auto date = QDateTime(QDate(2015, 7, 7), QTime(12, 0)); + { + Sink::ApplicationDomain::Mail mail("org.kde.dummy.instance1"); + mail.setProperty("uid", "testSecond"); + mail.setProperty("folder", folderEntity->identifier()); + mail.setProperty("date", date.addDays(-1)); + Sink::Store::create(mail).exec().waitForFinished(); + } + { + Sink::ApplicationDomain::Mail mail("org.kde.dummy.instance1"); + mail.setProperty("uid", "testLatest"); + mail.setProperty("folder", folderEntity->identifier()); + mail.setProperty("date", date); + Sink::Store::create(mail).exec().waitForFinished(); + } + { + Sink::ApplicationDomain::Mail mail("org.kde.dummy.instance1"); + mail.setProperty("uid", "testLast"); + mail.setProperty("folder", folderEntity->identifier()); + mail.setProperty("date", date.addDays(-2)); + Sink::Store::create(mail).exec().waitForFinished(); + } + } + + //Test + Sink::Query query; + query.resources << "org.kde.dummy.instance1"; + query.propertyFilter.insert("folder", folderEntity->identifier()); + query.sortProperty = "date"; + query.limit = 1; + + //Ensure all local data is processed + Sink::ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + //We fetch before the data is available and rely on the live query mechanism to deliver the actual data + auto model = Sink::Store::loadModel(query); + QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()); + //The model is not sorted, but the limited set is sorted, so we can only test for the latest result. + QCOMPARE(model->rowCount(), 1); + QCOMPARE(model->index(0, 0).data(Sink::Store::DomainObjectRole).value()->getProperty("uid").toByteArray(), QByteArray("testLatest")); + } }; QTEST_MAIN(QueryTest) -- cgit v1.2.3