Re: Avoiding the "huge INBOX of death"
[notmuch-archives.git] / 4b / 36fa0d037c5ab68c77ba3275fafbe00463a667
1 Return-Path: <sojkam1@fel.cvut.cz>\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 E6F8A429E2E\r
6         for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 07:51:27 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id aIsqRaU+i1tm for <notmuch@notmuchmail.org>;\r
16         Mon, 27 Oct 2014 07:51:20 -0700 (PDT)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id 201B0431FDD\r
19         for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 07:51:16 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 68D1C5CCF92\r
22         for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 15:51:15 +0100 (CET)\r
23 X-Virus-Scanned: IMAP STYX AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
26         port 10044) with ESMTP id O_3Xs0EmMl-a for <notmuch@notmuchmail.org>;\r
27         Mon, 27 Oct 2014 15:51:12 +0100 (CET)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 9E1545CCF99\r
30         for <notmuch@notmuchmail.org>; Mon, 27 Oct 2014 15:51:08 +0100 (CET)\r
31 Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
32         (envelope-from <sojkam1@fel.cvut.cz>)\r
33         id 1XildT-0000ox-KU; Mon, 27 Oct 2014 15:51:07 +0100\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH v4 6/6] cli: search: Add --output=count\r
37 Date: Mon, 27 Oct 2014 15:50:55 +0100\r
38 Message-Id: <1414421455-3037-7-git-send-email-sojkam1@fel.cvut.cz>\r
39 X-Mailer: git-send-email 2.1.1\r
40 In-Reply-To: <1414421455-3037-1-git-send-email-sojkam1@fel.cvut.cz>\r
41 References: <1414421455-3037-1-git-send-email-sojkam1@fel.cvut.cz>\r
42 MIME-Version: 1.0\r
43 Content-Type: text/plain; charset=UTF-8\r
44 Content-Transfer-Encoding: 8bit\r
45 X-BeenThere: notmuch@notmuchmail.org\r
46 X-Mailman-Version: 2.1.13\r
47 Precedence: list\r
48 List-Id: "Use and development of the notmuch mail system."\r
49         <notmuch.notmuchmail.org>\r
50 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
51         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
52 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
53 List-Post: <mailto:notmuch@notmuchmail.org>\r
54 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
55 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
56         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
57 X-List-Received-Date: Mon, 27 Oct 2014 14:51:28 -0000\r
58 \r
59 This output can be used with --output=recipients or --output=sender and\r
60 in addition to the mailboxes, it prints how many times was each mailbox\r
61 encountered during search.\r
62 ---\r
63  completion/notmuch-completion.bash |  2 +-\r
64  completion/notmuch-completion.zsh  |  2 +-\r
65  doc/man1/notmuch-search.rst        |  9 +++++--\r
66  notmuch-search.c                   | 47 ++++++++++++++++++++++++++++++-----\r
67  test/T090-search-output.sh         | 50 ++++++++++++++++++++++++++++++++++++++\r
68  5 files changed, 100 insertions(+), 10 deletions(-)\r
69 \r
70 diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
71 index 6b6d43a..b625b02 100644\r
72 --- a/completion/notmuch-completion.bash\r
73 +++ b/completion/notmuch-completion.bash\r
74 @@ -294,7 +294,7 @@ _notmuch_search()\r
75             return\r
76             ;;\r
77         --output)\r
78 -           COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients" -- "${cur}" ) )\r
79 +           COMPREPLY=( $( compgen -W "summary threads messages files tags sender recipients count" -- "${cur}" ) )\r
80             return\r
81             ;;\r
82         --sort)\r
83 diff --git a/completion/notmuch-completion.zsh b/completion/notmuch-completion.zsh\r
84 index 3e535df..c1ccc32 100644\r
85 --- a/completion/notmuch-completion.zsh\r
86 +++ b/completion/notmuch-completion.zsh\r
87 @@ -53,7 +53,7 @@ _notmuch_search()\r
88      '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \\r
89      '--first=[omit the first x threads from the search results]:number of threads to omit: ' \\r
90      '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \\r
91 -    '--output=[select what to output]:output:((summary threads messages files tags sender recipients))' \\r
92 +    '--output=[select what to output]:output:((summary threads messages files tags sender recipients count))' \\r
93      '--filter-by=[filter out duplicate addresses]:filter-by:((nameaddr\:"both name and address part" name\:"name part" addr\:"address part" addrfold\:"case-insensitive address part" nameaddrfold\:"name and case-insensitive address part"))'\r
94  }\r
95  \r
96 diff --git a/doc/man1/notmuch-search.rst b/doc/man1/notmuch-search.rst\r
97 index 84af2da..4b408f6 100644\r
98 --- a/doc/man1/notmuch-search.rst\r
99 +++ b/doc/man1/notmuch-search.rst\r
100 @@ -97,9 +97,14 @@ Supported options for **search** include\r
101              Like **sender** but for addresses from *To*, *Cc* and\r
102             *Bcc* headers.\r
103  \r
104 +       **count**\r
105 +           Can be used in combination with **sender** or\r
106 +           **recipients** to print the count of how many times was\r
107 +           the address encountered during search.\r
108 +\r
109         This option can be given multiple times to combine different\r
110 -       outputs. Currently, this is only supported for **sender** and\r
111 -       **recipients** outputs.\r
112 +       outputs. Currently, this is only supported for **sender**,\r
113 +       **recipients** and **count** outputs.\r
114  \r
115      ``--sort=``\ (**newest-first**\ \|\ **oldest-first**)\r
116          This option can be used to present results in either\r
117 diff --git a/notmuch-search.c b/notmuch-search.c\r
118 index 47aa979..41f4107 100644\r
119 --- a/notmuch-search.c\r
120 +++ b/notmuch-search.c\r
121 @@ -30,9 +30,10 @@ typedef enum {\r
122      OUTPUT_TAGS                = 1 << 4,\r
123      OUTPUT_SENDER      = 1 << 5,\r
124      OUTPUT_RECIPIENTS  = 1 << 6,\r
125 +    OUTPUT_COUNT       = 1 << 7,\r
126  } output_t;\r
127  \r
128 -#define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)\r
129 +#define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS | OUTPUT_COUNT)\r
130  \r
131  typedef enum {\r
132      FILTER_BY_NAMEADDR = 0,\r
133 @@ -56,6 +57,7 @@ typedef struct {\r
134  typedef struct {\r
135      const char *name;\r
136      const char *addr;\r
137 +    int count;\r
138  } mailbox_t;\r
139  \r
140  /* Return two stable query strings that identify exactly the matched\r
141 @@ -244,6 +246,7 @@ check_duplicite (const search_options_t *opt, GHashTable *addrs, const char *nam\r
142  {\r
143      notmuch_bool_t duplicite;\r
144      char *key;\r
145 +    mailbox_t *mailbox;\r
146  \r
147      if (opt->filter_by == FILTER_BY_ADDRFOLD ||\r
148         opt->filter_by == FILTER_BY_NAMEADDRFOLD) {\r
149 @@ -274,12 +277,18 @@ check_duplicite (const search_options_t *opt, GHashTable *addrs, const char *nam\r
150      if (! key)\r
151         return FALSE;\r
152  \r
153 -    duplicite = g_hash_table_lookup_extended (addrs, key, NULL, NULL);\r
154 +    duplicite = g_hash_table_lookup_extended (addrs, key, NULL, (gpointer)&mailbox);\r
155  \r
156 -    if (! duplicite)\r
157 -       g_hash_table_insert (addrs, key, NULL);\r
158 -    else\r
159 +    if (! duplicite) {\r
160 +       mailbox = talloc (opt->format, mailbox_t);\r
161 +       mailbox->name = talloc_strdup (mailbox, name);\r
162 +       mailbox->addr = talloc_strdup (mailbox, addr);\r
163 +       mailbox->count = 1;\r
164 +       g_hash_table_insert (addrs, key, mailbox);\r
165 +    } else {\r
166 +       mailbox->count++;\r
167         talloc_free (key);\r
168 +    }\r
169  \r
170      return duplicite;\r
171  }\r
172 @@ -289,6 +298,7 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
173  {\r
174      const char *name = mailbox->name;\r
175      const char *addr = mailbox->addr;\r
176 +    int count = mailbox->count;\r
177  \r
178      if (opt->format->is_text_printer) {\r
179         char *mailbox_str;\r
180 @@ -302,6 +312,10 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
181             fprintf (stderr, "Error: out of memory\n");\r
182             return;\r
183         }\r
184 +       if (count > 0) {\r
185 +           opt->format->integer (opt->format, count);\r
186 +           opt->format->string (opt->format, "\t");\r
187 +       }\r
188         opt->format->string (opt->format, mailbox_str);\r
189         opt->format->separator (opt->format);\r
190  \r
191 @@ -312,6 +326,10 @@ print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)\r
192         opt->format->string (opt->format, name);\r
193         opt->format->map_key (opt->format, "address");\r
194         opt->format->string (opt->format, addr);\r
195 +       if (count > 0) {\r
196 +           opt->format->map_key (opt->format, "count");\r
197 +           opt->format->integer (opt->format, count);\r
198 +       }\r
199         opt->format->end (opt->format);\r
200         opt->format->separator (opt->format);\r
201      }\r
202 @@ -341,11 +359,15 @@ process_address_list (const search_options_t *opt, GHashTable *addrs,\r
203             mailbox_t mbx = {\r
204                 .name = internet_address_get_name (address),\r
205                 .addr = internet_address_mailbox_get_addr (mailbox),\r
206 +               .count = 0,\r
207             };\r
208  \r
209             if (check_duplicite (opt, addrs, mbx.name, mbx.addr))\r
210                 continue;\r
211  \r
212 +           if (opt->output & OUTPUT_COUNT)\r
213 +               continue;\r
214 +\r
215             print_mailbox (opt, &mbx);\r
216         }\r
217      }\r
218 @@ -372,6 +394,15 @@ _my_talloc_free_for_g_hash (void *ptr)\r
219      talloc_free (ptr);\r
220  }\r
221  \r
222 +static void\r
223 +print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)\r
224 +{\r
225 +    const mailbox_t *mailbox = value;\r
226 +    search_options_t *opt = user_data;\r
227 +\r
228 +    print_mailbox (opt, mailbox);\r
229 +}\r
230 +\r
231  static int\r
232  do_search_messages (search_options_t *opt)\r
233  {\r
234 @@ -384,7 +415,7 @@ do_search_messages (search_options_t *opt)\r
235  \r
236      if (opt->output & OUTPUT_ADDRESS_FLAGS)\r
237         addresses = g_hash_table_new_full (g_str_hash, g_str_equal,\r
238 -                                          _my_talloc_free_for_g_hash, NULL);\r
239 +                                          _my_talloc_free_for_g_hash, _my_talloc_free_for_g_hash);\r
240  \r
241      if (opt->offset < 0) {\r
242         opt->offset += notmuch_query_count_messages (opt->query);\r
243 @@ -451,6 +482,9 @@ do_search_messages (search_options_t *opt)\r
244         notmuch_message_destroy (message);\r
245      }\r
246  \r
247 +    if (addresses && opt->output & OUTPUT_COUNT)\r
248 +       g_hash_table_foreach (addresses, print_hash_value, opt);\r
249 +\r
250      if (addresses)\r
251         g_hash_table_unref (addresses);\r
252  \r
253 @@ -554,6 +588,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
254                                   { "recipients", OUTPUT_RECIPIENTS },\r
255                                   { "files", OUTPUT_FILES },\r
256                                   { "tags", OUTPUT_TAGS },\r
257 +                                 { "count", OUTPUT_COUNT },\r
258                                   { 0, 0 } } },\r
259          { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',\r
260            (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },\r
261 diff --git a/test/T090-search-output.sh b/test/T090-search-output.sh\r
262 index 841a721..5a9bbc9 100755\r
263 --- a/test/T090-search-output.sh\r
264 +++ b/test/T090-search-output.sh\r
265 @@ -433,6 +433,56 @@ cat <<EOF >EXPECTED\r
266  EOF\r
267  test_expect_equal_file OUTPUT EXPECTED\r
268  \r
269 +test_begin_subtest "--output=sender --output=count"\r
270 +notmuch search --output=sender --output=count '*' | sort -n >OUTPUT\r
271 +cat <<EOF >EXPECTED\r
272 +1      Adrian Perez de Castro <aperez@igalia.com>\r
273 +1      Aron Griffis <agriffis@n01se.net>\r
274 +1      Chris Wilson <chris@chris-wilson.co.uk>\r
275 +1      François Boulogne <boulogne.f@gmail.com>\r
276 +1      Ingmar Vanhassel <ingmar@exherbo.org>\r
277 +1      Israel Herraiz <isra@herraiz.org>\r
278 +1      Olivier Berger <olivier.berger@it-sudparis.eu>\r
279 +1      Rolland Santimano <rollandsantimano@yahoo.com>\r
280 +2      Alex Botero-Lowry <alex.boterolowry@gmail.com>\r
281 +2      Jjgod Jiang <gzjjgod@gmail.com>\r
282 +3      Stewart Smith <stewart@flamingspork.com>\r
283 +4      Alexander Botero-Lowry <alex.boterolowry@gmail.com>\r
284 +4      Jan Janak <jan@ryngle.com>\r
285 +5      Lars Kellogg-Stedman <lars@seas.harvard.edu>\r
286 +5      Mikhail Gusarov <dottedmag@dottedmag.net>\r
287 +7      Keith Packard <keithp@keithp.com>\r
288 +12     Carl Worth <cworth@cworth.org>\r
289 +EOF\r
290 +test_expect_equal_file OUTPUT EXPECTED\r
291 +\r
292 +test_begin_subtest "--output=sender --output=count --format=json"\r
293 +# Since the iteration order of GHashTable is not specified, we\r
294 +# preprocess and sort the results to keep the order stable here.\r
295 +notmuch search --output=sender --output=count --format=json '*' | \\r
296 +    sed -e 's/^\[//' -e 's/]$//' -e 's/,$//' | \\r
297 +    sort --field-separator=":" --key=4n --key=2 >OUTPUT\r
298 +cat <<EOF >EXPECTED\r
299 +{"name": "Adrian Perez de Castro", "address": "aperez@igalia.com", "count": 1}\r
300 +{"name": "Aron Griffis", "address": "agriffis@n01se.net", "count": 1}\r
301 +{"name": "Chris Wilson", "address": "chris@chris-wilson.co.uk", "count": 1}\r
302 +{"name": "François Boulogne", "address": "boulogne.f@gmail.com", "count": 1}\r
303 +{"name": "Ingmar Vanhassel", "address": "ingmar@exherbo.org", "count": 1}\r
304 +{"name": "Israel Herraiz", "address": "isra@herraiz.org", "count": 1}\r
305 +{"name": "Olivier Berger", "address": "olivier.berger@it-sudparis.eu", "count": 1}\r
306 +{"name": "Rolland Santimano", "address": "rollandsantimano@yahoo.com", "count": 1}\r
307 +{"name": "Alex Botero-Lowry", "address": "alex.boterolowry@gmail.com", "count": 2}\r
308 +{"name": "Jjgod Jiang", "address": "gzjjgod@gmail.com", "count": 2}\r
309 +{"name": "Stewart Smith", "address": "stewart@flamingspork.com", "count": 3}\r
310 +{"name": "Alexander Botero-Lowry", "address": "alex.boterolowry@gmail.com", "count": 4}\r
311 +{"name": "Jan Janak", "address": "jan@ryngle.com", "count": 4}\r
312 +{"name": "Lars Kellogg-Stedman", "address": "lars@seas.harvard.edu", "count": 5}\r
313 +{"name": "Mikhail Gusarov", "address": "dottedmag@dottedmag.net", "count": 5}\r
314 +{"name": "Keith Packard", "address": "keithp@keithp.com", "count": 7}\r
315 +{"name": "Carl Worth", "address": "cworth@cworth.org", "count": 12}\r
316 +EOF\r
317 +test_expect_equal_file OUTPUT EXPECTED\r
318 +\r
319  test_begin_subtest "--output=recipients"\r
320  notmuch search --output=recipients '*' >OUTPUT\r
321  cat <<EOF >EXPECTED\r
322 -- \r
323 2.1.1\r
324 \r