Re: compile error of current git on F15
[notmuch-archives.git] / f3 / 70088534d5dc13db6cb1b9ee0df71296bd3a22
1 Return-Path: <keithp@keithp.com>\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 D65B1431FBF\r
6         for <notmuch@notmuchmail.org>; Fri, 20 Nov 2009 23:15:21 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id gqoniYFNYKxp for <notmuch@notmuchmail.org>;\r
11         Fri, 20 Nov 2009 23:15:17 -0800 (PST)\r
12 Received: from keithp.com (home.keithp.com [63.227.221.253])\r
13         by olra.theworths.org (Postfix) with ESMTP id 64B03431FAE\r
14         for <notmuch@notmuchmail.org>; Fri, 20 Nov 2009 23:15:17 -0800 (PST)\r
15 Received: from localhost (localhost [127.0.0.1])\r
16         by keithp.com (Postfix) with ESMTP id CC404760222\r
17         for <notmuch@notmuchmail.org>; Fri, 20 Nov 2009 23:15:16 -0800 (PST)\r
18 X-Virus-Scanned: Debian amavisd-new at keithp.com\r
19 Received: from keithp.com ([127.0.0.1])\r
20         by localhost (keithp.com [127.0.0.1]) (amavisd-new, port 10024)\r
21         with LMTP id H3w9F2ijbayT; Fri, 20 Nov 2009 23:15:12 -0800 (PST)\r
22 Received: by keithp.com (Postfix, from userid 1033)\r
23         id C5AC1B9404C; Fri, 20 Nov 2009 23:15:12 -0800 (PST)\r
24 Received: from koto.keithp.com (localhost [127.0.0.1])\r
25         by keithp.com (Postfix) with ESMTP id C0C9076012C;\r
26         Fri, 20 Nov 2009 23:15:12 -0800 (PST)\r
27 Received: by koto.keithp.com (Postfix, from userid 1488)\r
28         id 6C3681982A8; Fri, 20 Nov 2009 23:15:12 -0800 (PST)\r
29 From: Keith Packard <keithp@keithp.com>\r
30 To: notmuch@notmuchmail.org\r
31 Date: Fri, 20 Nov 2009 23:15:07 -0800\r
32 Message-Id: <1258787708-21121-2-git-send-email-keithp@keithp.com>\r
33 X-Mailer: git-send-email 1.6.5.2\r
34 In-Reply-To: <1258787708-21121-1-git-send-email-keithp@keithp.com>\r
35 References: <1258787708-21121-1-git-send-email-keithp@keithp.com>\r
36 Subject: [notmuch] [PATCH 2/3] Add 'notmuch count' command to show the count\r
37         of matching messages\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.12\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42         <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Sat, 21 Nov 2009 07:15:22 -0000\r
51 \r
52 Getting the count of matching threads or messages is a fairly\r
53 expensive operation. Xapian provides a very efficient mechanism that\r
54 returns an approximate value, so use that for this new command.\r
55 \r
56 This returns the number of matching messages, not threads, as that is\r
57 cheap to compute.\r
58 \r
59 Signed-off-by: Keith Packard <keithp@keithp.com>\r
60 ---\r
61  Makefile.local   |    1 +\r
62  lib/notmuch.h    |    8 ++++\r
63  lib/query.cc     |   52 ++++++++++++++++++++++++++\r
64  notmuch-client.h |    3 +\r
65  notmuch-count.c  |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
66  notmuch.c        |   11 +++++\r
67  6 files changed, 184 insertions(+), 0 deletions(-)\r
68  create mode 100644 notmuch-count.c\r
69 \r
70 diff --git a/Makefile.local b/Makefile.local\r
71 index 3c99624..cbd75ce 100644\r
72 --- a/Makefile.local\r
73 +++ b/Makefile.local\r
74 @@ -5,6 +5,7 @@ emacs: notmuch.elc\r
75  notmuch_client_srcs =          \\r
76         notmuch.c               \\r
77         notmuch-config.c        \\r
78 +       notmuch-count.c         \\r
79         notmuch-dump.c          \\r
80         notmuch-new.c           \\r
81         notmuch-reply.c         \\r
82 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
83 index cc713a3..937f3b6 100644\r
84 --- a/lib/notmuch.h\r
85 +++ b/lib/notmuch.h\r
86 @@ -459,6 +459,14 @@ notmuch_threads_advance (notmuch_threads_t *threads);\r
87  void\r
88  notmuch_threads_destroy (notmuch_threads_t *threads);\r
89  \r
90 +/* Return an estimate of the number of messages matching a search\r
91 + *\r
92 + * This function performs a search and returns Xapian's best\r
93 + * guess as to number of matching messages.\r
94 + */\r
95 +unsigned\r
96 +notmuch_query_count_messages (notmuch_query_t *query);\r
97\r
98  /* Get the thread ID of 'thread'.\r
99   *\r
100   * The returned string belongs to 'thread' and as such, should not be\r
101 diff --git a/lib/query.cc b/lib/query.cc\r
102 index ea521dd..dff9634 100644\r
103 --- a/lib/query.cc\r
104 +++ b/lib/query.cc\r
105 @@ -278,3 +278,55 @@ notmuch_threads_destroy (notmuch_threads_t *threads)\r
106  {\r
107      talloc_free (threads);\r
108  }\r
109 +\r
110 +unsigned\r
111 +notmuch_query_count_messages (notmuch_query_t *query)\r
112 +{\r
113 +    notmuch_database_t *notmuch = query->notmuch;\r
114 +    const char *query_string = query->query_string;\r
115 +    Xapian::doccount count;\r
116 +\r
117 +    try {\r
118 +       Xapian::Enquire enquire (*notmuch->xapian_db);\r
119 +       Xapian::Query mail_query (talloc_asprintf (query, "%s%s",\r
120 +                                                  _find_prefix ("type"),\r
121 +                                                  "mail"));\r
122 +       Xapian::Query string_query, final_query;\r
123 +       Xapian::MSet mset;\r
124 +       unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |\r
125 +                             Xapian::QueryParser::FLAG_PHRASE |\r
126 +                             Xapian::QueryParser::FLAG_LOVEHATE |\r
127 +                             Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |\r
128 +                             Xapian::QueryParser::FLAG_WILDCARD |\r
129 +                             Xapian::QueryParser::FLAG_PURE_NOT);\r
130 +\r
131 +       if (strcmp (query_string, "") == 0) {\r
132 +           final_query = mail_query;\r
133 +       } else {\r
134 +           string_query = notmuch->query_parser->\r
135 +               parse_query (query_string, flags);\r
136 +           final_query = Xapian::Query (Xapian::Query::OP_AND,\r
137 +                                        mail_query, string_query);\r
138 +       }\r
139 +\r
140 +       enquire.set_weighting_scheme(Xapian::BoolWeight());\r
141 +       enquire.set_docid_order(Xapian::Enquire::ASCENDING);\r
142 +\r
143 +#if DEBUG_QUERY\r
144 +       fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());\r
145 +#endif\r
146 +\r
147 +       enquire.set_query (final_query);\r
148 +\r
149 +       mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());\r
150 +\r
151 +       count = mset.get_matches_estimated();\r
152 +\r
153 +    } catch (const Xapian::Error &error) {\r
154 +       fprintf (stderr, "A Xapian exception occurred: %s\n",\r
155 +                error.get_msg().c_str());\r
156 +       fprintf (stderr, "Query string was: %s\n", query->query_string);\r
157 +    }\r
158 +\r
159 +    return count;\r
160 +}\r
161 diff --git a/notmuch-client.h b/notmuch-client.h\r
162 index b65aa77..9884497 100644\r
163 --- a/notmuch-client.h\r
164 +++ b/notmuch-client.h\r
165 @@ -91,6 +91,9 @@ chomp_newline (char *str)\r
166  }\r
167  \r
168  int\r
169 +notmuch_count_command (void *ctx, int argc, char *argv[]);\r
170 +\r
171 +int\r
172  notmuch_dump_command (void *ctx, int argc, char *argv[]);\r
173  \r
174  int\r
175 diff --git a/notmuch-count.c b/notmuch-count.c\r
176 new file mode 100644\r
177 index 0000000..68e428f\r
178 --- /dev/null\r
179 +++ b/notmuch-count.c\r
180 @@ -0,0 +1,109 @@\r
181 +/* notmuch - Not much of an email program, (just index and search)\r
182 + *\r
183 + * Copyright © 2009 Carl Worth\r
184 + * Copyright © 2009 Keith Packard\r
185 + *\r
186 + * This program is free software: you can redistribute it and/or modify\r
187 + * it under the terms of the GNU General Public License as published by\r
188 + * the Free Software Foundation, either version 3 of the License, or\r
189 + * (at your option) any later version.\r
190 + *\r
191 + * This program is distributed in the hope that it will be useful,\r
192 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
193 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
194 + * GNU General Public License for more details.\r
195 + *\r
196 + * You should have received a copy of the GNU General Public License\r
197 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
198 + *\r
199 + * Author: Keith Packard <keithp@keithp.com>\r
200 + */\r
201 +\r
202 +#include "notmuch-client.h"\r
203 +\r
204 +int\r
205 +notmuch_count_command (void *ctx, int argc, char *argv[])\r
206 +{\r
207 +    notmuch_config_t *config;\r
208 +    notmuch_database_t *notmuch;\r
209 +    notmuch_query_t *query;\r
210 +    char *query_str;\r
211 +    char *opt, *end;\r
212 +    int i;\r
213 +#if 0\r
214 +    int i, first = 0, max_threads = -1;\r
215 +    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
216 +#endif\r
217 +\r
218 +    for (i = 0; i < argc && argv[i][0] == '-'; i++) {\r
219 +       if (strcmp (argv[i], "--") == 0) {\r
220 +           i++;\r
221 +           break;\r
222 +       }\r
223 +#if 0\r
224 +       if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {\r
225 +           opt = argv[i] + sizeof ("--first=") - 1;\r
226 +           first = strtoul (opt, &end, 10);\r
227 +           if (*opt == '\0' || *end != '\0') {\r
228 +               fprintf (stderr, "Invalid value for --first: %s\n", opt);\r
229 +               return 1;\r
230 +           }\r
231 +       } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {\r
232 +           opt = argv[i] + sizeof ("--max-threads=") - 1;\r
233 +           max_threads = strtoul (opt, &end, 10);\r
234 +           if (*opt == '\0' || *end != '\0') {\r
235 +               fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);\r
236 +               return 1;\r
237 +           }\r
238 +       } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {\r
239 +           opt = argv[i] + sizeof ("--sort=") - 1;\r
240 +           if (strcmp (opt, "oldest-first") == 0) {\r
241 +               sort = NOTMUCH_SORT_OLDEST_FIRST;\r
242 +           } else if (strcmp (opt, "newest-first") == 0) {\r
243 +               sort = NOTMUCH_SORT_NEWEST_FIRST;\r
244 +           } else {\r
245 +               fprintf (stderr, "Invalid value for --sort: %s\n", opt);\r
246 +               return 1;\r
247 +           }\r
248 +       } else\r
249 +#endif\r
250 +       {\r
251 +           fprintf (stderr, "Unrecognized option: %s\n", argv[i]);\r
252 +           return 1;\r
253 +       }\r
254 +    }\r
255 +\r
256 +    argc -= i;\r
257 +    argv += i;\r
258 +\r
259 +    config = notmuch_config_open (ctx, NULL, NULL);\r
260 +    if (config == NULL)\r
261 +       return 1;\r
262 +\r
263 +    notmuch = notmuch_database_open (notmuch_config_get_database_path (config));\r
264 +    if (notmuch == NULL)\r
265 +       return 1;\r
266 +\r
267 +    query_str = query_string_from_args (ctx, argc, argv);\r
268 +    if (query_str == NULL) {\r
269 +       fprintf (stderr, "Out of memory.\n");\r
270 +       return 1;\r
271 +    }\r
272 +    if (*query_str == '\0') {\r
273 +       fprintf (stderr, "Error: notmuch count requires at least one count term.\n");\r
274 +       return 1;\r
275 +    }\r
276 +\r
277 +    query = notmuch_query_create (notmuch, query_str);\r
278 +    if (query == NULL) {\r
279 +       fprintf (stderr, "Out of memory\n");\r
280 +       return 1;\r
281 +    }\r
282 +\r
283 +    printf ("%u\n", notmuch_query_count_messages(query));\r
284 +\r
285 +    notmuch_query_destroy (query);\r
286 +    notmuch_database_close (notmuch);\r
287 +\r
288 +    return 0;\r
289 +}\r
290 diff --git a/notmuch.c b/notmuch.c\r
291 index 5cc8e4c..8387692 100644\r
292 --- a/notmuch.c\r
293 +++ b/notmuch.c\r
294 @@ -179,6 +179,17 @@ command_t commands[] = {\r
295        "\n"\r
296        "\t\tSee \"notmuch help search-terms\" for details of the search\n"\r
297        "\t\tterms syntax." },\r
298 +    { "count", notmuch_count_command,\r
299 +      "<search-terms> [...]",\r
300 +      "\t\tCount messages matching the search terms.",\r
301 +      "\t\tThe number of matching messages is output to stdout.\n"\r
302 +      "\n"\r
303 +      "\t\tA common use of \"notmuch count\" is to display the count\n"\r
304 +      "\t\tof messages matching both a specific tag and either inbox\n"\r
305 +      "\t\tor unread\n"\r
306 +      "\n"\r
307 +      "\t\tSee \"notmuch help search-terms\" for details of the search\n"\r
308 +      "\t\tterms syntax." },\r
309      { "reply", notmuch_reply_command,\r
310        "<search-terms> [...]",\r
311        "\t\tConstruct a reply template for a set of messages.",\r
312 -- \r
313 1.6.5.2\r
314 \r