--- /dev/null
+Return-Path: <amthrax@drake.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 2473E429E23\r
+ for <notmuch@notmuchmail.org>; Thu, 17 Feb 2011 23:59:40 -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 lfrgXpE-wUEj for <notmuch@notmuchmail.org>;\r
+ Thu, 17 Feb 2011 23:59:38 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
+ [18.9.25.15])\r
+ by olra.theworths.org (Postfix) with ESMTP id 3D55A429E32\r
+ for <notmuch@notmuchmail.org>; Thu, 17 Feb 2011 23:59:30 -0800 (PST)\r
+X-AuditID: 1209190f-b7c1dae000000a2b-6c-4d5e26e27861\r
+Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
+ by dmz-mailsec-scanner-4.mit.edu (Symantec Brightmail Gateway) with\r
+ SMTP id 6C.42.02603.2E62E5D4; Fri, 18 Feb 2011 02:59:30 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id p1I7xTUn027142; \r
+ Fri, 18 Feb 2011 02:59:29 -0500\r
+Received: from drake.mit.edu\r
+ (209-6-116-242.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [209.6.116.242]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id p1I7xSXw001550\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Fri, 18 Feb 2011 02:59:29 -0500 (EST)\r
+Received: from amthrax by drake.mit.edu with local (Exim 4.72)\r
+ (envelope-from <amthrax@drake.mit.edu>)\r
+ id 1PqLFQ-0008LQ-Oq; Fri, 18 Feb 2011 02:59:28 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 08/10] new: Synchronize maildir flags eagerly.\r
+Date: Fri, 18 Feb 2011 02:58:58 -0500\r
+Message-Id: <1298015940-31986-9-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.2.3\r
+In-Reply-To: <1298015940-31986-1-git-send-email-amdragon@mit.edu>\r
+References: <1298015940-31986-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker: AAAAARdyUy0=\r
+Cc: amdragon@mit.edu\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, 18 Feb 2011 07:59:40 -0000\r
+\r
+Because flag synchronization is stateless, it can be performed at any\r
+time as long as it's guaranteed to be performed after any change to a\r
+message's filename list. Take advantage of this to synchronize tags\r
+immediately after a filename is added or removed while the message is\r
+still frozen. With this change, filename removal is atomic and the\r
+unsynchronized window for filename add is very short.\r
+---\r
+ notmuch-new.c | 57 +++++++++++++++++----------------------------------------\r
+ 1 files changed, 17 insertions(+), 40 deletions(-)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 56fe7b0..767722a 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -50,7 +50,6 @@ typedef struct {\r
+ _filename_list_t *directory_mtimes;\r
+ \r
+ notmuch_bool_t synchronize_flags;\r
+- _filename_list_t *message_ids_to_sync;\r
+ } add_files_state_t;\r
+ \r
+ static volatile sig_atomic_t do_print_progress = 0;\r
+@@ -439,11 +438,8 @@ add_files_recursive (notmuch_database_t *notmuch,\r
+ break;\r
+ /* Non-fatal issues (go on to next file) */\r
+ case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+- /* Defer sync of maildir flags until after old filenames\r
+- * are removed in the case of a rename. */\r
+ if (state->synchronize_flags == TRUE)\r
+- _filename_list_add (state->message_ids_to_sync,\r
+- notmuch_message_get_message_id (message));\r
++ notmuch_message_maildir_flags_to_tags (message);\r
+ break;\r
+ case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
+ fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
+@@ -696,18 +692,23 @@ static notmuch_status_t\r
+ remove_file (notmuch_database_t *notmuch,\r
+ const char *path,\r
+ int *renamed_files,\r
+- int *removed_files)\r
++ int *removed_files,\r
++ add_files_state_t *state)\r
+ {\r
+ notmuch_status_t status;\r
+ notmuch_message_t *message;\r
+ message = notmuch_database_find_message_by_filename (notmuch, path);\r
+ if (!message)\r
+ return NOTMUCH_STATUS_SUCCESS;\r
++ notmuch_message_freeze (message);\r
+ status = notmuch_message_remove_filename (message, path);\r
+- if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)\r
++ if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
+ (*renamed_files)++;\r
+- else\r
++ if (state->synchronize_flags == TRUE)\r
++ notmuch_message_maildir_flags_to_tags (message);\r
++ } else\r
+ (*removed_files)++;\r
++ notmuch_message_thaw (message);\r
+ notmuch_message_destroy (message);\r
+ return status;\r
+ }\r
+@@ -719,7 +720,8 @@ _remove_directory (void *ctx,\r
+ notmuch_database_t *notmuch,\r
+ const char *path,\r
+ int *renamed_files,\r
+- int *removed_files)\r
++ int *removed_files,\r
++ add_files_state_t *state)\r
+ {\r
+ notmuch_directory_t *directory;\r
+ notmuch_filenames_t *files, *subdirs;\r
+@@ -733,7 +735,7 @@ _remove_directory (void *ctx,\r
+ {\r
+ absolute = talloc_asprintf (ctx, "%s/%s", path,\r
+ notmuch_filenames_get (files));\r
+- remove_file (notmuch, absolute, renamed_files, removed_files);\r
++ remove_file (notmuch, absolute, renamed_files, removed_files, state);\r
+ talloc_free (absolute);\r
+ }\r
+ \r
+@@ -743,7 +745,8 @@ _remove_directory (void *ctx,\r
+ {\r
+ absolute = talloc_asprintf (ctx, "%s/%s", path,\r
+ notmuch_filenames_get (subdirs));\r
+- _remove_directory (ctx, notmuch, absolute, renamed_files, removed_files);\r
++ _remove_directory (ctx, notmuch, absolute, renamed_files, removed_files,\r
++ state);\r
+ talloc_free (absolute);\r
+ }\r
+ \r
+@@ -785,7 +788,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+ \r
+ add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);\r
+ add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+- add_files_state.message_ids_to_sync = _filename_list_create (ctx);\r
+ db_path = notmuch_config_get_database_path (config);\r
+ \r
+ dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");\r
+@@ -854,7 +856,8 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+ renamed_files = 0;\r
+ gettimeofday (&tv_start, NULL);\r
+ for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {\r
+- remove_file (notmuch, f->filename, &renamed_files, &removed_files);\r
++ remove_file (notmuch, f->filename, &renamed_files, &removed_files,\r
++ &add_files_state);\r
+ if (do_print_progress) {\r
+ do_print_progress = 0;\r
+ generic_print_progress ("Cleaned up", "messages",\r
+@@ -866,7 +869,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+ gettimeofday (&tv_start, NULL);\r
+ for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {\r
+ _remove_directory (ctx, notmuch, f->filename,\r
+- &renamed_files, &removed_files);\r
++ &renamed_files, &removed_files, &add_files_state);\r
+ if (do_print_progress) {\r
+ do_print_progress = 0;\r
+ generic_print_progress ("Cleaned up", "directories",\r
+@@ -888,32 +891,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
+ talloc_free (add_files_state.removed_directories);\r
+ talloc_free (add_files_state.directory_mtimes);\r
+ \r
+- /* Now that removals are done (hence the database is aware of all\r
+- * renames), we can synchronize maildir_flags to tags for all\r
+- * messages that had new filenames appear on this run. */\r
+- gettimeofday (&tv_start, NULL);\r
+- if (add_files_state.synchronize_flags) {\r
+- _filename_node_t *node;\r
+- notmuch_message_t *message;\r
+- for (node = add_files_state.message_ids_to_sync->head, i = 0;\r
+- node;\r
+- node = node->next, i++)\r
+- {\r
+- message = notmuch_database_find_message (notmuch, node->filename);\r
+- notmuch_message_maildir_flags_to_tags (message);\r
+- notmuch_message_destroy (message);\r
+- if (do_print_progress) {\r
+- do_print_progress = 0;\r
+- generic_print_progress (\r
+- "Synchronized tags for", "messages",\r
+- tv_start, i, add_files_state.message_ids_to_sync->count);\r
+- }\r
+- }\r
+- }\r
+-\r
+- talloc_free (add_files_state.message_ids_to_sync);\r
+- add_files_state.message_ids_to_sync = NULL;\r
+-\r
+ if (timer_is_active)\r
+ stop_progress_printing_timer ();\r
+ \r
+-- \r
+1.7.2.3\r
+\r