--- /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 16FDC431FC2\r
+ for <notmuch@notmuchmail.org>; Sat, 8 Mar 2014 13:21:05 -0800 (PST)\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 czy-5kskL8lJ for <notmuch@notmuchmail.org>;\r
+ Sat, 8 Mar 2014 13:20:57 -0800 (PST)\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 DC752431FD7\r
+ for <notmuch@notmuchmail.org>; Sat, 8 Mar 2014 13:20:23 -0800 (PST)\r
+Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1WMOfP-00058U-9I; Sat, 08 Mar 2014 17:20:23 -0400\r
+Received: (nullmailer pid 28534 invoked by uid 1000); Sat, 08 Mar 2014\r
+ 21:20:03 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v3 04/15] lib: make folder: prefix literal\r
+Date: Sat, 8 Mar 2014 17:19:34 -0400\r
+Message-Id: <1394313585-28422-5-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.8.5.3\r
+In-Reply-To: <1394313585-28422-1-git-send-email-david@tethera.net>\r
+References: <1394313585-28422-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: Sat, 08 Mar 2014 21:21:05 -0000\r
+\r
+From: Jani Nikula <jani@nikula.org>\r
+\r
+In xapian terms, convert folder: prefix from probabilistic to boolean\r
+prefix, matching the paths, relative form the maildir root, of the\r
+message files, ignoring the maildir new and cur leaf directories.\r
+\r
+folder:foo matches all message files in foo, foo/new, and foo/cur.\r
+\r
+folder:foo/new does *not* match message files in foo/new.\r
+\r
+folder:"" matches all message files in the top level maildir and its\r
+new and cur subdirectories.\r
+\r
+This change constitutes a database change: bump the database version\r
+and add database upgrade support for folder: terms. The upgrade also\r
+adds path: terms.\r
+---\r
+ lib/database.cc | 38 ++++++++++++++++++++++--\r
+ lib/message.cc | 80 ++++++++++++++++++++++++++++++++++++++++++++-------\r
+ lib/notmuch-private.h | 3 ++\r
+ 3 files changed, 108 insertions(+), 13 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 93cc7f5..186e3a7 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -42,7 +42,7 @@ typedef struct {\r
+ const char *prefix;\r
+ } prefix_t;\r
+ \r
+-#define NOTMUCH_DATABASE_VERSION 1\r
++#define NOTMUCH_DATABASE_VERSION 2\r
+ \r
+ #define STRINGIFY(s) _SUB_STRINGIFY(s)\r
+ #define _SUB_STRINGIFY(s) #s\r
+@@ -210,6 +210,7 @@ static prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {\r
+ { "is", "K" },\r
+ { "id", "Q" },\r
+ { "path", "P" },\r
++ { "folder", "XFOLDER:" },\r
+ };\r
+ \r
+ static prefix_t PROBABILISTIC_PREFIX[]= {\r
+@@ -217,7 +218,6 @@ static prefix_t PROBABILISTIC_PREFIX[]= {\r
+ { "to", "XTO" },\r
+ { "attachment", "XATTACHMENT" },\r
+ { "subject", "XSUBJECT"},\r
+- { "folder", "XFOLDER"}\r
+ };\r
+ \r
+ const char *\r
+@@ -1168,6 +1168,40 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ }\r
+ }\r
+ \r
++ /*\r
++ * Prior to version 2, the "folder:" prefix was probabilistic and\r
++ * stemmed. Change it to the current boolean prefix. Add "path:"\r
++ * prefixes while at it.\r
++ */\r
++ if (version < 2) {\r
++ notmuch_query_t *query = notmuch_query_create (notmuch, "");\r
++ notmuch_messages_t *messages;\r
++ notmuch_message_t *message;\r
++\r
++ count = 0;\r
++ total = notmuch_query_count_messages (query);\r
++\r
++ for (messages = notmuch_query_search_messages (query);\r
++ notmuch_messages_valid (messages);\r
++ notmuch_messages_move_to_next (messages)) {\r
++ if (do_progress_notify) {\r
++ progress_notify (closure, (double) count / total);\r
++ do_progress_notify = 0;\r
++ }\r
++\r
++ message = notmuch_messages_get (messages);\r
++\r
++ _notmuch_message_upgrade_folder (message);\r
++ _notmuch_message_sync (message);\r
++\r
++ notmuch_message_destroy (message);\r
++\r
++ count++;\r
++ }\r
++\r
++ notmuch_query_destroy (query);\r
++ }\r
++\r
+ db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));\r
+ db->flush ();\r
+ \r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 21abe8e..31cb9f1 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -504,6 +504,56 @@ _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
+ }\r
+ }\r
+ \r
++/* Return true if p points at "new" or "cur". */\r
++static bool is_maildir (const char *p)\r
++{\r
++ return strcmp (p, "cur") == 0 || strcmp (p, "new") == 0;\r
++}\r
++\r
++/* Add "folder:" term for directory. */\r
++static notmuch_status_t\r
++_notmuch_message_add_folder_terms (notmuch_message_t *message,\r
++ const char *directory)\r
++{\r
++ char *folder, *last;\r
++\r
++ folder = talloc_strdup (NULL, directory);\r
++ if (! folder)\r
++ return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++\r
++ /*\r
++ * If the message file is in a leaf directory named "new" or\r
++ * "cur", presume maildir and index the parent directory. Thus a\r
++ * "folder:" prefix search matches messages in the specified\r
++ * maildir folder, i.e. in the specified directory and its "new"\r
++ * and "cur" subdirectories.\r
++ *\r
++ * Note that this means the "folder:" prefix can't be used for\r
++ * distinguishing between message files in "new" or "cur". The\r
++ * "path:" prefix needs to be used for that.\r
++ *\r
++ * Note the deliberate difference to _filename_is_in_maildir(). We\r
++ * don't want to index different things depending on the existence\r
++ * or non-existence of all maildir sibling directories "new",\r
++ * "cur", and "tmp". Doing so would be surprising, and difficult\r
++ * for the user to fix in case all subdirectories were not in\r
++ * place during indexing.\r
++ */\r
++ last = strrchr (folder, '/');\r
++ if (last) {\r
++ if (is_maildir (last + 1))\r
++ *last = '\0';\r
++ } else if (is_maildir (folder)) {\r
++ *folder = '\0';\r
++ }\r
++\r
++ _notmuch_message_add_term (message, "folder", folder);\r
++\r
++ talloc_free (folder);\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
+ #define RECURSIVE_SUFFIX "/**"\r
+ \r
+ /* Add "path:" terms for directory. */\r
+@@ -570,9 +620,8 @@ _notmuch_message_add_directory_terms (void *ctx, notmuch_message_t *message)\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
++ _notmuch_message_add_folder_terms (message, directory);\r
+ _notmuch_message_add_path_terms (message, directory);\r
+ }\r
+ \r
+@@ -610,9 +659,7 @@ _notmuch_message_add_filename (notmuch_message_t *message,\r
+ * notmuch_directory_get_child_files() . */\r
+ _notmuch_message_add_term (message, "file-direntry", direntry);\r
+ \r
+- /* New terms allow user to search with folder: specification. */\r
+- _notmuch_message_gen_terms (message, "folder", directory);\r
+-\r
++ _notmuch_message_add_folder_terms (message, directory);\r
+ _notmuch_message_add_path_terms (message, directory);\r
+ \r
+ talloc_free (local);\r
+@@ -637,8 +684,6 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ const char *filename)\r
+ {\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
+ char *direntry;\r
+ notmuch_private_status_t private_status;\r
+ notmuch_status_t status;\r
+@@ -659,10 +704,7 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ /* Re-synchronize "folder:" and "path:" terms for this message. */\r
+ \r
+ /* Remove all "folder:" terms. */\r
+- _notmuch_message_remove_terms (message, folder_prefix);\r
+-\r
+- /* Remove all "folder:" stemmed terms. */\r
+- _notmuch_message_remove_terms (message, zfolder_prefix);\r
++ _notmuch_message_remove_terms (message, _find_prefix ("folder"));\r
+ \r
+ /* Remove all "path:" terms. */\r
+ _notmuch_message_remove_terms (message, _find_prefix ("path"));\r
+@@ -675,6 +717,22 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+ return status;\r
+ }\r
+ \r
++/* Upgrade the "folder:" prefix from V1 to V2. */\r
++#define FOLDER_PREFIX_V1 "XFOLDER"\r
++#define ZFOLDER_PREFIX_V1 "Z" FOLDER_PREFIX_V1\r
++void\r
++_notmuch_message_upgrade_folder (notmuch_message_t *message)\r
++{\r
++ /* Remove all old "folder:" terms. */\r
++ _notmuch_message_remove_terms (message, FOLDER_PREFIX_V1);\r
++\r
++ /* Remove all old "folder:" stemmed terms. */\r
++ _notmuch_message_remove_terms (message, ZFOLDER_PREFIX_V1);\r
++\r
++ /* Add new boolean "folder:" and "path:" terms. */\r
++ _notmuch_message_add_directory_terms (message, message);\r
++}\r
++\r
+ char *\r
+ _notmuch_message_talloc_copy_data (notmuch_message_t *message)\r
+ {\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index af185c7..59eb2bc 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -263,6 +263,9 @@ _notmuch_message_gen_terms (notmuch_message_t *message,\r
+ void\r
+ _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);\r
+ \r
++void\r
++_notmuch_message_upgrade_folder (notmuch_message_t *message);\r
++\r
+ notmuch_status_t\r
+ _notmuch_message_add_filename (notmuch_message_t *message,\r
+ const char *filename);\r
+-- \r
+1.8.5.3\r
+\r