[PATCH v2 08/14] cli/reply: reduce the reply format abstractions
[notmuch-archives.git] / 0f / bfce1784f3a36d161e6ba497ae2ec8bd0d2d7f
1 Return-Path: <bremner@tesseract.cs.unb.ca>\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 00FE0431FD2\r
6         for <notmuch@notmuchmail.org>; Mon, 29 Dec 2014 04:15:20 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         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 X9JGzWiQ2AMP for <notmuch@notmuchmail.org>;\r
16         Mon, 29 Dec 2014 04:15:16 -0800 (PST)\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
18         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 791CE431FC9\r
21         for <notmuch@notmuchmail.org>; Mon, 29 Dec 2014 04:15:16 -0800 (PST)\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
23         (envelope-from <bremner@tesseract.cs.unb.ca>)\r
24         id 1Y5ZE8-0007Xf-Da; Mon, 29 Dec 2014 08:15:12 -0400\r
25 Received: (nullmailer pid 32219 invoked by uid 1000); Mon, 29 Dec 2014\r
26         12:15:07 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [PATCH] lib: add status return to\r
30         notmuch_query_count_{message,threads}\r
31 Date: Mon, 29 Dec 2014 13:14:51 +0100\r
32 Message-Id: <1419855291-31972-1-git-send-email-david@tethera.net>\r
33 X-Mailer: git-send-email 2.1.3\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.13\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38         <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
40         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
45         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Mon, 29 Dec 2014 12:15:20 -0000\r
47 \r
48 We follow many other notmuch_ functions by having a status return and\r
49 an output parameter.  I decided not to add compatibility wrappers but\r
50 just to break the API because the old API seems particularly awful.\r
51 ---\r
52 I guess this breaks all of the bindings, which serves them right for not having adequate test suites ;).\r
53 \r
54 lib/Makefile.local |  4 ++--\r
55  lib/database.cc    |  8 +++++++-\r
56  lib/notmuch.h      | 26 +++++++++++++++++++-------\r
57  lib/query.cc       | 24 +++++++++++++-----------\r
58  notmuch-count.c    | 14 ++++++++++++--\r
59  notmuch-reply.c    |  8 +++++++-\r
60  notmuch-search.c   | 14 ++++++++++++--\r
61  notmuch-show.c     |  8 +++++++-\r
62  8 files changed, 79 insertions(+), 27 deletions(-)\r
63 \r
64 diff --git a/lib/Makefile.local b/lib/Makefile.local\r
65 index 4120390..4bf8116 100644\r
66 --- a/lib/Makefile.local\r
67 +++ b/lib/Makefile.local\r
68 @@ -5,13 +5,13 @@\r
69  # the library interface, (such as the deletion of an API or a major\r
70  # semantic change that breaks formerly functioning code).\r
71  #\r
72 -LIBNOTMUCH_VERSION_MAJOR = 4\r
73 +LIBNOTMUCH_VERSION_MAJOR = 5\r
74  \r
75  # The minor version of the library interface. This should be incremented at\r
76  # the time of release for any additions to the library interface,\r
77  # (and when it is incremented, the release version of the library should\r
78  #  be reset to 0).\r
79 -LIBNOTMUCH_VERSION_MINOR = 1\r
80 +LIBNOTMUCH_VERSION_MINOR = 0\r
81  \r
82  # The release version the library interface. This should be incremented at\r
83  # the time of release if there have been no changes to the interface, (but\r
84 diff --git a/lib/database.cc b/lib/database.cc\r
85 index 3601f9d..b7fbc63 100644\r
86 --- a/lib/database.cc\r
87 +++ b/lib/database.cc\r
88 @@ -1268,7 +1268,13 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
89      if (new_features &\r
90         (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER)) {\r
91         notmuch_query_t *query = notmuch_query_create (notmuch, "");\r
92 -       total += notmuch_query_count_messages (query);\r
93 +       unsigned msg_count;\r
94 +\r
95 +       status = notmuch_query_count_messages (query, &msg_count);\r
96 +       if (status)\r
97 +           goto DONE;\r
98 +\r
99 +       total += msg_count;\r
100         notmuch_query_destroy (query);\r
101      }\r
102      if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) {\r
103 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
104 index 220839b..37bf0bd 100644\r
105 --- a/lib/notmuch.h\r
106 +++ b/lib/notmuch.h\r
107 @@ -898,11 +898,15 @@ notmuch_threads_destroy (notmuch_threads_t *threads);\r
108   * This function performs a search and returns Xapian's best\r
109   * guess as to number of matching messages.\r
110   *\r
111 - * If a Xapian exception occurs, this function may return 0 (after\r
112 - * printing a message).\r
113 + * Return value:\r
114 + *\r
115 + * NOTMUCH_STATUS_SUCCESS: query complete successfully.\r
116 + *\r
117 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The\r
118 + *      value of *count is not defined.\r
119   */\r
120 -unsigned\r
121 -notmuch_query_count_messages (notmuch_query_t *query);\r
122 +notmuch_status_t\r
123 +notmuch_query_count_messages (notmuch_query_t *query, unsigned *count);\r
124  \r
125  /**\r
126   * Return the number of threads matching a search.\r
127 @@ -914,10 +918,18 @@ notmuch_query_count_messages (notmuch_query_t *query);\r
128   * Note that this is a significantly heavier operation than\r
129   * notmuch_query_count_messages().\r
130   *\r
131 - * If an error occurs, this function may return 0.\r
132 + * Return value:\r
133 + *\r
134 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Memory allocation failed. The value\r
135 + *      of *count is not defined\r
136 +\r
137 + * NOTMUCH_STATUS_SUCCESS: query complete successfully.\r
138 + *\r
139 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The\r
140 + *      value of *count is not defined.\r
141   */\r
142 -unsigned\r
143 -notmuch_query_count_threads (notmuch_query_t *query);\r
144 +notmuch_status_t\r
145 +notmuch_query_count_threads (notmuch_query_t *query, unsigned *count);\r
146  \r
147  /**\r
148   * Get the thread ID of 'thread'.\r
149 diff --git a/lib/query.cc b/lib/query.cc\r
150 index 60ff8bd..10ecf2e 100644\r
151 --- a/lib/query.cc\r
152 +++ b/lib/query.cc\r
153 @@ -508,8 +508,8 @@ notmuch_threads_destroy (notmuch_threads_t *threads)\r
154      talloc_free (threads);\r
155  }\r
156  \r
157 -unsigned\r
158 -notmuch_query_count_messages (notmuch_query_t *query)\r
159 +notmuch_status_t\r
160 +notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)\r
161  {\r
162      notmuch_database_t *notmuch = query->notmuch;\r
163      const char *query_string = query->query_string;\r
164 @@ -565,30 +565,32 @@ notmuch_query_count_messages (notmuch_query_t *query)\r
165         fprintf (stderr, "A Xapian exception occurred: %s\n",\r
166                  error.get_msg().c_str());\r
167         fprintf (stderr, "Query string was: %s\n", query->query_string);\r
168 +       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
169      }\r
170  \r
171 -    return count;\r
172 +    *count_out=count;\r
173 +    return NOTMUCH_STATUS_SUCCESS;\r
174  }\r
175  \r
176 -unsigned\r
177 -notmuch_query_count_threads (notmuch_query_t *query)\r
178 +notmuch_status_t\r
179 +notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)\r
180  {\r
181      notmuch_messages_t *messages;\r
182      GHashTable *hash;\r
183 -    unsigned int count;\r
184      notmuch_sort_t sort;\r
185 +    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
186  \r
187      sort = query->sort;\r
188      query->sort = NOTMUCH_SORT_UNSORTED;\r
189      messages = notmuch_query_search_messages (query);\r
190      query->sort = sort;\r
191      if (messages == NULL)\r
192 -       return 0;\r
193 +       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
194  \r
195      hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);\r
196      if (hash == NULL) {\r
197         talloc_free (messages);\r
198 -       return 0;\r
199 +       return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
200      }\r
201  \r
202      while (notmuch_messages_valid (messages)) {\r
203 @@ -597,7 +599,7 @@ notmuch_query_count_threads (notmuch_query_t *query)\r
204         char *thread_id_copy = talloc_strdup (messages, thread_id);\r
205         if (unlikely (thread_id_copy == NULL)) {\r
206             notmuch_message_destroy (message);\r
207 -           count = 0;\r
208 +           ret = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
209             goto DONE;\r
210         }\r
211         g_hash_table_insert (hash, thread_id_copy, NULL);\r
212 @@ -605,11 +607,11 @@ notmuch_query_count_threads (notmuch_query_t *query)\r
213         notmuch_messages_move_to_next (messages);\r
214      }\r
215  \r
216 -    count = g_hash_table_size (hash);\r
217 +    *count = g_hash_table_size (hash);\r
218  \r
219    DONE:\r
220      g_hash_table_unref (hash);\r
221      talloc_free (messages);\r
222  \r
223 -    return count;\r
224 +    return ret;\r
225  }\r
226 diff --git a/notmuch-count.c b/notmuch-count.c\r
227 index 6058f7c..7e3c1b7 100644\r
228 --- a/notmuch-count.c\r
229 +++ b/notmuch-count.c\r
230 @@ -71,6 +71,8 @@ print_count (notmuch_database_t *notmuch, const char *query_str,\r
231  {\r
232      notmuch_query_t *query;\r
233      size_t i;\r
234 +    unsigned count;\r
235 +    notmuch_status_t status;\r
236  \r
237      query = notmuch_query_create (notmuch, query_str);\r
238      if (query == NULL) {\r
239 @@ -83,10 +85,18 @@ print_count (notmuch_database_t *notmuch, const char *query_str,\r
240  \r
241      switch (output) {\r
242      case OUTPUT_MESSAGES:\r
243 -       printf ("%u\n", notmuch_query_count_messages (query));\r
244 +       if ((status = notmuch_query_count_messages (query, &count))) {\r
245 +           fprintf (stderr, "Error: %s\n", notmuch_status_to_string (status));\r
246 +           return 1;\r
247 +       }\r
248 +       printf ("%u\n", count);\r
249         break;\r
250      case OUTPUT_THREADS:\r
251 -       printf ("%u\n", notmuch_query_count_threads (query));\r
252 +       if ((status = notmuch_query_count_threads (query, &count))) {\r
253 +           fprintf (stderr, "Error: %s\n", notmuch_status_to_string (status));\r
254 +           return 1;\r
255 +       }\r
256 +       printf ("%u\n", count);\r
257         break;\r
258      case OUTPUT_FILES:\r
259         printf ("%u\n", count_files (query));\r
260 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
261 index 7c1c809..c5f1b92 100644\r
262 --- a/notmuch-reply.c\r
263 +++ b/notmuch-reply.c\r
264 @@ -650,8 +650,14 @@ notmuch_reply_format_sprinter(void *ctx,\r
265      notmuch_messages_t *messages;\r
266      notmuch_message_t *message;\r
267      mime_node_t *node;\r
268 +    unsigned count;\r
269 +    notmuch_status_t status;\r
270  \r
271 -    if (notmuch_query_count_messages (query) != 1) {\r
272 +    if ((status = notmuch_query_count_messages (query, &count))) {\r
273 +       fprintf (stderr, "Error: %s.\n", notmuch_status_to_string (status));\r
274 +       return 1;\r
275 +    }\r
276 +    if (count != 1) {\r
277         fprintf (stderr, "Error: search term did not match precisely one message.\n");\r
278         return 1;\r
279      }\r
280 diff --git a/notmuch-search.c b/notmuch-search.c\r
281 index 14b9f01..cadfc0c 100644\r
282 --- a/notmuch-search.c\r
283 +++ b/notmuch-search.c\r
284 @@ -113,7 +113,14 @@ do_search_threads (search_context_t *ctx)\r
285      int i;\r
286  \r
287      if (ctx->offset < 0) {\r
288 -       ctx->offset += notmuch_query_count_threads (ctx->query);\r
289 +       unsigned count;\r
290 +       notmuch_status_t status;\r
291 +       if ((status = notmuch_query_count_threads (ctx->query, &count))) {\r
292 +           fprintf (stderr, "Error: %s\n", notmuch_status_to_string (status));\r
293 +           return 1;\r
294 +       }\r
295 +\r
296 +       ctx->offset += count;\r
297         if (ctx->offset < 0)\r
298             ctx->offset = 0;\r
299      }\r
300 @@ -414,7 +421,10 @@ do_search_messages (search_context_t *ctx)\r
301      int i;\r
302  \r
303      if (ctx->offset < 0) {\r
304 -       ctx->offset += notmuch_query_count_messages (ctx->query);\r
305 +       unsigned count;\r
306 +       if (notmuch_query_count_messages (ctx->query, &count))\r
307 +           return 1;\r
308 +       ctx->offset += count;\r
309         if (ctx->offset < 0)\r
310             ctx->offset = 0;\r
311      }\r
312 diff --git a/notmuch-show.c b/notmuch-show.c\r
313 index d416fbd..16835a3 100644\r
314 --- a/notmuch-show.c\r
315 +++ b/notmuch-show.c\r
316 @@ -982,8 +982,14 @@ do_show_single (void *ctx,\r
317  {\r
318      notmuch_messages_t *messages;\r
319      notmuch_message_t *message;\r
320 +    unsigned count;\r
321 +    notmuch_status_t status;\r
322 +    if ((status = notmuch_query_count_messages (query, &count))) {\r
323 +       fprintf (stderr, "Error: %s\n", notmuch_status_to_string (status));\r
324 +       return 1;\r
325 +    }\r
326  \r
327 -    if (notmuch_query_count_messages (query) != 1) {\r
328 +    if (count != 1) {\r
329         fprintf (stderr, "Error: search term did not match precisely one message.\n");\r
330         return 1;\r
331      }\r
332 -- \r
333 2.1.3\r
334 \r