[PATCH 07/11] lib: Implement ghost-based thread linking
authorAustin Clements <aclements@csail.mit.edu>
Fri, 3 Oct 2014 14:19:14 +0000 (10:19 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:04:54 +0000 (10:04 -0800)
e3/5ab120c17944ee0e2f58e44024726c73608f2e [new file with mode: 0644]

diff --git a/e3/5ab120c17944ee0e2f58e44024726c73608f2e b/e3/5ab120c17944ee0e2f58e44024726c73608f2e
new file mode 100644 (file)
index 0000000..4f6ddaa
--- /dev/null
@@ -0,0 +1,254 @@
+Return-Path: <aclements@csail.mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 1F458429E40\r
+       for <notmuch@notmuchmail.org>; Fri,  3 Oct 2014 07:19:59 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id McwXxeHu-v1x for <notmuch@notmuchmail.org>;\r
+       Fri,  3 Oct 2014 07:19:51 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (dmz-mailsec-scanner-4.mit.edu\r
+       [18.9.25.15])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id BA631431FD5\r
+       for <notmuch@notmuchmail.org>; Fri,  3 Oct 2014 07:19:29 -0700 (PDT)\r
+X-AuditID: 1209190f-f79aa6d000005b45-b9-542eb07123d2\r
+Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
+       (using TLS with cipher AES256-SHA (256/256 bits))\r
+       (Client did not present a certificate)\r
+       by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 8D.24.23365.170BE245; Fri,  3 Oct 2014 10:19:29 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+       by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id s93EJOTE006610; \r
+       Fri, 3 Oct 2014 10:19:25 -0400\r
+Received: from drake.dyndns.org\r
+       (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+       [216.15.114.40]) (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s93EJL8t023318\r
+       (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+       Fri, 3 Oct 2014 10:19:24 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+       (envelope-from <aclements@csail.mit.edu>)\r
+       id 1Xa3hZ-0002K2-7z; Fri, 03 Oct 2014 10:19:21 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 07/11] lib: Implement ghost-based thread linking\r
+Date: Fri,  3 Oct 2014 10:19:14 -0400\r
+Message-Id: <1412345958-8278-8-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1412345958-8278-1-git-send-email-aclements@csail.mit.edu>\r
+References: <1412345958-8278-1-git-send-email-aclements@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrEIsWRmVeSWpSXmKPExsUixCmqrFu4QS/EYPM7eYvrN2cyOzB6PFt1\r
+       izmAMYrLJiU1J7MstUjfLoErY/HWBcwFU80rWufkNDC263QxcnJICJhITOxYwQJhi0lcuLee\r
+       rYuRi0NIYDaTxLL5t5khnA2MEhsPbGaFcG4zSTy+eQLKWcIo8al3Blg/m4C+xIq1k1hBbBEB\r
+       aYmdd2cD2RwczAJqEn+6VEDCwgIOEuferwcrYRFQlTj4YydYK6+Am0T3xZWMEGfISWzY/R/M\r
+       5hRwl7hw9ykbiC0EVLPqWDfbBEb+BYwMqxhlU3KrdHMTM3OKU5N1i5MT8/JSi3RN9HIzS/RS\r
+       U0o3MYKDRpJ/B+O3g0qHGAU4GJV4eD/c0A0RYk0sK67MPcQoycGkJMpbuVgvRIgvKT+lMiOx\r
+       OCO+qDQntfgQowQHs5II74KVQDnelMTKqtSifJiUNAeLkjjvph98IUIC6YklqdmpqQWpRTBZ\r
+       GQ4OJQne9vVAjYJFqempFWmZOSUIaSYOTpDhPEDDwWp4iwsSc4sz0yHypxgVpcR5H64DSgiA\r
+       JDJK8+B6YVH9ilEc6BVhXiZgjAvxABMCXPcroMFMQIPf2euCDC5JREhJNTDyesh++tL69OIq\r
+       v/IbzxjEovfHxijM35qs5qm7xumz/IcbM61KV4j/arZV/lN8NPv0djsuAb9tUeckzUv/pfD/\r
+       yduwec29Vr3XLy4Itl87od1ua8Wz/0z3yV/WB5c8vnvr3gSDqduf7C10tZV/s4h5d22gq/Su\r
+       ba/2crCeyZ7Rcs1+6ofJLztmKbEUZyQaajEXFScCACZ0yFfFAgAA\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 03 Oct 2014 14:19:59 -0000\r
+\r
+From: Austin Clements <amdragon@mit.edu>\r
+\r
+This updates the thread linking code to use ghost messages instead of\r
+user metadata to link messages into threads.\r
+\r
+In contrast with the old approach, this is actually correct.\r
+Previously, thread merging updated only the thread IDs of message\r
+documents, not thread IDs stored in user metadata.  As originally\r
+diagnosed by Mark Walters [1] and as demonstrated by the broken\r
+T260-thread-order test, this can cause notmuch to fail to link\r
+messages even though they're in the same thread.  In principle the old\r
+approach could have been fixed by updating the user metadata thread\r
+IDs as well, but these are not indexed and hence this would have\r
+required a full scan of all stored thread IDs.  Ghost messages solve\r
+this problem naturally by reusing the exact same thread ID and message\r
+ID representation and indexing as regular messages.\r
+\r
+Furthermore, thanks to this greater symmetry, ghost messages are also\r
+algorithmically simpler.  We continue to support the old user metadata\r
+format, so this patch can't delete any code, but when we do remove\r
+support for the old format, several functions can simply be deleted.\r
+\r
+[1] id:8738h7kv2q.fsf@qmul.ac.uk\r
+---\r
+ lib/database.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++--------\r
+ 1 file changed, 75 insertions(+), 11 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index c641bcd..fdcc526 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -1752,6 +1752,12 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)\r
+                           message_id);\r
+ }\r
\r
++static notmuch_status_t\r
++_resolve_message_id_to_thread_id_old (notmuch_database_t *notmuch,\r
++                                    void *ctx,\r
++                                    const char *message_id,\r
++                                    const char **thread_id_ret);\r
++\r
+ /* Find the thread ID to which the message with 'message_id' belongs.\r
+  *\r
+  * Note: 'thread_id_ret' must not be NULL!\r
+@@ -1760,9 +1766,9 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)\r
+  *\r
+  * Note: If there is no message in the database with the given\r
+  * 'message_id' then a new thread_id will be allocated for this\r
+- * message and stored in the database metadata, (where this same\r
++ * message ID and stored in the database metadata so that the\r
+  * thread ID can be looked up if the message is added to the database\r
+- * later).\r
++ * later.\r
+  */\r
+ static notmuch_status_t\r
+ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,\r
+@@ -1770,6 +1776,49 @@ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,\r
+                                 const char *message_id,\r
+                                 const char **thread_id_ret)\r
+ {\r
++    notmuch_private_status_t status;\r
++    notmuch_message_t *message;\r
++\r
++    if (! (notmuch->features & NOTMUCH_FEATURE_GHOSTS))\r
++      return _resolve_message_id_to_thread_id_old (notmuch, ctx, message_id,\r
++                                                   thread_id_ret);\r
++\r
++    /* Look for this message (regular or ghost) */\r
++    message = _notmuch_message_create_for_message_id (\r
++      notmuch, message_id, &status);\r
++    if (status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
++      /* Message exists */\r
++      *thread_id_ret = talloc_steal (\r
++          ctx, notmuch_message_get_thread_id (message));\r
++    } else if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
++      /* Message did not exist.  Give it a fresh thread ID and\r
++       * populate this message as a ghost message. */\r
++      *thread_id_ret = talloc_strdup (\r
++          ctx, _notmuch_database_generate_thread_id (notmuch));\r
++      if (! *thread_id_ret) {\r
++          status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;\r
++      } else {\r
++          status = _notmuch_message_initialize_ghost (message, *thread_id_ret);\r
++          if (status == 0)\r
++              /* Commit the new ghost message */\r
++              _notmuch_message_sync (message);\r
++      }\r
++    } else {\r
++      /* Create failed. Fall through. */\r
++    }\r
++\r
++    notmuch_message_destroy (message);\r
++\r
++    return COERCE_STATUS (status, "Error creating ghost message");\r
++}\r
++\r
++/* Pre-ghost messages _resolve_message_id_to_thread_id */\r
++static notmuch_status_t\r
++_resolve_message_id_to_thread_id_old (notmuch_database_t *notmuch,\r
++                                    void *ctx,\r
++                                    const char *message_id,\r
++                                    const char **thread_id_ret)\r
++{\r
+     notmuch_status_t status;\r
+     notmuch_message_t *message;\r
+     string thread_id_string;\r
+@@ -2007,7 +2056,7 @@ _consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch,\r
+     }\r
+ }\r
\r
+-/* Given a (mostly empty) 'message' and its corresponding\r
++/* Given a blank or ghost 'message' and its corresponding\r
+  * 'message_file' link it to existing threads in the database.\r
+  *\r
+  * The first check is in the metadata of the database to see if we\r
+@@ -2035,16 +2084,22 @@ _consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch,\r
+ static notmuch_status_t\r
+ _notmuch_database_link_message (notmuch_database_t *notmuch,\r
+                               notmuch_message_t *message,\r
+-                              notmuch_message_file_t *message_file)\r
++                              notmuch_message_file_t *message_file,\r
++                              notmuch_bool_t is_ghost)\r
+ {\r
+     void *local = talloc_new (NULL);\r
+     notmuch_status_t status;\r
+-    const char *thread_id;\r
++    const char *thread_id = NULL;\r
\r
+     /* Check if the message already had a thread ID */\r
+-    thread_id = _consume_metadata_thread_id (local, notmuch, message);\r
+-    if (thread_id)\r
+-      _notmuch_message_add_term (message, "thread", thread_id);\r
++    if (notmuch->features & NOTMUCH_FEATURE_GHOSTS) {\r
++      if (is_ghost)\r
++          thread_id = notmuch_message_get_thread_id (message);\r
++    } else {\r
++      thread_id = _consume_metadata_thread_id (local, notmuch, message);\r
++      if (thread_id)\r
++          _notmuch_message_add_term (message, "thread", thread_id);\r
++    }\r
\r
+     status = _notmuch_database_link_message_to_parents (notmuch, message,\r
+                                                       message_file,\r
+@@ -2079,6 +2134,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
+     notmuch_message_t *message = NULL;\r
+     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, ret2;\r
+     notmuch_private_status_t private_status;\r
++    notmuch_bool_t is_ghost = false;\r
\r
+     const char *date, *header;\r
+     const char *from, *to, *subject;\r
+@@ -2171,12 +2227,20 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
\r
+       _notmuch_message_add_filename (message, filename);\r
\r
+-      /* Is this a newly created message object? */\r
+-      if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
++      /* Is this a newly created message object or a ghost\r
++       * message?  We have to be slightly careful: if this is a\r
++       * blank message, it's not safe to call\r
++       * notmuch_message_get_flag yet. */\r
++      if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND ||\r
++          (is_ghost = notmuch_message_get_flag (\r
++              message, NOTMUCH_MESSAGE_FLAG_GHOST))) {\r
+           _notmuch_message_add_term (message, "type", "mail");\r
++          if (is_ghost)\r
++              /* Convert ghost message to a regular message */\r
++              _notmuch_message_remove_term (message, "type", "ghost");\r
\r
+           ret = _notmuch_database_link_message (notmuch, message,\r
+-                                                message_file);\r
++                                                message_file, is_ghost);\r
+           if (ret)\r
+               goto DONE;\r
\r
+-- \r
+2.1.0\r
+\r