From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id E33D4429E28 for ; Fri, 28 Oct 2011 13:59:44 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qLdbw+KNXXul for ; Fri, 28 Oct 2011 13:59:44 -0700 (PDT) Received: from mail-fx0-f53.google.com (mail-fx0-f53.google.com [209.85.161.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 07EA2429E21 for ; Fri, 28 Oct 2011 13:59:43 -0700 (PDT) Received: by mail-fx0-f53.google.com with SMTP id i28so4419077faa.26 for ; Fri, 28 Oct 2011 13:59:43 -0700 (PDT) Received: by 10.223.61.211 with SMTP id u19mr8516568fah.29.1319835583698; Fri, 28 Oct 2011 13:59:43 -0700 (PDT) Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi. [80.220.92.23]) by mx.google.com with ESMTPS id a21sm19428171fao.18.2011.10.28.13.59.41 (version=SSLv3 cipher=OTHER); Fri, 28 Oct 2011 13:59:42 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [RFC PATCH 1/3] lib: add support for limiting the number of search results Date: Fri, 28 Oct 2011 23:59:29 +0300 Message-Id: <60f260275738ed04c611223316fd6ee433210dfb.1319833617.git.jani@nikula.org> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: References: In-Reply-To: References: Cc: amdragon@mit.edu X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 28 Oct 2011 20:59:45 -0000 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 --- 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