]> git.notmuchmail.org Git - notmuch/commitdiff
database: Add private find_unique_doc_id and find_unique_document functions
authorCarl Worth <cworth@cworth.org>
Fri, 23 Oct 2009 21:24:07 +0000 (14:24 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 23 Oct 2009 21:24:07 +0000 (14:24 -0700)
These are a generalization of the unique-ness testing of
notmuch_database_find_message. More preparation for
firectory timestamps.

database.cc
notmuch-private.h

index 16c514591fbe0c36fed80391604fc58096ac697e..442b850d2264874442837fe2ec8e279d03fdc352 100644 (file)
@@ -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
index a56501e98908150fe62ea63d6845e5c650268db3..5d0c1fae31edd36476802afa52f30b5c946dbece 100644 (file)
@@ -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;