--- /dev/null
+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 1CF58431FC9\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 07:19:51 -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 4BS46qkmBdwK for <notmuch@notmuchmail.org>;\r
+ Fri, 3 Oct 2014 07:19:45 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (dmz-mailsec-scanner-3.mit.edu\r
+ [18.9.25.14])\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 A4DFF431FD4\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 07:19:26 -0700 (PDT)\r
+X-AuditID: 1209190e-f79d46d000003643-1c-542eb06db730\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 11.C6.13891.D60BE245; Fri, 3 Oct 2014 10:19:25 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s93EJOum021145; \r
+ Fri, 3 Oct 2014 10:19:24 -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 s93EJLOT023308\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-0002Jc-0V; Fri, 03 Oct 2014 10:19:21 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 02/11] lib: Refactor _notmuch_database_link_message\r
+Date: Fri, 3 Oct 2014 10:19:09 -0400\r
+Message-Id: <1412345958-8278-3-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+NgFtrAIsWRmVeSWpSXmKPExsUixG6nopu7QS/E4MIxC4vrN2cyOzB6PFt1\r
+ izmAMYrLJiU1J7MstUjfLoEr48JShYJmuYpl8zezNzBuFu9i5OSQEDCRODxpBzuELSZx4d56\r
+ ti5GLg4hgdlMEpNuHYByNjBKHPmxiR3Cuc0kcWXBHlaQFiGBJYwS0ydpgdhsAvoSK9ZOAouL\r
+ CEhL7Lw7G8jm4GAWUJP406UCYgoLOEt836ECUsEioCqxdOsqsMW8Am4SK548YoY4Qk5iw+7/\r
+ jCA2p4C7xIW7T9kgNrlJrDrWzTaBkX8BI8MqRtmU3Crd3MTMnOLUZN3i5MS8vNQiXWO93MwS\r
+ vdSU0k2MoIDhlOTbwfj1oNIhRgEORiUe3g83dEOEWBPLiitzDzFKcjApifJWLtYLEeJLyk+p\r
+ zEgszogvKs1JLT7EKMHBrCTCu2AlUI43JbGyKrUoHyYlzcGiJM676QdfiJBAemJJanZqakFq\r
+ EUxWhoNDSYK3fT1Qo2BRanpqRVpmTglCmomDE2Q4D9BwsBre4oLE3OLMdIj8KUZFKXHeh+uA\r
+ EgIgiYzSPLheWES/YhQHekWYtwuknQeYDOC6XwENZgIa/M5eF2RwSSJCSqqBccHSqXas/0V8\r
+ WB0C7DjjdnoxB7t7/tzwKfH7tAWvDQ09NBZIz/OoE97CE7ivNzmzsuXiyV81j9Y3fQ9JvLQh\r
+ Ln3V4rtMN5e9/ZPRxGqsIaqxZPati6kBm5j2vFS4+2RRC4+0aMqh0/7/SnLC1fTX8G03cBIy\r
+ ZRTq4Xyhcs7igMDjKYJinZf6lViKMxINtZiLihMBKo+WW8MCAAA=\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:51 -0000\r
+\r
+From: Austin Clements <amdragon@mit.edu>\r
+\r
+This moves the code to retrieve and clear the metadata thread ID out\r
+of _notmuch_database_link_message into its own function. This will\r
+simplify future changes.\r
+---\r
+ lib/database.cc | 69 +++++++++++++++++++++++++++++++++++----------------------\r
+ 1 file changed, 43 insertions(+), 26 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 4e68706..1c6ffc5 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -1958,6 +1958,37 @@ _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,\r
+ return ret;\r
+ }\r
+ \r
++/* Fetch and clear the stored thread_id for message, or NULL if none. */\r
++static char *\r
++_consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch,\r
++ notmuch_message_t *message)\r
++{\r
++ const char *message_id;\r
++ string stored_id;\r
++ char *metadata_key;\r
++\r
++ message_id = notmuch_message_get_message_id (message);\r
++ metadata_key = _get_metadata_thread_id_key (ctx, message_id);\r
++\r
++ /* Check if we have already seen related messages to this one.\r
++ * If we have then use the thread_id that we stored at that time.\r
++ */\r
++ stored_id = notmuch->xapian_db->get_metadata (metadata_key);\r
++ if (stored_id.empty ()) {\r
++ return NULL;\r
++ } else {\r
++ Xapian::WritableDatabase *db;\r
++\r
++ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
++\r
++ /* Clear the metadata for this message ID. We don't need it\r
++ * anymore. */\r
++ db->set_metadata (metadata_key, "");\r
++\r
++ return talloc_strdup (ctx, stored_id.c_str ());\r
++ }\r
++}\r
++\r
+ /* Given a (mostly empty) 'message' and its corresponding\r
+ * 'message_file' link it to existing threads in the database.\r
+ *\r
+@@ -1988,42 +2019,25 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,\r
+ notmuch_message_t *message,\r
+ notmuch_message_file_t *message_file)\r
+ {\r
++ void *local = talloc_new (NULL);\r
+ notmuch_status_t status;\r
+- const char *message_id, *thread_id = NULL;\r
+- char *metadata_key;\r
+- string stored_id;\r
+-\r
+- message_id = notmuch_message_get_message_id (message);\r
+- metadata_key = _get_metadata_thread_id_key (message, message_id);\r
+-\r
+- /* Check if we have already seen related messages to this one.\r
+- * If we have then use the thread_id that we stored at that time.\r
+- */\r
+- stored_id = notmuch->xapian_db->get_metadata (metadata_key);\r
+- if (! stored_id.empty()) {\r
+- Xapian::WritableDatabase *db;\r
+-\r
+- db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
+-\r
+- /* Clear the metadata for this message ID. We don't need it\r
+- * anymore. */\r
+- db->set_metadata (metadata_key, "");\r
+- thread_id = stored_id.c_str();\r
++ const char *thread_id;\r
+ \r
+- _notmuch_message_add_term (message, "thread", thread_id);\r
+- }\r
+- talloc_free (metadata_key);\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
+ \r
+ status = _notmuch_database_link_message_to_parents (notmuch, message,\r
+ message_file,\r
+ &thread_id);\r
+ if (status)\r
+- return status;\r
++ goto DONE;\r
+ \r
+ status = _notmuch_database_link_message_to_children (notmuch, message,\r
+ &thread_id);\r
+ if (status)\r
+- return status;\r
++ goto DONE;\r
+ \r
+ /* If not part of any existing thread, generate a new thread ID. */\r
+ if (thread_id == NULL) {\r
+@@ -2032,7 +2046,10 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,\r
+ _notmuch_message_add_term (message, "thread", thread_id);\r
+ }\r
+ \r
+- return NOTMUCH_STATUS_SUCCESS;\r
++ DONE:\r
++ talloc_free (local);\r
++\r
++ return status;\r
+ }\r
+ \r
+ notmuch_status_t\r
+-- \r
+2.1.0\r
+\r