[PATCH v7 1/6] lib: add function to get the number of threads matching a search
authorJani Nikula <jani@nikula.org>
Tue, 15 Nov 2011 20:08:48 +0000 (22:08 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:40:11 +0000 (09:40 -0800)
09/a644f4cf1615f005d7dfb317f8f11485aa3226 [new file with mode: 0644]

diff --git a/09/a644f4cf1615f005d7dfb317f8f11485aa3226 b/09/a644f4cf1615f005d7dfb317f8f11485aa3226
new file mode 100644 (file)
index 0000000..3baebda
--- /dev/null
@@ -0,0 +1,147 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 7B5FC431FB6\r
+       for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:07 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id W8jUqyUlzrUL for <notmuch@notmuchmail.org>;\r
+       Tue, 15 Nov 2011 12:09:03 -0800 (PST)\r
+Received: from mail-fx0-f53.google.com (mail-fx0-f53.google.com\r
+       [209.85.161.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 14A44429E21\r
+       for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:02 -0800 (PST)\r
+Received: by mail-fx0-f53.google.com with SMTP id n15so895260faa.26\r
+       for <notmuch@notmuchmail.org>; Tue, 15 Nov 2011 12:09:02 -0800 (PST)\r
+Received: by 10.204.152.87 with SMTP id f23mr17256585bkw.18.1321387742606;\r
+       Tue, 15 Nov 2011 12:09:02 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
+       [80.220.92.23])\r
+       by mx.google.com with ESMTPS id j4sm672364fae.3.2011.11.15.12.09.00\r
+       (version=SSLv3 cipher=OTHER); Tue, 15 Nov 2011 12:09:01 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v7 1/6] lib: add function to get the number of threads\r
+       matching a search\r
+Date: Tue, 15 Nov 2011 22:08:48 +0200\r
+Message-Id:\r
+ <3f852070445fb2eee88eae9c8c086dec7c533d7a.1321386762.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1321386762.git.jani@nikula.org>\r
+References: <cover.1321386762.git.jani@nikula.org>\r
+In-Reply-To: <cover.1321386762.git.jani@nikula.org>\r
+References: <cover.1321386762.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 15 Nov 2011 20:09:07 -0000\r
+\r
+Add function notmuch_query_count_threads() to get the number of threads\r
+matching a search. This is done by performing a search and figuring out the\r
+number of unique thread IDs in the matching messages, a significantly\r
+heavier operation than notmuch_query_count_messages().\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ lib/notmuch.h |   14 ++++++++++++++\r
+ lib/query.cc  |   44 ++++++++++++++++++++++++++++++++++++++++++++\r
+ 2 files changed, 58 insertions(+), 0 deletions(-)\r
+\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index c4330e4..9f23a10 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -609,6 +609,20 @@ notmuch_threads_destroy (notmuch_threads_t *threads);\r
+ unsigned\r
+ notmuch_query_count_messages (notmuch_query_t *query);\r
+  \r
++/* Return the number of threads matching a search.\r
++ *\r
++ * This function performs a search and returns the number of unique thread IDs\r
++ * in the matching messages. This is the same as number of threads matching a\r
++ * search.\r
++ *\r
++ * Note that this is a significantly heavier operation than\r
++ * notmuch_query_count_messages().\r
++ *\r
++ * If an error occurs, this function may return 0.\r
++ */\r
++unsigned\r
++notmuch_query_count_threads (notmuch_query_t *query);\r
++\r
+ /* Get the thread ID of 'thread'.\r
+  *\r
+  * The returned string belongs to 'thread' and as such, should not be\r
+diff --git a/lib/query.cc b/lib/query.cc\r
+index 6f02b04..b6c0f12 100644\r
+--- a/lib/query.cc\r
++++ b/lib/query.cc\r
+@@ -457,3 +457,47 @@ notmuch_query_count_messages (notmuch_query_t *query)\r
\r
+     return count;\r
+ }\r
++\r
++unsigned\r
++notmuch_query_count_threads (notmuch_query_t *query)\r
++{\r
++    notmuch_messages_t *messages;\r
++    GHashTable *hash;\r
++    unsigned int count;\r
++    notmuch_sort_t sort;\r
++\r
++    sort = query->sort;\r
++    query->sort = NOTMUCH_SORT_UNSORTED;\r
++    messages = notmuch_query_search_messages (query);\r
++    query->sort = sort;\r
++    if (messages == NULL)\r
++      return 0;\r
++\r
++    hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);\r
++    if (hash == NULL) {\r
++      talloc_free (messages);\r
++      return 0;\r
++    }\r
++\r
++    while (notmuch_messages_valid (messages)) {\r
++      notmuch_message_t *message = notmuch_messages_get (messages);\r
++      const char *thread_id = notmuch_message_get_thread_id (message);\r
++      char *thread_id_copy = talloc_strdup (messages, thread_id);\r
++      if (unlikely (thread_id_copy == NULL)) {\r
++          notmuch_message_destroy (message);\r
++          count = 0;\r
++          goto DONE;\r
++      }\r
++      g_hash_table_insert (hash, thread_id_copy, NULL);\r
++      notmuch_message_destroy (message);\r
++      notmuch_messages_move_to_next (messages);\r
++    }\r
++\r
++    count = g_hash_table_size (hash);\r
++\r
++  DONE:\r
++    g_hash_table_unref (hash);\r
++    talloc_free (messages);\r
++\r
++    return count;\r
++}\r
+-- \r
+1.7.5.4\r
+\r