--- /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 16CEF431FAF\r
+ for <notmuch@notmuchmail.org>; Mon, 6 Oct 2014 16:17:39 -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 94FECnKpB2Wf for <notmuch@notmuchmail.org>;\r
+ Mon, 6 Oct 2014 16:17:34 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-5.mit.edu (dmz-mailsec-scanner-5.mit.edu\r
+ [18.7.68.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id 43228431FAE\r
+ for <notmuch@notmuchmail.org>; Mon, 6 Oct 2014 16:17:30 -0700 (PDT)\r
+X-AuditID: 12074422-f79436d000000c21-0f-54332309846b\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-5.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 62.37.03105.90323345; Mon, 6 Oct 2014 19:17:29 -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 s96NHP4J025204; \r
+ Mon, 6 Oct 2014 19:17:25 -0400\r
+Received: from drake.dyndns.org ([12.185.136.2]) (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 s96NHMOI014461\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Mon, 6 Oct 2014 19:17:24 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1XbHWr-0001JL-LH; Mon, 06 Oct 2014 17:17:21 -0600\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 02/12] lib: Refactor _notmuch_database_link_message\r
+Date: Mon, 6 Oct 2014 17:17:08 -0600\r
+Message-Id: <1412637438-4821-3-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu>\r
+References: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nosupbBxi0LvDxOJGazejxfWbM5kd\r
+ mDyerbrF7LHl0HvmAKYoLpuU1JzMstQifbsErowLSxUKmuUqls3fzN7AuFm8i5GTQ0LAROLf\r
+ 1Q3sELaYxIV769m6GLk4hARmM0l0fD0O5WxglPjyYysLhLOUSeJWw3RGCGcJo8TS/9vB+tkE\r
+ 9CVWrJ3ECmKLCEhL7Lw7G8xmFrCUuPq1nQnEFhZwk5i3dg5QnIODRUBV4uFXIZAwL1B4T9ss\r
+ Nogz5CQ27P7PCGJzCrhLrLkyGWyMEFDN859PWSYw8i9gZFjFKJuSW6Wbm5iZU5yarFucnJiX\r
+ l1qka6qXm1mil5pSuokRHEguSjsYfx5UOsQowMGoxMMbscMwRIg1say4MvcQoyQHk5Io7xx2\r
+ 4xAhvqT8lMqMxOKM+KLSnNTiQ4wSHMxKIrxcv41ChHhTEiurUovyYVLSHCxK4rybfvCFCAmk\r
+ J5akZqemFqQWwWRlODiUJHg7FIGGChalpqdWpGXmlCCkmTg4QYbzAA0/BFLDW1yQmFucmQ6R\r
+ P8WoKCXOGwySEABJZJTmwfXCIv0VozjQK8K880GqeIBJAq77FdBgJqDBpnP0QQaXJCKkpBoY\r
+ Lf618wt7+e+7wOyfsuCHy93MU/2vr5oxKB09ocboOvNEg1CC3Ldbf07p2kf1SaVdD+d/u/vR\r
+ aQsR75xwvj+pKVXTJ06WPc16SuWJe/iTTJdXpxNYulmuuZhfvKeV0HM+1urMg8CrhzhYchRa\r
+ 5zj6Giz+mbzeY+n79A+127fn7FyklWKtZKCmxFKckWioxVxUnAgAlPfUXs8CAAA=\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: Mon, 06 Oct 2014 23:17:39 -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