--- /dev/null
+Return-Path: <bremner@tethera.net>\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 F2694431FDC\r
+ for <notmuch@notmuchmail.org>; Tue, 11 Mar 2014 16:03:14 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ 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 ImtzEESjRaGX for <notmuch@notmuchmail.org>;\r
+ Tue, 11 Mar 2014 16:03:12 -0700 (PDT)\r
+Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
+ (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id D31B4431FD0\r
+ for <notmuch@notmuchmail.org>; Tue, 11 Mar 2014 16:02:31 -0700 (PDT)\r
+Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1WNVgt-0001w3-9r; Tue, 11 Mar 2014 20:02:31 -0300\r
+Received: (nullmailer pid 25847 invoked by uid 1000); Tue, 11 Mar 2014\r
+ 23:01:45 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v6 04/14] lib: refactor folder term update after filename\r
+ removal\r
+Date: Tue, 11 Mar 2014 20:01:30 -0300\r
+Message-Id: <1394578900-25618-5-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.8.5.3\r
+In-Reply-To: <1394578900-25618-1-git-send-email-david@tethera.net>\r
+References: <1394578900-25618-1-git-send-email-david@tethera.net>\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: Tue, 11 Mar 2014 23:03:15 -0000\r
+\r
+From: Jani Nikula <jani@nikula.org>\r
+\r
+Abstract some blocks of code for reuse. No functional changes.\r
+---\r
+ lib/message.cc | 135 ++++++++++++++++++++++++++++-----------------------------\r
+ 1 file changed, 66 insertions(+), 69 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index c91f3a5..7aff4ae 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -481,6 +481,68 @@ notmuch_message_get_replies (notmuch_message_t *message)\r
+ return _notmuch_messages_create (message->replies);\r
+ }\r
+ \r
++static void\r
++_notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
++{\r
++ Xapian::TermIterator i;\r
++ size_t prefix_len = strlen (prefix);\r
++\r
++ while (1) {\r
++ i = message->doc.termlist_begin ();\r
++ i.skip_to (prefix);\r
++\r
++ /* Terminate loop when no terms remain with desired prefix. */\r
++ if (i == message->doc.termlist_end () ||\r
++ strncmp ((*i).c_str (), prefix, prefix_len))\r
++ break;\r
++\r
++ try {\r
++ message->doc.remove_term ((*i));\r
++ } catch (const Xapian::InvalidArgumentError) {\r
++ /* Ignore failure to remove non-existent term. */\r
++ }\r
++ }\r
++}\r
++\r
++/* Add directory based terms for all filenames of the message. */\r
++static notmuch_status_t\r
++_notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)\r
++{\r
++ const char *direntry_prefix = _find_prefix ("file-direntry");\r
++ int direntry_prefix_len = strlen (direntry_prefix);\r
++ Xapian::TermIterator i = message->doc.termlist_begin ();\r
++ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
++\r
++ for (i.skip_to (direntry_prefix); i != message->doc.termlist_end (); i++) {\r
++ unsigned int directory_id;\r
++ const char *direntry, *directory;\r
++ char *colon;\r
++\r
++ /* Terminate loop at first term without desired prefix. */\r
++ if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))\r
++ break;\r
++\r
++ /* Indicate that there are filenames remaining. */\r
++ status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
++\r
++ direntry = (*i).c_str ();\r
++ direntry += direntry_prefix_len;\r
++\r
++ directory_id = strtol (direntry, &colon, 10);\r
++\r
++ if (colon == NULL || *colon != ':')\r
++ INTERNAL_ERROR ("malformed direntry");\r
++\r
++ directory = _notmuch_database_get_directory_path (ctx,\r
++ message->notmuch,\r
++ directory_id);\r
++ if (strlen (directory))\r
++ _notmuch_message_gen_terms (message, "folder", directory);\r
++ }\r
++\r
++ return status;\r
++}\r
++\r
+ /* Add an additional 'filename' for 'message'.\r
+ *\r
+ * This change will not be reflected in the database until the next\r
+@@ -536,17 +598,12 @@ notmuch_status_t\r
+ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ const char *filename)\r
+ {\r
+- const char *direntry_prefix = _find_prefix ("file-direntry");\r
+- int direntry_prefix_len = strlen (direntry_prefix);\r
+- const char *folder_prefix = _find_prefix ("folder");\r
+- int folder_prefix_len = strlen (folder_prefix);\r
+ void *local = talloc_new (message);\r
++ const char *folder_prefix = _find_prefix ("folder");\r
+ char *zfolder_prefix = talloc_asprintf(local, "Z%s", folder_prefix);\r
+- int zfolder_prefix_len = strlen (zfolder_prefix);\r
+ char *direntry;\r
+ notmuch_private_status_t private_status;\r
+ notmuch_status_t status;\r
+- Xapian::TermIterator i, last;\r
+ \r
+ status = _notmuch_database_filename_to_direntry (\r
+ local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);\r
+@@ -567,73 +624,13 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ * 3. adding back terms for all remaining filenames of the message. */\r
+ \r
+ /* 1. removing all "folder:" terms */\r
+- while (1) {\r
+- i = message->doc.termlist_begin ();\r
+- i.skip_to (folder_prefix);\r
+-\r
+- /* Terminate loop when no terms remain with desired prefix. */\r
+- if (i == message->doc.termlist_end () ||\r
+- strncmp ((*i).c_str (), folder_prefix, folder_prefix_len))\r
+- {\r
+- break;\r
+- }\r
+-\r
+- try {\r
+- message->doc.remove_term ((*i));\r
+- } catch (const Xapian::InvalidArgumentError) {\r
+- /* Ignore failure to remove non-existent term. */\r
+- }\r
+- }\r
++ _notmuch_message_remove_terms (message, folder_prefix);\r
+ \r
+ /* 2. removing all "folder:" stemmed terms */\r
+- while (1) {\r
+- i = message->doc.termlist_begin ();\r
+- i.skip_to (zfolder_prefix);\r
+-\r
+- /* Terminate loop when no terms remain with desired prefix. */\r
+- if (i == message->doc.termlist_end () ||\r
+- strncmp ((*i).c_str (), zfolder_prefix, zfolder_prefix_len))\r
+- {\r
+- break;\r
+- }\r
+-\r
+- try {\r
+- message->doc.remove_term ((*i));\r
+- } catch (const Xapian::InvalidArgumentError) {\r
+- /* Ignore failure to remove non-existent term. */\r
+- }\r
+- }\r
++ _notmuch_message_remove_terms (message, zfolder_prefix);\r
+ \r
+ /* 3. adding back terms for all remaining filenames of the message. */\r
+- i = message->doc.termlist_begin ();\r
+- i.skip_to (direntry_prefix);\r
+-\r
+- for (; i != message->doc.termlist_end (); i++) {\r
+- unsigned int directory_id;\r
+- const char *direntry, *directory;\r
+- char *colon;\r
+-\r
+- /* Terminate loop at first term without desired prefix. */\r
+- if (strncmp ((*i).c_str (), direntry_prefix, direntry_prefix_len))\r
+- break;\r
+-\r
+- /* Indicate that there are filenames remaining. */\r
+- status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
+-\r
+- direntry = (*i).c_str ();\r
+- direntry += direntry_prefix_len;\r
+-\r
+- directory_id = strtol (direntry, &colon, 10);\r
+-\r
+- if (colon == NULL || *colon != ':')\r
+- INTERNAL_ERROR ("malformed direntry");\r
+-\r
+- directory = _notmuch_database_get_directory_path (local,\r
+- message->notmuch,\r
+- directory_id);\r
+- if (strlen (directory))\r
+- _notmuch_message_gen_terms (message, "folder", directory);\r
+- }\r
++ status = _notmuch_message_add_directory_terms (local, message);\r
+ \r
+ talloc_free (local);\r
+ \r
+-- \r
+1.8.5.3\r
+\r