Re: [PATCH 1/2] Add Google Inc. to AUTHORS as a contributor.
[notmuch-archives.git] / 41 / dcdd335477f4cb2e480fe08531b2d5ca3e4730
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 99D84429E2F\r
6         for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:19 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id tHuALSwtCkHS for <notmuch@notmuchmail.org>;\r
17         Mon, 31 Oct 2011 14:18:18 -0700 (PDT)\r
18 Received: from mail-fx0-f53.google.com (mail-fx0-f53.google.com\r
19         [209.85.161.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 04CA5431FD0\r
22         for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:17 -0700 (PDT)\r
23 Received: by mail-fx0-f53.google.com with SMTP id i28so6745606faa.26\r
24         for <notmuch@notmuchmail.org>; Mon, 31 Oct 2011 14:18:17 -0700 (PDT)\r
25 Received: by 10.223.7.20 with SMTP id b20mr31807068fab.21.1320095897670;\r
26         Mon, 31 Oct 2011 14:18:17 -0700 (PDT)\r
27 Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
28         [80.220.92.23])\r
29         by mx.google.com with ESMTPS id m4sm13703384fad.1.2011.10.31.14.18.15\r
30         (version=SSLv3 cipher=OTHER); Mon, 31 Oct 2011 14:18:16 -0700 (PDT)\r
31 From: Jani Nikula <jani@nikula.org>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [RFC PATCH v2 1/3] lib: add function to get the number of threads\r
34         matching a search\r
35 Date: Mon, 31 Oct 2011 23:18:08 +0200\r
36 Message-Id:\r
37  <fe6097ce0e98fabe88b19bc4e674a069aeee3a3d.1320093940.git.jani@nikula.org>\r
38 X-Mailer: git-send-email 1.7.5.4\r
39 In-Reply-To: <cover.1320093940.git.jani@nikula.org>\r
40 References: <cover.1320093940.git.jani@nikula.org>\r
41 In-Reply-To: <cover.1320093940.git.jani@nikula.org>\r
42 References: <cover.1320093940.git.jani@nikula.org>\r
43 Cc: amdragon@mit.edu\r
44 X-BeenThere: notmuch@notmuchmail.org\r
45 X-Mailman-Version: 2.1.13\r
46 Precedence: list\r
47 List-Id: "Use and development of the notmuch mail system."\r
48         <notmuch.notmuchmail.org>\r
49 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
50         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
51 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
52 List-Post: <mailto:notmuch@notmuchmail.org>\r
53 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
54 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
56 X-List-Received-Date: Mon, 31 Oct 2011 21:18:20 -0000\r
57 \r
58 Add function notmuch_query_count_threads() to get the number of threads\r
59 matching a search. This is done by performing a search and figuring out the\r
60 number of unique thread IDs in the matching messages, a significantly\r
61 heavier operation than notmuch_query_count_messages().\r
62 \r
63 Signed-off-by: Jani Nikula <jani@nikula.org>\r
64 ---\r
65  lib/notmuch.h |   14 ++++++++++++++\r
66  lib/query.cc  |   40 ++++++++++++++++++++++++++++++++++++++++\r
67  2 files changed, 54 insertions(+), 0 deletions(-)\r
68 \r
69 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
70 index c4330e4..662d594 100644\r
71 --- a/lib/notmuch.h\r
72 +++ b/lib/notmuch.h\r
73 @@ -609,6 +609,20 @@ notmuch_threads_destroy (notmuch_threads_t *threads);\r
74  unsigned\r
75  notmuch_query_count_messages (notmuch_query_t *query);\r
76   \r
77 +/* Return the number of threads matching a search.\r
78 + *\r
79 + * This function performs a search and returns the number of unique thread IDs\r
80 + * in the matching messages. This is the same as number of threads matching a\r
81 + * search.\r
82 + *\r
83 + * Note that this is a fairly heavy operation, much more so than\r
84 + * notmuch_query_count_messages().\r
85 + *\r
86 + * If an error occurs, this function may return 0.\r
87 + */\r
88 +unsigned\r
89 +notmuch_query_count_threads (notmuch_query_t *query);\r
90 +\r
91  /* Get the thread ID of 'thread'.\r
92   *\r
93   * The returned string belongs to 'thread' and as such, should not be\r
94 diff --git a/lib/query.cc b/lib/query.cc\r
95 index 6f02b04..2c0610a 100644\r
96 --- a/lib/query.cc\r
97 +++ b/lib/query.cc\r
98 @@ -457,3 +457,43 @@ notmuch_query_count_messages (notmuch_query_t *query)\r
99  \r
100      return count;\r
101  }\r
102 +\r
103 +unsigned\r
104 +notmuch_query_count_threads (notmuch_query_t *query)\r
105 +{\r
106 +    notmuch_messages_t *messages;\r
107 +    GHashTable *hash;\r
108 +    unsigned int count;\r
109 +\r
110 +    messages = notmuch_query_search_messages (query);\r
111 +    if (messages == NULL)\r
112 +       return 0;\r
113 +\r
114 +    hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);\r
115 +    if (hash == NULL) {\r
116 +       talloc_free (messages);\r
117 +       return 0;\r
118 +    }\r
119 +\r
120 +    while (notmuch_messages_valid (messages)) {\r
121 +       notmuch_message_t *message = notmuch_messages_get (messages);\r
122 +       const char *thread_id = notmuch_message_get_thread_id (message);\r
123 +       char *thread_id_copy = talloc_strdup(messages, thread_id);\r
124 +       if (unlikely (thread_id_copy == NULL)) {\r
125 +           notmuch_message_destroy (message);\r
126 +           count = 0;\r
127 +           goto DONE;\r
128 +       }\r
129 +       g_hash_table_insert (hash, thread_id_copy, NULL);\r
130 +       notmuch_message_destroy (message);\r
131 +       notmuch_messages_move_to_next (messages);\r
132 +    }\r
133 +\r
134 +    count = g_hash_table_size(hash);\r
135 +\r
136 +  DONE:\r
137 +    g_hash_table_unref (hash);\r
138 +    talloc_free (messages);\r
139 +\r
140 +    return count;\r
141 +}\r
142 -- \r
143 1.7.5.4\r
144 \r