lib: add versions of n_q_count_{message,threads} with status return

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
This commit is contained in:
David Bremner 2015-09-27 12:31:57 -03:00
parent 81bd41c7cb
commit 87ee9a53e3
3 changed files with 87 additions and 21 deletions

View file

@ -1410,8 +1410,15 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
(NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER | (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |
NOTMUCH_FEATURE_LAST_MOD)) { NOTMUCH_FEATURE_LAST_MOD)) {
query = notmuch_query_create (notmuch, ""); 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); notmuch_query_destroy (query);
query = NULL;
} }
if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) { if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) {
t_end = db->allterms_end ("XTIMESTAMP"); t_end = db->allterms_end ("XTIMESTAMP");
@ -1492,6 +1499,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
} }
notmuch_query_destroy (query); notmuch_query_destroy (query);
query = NULL;
} }
/* Perform per-directory upgrades. */ /* Perform per-directory upgrades. */
@ -1612,6 +1620,9 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
sigaction (SIGALRM, &action, NULL); sigaction (SIGALRM, &action, NULL);
} }
if (query)
notmuch_query_destroy (query);
talloc_free (local); talloc_free (local);
return status; return status;
} }

View file

@ -984,10 +984,26 @@ notmuch_threads_destroy (notmuch_threads_t *threads);
* This function performs a search and returns the number of matching * This function performs a search and returns the number of matching
* messages. * messages.
* *
* If a Xapian exception occurs, this function may return 0 (after * @returns
* printing a message). *
* NOTMUCH_STATUS_SUCCESS: query completed 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 int *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); notmuch_query_count_messages (notmuch_query_t *query);
/** /**
@ -998,11 +1014,31 @@ notmuch_query_count_messages (notmuch_query_t *query);
* search. * search.
* *
* Note that this is a significantly heavier operation than * 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 completed 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); notmuch_query_count_threads (notmuch_query_t *query);
/** /**

View file

@ -541,8 +541,18 @@ notmuch_threads_destroy (notmuch_threads_t *threads)
talloc_free (threads); talloc_free (threads);
} }
unsigned unsigned int
notmuch_query_count_messages (notmuch_query_t *query) 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; notmuch_database_t *notmuch = query->notmuch;
const char *query_string = query->query_string; const char *query_string = query->query_string;
@ -605,35 +615,44 @@ notmuch_query_count_messages (notmuch_query_t *query)
"Query string was: %s\n", "Query string was: %s\n",
error.get_msg().c_str(), error.get_msg().c_str(),
query->query_string); query->query_string);
return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
} }
return count; *count_out = count;
return NOTMUCH_STATUS_SUCCESS;
} }
unsigned unsigned
notmuch_query_count_threads (notmuch_query_t *query) 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; notmuch_messages_t *messages;
GHashTable *hash; GHashTable *hash;
unsigned int count;
notmuch_sort_t sort; notmuch_sort_t sort;
notmuch_status_t status; notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
sort = query->sort; sort = query->sort;
query->sort = NOTMUCH_SORT_UNSORTED; query->sort = NOTMUCH_SORT_UNSORTED;
status = notmuch_query_search_messages_st (query, &messages); ret = notmuch_query_search_messages_st (query, &messages);
if (status) if (ret)
return 0; return ret;
query->sort = sort; query->sort = sort;
if (messages == NULL) if (messages == NULL)
return 0; return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL); hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
if (hash == NULL) { if (hash == NULL) {
talloc_free (messages); talloc_free (messages);
return 0; return NOTMUCH_STATUS_OUT_OF_MEMORY;
} }
while (notmuch_messages_valid (messages)) { 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); char *thread_id_copy = talloc_strdup (messages, thread_id);
if (unlikely (thread_id_copy == NULL)) { if (unlikely (thread_id_copy == NULL)) {
notmuch_message_destroy (message); notmuch_message_destroy (message);
count = 0; ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE; goto DONE;
} }
g_hash_table_insert (hash, thread_id_copy, NULL); 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); notmuch_messages_move_to_next (messages);
} }
count = g_hash_table_size (hash); *count = g_hash_table_size (hash);
DONE: DONE:
g_hash_table_unref (hash); g_hash_table_unref (hash);
talloc_free (messages); talloc_free (messages);
return count; return ret;
} }
notmuch_database_t * notmuch_database_t *