[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdepimlibs/akonadi/server-search] akonadi: SearchQuery: Implemented limit and fixed comparison oper
From:       Christian Mollekopf <chrigi_1 () fastmail ! fm>
Date:       2014-01-31 14:56:01
Message-ID: E1W9FVh-00061d-G2 () scm ! kde ! org
[Download RAW message or body]

Git commit 33a721a7dc595d653bc8ed76ef82d4eb3f5b39d0 by Christian Mollekopf.
Committed on 31/01/2014 at 14:53.
Pushed by cmollekopf into branch 'akonadi/server-search'.

SearchQuery: Implemented limit and fixed comparison operators.

M  +28   -10   akonadi/searchquery.cpp
M  +17   -2    akonadi/searchquery.h
M  +18   -0    akonadi/tests/searchquerytest.cpp

http://commits.kde.org/kdepimlibs/33a721a7dc595d653bc8ed76ef82d4eb3f5b39d0

diff --git a/akonadi/searchquery.cpp b/akonadi/searchquery.cpp
index e1779ac..ac8a8f4 100644
--- a/akonadi/searchquery.cpp
+++ b/akonadi/searchquery.cpp
@@ -50,7 +50,7 @@ class SearchTerm::Private: public QSharedData
     {
     }
 
-    bool operator==( const Private &other )
+    bool operator==( const Private &other ) const
     {
       return relation == other.relation
              && isNegated == other.isNegated
@@ -72,19 +72,21 @@ class SearchQuery::Private: public QSharedData
 {
   public:
     Private():
-      QSharedData()
+      QSharedData(),
+      limit(-1)
     {
     }
 
     Private( const Private &other ):
       QSharedData( other ),
-      rootTerm( other.rootTerm )
+      rootTerm( other.rootTerm ),
+      limit( other.limit )
     {
     }
 
-    bool operator==( const Private &other )
+    bool operator==( const Private &other ) const
     {
-      return rootTerm == other.rootTerm;
+      return rootTerm == other.rootTerm && limit == other.limit;
     }
 
     static QVariantMap termToJSON( const SearchTerm &term )
@@ -131,6 +133,7 @@ class SearchQuery::Private: public QSharedData
     }
 
     SearchTerm rootTerm;
+    int limit;
 };
 
 SearchTerm::SearchTerm( SearchTerm::Relation relation ):
@@ -163,9 +166,9 @@ SearchTerm& SearchTerm::operator=( const SearchTerm &other )
   return *this;
 }
 
-bool SearchTerm::operator==( const SearchTerm &other )
+bool SearchTerm::operator==( const SearchTerm &other ) const
 {
-  return d == other.d;
+  return *d == *other.d;
 }
 
 bool SearchTerm::isNull() const
@@ -235,9 +238,9 @@ SearchQuery& SearchQuery::operator=( const SearchQuery &other )
   return *this;
 }
 
-bool SearchQuery::operator==( const SearchQuery &other )
+bool SearchQuery::operator==( const SearchQuery &other ) const
 {
-  return d->rootTerm == other.d->rootTerm;
+  return *d == *other.d;
 }
 
 bool SearchQuery::isNull() const
@@ -265,9 +268,20 @@ void SearchQuery::setTerm( const SearchTerm& term )
   d->rootTerm = term;
 }
 
+void SearchQuery::setLimit( int limit )
+{
+  d->limit = limit;
+}
+
+int SearchQuery::limit() const
+{
+  return d->limit;
+}
+
 QByteArray SearchQuery::toJSON() const
 {
   QVariantMap root = Private::termToJSON( d->rootTerm );
+  root.insert( QLatin1String( "limit" ), d->limit );
 
   QJson::Serializer serializer;
   return serializer.serialize( root );
@@ -282,8 +296,12 @@ SearchQuery SearchQuery::fromJSON( const QByteArray &jsonData )
     return SearchQuery();
   }
 
+  const QVariantMap map = json.toMap();
   SearchQuery query;
-  query.d->rootTerm = Private::JSONToTerm( json.toMap() );
+  query.d->rootTerm = Private::JSONToTerm( map );
+  if ( map.contains( QLatin1String("limit") ) ) {
+    query.d->limit = map.value( QLatin1String("limit") ).toInt();
+  }
   return query;
 }
 
diff --git a/akonadi/searchquery.h b/akonadi/searchquery.h
index 4863832..2ccd30b 100644
--- a/akonadi/searchquery.h
+++ b/akonadi/searchquery.h
@@ -68,7 +68,7 @@ class AKONADI_EXPORT SearchTerm
     ~SearchTerm();
 
     SearchTerm& operator=( const SearchTerm &other );
-    bool operator==( const SearchTerm &other );
+    bool operator==( const SearchTerm &other ) const;
 
     bool isNull() const;
 
@@ -137,7 +137,7 @@ class AKONADI_EXPORT SearchQuery
     ~SearchQuery();
     SearchQuery( const SearchQuery &other );
     SearchQuery& operator=( const SearchQuery &other );
-    bool operator==( const SearchQuery &other );
+    bool operator==( const SearchQuery &other ) const;
 
     bool isNull() const;
 
@@ -161,6 +161,21 @@ class AKONADI_EXPORT SearchQuery
      */
     SearchTerm term() const;
 
+    /**
+     * Sets the maximum number of results.
+     *
+     * Note that this limit is only evaluated per search backend,
+     * so the total number of results retrieved may be larger.
+     */
+    void setLimit( int limit );
+
+    /**
+     * Returns the maximum number of results.
+     *
+     * The default value is -1, indicating no limit.
+     */
+    int limit() const;
+
     QByteArray toJSON() const;
     static SearchQuery fromJSON( const QByteArray &json );
 
diff --git a/akonadi/tests/searchquerytest.cpp b/akonadi/tests/searchquerytest.cpp
index 1445a2b..b7d644f 100644
--- a/akonadi/tests/searchquerytest.cpp
+++ b/akonadi/tests/searchquerytest.cpp
@@ -133,6 +133,24 @@ class SearchQueryTest : public QObject
         }
       }
     }
+
+    void testFullQuery()
+    {
+      {
+        SearchQuery query;
+        query.addTerm("key", "value");
+        const QByteArray serialized = query.toJSON();
+        QCOMPARE(SearchQuery::fromJSON(serialized), query);
+      }
+      {
+        SearchQuery query;
+        query.setLimit(10);
+        query.addTerm("key", "value");
+        const QByteArray serialized = query.toJSON();
+        QCOMPARE(SearchQuery::fromJSON(serialized), query);
+      }
+    }
+
 };
 
 QTEST_AKONADIMAIN( SearchQueryTest, NoGUI )
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic