[PATCH v6 1/6] lib: add function to get the number of threads matching a search
authorJani Nikula <jani@nikula.org>
Sun, 13 Nov 2011 21:15:30 +0000 (23:15 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:40:09 +0000 (09:40 -0800)
02/411442f12d624016383343d619155daa5aa560 [new file with mode: 0644]

diff --git a/02/411442f12d624016383343d619155daa5aa560 b/02/411442f12d624016383343d619155daa5aa560
new file mode 100644 (file)
index 0000000..86cc4d0
--- /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 623AC429E35\r
+       for <notmuch@notmuchmail.org>; Sun, 13 Nov 2011 13:15:48 -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: 1.075\r
+X-Spam-Level: *\r
+X-Spam-Status: No, score=1.075 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7, URIBL_BLACK=1.775] 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 pQx1s2UIaf7C for <notmuch@notmuchmail.org>;\r
+       Sun, 13 Nov 2011 13:15:46 -0800 (PST)\r
+Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com\r
+       [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 812DE429E2E\r
+       for <notmuch@notmuchmail.org>; Sun, 13 Nov 2011 13:15:43 -0800 (PST)\r
+Received: by mail-bw0-f53.google.com with SMTP id q10so6289712bka.26\r
+       for <notmuch@notmuchmail.org>; Sun, 13 Nov 2011 13:15:43 -0800 (PST)\r
+Received: by 10.205.128.15 with SMTP id hc15mr8656045bkc.110.1321218943033;\r
+       Sun, 13 Nov 2011 13:15:43 -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 o7sm17662101bkw.16.2011.11.13.13.15.40\r
+       (version=SSLv3 cipher=OTHER); Sun, 13 Nov 2011 13:15:42 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v6 1/6] lib: add function to get the number of threads\r
+       matching a search\r
+Date: Sun, 13 Nov 2011 23:15:30 +0200\r
+Message-Id:\r
+ <a6b88f21c57d64605f25235a3a82264fc837d252.1321217854.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1321217854.git.jani@nikula.org>\r
+References: <cover.1321217854.git.jani@nikula.org>\r
+In-Reply-To: <cover.1321217854.git.jani@nikula.org>\r
+References: <cover.1321217854.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: Sun, 13 Nov 2011 21:15:48 -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