unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* Version 3 of count API changes
@ 2015-09-27  2:50 David Bremner
  2015-09-27  2:50 ` [Patch v3 1/4] cli/count: simplify and document return value of print_count David Bremner
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: David Bremner @ 2015-09-27  2:50 UTC (permalink / raw)
  To: notmuch

These are in some sense more urgent than than the search API changes
because the current API overloads a legitimate return value with an
error indicator.

Compared to the last version, these mainly use print_status_query.

Also, in contrast to the search API changes, these do not introduce
any new deprecation warnings (at least if the whole series is
applied).

TBH version 3 is just a guess.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Patch v3 1/4] cli/count: simplify and document return value of print_count
  2015-09-27  2:50 Version 3 of count API changes David Bremner
@ 2015-09-27  2:50 ` David Bremner
  2015-09-27  2:50 ` [Patch v3 2/4] lib: add versions of n_q_count_{message, threads} with status return David Bremner
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: David Bremner @ 2015-09-27  2:50 UTC (permalink / raw)
  To: notmuch

Essentially a cosmetic change.
---
 notmuch-count.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/notmuch-count.c b/notmuch-count.c
index 09613e6..be3e236 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -67,6 +67,7 @@ count_files (notmuch_query_t *query)
     return count;
 }
 
+/* return 0 on success, -1 on failure */
 static int
 print_count (notmuch_database_t *notmuch, const char *query_str,
 	     const char **exclude_tags, size_t exclude_tags_length, int output, int print_lastmod)
@@ -81,7 +82,7 @@ print_count (notmuch_database_t *notmuch, const char *query_str,
     query = notmuch_query_create (notmuch, query_str);
     if (query == NULL) {
 	fprintf (stderr, "Out of memory\n");
-	return 1;
+	return -1;
     }
 
     for (i = 0; i < exclude_tags_length; i++)
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [Patch v3 2/4] lib: add versions of n_q_count_{message, threads} with status return
  2015-09-27  2:50 Version 3 of count API changes David Bremner
  2015-09-27  2:50 ` [Patch v3 1/4] cli/count: simplify and document return value of print_count David Bremner
@ 2015-09-27  2:50 ` David Bremner
  2015-09-27 12:16   ` [patch v3.1] lib: add versions of n_q_count_{message,threads} " David Bremner
  2015-09-27  2:50 ` [Patch v3 3/4] cli: update to use new count API David Bremner
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 9+ messages in thread
From: David Bremner @ 2015-09-27  2:50 UTC (permalink / raw)
  To: notmuch

Although I think it's a pretty bad idea to continue using the old API,
this allows both a more gentle transition for clients of the library,
and allows us to break one monolithic change into a series
---
 lib/database.cc |  8 +++++++-
 lib/notmuch.h   | 52 ++++++++++++++++++++++++++++++++++++++++++++++------
 lib/query.cc    | 47 +++++++++++++++++++++++++++++++++--------------
 3 files changed, 86 insertions(+), 21 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 6d0e5a6..cf93533 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1409,7 +1409,13 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
 	(NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |
 	 NOTMUCH_FEATURE_LAST_MOD)) {
 	notmuch_query_t *query = notmuch_query_create (notmuch, "");
-	total += notmuch_query_count_messages (query);
+	unsigned msg_count;
+
+	status = notmuch_query_count_messages_st (query, &msg_count);
+	if (status)
+	    goto DONE;
+
+	total += msg_count;
 	notmuch_query_destroy (query);
     }
     if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) {
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 8775683..5832f89 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -984,12 +984,31 @@ notmuch_threads_destroy (notmuch_threads_t *threads);
  * This function performs a search and returns the number of matching
  * messages.
  *
- * If a Xapian exception occurs, this function may return 0 (after
- * printing a message).
+ * @returns
+ *
+ * NOTMUCH_STATUS_SUCCESS: query complete successfully.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The
+ *      value of *count is not defined.
  */
-unsigned
+notmuch_status_t
+notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count);
+
+/**
+ * like notmuch_query_count_messages_st, but without a status return
+ *
+ * May return 0 in the case of errors.
+ *
+ * @deprecated Deprecated since libnotmuch 4.3 (notmuch 0.21). Please
+ * use notmuch_query_count_messages_st instead.
+ */
+NOTMUCH_DEPRECATED(4,3)
+unsigned int
 notmuch_query_count_messages (notmuch_query_t *query);
 
+
+
+
 /**
  * Return the number of threads matching a search.
  *
@@ -998,13 +1017,34 @@ notmuch_query_count_messages (notmuch_query_t *query);
  * search.
  *
  * Note that this is a significantly heavier operation than
- * notmuch_query_count_messages().
+ * notmuch_query_count_messages{_st}().
  *
- * If an error occurs, this function may return 0.
+ * @returns
+ *
+ * NOTMUCH_STATUS_OUT_OF_MEMORY: Memory allocation failed. The value
+ *      of *count is not defined
+
+ * NOTMUCH_STATUS_SUCCESS: query complete successfully.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The
+ *      value of *count is not defined.
  */
-unsigned
+notmuch_status_t
+notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count);
+
+/**
+ * like notmuch_query_count_threads, but without a status return.
+ *
+ * May return 0 in case of errors.
+ *
+ * @deprecated Deprecated as of libnotmuch 4.3 (notmuch 0.21). Please
+ * use notmuch_query_count_threads_st instead.
+ */
+NOTMUCH_DEPRECATED(4,3)
+unsigned int
 notmuch_query_count_threads (notmuch_query_t *query);
 
+
 /**
  * Get the thread ID of 'thread'.
  *
diff --git a/lib/query.cc b/lib/query.cc
index 8cf0a07..8419b2e 100644
--- a/lib/query.cc
+++ b/lib/query.cc
@@ -541,9 +541,19 @@ notmuch_threads_destroy (notmuch_threads_t *threads)
     talloc_free (threads);
 }
 
-unsigned
+unsigned int
 notmuch_query_count_messages (notmuch_query_t *query)
 {
+    notmuch_status_t status;
+    unsigned int count;
+
+    status = notmuch_query_count_messages_st (query, &count);
+    return status ? 0 : count;
+}
+
+notmuch_status_t
+notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
+{
     notmuch_database_t *notmuch = query->notmuch;
     const char *query_string = query->query_string;
     Xapian::doccount count = 0;
@@ -605,35 +615,44 @@ notmuch_query_count_messages (notmuch_query_t *query)
 			       "Query string was: %s\n",
 			       error.get_msg().c_str(),
 			       query->query_string);
-
+	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
     }
 
-    return count;
+    *count_out = count;
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
 unsigned
 notmuch_query_count_threads (notmuch_query_t *query)
 {
+    notmuch_status_t status;
+    unsigned int count;
+
+    status = notmuch_query_count_threads_st (query, &count);
+    return status ? 0 : count;
+}
+    
+notmuch_status_t
+notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
+{
     notmuch_messages_t *messages;
     GHashTable *hash;
-    unsigned int count;
     notmuch_sort_t sort;
-    notmuch_status_t status;
+    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
 
     sort = query->sort;
     query->sort = NOTMUCH_SORT_UNSORTED;
-    status = notmuch_query_search_messages_st (query, &messages);
-    if (status)
-	return 0;
-
+    ret = notmuch_query_search_messages_st (query, &messages);
+    if (ret)
+	return ret;
     query->sort = sort;
     if (messages == NULL)
-	return 0;
+	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
 
     hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
     if (hash == NULL) {
 	talloc_free (messages);
-	return 0;
+	return NOTMUCH_STATUS_OUT_OF_MEMORY;
     }
 
     while (notmuch_messages_valid (messages)) {
@@ -642,7 +661,7 @@ notmuch_query_count_threads (notmuch_query_t *query)
 	char *thread_id_copy = talloc_strdup (messages, thread_id);
 	if (unlikely (thread_id_copy == NULL)) {
 	    notmuch_message_destroy (message);
-	    count = 0;
+	    ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
 	    goto DONE;
 	}
 	g_hash_table_insert (hash, thread_id_copy, NULL);
@@ -650,13 +669,13 @@ notmuch_query_count_threads (notmuch_query_t *query)
 	notmuch_messages_move_to_next (messages);
     }
 
-    count = g_hash_table_size (hash);
+    *count = g_hash_table_size (hash);
 
   DONE:
     g_hash_table_unref (hash);
     talloc_free (messages);
 
-    return count;
+    return ret;
 }
 
 notmuch_database_t *
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [Patch v3 3/4] cli: update to use new count API
  2015-09-27  2:50 Version 3 of count API changes David Bremner
  2015-09-27  2:50 ` [Patch v3 1/4] cli/count: simplify and document return value of print_count David Bremner
  2015-09-27  2:50 ` [Patch v3 2/4] lib: add versions of n_q_count_{message, threads} with status return David Bremner
@ 2015-09-27  2:50 ` David Bremner
  2015-09-27  2:59   ` [patch v3.1] " David Bremner
  2015-09-27  2:50 ` [Patch v3 4/4] ruby: " David Bremner
  2015-09-27 12:14 ` [patch v3.1] lib: move query variable to function scope David Bremner
  4 siblings, 1 reply; 9+ messages in thread
From: David Bremner @ 2015-09-27  2:50 UTC (permalink / raw)
  To: notmuch

Essentially replace each call to notmuch_count_* with the corresponding
_st call, followed by print_status_query.
---
 notmuch-count.c  | 12 ++++++++++--
 notmuch-reply.c  |  7 ++++++-
 notmuch-search.c | 16 ++++++++++++++--
 notmuch-show.c   |  7 ++++++-
 4 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/notmuch-count.c b/notmuch-count.c
index be3e236..0b6e6f5 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -75,9 +75,11 @@ print_count (notmuch_database_t *notmuch, const char *query_str,
     notmuch_query_t *query;
     size_t i;
     int count;
+    unsigned int ucount;
     unsigned long revision;
     const char *uuid;
     int ret = 0;
+    notmuch_status_t status;
 
     query = notmuch_query_create (notmuch, query_str);
     if (query == NULL) {
@@ -90,10 +92,16 @@ print_count (notmuch_database_t *notmuch, const char *query_str,
 
     switch (output) {
     case OUTPUT_MESSAGES:
-	printf ("%u", notmuch_query_count_messages (query));
+	status = notmuch_query_count_messages_st (query, &ucount);
+	if (print_status_query ("notmuch count", query, status))
+	    return -1;
+	printf ("%u", ucount);
 	break;
     case OUTPUT_THREADS:
-	printf ("%u", notmuch_query_count_threads (query));
+	status = notmuch_query_count_threads_st (query, &ucount);
+	if (print_status_query ("notmuch count", query, status))
+	    return -1;
+	printf ("%u", ucount);
 	break;
     case OUTPUT_FILES:
 	count = count_files (query);
diff --git a/notmuch-reply.c b/notmuch-reply.c
index fd6a1ec..1357142 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -655,9 +655,14 @@ notmuch_reply_format_sprinter(void *ctx,
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     mime_node_t *node;
+    unsigned count;
     notmuch_status_t status;
 
-    if (notmuch_query_count_messages (query) != 1) {
+    status = notmuch_query_count_messages_st (query, &count);
+    if (print_status_query ("notmuch reply", query, status))
+	return 1;
+
+    if (count != 1) {
 	fprintf (stderr, "Error: search term did not match precisely one message.\n");
 	return 1;
     }
diff --git a/notmuch-search.c b/notmuch-search.c
index 44e582c..56895e0 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -121,7 +121,13 @@ do_search_threads (search_context_t *ctx)
     notmuch_status_t status;
 
     if (ctx->offset < 0) {
-	ctx->offset += notmuch_query_count_threads (ctx->query);
+	unsigned count;
+	notmuch_status_t status;
+	status = notmuch_query_count_threads_st (ctx->query, &count);
+	if (print_status_query ("notmuch search", ctx->query, status)
+	    return 1;
+
+	ctx->offset += count;
 	if (ctx->offset < 0)
 	    ctx->offset = 0;
     }
@@ -521,7 +527,13 @@ do_search_messages (search_context_t *ctx)
     notmuch_status_t status;
 
     if (ctx->offset < 0) {
-	ctx->offset += notmuch_query_count_messages (ctx->query);
+	unsigned count;
+	notmuch_status_t status;
+	status = notmuch_query_count_messages_st (ctx->query, &count);
+	if (print_status_query ("notmuch search", ctx->query, status)
+	    return 1;
+
+	ctx->offset += count;
 	if (ctx->offset < 0)
 	    ctx->offset = 0;
     }
diff --git a/notmuch-show.c b/notmuch-show.c
index e054808..5a83c60 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -896,8 +896,13 @@ do_show_single (void *ctx,
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     notmuch_status_t status;
+    unsigned int count;
 
-    if (notmuch_query_count_messages (query) != 1) {
+    status = notmuch_query_count_messages_st (query, &count);
+    if (print_status_query ("notmuch show", query, status))
+	return 1;
+
+    if (count != 1) {
 	fprintf (stderr, "Error: search term did not match precisely one message.\n");
 	return 1;
     }
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [Patch v3 4/4] ruby: use new count API
  2015-09-27  2:50 Version 3 of count API changes David Bremner
                   ` (2 preceding siblings ...)
  2015-09-27  2:50 ` [Patch v3 3/4] cli: update to use new count API David Bremner
@ 2015-09-27  2:50 ` David Bremner
  2015-09-27  3:01   ` [PATCH] python: update bindings for " David Bremner
  2015-09-27 12:14 ` [patch v3.1] lib: move query variable to function scope David Bremner
  4 siblings, 1 reply; 9+ messages in thread
From: David Bremner @ 2015-09-27  2:50 UTC (permalink / raw)
  To: notmuch

This change of replacing ignoring errors with exceptions is intended,
and indeed one of the main motivations for the libnotmuch API changes.
---
 bindings/ruby/query.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/bindings/ruby/query.c b/bindings/ruby/query.c
index a7dacba..f87700a 100644
--- a/bindings/ruby/query.c
+++ b/bindings/ruby/query.c
@@ -173,14 +173,16 @@ VALUE
 notmuch_rb_query_count_messages (VALUE self)
 {
     notmuch_query_t *query;
+    notmuch_status_t status;
+    unsigned int count;
 
     Data_Get_Notmuch_Query (self, query);
 
-    /* Xapian exceptions are not handled properly.
-     * (function may return 0 after printing a message)
-     * Thus there is nothing we can do here...
-     */
-    return UINT2NUM(notmuch_query_count_messages(query));
+    status = notmuch_query_count_messages_st (query, &count);
+    if (status)
+	notmuch_rb_status_raise (status);
+
+    return UINT2NUM(count);
 }
 
 /*
@@ -192,12 +194,14 @@ VALUE
 notmuch_rb_query_count_threads (VALUE self)
 {
     notmuch_query_t *query;
+    notmuch_status_t status;
+    unsigned int count;
 
     Data_Get_Notmuch_Query (self, query);
 
-    /* Xapian exceptions are not handled properly.
-     * (function may return 0 after printing a message)
-     * Thus there is nothing we can do here...
-     */
-    return UINT2NUM(notmuch_query_count_threads(query));
+    status = notmuch_query_count_threads_st (query, &count);
+    if (status)
+	notmuch_rb_status_raise (status);
+
+    return UINT2NUM(count);
 }
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [patch v3.1] cli: update to use new count API
  2015-09-27  2:50 ` [Patch v3 3/4] cli: update to use new count API David Bremner
@ 2015-09-27  2:59   ` David Bremner
  0 siblings, 0 replies; 9+ messages in thread
From: David Bremner @ 2015-09-27  2:59 UTC (permalink / raw)
  To: David Bremner, notmuch

Essentially replace each call to notmuch_count_* with the corresponding
_st call, followed by print_status_query.
---
 notmuch-count.c  | 12 ++++++++++--
 notmuch-reply.c  |  7 ++++++-
 notmuch-search.c | 16 ++++++++++++++--
 notmuch-show.c   |  7 ++++++-
 4 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/notmuch-count.c b/notmuch-count.c
index be3e236..0b6e6f5 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -75,9 +75,11 @@ print_count (notmuch_database_t *notmuch, const char *query_str,
     notmuch_query_t *query;
     size_t i;
     int count;
+    unsigned int ucount;
     unsigned long revision;
     const char *uuid;
     int ret = 0;
+    notmuch_status_t status;
 
     query = notmuch_query_create (notmuch, query_str);
     if (query == NULL) {
@@ -90,10 +92,16 @@ print_count (notmuch_database_t *notmuch, const char *query_str,
 
     switch (output) {
     case OUTPUT_MESSAGES:
-	printf ("%u", notmuch_query_count_messages (query));
+	status = notmuch_query_count_messages_st (query, &ucount);
+	if (print_status_query ("notmuch count", query, status))
+	    return -1;
+	printf ("%u", ucount);
 	break;
     case OUTPUT_THREADS:
-	printf ("%u", notmuch_query_count_threads (query));
+	status = notmuch_query_count_threads_st (query, &ucount);
+	if (print_status_query ("notmuch count", query, status))
+	    return -1;
+	printf ("%u", ucount);
 	break;
     case OUTPUT_FILES:
 	count = count_files (query);
diff --git a/notmuch-reply.c b/notmuch-reply.c
index fd6a1ec..1357142 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -655,9 +655,14 @@ notmuch_reply_format_sprinter(void *ctx,
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     mime_node_t *node;
+    unsigned count;
     notmuch_status_t status;
 
-    if (notmuch_query_count_messages (query) != 1) {
+    status = notmuch_query_count_messages_st (query, &count);
+    if (print_status_query ("notmuch reply", query, status))
+	return 1;
+
+    if (count != 1) {
 	fprintf (stderr, "Error: search term did not match precisely one message.\n");
 	return 1;
     }
diff --git a/notmuch-search.c b/notmuch-search.c
index 44e582c..6d08c25 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -121,7 +121,13 @@ do_search_threads (search_context_t *ctx)
     notmuch_status_t status;
 
     if (ctx->offset < 0) {
-	ctx->offset += notmuch_query_count_threads (ctx->query);
+	unsigned count;
+	notmuch_status_t status;
+	status = notmuch_query_count_threads_st (ctx->query, &count);
+	if (print_status_query ("notmuch search", ctx->query, status))
+	    return 1;
+
+	ctx->offset += count;
 	if (ctx->offset < 0)
 	    ctx->offset = 0;
     }
@@ -521,7 +527,13 @@ do_search_messages (search_context_t *ctx)
     notmuch_status_t status;
 
     if (ctx->offset < 0) {
-	ctx->offset += notmuch_query_count_messages (ctx->query);
+	unsigned count;
+	notmuch_status_t status;
+	status = notmuch_query_count_messages_st (ctx->query, &count);
+	if (print_status_query ("notmuch search", ctx->query, status))
+	    return 1;
+
+	ctx->offset += count;
 	if (ctx->offset < 0)
 	    ctx->offset = 0;
     }
diff --git a/notmuch-show.c b/notmuch-show.c
index e054808..5a83c60 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -896,8 +896,13 @@ do_show_single (void *ctx,
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     notmuch_status_t status;
+    unsigned int count;
 
-    if (notmuch_query_count_messages (query) != 1) {
+    status = notmuch_query_count_messages_st (query, &count);
+    if (print_status_query ("notmuch show", query, status))
+	return 1;
+
+    if (count != 1) {
 	fprintf (stderr, "Error: search term did not match precisely one message.\n");
 	return 1;
     }
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH] python: update bindings for new count API
  2015-09-27  2:50 ` [Patch v3 4/4] ruby: " David Bremner
@ 2015-09-27  3:01   ` David Bremner
  0 siblings, 0 replies; 9+ messages in thread
From: David Bremner @ 2015-09-27  3:01 UTC (permalink / raw)
  To: David Bremner, notmuch

Note that any mismatches are not detected until runtime (if at all)
with the python bindings, so tests are crucial
---
 bindings/python/notmuch/query.py | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/bindings/python/notmuch/query.py b/bindings/python/notmuch/query.py
index 94773ac..378aa47 100644
--- a/bindings/python/notmuch/query.py
+++ b/bindings/python/notmuch/query.py
@@ -17,7 +17,7 @@ along with notmuch.  If not, see <http://www.gnu.org/licenses/>.
 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>
 """
 
-from ctypes import c_char_p, c_uint
+from ctypes import c_char_p, c_uint, POINTER, byref
 from .globals import (
     nmlib,
     Enum,
@@ -178,8 +178,8 @@ class Query(object):
             raise NullPointerError
         return Messages(msgs_p, self)
 
-    _count_messages = nmlib.notmuch_query_count_messages
-    _count_messages.argtypes = [NotmuchQueryP]
+    _count_messages = nmlib.notmuch_query_count_messages_st
+    _count_messages.argtypes = [NotmuchQueryP, POINTER(c_uint)]
     _count_messages.restype = c_uint
 
     def count_messages(self):
@@ -191,10 +191,14 @@ class Query(object):
         :rtype:   int
         '''
         self._assert_query_is_initialized()
-        return Query._count_messages(self._query)
-
-    _count_threads = nmlib.notmuch_query_count_threads
-    _count_threads.argtypes = [NotmuchQueryP]
+        count = c_uint(0)
+        status = Query._count_messages(self._query, byref(count))
+        if status != 0:
+            raise NotmuchError(status)
+        return count.value
+
+    _count_threads = nmlib.notmuch_query_count_threads_st
+    _count_threads.argtypes = [NotmuchQueryP, POINTER(c_uint)]
     _count_threads.restype = c_uint
 
     def count_threads(self):
@@ -210,7 +214,11 @@ class Query(object):
         :rtype:   int
         '''
         self._assert_query_is_initialized()
-        return Query._count_threads(self._query)
+        count = c_uint(0)
+        status = Query._count_threads(self._query, byref(count))
+        if status != 0:
+            raise NotmuchError(status)
+        return count.value
 
     _destroy = nmlib.notmuch_query_destroy
     _destroy.argtypes = [NotmuchQueryP]
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [patch v3.1] lib: move query variable to function scope
  2015-09-27  2:50 Version 3 of count API changes David Bremner
                   ` (3 preceding siblings ...)
  2015-09-27  2:50 ` [Patch v3 4/4] ruby: " David Bremner
@ 2015-09-27 12:14 ` David Bremner
  4 siblings, 0 replies; 9+ messages in thread
From: David Bremner @ 2015-09-27 12:14 UTC (permalink / raw)
  To: David Bremner, notmuch

This is a prelude to deallocating it (if necessary) on the error path.
---
 lib/database.cc | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 6d0e5a6..dcfad8c 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1372,6 +1372,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     enum _notmuch_features target_features, new_features;
     notmuch_status_t status;
     notmuch_private_status_t private_status;
+    notmuch_query_t *query = NULL;
     unsigned int count = 0, total = 0;
 
     status = _notmuch_database_ensure_writable (notmuch);
@@ -1408,7 +1409,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     if (new_features &
 	(NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |
 	 NOTMUCH_FEATURE_LAST_MOD)) {
-	notmuch_query_t *query = notmuch_query_create (notmuch, "");
+	query = notmuch_query_create (notmuch, "");
 	total += notmuch_query_count_messages (query);
 	notmuch_query_destroy (query);
     }
@@ -1436,11 +1437,12 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     if (new_features &
 	(NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |
 	 NOTMUCH_FEATURE_LAST_MOD)) {
-	notmuch_query_t *query = notmuch_query_create (notmuch, "");
 	notmuch_messages_t *messages;
 	notmuch_message_t *message;
 	char *filename;
 
+	query = notmuch_query_create (notmuch, "");
+
 	/* XXX: this should use the _st version, but needs an error
 	   path */
 	for (messages = notmuch_query_search_messages (query);
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [patch v3.1] lib: add versions of n_q_count_{message,threads} with status return
  2015-09-27  2:50 ` [Patch v3 2/4] lib: add versions of n_q_count_{message, threads} with status return David Bremner
@ 2015-09-27 12:16   ` David Bremner
  0 siblings, 0 replies; 9+ messages in thread
From: David Bremner @ 2015-09-27 12:16 UTC (permalink / raw)
  To: David Bremner, notmuch

Although I think it's a pretty bad idea to continue using the old API,
this allows both a more gentle transition for clients of the library,
and allows us to break one monolithic change into a series
---
 lib/database.cc | 11 ++++++++++-
 lib/notmuch.h   | 48 ++++++++++++++++++++++++++++++++++++++++++------
 lib/query.cc    | 47 +++++++++++++++++++++++++++++++++--------------
 3 files changed, 85 insertions(+), 21 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index dcfad8c..fbfb1af 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1410,7 +1410,13 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
 	(NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |
 	 NOTMUCH_FEATURE_LAST_MOD)) {
 	query = notmuch_query_create (notmuch, "");
-	total += notmuch_query_count_messages (query);
+	unsigned msg_count;
+
+	status = notmuch_query_count_messages_st (query, &msg_count);
+	if (status)
+	    goto DONE;
+
+	total += msg_count;
 	notmuch_query_destroy (query);
     }
     if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) {
@@ -1612,6 +1618,9 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
 	sigaction (SIGALRM, &action, NULL);
     }
 
+    if (query)
+	notmuch_query_destroy (query);
+
     talloc_free (local);
     return status;
 }
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 8775683..2fb778b 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -984,10 +984,26 @@ notmuch_threads_destroy (notmuch_threads_t *threads);
  * This function performs a search and returns the number of matching
  * messages.
  *
- * If a Xapian exception occurs, this function may return 0 (after
- * printing a message).
+ * @returns
+ *
+ * NOTMUCH_STATUS_SUCCESS: query complete successfully.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The
+ *      value of *count is not defined.
  */
-unsigned
+notmuch_status_t
+notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count);
+
+/**
+ * like notmuch_query_count_messages_st, but without a status return
+ *
+ * May return 0 in the case of errors.
+ *
+ * @deprecated Deprecated since libnotmuch 4.3 (notmuch 0.21). Please
+ * use notmuch_query_count_messages_st instead.
+ */
+NOTMUCH_DEPRECATED(4,3)
+unsigned int
 notmuch_query_count_messages (notmuch_query_t *query);
 
 /**
@@ -998,11 +1014,31 @@ notmuch_query_count_messages (notmuch_query_t *query);
  * search.
  *
  * Note that this is a significantly heavier operation than
- * notmuch_query_count_messages().
+ * notmuch_query_count_messages{_st}().
+ *
+ * @returns
  *
- * If an error occurs, this function may return 0.
+ * NOTMUCH_STATUS_OUT_OF_MEMORY: Memory allocation failed. The value
+ *      of *count is not defined
+
+ * NOTMUCH_STATUS_SUCCESS: query complete successfully.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: a Xapian exception occured. The
+ *      value of *count is not defined.
  */
-unsigned
+notmuch_status_t
+notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count);
+
+/**
+ * like notmuch_query_count_threads, but without a status return.
+ *
+ * May return 0 in case of errors.
+ *
+ * @deprecated Deprecated as of libnotmuch 4.3 (notmuch 0.21). Please
+ * use notmuch_query_count_threads_st instead.
+ */
+NOTMUCH_DEPRECATED(4,3)
+unsigned int
 notmuch_query_count_threads (notmuch_query_t *query);
 
 /**
diff --git a/lib/query.cc b/lib/query.cc
index 8cf0a07..e627bfc 100644
--- a/lib/query.cc
+++ b/lib/query.cc
@@ -541,9 +541,19 @@ notmuch_threads_destroy (notmuch_threads_t *threads)
     talloc_free (threads);
 }
 
-unsigned
+unsigned int
 notmuch_query_count_messages (notmuch_query_t *query)
 {
+    notmuch_status_t status;
+    unsigned int count;
+
+    status = notmuch_query_count_messages_st (query, &count);
+    return status ? 0 : count;
+}
+
+notmuch_status_t
+notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
+{
     notmuch_database_t *notmuch = query->notmuch;
     const char *query_string = query->query_string;
     Xapian::doccount count = 0;
@@ -605,35 +615,44 @@ notmuch_query_count_messages (notmuch_query_t *query)
 			       "Query string was: %s\n",
 			       error.get_msg().c_str(),
 			       query->query_string);
-
+	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
     }
 
-    return count;
+    *count_out = count;
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
 unsigned
 notmuch_query_count_threads (notmuch_query_t *query)
 {
+    notmuch_status_t status;
+    unsigned int count;
+
+    status = notmuch_query_count_threads_st (query, &count);
+    return status ? 0 : count;
+}
+
+notmuch_status_t
+notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
+{
     notmuch_messages_t *messages;
     GHashTable *hash;
-    unsigned int count;
     notmuch_sort_t sort;
-    notmuch_status_t status;
+    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
 
     sort = query->sort;
     query->sort = NOTMUCH_SORT_UNSORTED;
-    status = notmuch_query_search_messages_st (query, &messages);
-    if (status)
-	return 0;
-
+    ret = notmuch_query_search_messages_st (query, &messages);
+    if (ret)
+	return ret;
     query->sort = sort;
     if (messages == NULL)
-	return 0;
+	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
 
     hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
     if (hash == NULL) {
 	talloc_free (messages);
-	return 0;
+	return NOTMUCH_STATUS_OUT_OF_MEMORY;
     }
 
     while (notmuch_messages_valid (messages)) {
@@ -642,7 +661,7 @@ notmuch_query_count_threads (notmuch_query_t *query)
 	char *thread_id_copy = talloc_strdup (messages, thread_id);
 	if (unlikely (thread_id_copy == NULL)) {
 	    notmuch_message_destroy (message);
-	    count = 0;
+	    ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
 	    goto DONE;
 	}
 	g_hash_table_insert (hash, thread_id_copy, NULL);
@@ -650,13 +669,13 @@ notmuch_query_count_threads (notmuch_query_t *query)
 	notmuch_messages_move_to_next (messages);
     }
 
-    count = g_hash_table_size (hash);
+    *count = g_hash_table_size (hash);
 
   DONE:
     g_hash_table_unref (hash);
     talloc_free (messages);
 
-    return count;
+    return ret;
 }
 
 notmuch_database_t *
-- 
2.5.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-09-27 12:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-27  2:50 Version 3 of count API changes David Bremner
2015-09-27  2:50 ` [Patch v3 1/4] cli/count: simplify and document return value of print_count David Bremner
2015-09-27  2:50 ` [Patch v3 2/4] lib: add versions of n_q_count_{message, threads} with status return David Bremner
2015-09-27 12:16   ` [patch v3.1] lib: add versions of n_q_count_{message,threads} " David Bremner
2015-09-27  2:50 ` [Patch v3 3/4] cli: update to use new count API David Bremner
2015-09-27  2:59   ` [patch v3.1] " David Bremner
2015-09-27  2:50 ` [Patch v3 4/4] ruby: " David Bremner
2015-09-27  3:01   ` [PATCH] python: update bindings for " David Bremner
2015-09-27 12:14 ` [patch v3.1] lib: move query variable to function scope David Bremner

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).