Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 16CEF431FAF for ; Mon, 6 Oct 2014 16:17:39 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Level: X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 94FECnKpB2Wf for ; Mon, 6 Oct 2014 16:17:34 -0700 (PDT) Received: from dmz-mailsec-scanner-5.mit.edu (dmz-mailsec-scanner-5.mit.edu [18.7.68.34]) by olra.theworths.org (Postfix) with ESMTP id 43228431FAE for ; Mon, 6 Oct 2014 16:17:30 -0700 (PDT) X-AuditID: 12074422-f79436d000000c21-0f-54332309846b Received: from mailhub-auth-2.mit.edu ( [18.7.62.36]) (using TLS with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by dmz-mailsec-scanner-5.mit.edu (Symantec Messaging Gateway) with SMTP id 62.37.03105.90323345; Mon, 6 Oct 2014 19:17:29 -0400 (EDT) Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s96NHP4J025204; Mon, 6 Oct 2014 19:17:25 -0400 Received: from drake.dyndns.org ([12.185.136.2]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s96NHMOI014461 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT); Mon, 6 Oct 2014 19:17:24 -0400 Received: from amthrax by drake.dyndns.org with local (Exim 4.84) (envelope-from ) id 1XbHWr-0001JL-LH; Mon, 06 Oct 2014 17:17:21 -0600 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v2 02/12] lib: Refactor _notmuch_database_link_message Date: Mon, 6 Oct 2014 17:17:08 -0600 Message-Id: <1412637438-4821-3-git-send-email-aclements@csail.mit.edu> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu> References: <1412637438-4821-1-git-send-email-aclements@csail.mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nosupbBxi0LvDxOJGazejxfWbM5kd mDyerbrF7LHl0HvmAKYoLpuU1JzMstQifbsErowLSxUKmuUqls3fzN7AuFm8i5GTQ0LAROLf 1Q3sELaYxIV769m6GLk4hARmM0l0fD0O5WxglPjyYysLhLOUSeJWw3RGCGcJo8TS/9vB+tkE 9CVWrJ3ECmKLCEhL7Lw7G8xmFrCUuPq1nQnEFhZwk5i3dg5QnIODRUBV4uFXIZAwL1B4T9ss Nogz5CQ27P7PCGJzCrhLrLkyGWyMEFDN859PWSYw8i9gZFjFKJuSW6Wbm5iZU5yarFucnJiX l1qka6qXm1mil5pSuokRHEguSjsYfx5UOsQowMGoxMMbscMwRIg1say4MvcQoyQHk5Io7xx2 4xAhvqT8lMqMxOKM+KLSnNTiQ4wSHMxKIrxcv41ChHhTEiurUovyYVLSHCxK4rybfvCFCAmk J5akZqemFqQWwWRlODiUJHg7FIGGChalpqdWpGXmlCCkmTg4QYbzAA0/BFLDW1yQmFucmQ6R P8WoKCXOGwySEABJZJTmwfXCIv0VozjQK8K880GqeIBJAq77FdBgJqDBpnP0QQaXJCKkpBoY Lf618wt7+e+7wOyfsuCHy93MU/2vr5oxKB09ocboOvNEg1CC3Ldbf07p2kf1SaVdD+d/u/vR aQsR75xwvj+pKVXTJ06WPc16SuWJe/iTTJdXpxNYulmuuZhfvKeV0HM+1urMg8CrhzhYchRa 5zj6Giz+mbzeY+n79A+127fn7FyklWKtZKCmxFKckWioxVxUnAgAlPfUXs8CAAA= X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Oct 2014 23:17:39 -0000 From: Austin Clements This moves the code to retrieve and clear the metadata thread ID out of _notmuch_database_link_message into its own function. This will simplify future changes. --- lib/database.cc | 69 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/lib/database.cc b/lib/database.cc index 4e68706..1c6ffc5 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -1958,6 +1958,37 @@ _notmuch_database_link_message_to_children (notmuch_database_t *notmuch, return ret; } +/* Fetch and clear the stored thread_id for message, or NULL if none. */ +static char * +_consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch, + notmuch_message_t *message) +{ + const char *message_id; + string stored_id; + char *metadata_key; + + message_id = notmuch_message_get_message_id (message); + metadata_key = _get_metadata_thread_id_key (ctx, message_id); + + /* 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. + */ + stored_id = notmuch->xapian_db->get_metadata (metadata_key); + if (stored_id.empty ()) { + return NULL; + } else { + Xapian::WritableDatabase *db; + + db = static_cast (notmuch->xapian_db); + + /* Clear the metadata for this message ID. We don't need it + * anymore. */ + db->set_metadata (metadata_key, ""); + + return talloc_strdup (ctx, stored_id.c_str ()); + } +} + /* Given a (mostly empty) 'message' and its corresponding * 'message_file' link it to existing threads in the database. * @@ -1988,42 +2019,25 @@ _notmuch_database_link_message (notmuch_database_t *notmuch, notmuch_message_t *message, notmuch_message_file_t *message_file) { + void *local = talloc_new (NULL); notmuch_status_t status; - const char *message_id, *thread_id = NULL; - char *metadata_key; - string stored_id; - - message_id = notmuch_message_get_message_id (message); - metadata_key = _get_metadata_thread_id_key (message, message_id); - - /* 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. - */ - stored_id = notmuch->xapian_db->get_metadata (metadata_key); - if (! stored_id.empty()) { - Xapian::WritableDatabase *db; - - db = static_cast (notmuch->xapian_db); - - /* Clear the metadata for this message ID. We don't need it - * anymore. */ - db->set_metadata (metadata_key, ""); - thread_id = stored_id.c_str(); + const char *thread_id; - _notmuch_message_add_term (message, "thread", thread_id); - } - talloc_free (metadata_key); + /* Check if the message already had a thread ID */ + thread_id = _consume_metadata_thread_id (local, notmuch, message); + if (thread_id) + _notmuch_message_add_term (message, "thread", thread_id); status = _notmuch_database_link_message_to_parents (notmuch, message, message_file, &thread_id); if (status) - return status; + goto DONE; status = _notmuch_database_link_message_to_children (notmuch, message, &thread_id); if (status) - return status; + goto DONE; /* If not part of any existing thread, generate a new thread ID. */ if (thread_id == NULL) { @@ -2032,7 +2046,10 @@ _notmuch_database_link_message (notmuch_database_t *notmuch, _notmuch_message_add_term (message, "thread", thread_id); } - return NOTMUCH_STATUS_SUCCESS; + DONE: + talloc_free (local); + + return status; } notmuch_status_t -- 2.1.0