+ _notmuch_database_log (notmuch,
+ "A Xapian exception occurred performing query: %s\n",
+ error.get_msg ().c_str ());
+ _notmuch_database_log_append (notmuch,
+ "Query string was: %s\n",
+ query->query_string);
+ return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
+ }
+
+ *count_out = count;
+ return NOTMUCH_STATUS_SUCCESS;
+}
+
+notmuch_status_t
+notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
+{
+ return notmuch_query_count_threads (query, count);
+}
+
+notmuch_status_t
+notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
+{
+ notmuch_messages_t *messages;
+ GHashTable *hash;
+ notmuch_sort_t sort;
+ notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
+
+ sort = query->sort;
+ query->sort = NOTMUCH_SORT_UNSORTED;
+ ret = notmuch_query_search_messages (query, &messages);
+ if (ret)
+ return ret;
+ query->sort = sort;
+ if (messages == NULL)
+ 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 NOTMUCH_STATUS_OUT_OF_MEMORY;
+ }
+
+ while (notmuch_messages_valid (messages)) {
+ notmuch_message_t *message = notmuch_messages_get (messages);
+ const char *thread_id = notmuch_message_get_thread_id (message);
+ char *thread_id_copy = talloc_strdup (messages, thread_id);
+ if (unlikely (thread_id_copy == NULL)) {
+ notmuch_message_destroy (message);
+ ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
+ goto DONE;
+ }
+ g_hash_table_insert (hash, thread_id_copy, NULL);
+ notmuch_message_destroy (message);
+ notmuch_messages_move_to_next (messages);