diff --git a/database.cc b/database.cc index 16c51459..442b850d 100644 --- a/database.cc +++ b/database.cc @@ -92,12 +92,51 @@ find_doc_ids (notmuch_database_t *notmuch, free (term); } +static notmuch_private_status_t +find_unique_doc_id (notmuch_database_t *notmuch, + const char *prefix_name, + const char *value, + unsigned int *doc_id) +{ + Xapian::PostingIterator i, end; + + find_doc_ids (notmuch, prefix_name, value, &i, &end); + + if (i == end) { + *doc_id = 0; + return NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND; + } else { + *doc_id = *i; + return NOTMUCH_PRIVATE_STATUS_SUCCESS; + } +} + static Xapian::Document find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id) { return notmuch->xapian_db->get_document (doc_id); } +static notmuch_private_status_t +find_unique_document (notmuch_database_t *notmuch, + const char *prefix_name, + const char *value, + Xapian::Document *document, + unsigned int *doc_id) +{ + notmuch_private_status_t status; + + status = find_unique_doc_id (notmuch, prefix_name, value, doc_id); + + if (status) { + *document = Xapian::Document (); + return status; + } + + *document = find_document_for_doc_id (notmuch, *doc_id); + return NOTMUCH_PRIVATE_STATUS_SUCCESS; +} + static void insert_thread_id (GHashTable *thread_ids, Xapian::Document doc) { @@ -125,14 +164,15 @@ notmuch_message_t * notmuch_database_find_message (notmuch_database_t *notmuch, const char *message_id) { - Xapian::PostingIterator i, end; + notmuch_private_status_t status; + unsigned int doc_id; - find_doc_ids (notmuch, "msgid", message_id, &i, &end); + status = find_unique_doc_id (notmuch, "msgid", message_id, &doc_id); - if (i == end) + if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) return NULL; - return _notmuch_message_create (notmuch, notmuch, *i); + return _notmuch_message_create (notmuch, notmuch, doc_id); } /* Return one or more thread_ids, (as a GPtrArray of strings), for the diff --git a/notmuch-private.h b/notmuch-private.h index a56501e9..5d0c1fae 100644 --- a/notmuch-private.h +++ b/notmuch-private.h @@ -91,6 +91,7 @@ typedef enum _notmuch_private_status { /* Then add our own private values. */ NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG, + NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND, NOTMUCH_PRIVATE_STATUS_LAST_STATUS } notmuch_private_status_t;