]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
lib: Fix internal documentation of _notmuch_database_link_message
[notmuch] / lib / database.cc
index d0262722cff27dce3086236ea7482e7ffaf968de..2511caec7a8623e36c20ba92d32613f28f662ae7 100644 (file)
@@ -147,6 +147,7 @@ prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
 prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
     { "thread",                        "G" },
     { "tag",                   "K" },
+    { "is",                    "K" },
     { "id",                    "Q" }
 };
 
@@ -533,6 +534,8 @@ notmuch_database_open (const char *path,
     notmuch->needs_upgrade = FALSE;
     notmuch->mode = mode;
     try {
+       string last_thread_id;
+
        if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
            notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
                                                               Xapian::DB_CREATE_OR_OPEN);
@@ -567,6 +570,20 @@ notmuch_database_open (const char *path,
                         notmuch_path, version, NOTMUCH_DATABASE_VERSION);
            }
        }
+
+       last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
+       if (last_thread_id.empty ()) {
+           notmuch->last_thread_id = 0;
+       } else {
+           const char *str;
+           char *end;
+
+           str = last_thread_id.c_str ();
+           notmuch->last_thread_id = strtoull (str, &end, 16);
+           if (*end != '\0')
+               INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
+       }
+
        notmuch->query_parser = new Xapian::QueryParser;
        notmuch->term_gen = new Xapian::TermGenerator;
        notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
@@ -681,8 +698,7 @@ handle_sigalrm (unused (int signal))
 notmuch_status_t
 notmuch_database_upgrade (notmuch_database_t *notmuch,
                          void (*progress_notify) (void *closure,
-                                                  unsigned int count,
-                                                  unsigned int total),
+                                                  double progress),
                          void *closure)
 {
     Xapian::WritableDatabase *db;
@@ -691,6 +707,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     notmuch_bool_t timer_is_active = FALSE;
     unsigned int version;
     notmuch_status_t status;
+    unsigned int count = 0, total = 0;
 
     status = _notmuch_database_ensure_writable (notmuch);
     if (status)
@@ -722,39 +739,46 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     }
 
     /* Before version 1, each message document had its filename in the
-     * data field. Move that into the new format by calling
+     * data field. Copy that into the new format by calling
      * notmuch_message_add_filename.
      */
     if (version < 1) {
-       unsigned int count = 0, total;
        notmuch_query_t *query = notmuch_query_create (notmuch, "");
        notmuch_messages_t *messages;
        notmuch_message_t *message;
+       char *filename;
+       Xapian::TermIterator t, t_end;
 
        total = notmuch_query_count_messages (query);
 
        for (messages = notmuch_query_search_messages (query);
-            notmuch_messages_has_more (messages);
-            notmuch_messages_advance (messages))
+            notmuch_messages_valid (messages);
+            notmuch_messages_move_to_next (messages))
        {
            if (do_progress_notify) {
-               progress_notify (closure, count, total);
+               progress_notify (closure, (double) count / total);
                do_progress_notify = 0;
            }
 
            message = notmuch_messages_get (messages);
 
-           _notmuch_message_upgrade_filename_storage (message);
+           filename = _notmuch_message_talloc_copy_data (message);
+           if (filename && *filename != '\0') {
+               _notmuch_message_add_filename (message, filename);
+               _notmuch_message_sync (message);
+           }
+           talloc_free (filename);
+
+           notmuch_message_destroy (message);
 
            count++;
        }
-    }
 
-    /* Also, before version 1 we stored directory timestamps in
-     * XTIMESTAMP documents instead of the current XDIRECTORY
-     * documents. So convert those as well. */
-    if (version < 1) {
-       Xapian::TermIterator t, t_end;
+       notmuch_query_destroy (query);
+
+       /* Also, before version 1 we stored directory timestamps in
+        * XTIMESTAMP documents instead of the current XDIRECTORY
+        * documents. So copy those as well. */
 
        t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
 
@@ -775,6 +799,11 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
                time_t mtime;
                notmuch_directory_t *directory;
 
+               if (do_progress_notify) {
+                   progress_notify (closure, (double) count / total);
+                   do_progress_notify = 0;
+               }
+
                document = find_document_for_doc_id (notmuch, *p);
                mtime = Xapian::sortable_unserialise (
                    document.get_value (NOTMUCH_VALUE_TIMESTAMP));
@@ -783,8 +812,6 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
                                                            term.c_str() + 10);
                notmuch_directory_set_mtime (directory, mtime);
                notmuch_directory_destroy (directory);
-
-               db->delete_document (*p);
            }
        }
     }
@@ -792,6 +819,66 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));
     db->flush ();
 
+    /* Now that the upgrade is complete we can remove the old data
+     * and documents that are no longer needed. */
+    if (version < 1) {
+       notmuch_query_t *query = notmuch_query_create (notmuch, "");
+       notmuch_messages_t *messages;
+       notmuch_message_t *message;
+       char *filename;
+
+       for (messages = notmuch_query_search_messages (query);
+            notmuch_messages_valid (messages);
+            notmuch_messages_move_to_next (messages))
+       {
+           if (do_progress_notify) {
+               progress_notify (closure, (double) count / total);
+               do_progress_notify = 0;
+           }
+
+           message = notmuch_messages_get (messages);
+
+           filename = _notmuch_message_talloc_copy_data (message);
+           if (filename && *filename != '\0') {
+               _notmuch_message_clear_data (message);
+               _notmuch_message_sync (message);
+           }
+           talloc_free (filename);
+
+           notmuch_message_destroy (message);
+       }
+
+       notmuch_query_destroy (query);
+    }
+
+    if (version < 1) {
+       Xapian::TermIterator t, t_end;
+
+       t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
+
+       for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP");
+            t != t_end;
+            t++)
+       {
+           Xapian::PostingIterator p, p_end;
+           std::string term = *t;
+
+           p_end = notmuch->xapian_db->postlist_end (term);
+
+           for (p = notmuch->xapian_db->postlist_begin (term);
+                p != p_end;
+                p++)
+           {
+               if (do_progress_notify) {
+                   progress_notify (closure, (double) count / total);
+                   do_progress_notify = 0;
+               }
+
+               db->delete_document (*p);
+           }
+       }
+    }
+
     if (timer_is_active) {
        /* Now stop the timer. */
        timerval.it_interval.tv_sec = 0;
@@ -1024,12 +1111,40 @@ notmuch_database_get_directory (notmuch_database_t *notmuch,
     return _notmuch_directory_create (notmuch, path, &status);
 }
 
+static const char *
+_notmuch_database_generate_thread_id (notmuch_database_t *notmuch)
+{
+    /* 16 bytes (+ terminator) for hexadecimal representation of
+     * a 64-bit integer. */
+    static char thread_id[17];
+    Xapian::WritableDatabase *db;
+
+    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+
+    notmuch->last_thread_id++;
+
+    sprintf (thread_id, "%016" PRIx64, notmuch->last_thread_id);
+
+    db->set_metadata ("last_thread_id", thread_id);
+
+    return thread_id;
+}
+
+static char *
+_get_metadata_thread_id_key (void *ctx, const char *message_id)
+{
+    return talloc_asprintf (ctx, "thread_id_%s", message_id);
+}
+
 /* Find the thread ID to which the message with 'message_id' belongs.
  *
- * Returns NULL if no message with message ID 'message_id' is in the
- * database.
+ * Always returns a newly talloced string belonging to 'ctx'.
  *
- * Otherwise, returns a newly talloced string belonging to 'ctx'.
+ * Note: If there is no message in the database with the given
+ * 'message_id' then a new thread_id will be allocated for this
+ * message and stored in the database metadata, (where this same
+ * thread ID can be looked up if the message is added to the database
+ * later).
  */
 static const char *
 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
@@ -1037,19 +1152,41 @@ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
                                  const char *message_id)
 {
     notmuch_message_t *message;
-    const char *ret = NULL;
+    string thread_id_string;
+    const char *thread_id;
+    char *metadata_key;
+    Xapian::WritableDatabase *db;
 
     message = notmuch_database_find_message (notmuch, message_id);
-    if (message == NULL)
-       goto DONE;
 
-    ret = talloc_steal (ctx, notmuch_message_get_thread_id (message));
+    if (message) {
+       thread_id = talloc_steal (ctx, notmuch_message_get_thread_id (message));
 
-  DONE:
-    if (message)
        notmuch_message_destroy (message);
 
-    return ret;
+       return thread_id;
+    }
+
+    /* Message has not been seen yet.
+     *
+     * We may have seen a reference to it already, in which case, we
+     * can return the thread ID stored in the metadata. Otherwise, we
+     * generate a new thread ID and store it there.
+     */
+    db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+    metadata_key = _get_metadata_thread_id_key (ctx, message_id);
+    thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
+
+    if (thread_id_string.empty()) {
+       thread_id = _notmuch_database_generate_thread_id (notmuch);
+       db->set_metadata (metadata_key, thread_id);
+    } else {
+       thread_id = thread_id_string.c_str();
+    }
+
+    talloc_free (metadata_key);
+
+    return thread_id;
 }
 
 static notmuch_status_t
@@ -1211,13 +1348,27 @@ _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
 /* Given a (mostly empty) 'message' and its corresponding
  * 'message_file' link it to existing threads in the database.
  *
- * We first look at 'message_file' and its link-relevant headers
- * (References and In-Reply-To) for message IDs. We also look in the
- * database for existing message that reference 'message'.
+ * The first check is in the metadata of the database to see if we
+ * have pre-allocated a thread_id in advance for this message, (which
+ * would have happened if a message was previously added that
+ * referenced this one).
+ *
+ * Second, we look at 'message_file' and its link-relevant headers
+ * (References and In-Reply-To) for message IDs.
  *
- * The end result is to call _notmuch_message_ensure_thread_id which
- * generates a new thread ID if the message doesn't connect to any
- * existing threads.
+ * Finally, we look in the database for existing message that
+ * reference 'message'.
+ *
+ * In all cases, we assign to the current message the first thread_id
+ * found (through either parent or child). We will also merge any
+ * existing, distinct threads where this message belongs to both,
+ * (which is not uncommon when mesages are processed out of order).
+ *
+ * Finally, if no thread ID has been found through parent or child, we
+ * call _notmuch_message_generate_thread_id to generate a new thread
+ * ID. This should only happen for new, top-level messages, (no
+ * References or In-Reply-To header in this message, and no previously
+ * added message refers to this message).
  */
 static notmuch_status_t
 _notmuch_database_link_message (notmuch_database_t *notmuch,
@@ -1226,6 +1377,19 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,
 {
     notmuch_status_t status;
     const char *thread_id = NULL;
+    char *metadata_key = _get_metadata_thread_id_key (message,
+            notmuch_message_get_message_id (message));
+    /* Check if we have already seen related messages to this one.
+     * If we have then use the thread_id that we stored at that time.
+     */
+    string stored_id = notmuch->xapian_db->get_metadata (metadata_key);
+    if (!stored_id.empty()) {
+        Xapian::WritableDatabase *db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+        db->set_metadata (metadata_key, "");
+        thread_id = stored_id.c_str();
+        _notmuch_message_add_term (message, "thread", thread_id);
+    }
+    talloc_free (metadata_key);
 
     status = _notmuch_database_link_message_to_parents (notmuch, message,
                                                        message_file,
@@ -1238,8 +1402,12 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,
     if (status)
        return status;
 
-    if (thread_id == NULL)
-       _notmuch_message_ensure_thread_id (message);
+    /* If not part of any existing thread, generate a new thread ID. */
+    if (thread_id == NULL) {
+       thread_id = _notmuch_database_generate_thread_id (notmuch);
+
+       _notmuch_message_add_term (message, "thread", thread_id);
+    }
 
     return NOTMUCH_STATUS_SUCCESS;
 }