]> git.notmuchmail.org Git - notmuch/commitdiff
lib: add new status reporting API for notmuch_query_search_{m,t}
authorDavid Bremner <david@tethera.net>
Tue, 20 Jan 2015 07:53:41 +0000 (08:53 +0100)
committerDavid Bremner <david@tethera.net>
Sun, 1 Mar 2015 07:49:14 +0000 (08:49 +0100)
This at least allows distinguishing between out of memory and Xapian
exceptions. Adding finer grained status codes would allow different
Xapian exceptions to be preserved.

Adding wrappers allows people to transition gradually to the new API,
at the cost of bloating the library API a bit.

lib/notmuch.h
lib/query.cc

index 220839b732a94ee95a37e3958d8acffb9d59162e..3e326bfc4da9a1ef844033878566b3e1bc509c28 100644 (file)
@@ -780,10 +780,18 @@ notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag);
  * to call it if the query is about to be destroyed).
  *
  * If a Xapian exception occurs this function will return NULL.
+ * For better error reporting, use the _st variant.
  */
 notmuch_threads_t *
 notmuch_query_search_threads (notmuch_query_t *query);
 
+/**
+ * Like notmuch_query_search_threads, but with a status return.
+ */
+notmuch_status_t
+notmuch_query_search_threads_st (notmuch_query_t *query,
+                                notmuch_threads_t **out);
+
 /**
  * Execute a query for messages, returning a notmuch_messages_t object
  * which can be used to iterate over the results. The returned
@@ -822,10 +830,18 @@ notmuch_query_search_threads (notmuch_query_t *query);
  * reason to call it if the query is about to be destroyed).
  *
  * If a Xapian exception occurs this function will return NULL.
+ * For better error reporting, use the _st variant.
  */
 notmuch_messages_t *
 notmuch_query_search_messages (notmuch_query_t *query);
 
+/**
+ * Like notmuch_query_search_messages, but with a status return.
+ */
+notmuch_status_t
+notmuch_query_search_messages_st (notmuch_query_t *query,
+                                 notmuch_messages_t **out);
+
 /**
  * Destroy a notmuch_query_t along with any associated resources.
  *
index 60ff8bd9a39e1707e12a4e96cff99890b00d669b..9279915d21ccfbccb9ad9929a4089695a9f55928 100644 (file)
@@ -173,6 +173,19 @@ _notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)
 
 notmuch_messages_t *
 notmuch_query_search_messages (notmuch_query_t *query)
+{
+    notmuch_status_t status;
+    notmuch_messages_t *messages;
+    status = notmuch_query_search_messages_st (query, &messages);
+    if (status)
+       return NULL;
+    else
+       return messages;
+}
+
+notmuch_status_t
+notmuch_query_search_messages_st (notmuch_query_t *query,
+                                 notmuch_messages_t **out)
 {
     notmuch_database_t *notmuch = query->notmuch;
     const char *query_string = query->query_string;
@@ -180,7 +193,7 @@ notmuch_query_search_messages (notmuch_query_t *query)
 
     messages = talloc (query, notmuch_mset_messages_t);
     if (unlikely (messages == NULL))
-       return NULL;
+       return NOTMUCH_STATUS_OUT_OF_MEMORY;
 
     try {
 
@@ -279,7 +292,8 @@ notmuch_query_search_messages (notmuch_query_t *query)
        messages->iterator = mset.begin ();
        messages->iterator_end = mset.end ();
 
-       return &messages->base;
+       *out = &messages->base;
+       return NOTMUCH_STATUS_SUCCESS;
 
     } catch (const Xapian::Error &error) {
        fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
@@ -287,7 +301,7 @@ notmuch_query_search_messages (notmuch_query_t *query)
        fprintf (stderr, "Query string was: %s\n", query->query_string);
        notmuch->exception_reported = TRUE;
        talloc_free (messages);
-       return NULL;
+       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
     }
 }
 
@@ -412,24 +426,39 @@ _notmuch_threads_destructor (notmuch_threads_t *threads)
     return 0;
 }
 
+
 notmuch_threads_t *
 notmuch_query_search_threads (notmuch_query_t *query)
+{
+    notmuch_status_t status;
+    notmuch_threads_t *threads;
+    status = notmuch_query_search_threads_st (query, &threads);
+    if (status)
+       return NULL;
+    else
+       return threads;
+}
+
+notmuch_status_t
+notmuch_query_search_threads_st (notmuch_query_t *query,
+                                notmuch_threads_t **out)
 {
     notmuch_threads_t *threads;
     notmuch_messages_t *messages;
+    notmuch_status_t status;
 
     threads = talloc (query, notmuch_threads_t);
     if (threads == NULL)
-       return NULL;
+       return NOTMUCH_STATUS_OUT_OF_MEMORY;
     threads->doc_ids = NULL;
     talloc_set_destructor (threads, _notmuch_threads_destructor);
 
     threads->query = query;
 
-    messages = notmuch_query_search_messages (query);
-    if (messages == NULL) {
-           talloc_free (threads);
-           return NULL;
+    status = notmuch_query_search_messages_st (query, &messages);
+    if (status) {
+       talloc_free (threads);
+       return status;
     }
 
     threads->doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
@@ -445,10 +474,11 @@ notmuch_query_search_threads (notmuch_query_t *query)
     if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
                                    threads->doc_ids)) {
        talloc_free (threads);
-       return NULL;
+       return NOTMUCH_STATUS_OUT_OF_MEMORY;
     }
 
-    return threads;
+    *out = threads;
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
 void