]> git.notmuchmail.org Git - notmuch/blobdiff - lib/query.cc
lib: Fix notmuch_query_search_threads to return NULL on any Xapian exception.
[notmuch] / lib / query.cc
index a571a618bdd1350aa7041487af19cdf788834c36..59f4011b31d3a3f5616227d352b8f7234b05ee0f 100644 (file)
@@ -125,7 +125,9 @@ notmuch_query_search_messages (notmuch_query_t *query)
                              Xapian::QueryParser::FLAG_WILDCARD |
                              Xapian::QueryParser::FLAG_PURE_NOT);
 
-       if (strcmp (query_string, "") == 0) {
+       if (strcmp (query_string, "") == 0 ||
+           strcmp (query_string, "*") == 0)
+       {
            final_query = mail_query;
        } else {
            string_query = notmuch->query_parser->
@@ -134,6 +136,8 @@ notmuch_query_search_messages (notmuch_query_t *query)
                                         mail_query, string_query);
        }
 
+       enquire.set_weighting_scheme (Xapian::BoolWeight());
+
        switch (query->sort) {
        case NOTMUCH_SORT_OLDEST_FIRST:
            enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, FALSE);
@@ -144,6 +148,8 @@ notmuch_query_search_messages (notmuch_query_t *query)
        case NOTMUCH_SORT_MESSAGE_ID:
            enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
            break;
+        case NOTMUCH_SORT_UNSORTED:
+           break;
        }
 
 #if DEBUG_QUERY
@@ -157,18 +163,20 @@ notmuch_query_search_messages (notmuch_query_t *query)
        messages->iterator = mset.begin ();
        messages->iterator_end = mset.end ();
 
+       return &messages->base;
+
     } catch (const Xapian::Error &error) {
        fprintf (stderr, "A Xapian exception occurred performing query: %s\n",
                 error.get_msg().c_str());
        fprintf (stderr, "Query string was: %s\n", query->query_string);
        notmuch->exception_reported = TRUE;
+       talloc_free (messages);
+       return NULL;
     }
-
-    return &messages->base;
 }
 
 notmuch_bool_t
-_notmuch_mset_messages_has_more (notmuch_messages_t *messages)
+_notmuch_mset_messages_valid (notmuch_messages_t *messages)
 {
     notmuch_mset_messages_t *mset_messages;
 
@@ -187,7 +195,7 @@ _notmuch_mset_messages_get (notmuch_messages_t *messages)
 
     mset_messages = (notmuch_mset_messages_t *) messages;
 
-    if (! _notmuch_mset_messages_has_more (&mset_messages->base))
+    if (! _notmuch_mset_messages_valid (&mset_messages->base))
        return NULL;
 
     doc_id = *mset_messages->iterator;
@@ -206,7 +214,7 @@ _notmuch_mset_messages_get (notmuch_messages_t *messages)
 }
 
 void
-_notmuch_mset_messages_advance (notmuch_messages_t *messages)
+_notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
 {
     notmuch_mset_messages_t *mset_messages;
 
@@ -241,6 +249,10 @@ notmuch_query_search_threads (notmuch_query_t *query)
                                              free, NULL);
 
     threads->messages = notmuch_query_search_messages (query);
+    if (threads->messages == NULL) {
+           talloc_free (threads);
+           return NULL;
+    }
 
     threads->thread_id = NULL;
 
@@ -256,14 +268,14 @@ notmuch_query_destroy (notmuch_query_t *query)
 }
 
 notmuch_bool_t
-notmuch_threads_has_more (notmuch_threads_t *threads)
+notmuch_threads_valid (notmuch_threads_t *threads)
 {
     notmuch_message_t *message;
 
     if (threads->thread_id)
        return TRUE;
 
-    while (notmuch_messages_has_more (threads->messages))
+    while (notmuch_messages_valid (threads->messages))
     {
        message = notmuch_messages_get (threads->messages);
 
@@ -275,11 +287,11 @@ notmuch_threads_has_more (notmuch_threads_t *threads)
        {
            g_hash_table_insert (threads->threads,
                                 xstrdup (threads->thread_id), NULL);
-           notmuch_messages_advance (threads->messages);
+           notmuch_messages_move_to_next (threads->messages);
            return TRUE;
        }
 
-       notmuch_messages_advance (threads->messages);
+       notmuch_messages_move_to_next (threads->messages);
     }
 
     threads->thread_id = NULL;
@@ -289,17 +301,18 @@ notmuch_threads_has_more (notmuch_threads_t *threads)
 notmuch_thread_t *
 notmuch_threads_get (notmuch_threads_t *threads)
 {
-    if (! notmuch_threads_has_more (threads))
+    if (! notmuch_threads_valid (threads))
        return NULL;
 
     return _notmuch_thread_create (threads->query,
                                   threads->query->notmuch,
                                   threads->thread_id,
-                                  threads->query->query_string);
+                                  threads->query->query_string,
+                                  threads->query->sort);
 }
 
 void
-notmuch_threads_advance (notmuch_threads_t *threads)
+notmuch_threads_move_to_next (notmuch_threads_t *threads)
 {
     threads->thread_id = NULL;
 }
@@ -315,7 +328,7 @@ notmuch_query_count_messages (notmuch_query_t *query)
 {
     notmuch_database_t *notmuch = query->notmuch;
     const char *query_string = query->query_string;
-    Xapian::doccount count;
+    Xapian::doccount count = 0;
 
     try {
        Xapian::Enquire enquire (*notmuch->xapian_db);
@@ -331,7 +344,9 @@ notmuch_query_count_messages (notmuch_query_t *query)
                              Xapian::QueryParser::FLAG_WILDCARD |
                              Xapian::QueryParser::FLAG_PURE_NOT);
 
-       if (strcmp (query_string, "") == 0) {
+       if (strcmp (query_string, "") == 0 ||
+           strcmp (query_string, "*") == 0)
+       {
            final_query = mail_query;
        } else {
            string_query = notmuch->query_parser->