[RFC PATCH 1/3] lib: add support for limiting the number of search results

Subject: [RFC PATCH 1/3] lib: add support for limiting the number of search results

Date: Fri, 28 Oct 2011 23:59:29 +0300

To: notmuch@notmuchmail.org

Cc: amdragon@mit.edu

From: Jani Nikula


Add a function to support limiting the number of messages in search
results. This is a fairly straightforward implementation just to support
the following patches. The proper design should probably support paging of
results (i.e. first give me results 0...49, then 50...99, etc.) That should
not be too difficult, as long as the library interface is properly thought
out.

Signed-off-by: Jani Nikula <jani@nikula.org>
---
 lib/notmuch.h |    3 +++
 lib/query.cc  |   26 ++++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/lib/notmuch.h b/lib/notmuch.h
index c4330e4..b5ef030 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -457,6 +457,9 @@ notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
 notmuch_sort_t
 notmuch_query_get_sort (notmuch_query_t *query);
 
+void
+notmuch_query_set_maxitems (notmuch_query_t *query, unsigned int maxitems);
+
 /* Execute a query for threads, returning a notmuch_threads_t object
  * which can be used to iterate over the results. The returned threads
  * object is owned by the query and as such, will only be valid until
diff --git a/lib/query.cc b/lib/query.cc
index 6f02b04..04dfbc5 100644
--- a/lib/query.cc
+++ b/lib/query.cc
@@ -27,6 +27,7 @@ struct _notmuch_query {
     notmuch_database_t *notmuch;
     const char *query_string;
     notmuch_sort_t sort;
+    Xapian::doccount maxitems;
 };
 
 typedef struct _notmuch_mset_messages {
@@ -76,6 +77,8 @@ notmuch_query_create (notmuch_database_t *notmuch,
 
     query->sort = NOTMUCH_SORT_NEWEST_FIRST;
 
+    query->maxitems = 0;
+
     return query;
 }
 
@@ -97,6 +100,12 @@ notmuch_query_get_sort (notmuch_query_t *query)
     return query->sort;
 }
 
+void
+notmuch_query_set_maxitems(notmuch_query_t *query, unsigned int maxitems)
+{
+    query->maxitems = maxitems;
+}
+
 /* We end up having to call the destructors explicitly because we had
  * to use "placement new" in order to initialize C++ objects within a
  * block that we allocated with talloc. So C++ is making talloc
@@ -181,8 +190,21 @@ notmuch_query_search_messages (notmuch_query_t *query)
 
 	mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
 
-	messages->iterator = mset.begin ();
-	messages->iterator_end = mset.end ();
+	if (query->maxitems && query->maxitems < mset.size()) {
+	    if (query->sort == NOTMUCH_SORT_OLDEST_FIRST) {
+		/* Sort oldest first, but return the newest messages. */
+		messages->iterator = mset[mset.size() - query->maxitems];
+		messages->iterator_end = mset.end ();
+	    } else {
+		/* This path could be optimized by using maxitems in
+		 * enquire.get_mset(). */
+		messages->iterator = mset.begin ();
+		messages->iterator_end = mset[query->maxitems];
+	    }
+	} else {
+	    messages->iterator = mset.begin ();
+	    messages->iterator_end = mset.end ();
+	}
 
 	return &messages->base;
 
-- 
1.7.5.4


Thread: