]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
lib: Remove unnecessary thread linking steps when using ghost messages
[notmuch] / lib / database.cc
index 92a92d9ae1cc1b1bbf930d28aa17ecad373c51d9..3601f9ded30778620e18a5835de00e6e00ef7854 100644 (file)
@@ -1231,6 +1231,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     notmuch_bool_t timer_is_active = FALSE;
     enum _notmuch_features target_features, new_features;
     notmuch_status_t status;
+    notmuch_private_status_t private_status;
     unsigned int count = 0, total = 0;
 
     status = _notmuch_database_ensure_writable (notmuch);
@@ -1275,6 +1276,13 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
        for (t = db->allterms_begin ("XTIMESTAMP"); t != t_end; t++)
            ++total;
     }
+    if (new_features & NOTMUCH_FEATURE_GHOSTS) {
+       /* The ghost message upgrade converts all thread_id_*
+        * metadata values into ghost message documents. */
+       t_end = db->metadata_keys_end ("thread_id_");
+       for (t = db->metadata_keys_begin ("thread_id_"); t != t_end; ++t)
+           ++total;
+    }
 
     /* Perform the upgrade in a transaction. */
     db->begin_transaction (true);
@@ -1378,10 +1386,64 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
        }
     }
 
+    /* Perform metadata upgrades. */
+
+    /* Prior to NOTMUCH_FEATURE_GHOSTS, thread IDs for missing
+     * messages were stored as database metadata. Change these to
+     * ghost messages.
+     */
+    if (new_features & NOTMUCH_FEATURE_GHOSTS) {
+       notmuch_message_t *message;
+       std::string message_id, thread_id;
+
+       t_end = db->metadata_keys_end (NOTMUCH_METADATA_THREAD_ID_PREFIX);
+       for (t = db->metadata_keys_begin (NOTMUCH_METADATA_THREAD_ID_PREFIX);
+            t != t_end; ++t) {
+           if (do_progress_notify) {
+               progress_notify (closure, (double) count / total);
+               do_progress_notify = 0;
+           }
+
+           message_id = (*t).substr (
+               strlen (NOTMUCH_METADATA_THREAD_ID_PREFIX));
+           thread_id = db->get_metadata (*t);
+
+           /* Create ghost message */
+           message = _notmuch_message_create_for_message_id (
+               notmuch, message_id.c_str (), &private_status);
+           if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
+               /* Document already exists; ignore the stored thread ID */
+           } else if (private_status ==
+                      NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
+               private_status = _notmuch_message_initialize_ghost (
+                   message, thread_id.c_str ());
+               if (! private_status)
+                   _notmuch_message_sync (message);
+           }
+
+           if (private_status) {
+               fprintf (stderr,
+                        "Upgrade failed while creating ghost messages.\n");
+               status = COERCE_STATUS (private_status, "Unexpected status from _notmuch_message_initialize_ghost");
+               goto DONE;
+           }
+
+           /* Clear saved metadata thread ID */
+           db->set_metadata (*t, "");
+
+           ++count;
+       }
+    }
+
+    status = NOTMUCH_STATUS_SUCCESS;
     db->set_metadata ("features", _print_features (local, notmuch->features));
     db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));
 
-    db->commit_transaction ();
+ DONE:
+    if (status == NOTMUCH_STATUS_SUCCESS)
+       db->commit_transaction ();
+    else
+       db->cancel_transaction ();
 
     if (timer_is_active) {
        /* Now stop the timer. */
@@ -1397,7 +1459,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
     }
 
     talloc_free (local);
-    return NOTMUCH_STATUS_SUCCESS;
+    return status;
 }
 
 notmuch_status_t
@@ -2074,11 +2136,11 @@ _consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch,
  * 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 messages are processed out of order).
+ * found. We will also merge any existing, distinct threads where this
+ * message belongs to both, (which is not uncommon when messages are
+ * processed out of order).
  *
- * Finally, if no thread ID has been found through parent or child, we
+ * Finally, if no thread ID has been found through referenced messages, 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
@@ -2110,10 +2172,23 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,
     if (status)
        goto DONE;
 
-    status = _notmuch_database_link_message_to_children (notmuch, message,
-                                                        &thread_id);
-    if (status)
-       goto DONE;
+    if (! (notmuch->features & NOTMUCH_FEATURE_GHOSTS)) {
+       /* In general, it shouldn't be necessary to link children,
+        * since the earlier indexing of those children will have
+        * stored a thread ID for the missing parent.  However, prior
+        * to ghost messages, these stored thread IDs were NOT
+        * rewritten during thread merging (and there was no
+        * performant way to do so), so if indexed children were
+        * pulled into a different thread ID by a merge, it was
+        * necessary to pull them *back* into the stored thread ID of
+        * the parent.  With ghost messages, we just rewrite the
+        * stored thread IDs during merging, so this workaround isn't
+        * necessary. */
+       status = _notmuch_database_link_message_to_children (notmuch, message,
+                                                            &thread_id);
+       if (status)
+           goto DONE;
+    }
 
     /* If not part of any existing thread, generate a new thread ID. */
     if (thread_id == NULL) {