--- /dev/null
+Return-Path: <amdragon@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 6A2BB431FAF\r
+ for <notmuch@notmuchmail.org>; Wed, 9 Oct 2013 07:37:08 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 6DwIkIDjBmao for <notmuch@notmuchmail.org>;\r
+ Wed, 9 Oct 2013 07:37:02 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-8.mit.edu (dmz-mailsec-scanner-8.mit.edu\r
+ [18.7.68.37])\r
+ by olra.theworths.org (Postfix) with ESMTP id 8EDE8431FAE\r
+ for <notmuch@notmuchmail.org>; Wed, 9 Oct 2013 07:37:02 -0700 (PDT)\r
+X-AuditID: 12074425-b7f1c8e0000009c7-f5-52556a0e8760\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id AF.CC.02503.E0A65525; Wed, 9 Oct 2013 10:37:02 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id r99Eb1X6006429; \r
+ Wed, 9 Oct 2013 10:37:01 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+ (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 r99EaxWn031902\r
+ (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
+ Wed, 9 Oct 2013 10:37:00 -0400\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1VTusk-000584-RI; Wed, 09 Oct 2013 10:36:58 -0400\r
+Date: Wed, 9 Oct 2013 10:36:58 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Jani Nikula <jani@nikula.org>\r
+Subject: Re: [PATCH 08/11] search: Add stable queries to thread search results\r
+Message-ID: <20131009143658.GQ21611@mit.edu>\r
+References: <1381185201-25197-1-git-send-email-amdragon@mit.edu>\r
+ <1381185201-25197-9-git-send-email-amdragon@mit.edu>\r
+ <87fvsaao2q.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=iso-8859-1\r
+Content-Disposition: inline\r
+Content-Transfer-Encoding: 8bit\r
+In-Reply-To: <87fvsaao2q.fsf@nikula.org>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFprJKsWRmVeSWpSXmKPExsUixG6nrsuXFRpk8OIJi0XTdGeL6zdnMjsw\r
+ edy6/5rd49mqW8wBTFFcNimpOZllqUX6dglcGSePGRTsiKy4d+QRSwNjg2sXIyeHhICJxNpt\r
+ v5ghbDGJC/fWs4HYQgL7GCUm3czuYuQCsjcwSszedYYRwjnFJPFl9ldmiKoljBIL+wVBbBYB\r
+ FYnN/z+BxdkENCS27V/OCGKLCChKbD65H8xmFpCW+Pa7mQnEFhbwk5i8qIEFxOYV0JH4NWMT\r
+ E8SCqYwSew6uZIRICEqcnPmEBaJZR2Ln1jtA53GADVr+jwMiLC/RvHU22F5OoL3952+DzRcF\r
+ umfKyW1sExiFZyGZNAvJpFkIk2YhmbSAkWUVo2xKbpVubmJmTnFqsm5xcmJeXmqRroVebmaJ\r
+ XmpK6SZGcAy4qO5gnHBI6RCjAAejEg9vB29IkBBrYllxZe4hRkkOJiVR3u0xoUFCfEn5KZUZ\r
+ icUZ8UWlOanFhxglOJiVRHiTUoByvCmJlVWpRfkwKWkOFiVx3lsc9kFCAumJJanZqakFqUUw\r
+ WRkODiUJXpYMoEbBotT01Iq0zJwShDQTByfIcB6g4TGZIMOLCxJzizPTIfKnGBWlxHk/gzQL\r
+ gCQySvPgemEp6hWjONArwhDtPMD0Btf9CmgwE9Dg7d9DQAaXJCKkpBoYZz90c3orc/dRBvdq\r
+ tvUHp5UdyLcpc9YQXWNdtYn//M1dLdd/1nfWzF6Xd0ju5HSWXyvusa0rXMOb8dmj8tOHAKPP\r
+ 8hwfr7PKVhdEBCz6fWmFlvf3sr7Cw0l+zzIuMMzgNTosVGxWENegcsTF4O0sh4DzXaXfHk0r\r
+ KIqqffenYUVPgvadCvNaJZbijERDLeai4kQACtu99iwDAAA=\r
+Cc: notmuch@notmuchmail.org\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: Wed, 09 Oct 2013 14:37:08 -0000\r
+\r
+Quoth Jani Nikula on Oct 09 at 9:41 am:\r
+> On Tue, 08 Oct 2013, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> > These queries will match exactly the set of messages currently in the\r
+> > thread, even if more messages later arrive. Two queries are provided:\r
+> > one for matched messages and one for unmatched messages.\r
+> >\r
+> > This can be used to fix race conditions with tagging threads from\r
+> > search results. While tagging based on a thread: query can affect\r
+> > messages that arrived after the search, tagging based on stable\r
+> > queries affects only the messages the user was shown in the search UI.\r
+> >\r
+> > Since we want clients to be able to depend on the presence of these\r
+> > queries, this ushers in schema version 2.\r
+> > ---\r
+> > devel/schemata | 22 +++++++++++++++++--\r
+> > notmuch-client.h | 2 +-\r
+> > notmuch-search.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+> > test/json | 2 ++\r
+> > test/missing-headers | 6 ++++--\r
+> > test/sexp | 4 ++--\r
+> > 6 files changed, 89 insertions(+), 7 deletions(-)\r
+> >\r
+> > diff --git a/devel/schemata b/devel/schemata\r
+> > index cdd0e43..41dc4a6 100644\r
+> > --- a/devel/schemata\r
+> > +++ b/devel/schemata\r
+> > @@ -14,7 +14,17 @@ are interleaved. Keys are printed as keywords (symbols preceded by a\r
+> > colon), e.g. (:id "123" :time 54321 :from "foobar"). Null is printed as\r
+> > nil, true as t and false as nil.\r
+> > \r
+> > -This is version 1 of the structured output format.\r
+> > +This is version 2 of the structured output format.\r
+> > +\r
+> > +Version history\r
+> > +---------------\r
+> > +\r
+> > +v1\r
+> > +- First versioned schema release.\r
+> > +- Added part.content-length and part.content-transfer-encoding fields.\r
+> > +\r
+> > +v2\r
+> > +- Added the thread_summary.query field.\r
+> > \r
+> > Common non-terminals\r
+> > --------------------\r
+> > @@ -145,7 +155,15 @@ thread_summary = {\r
+> > authors: string, # comma-separated names with | between\r
+> > # matched and unmatched\r
+> > subject: string,\r
+> > - tags: [string*]\r
+> > + tags: [string*],\r
+> > +\r
+> > + # Two stable query strings identifying exactly the matched and\r
+> > + # unmatched messages currently in this thread. The messages\r
+> > + # matched by these queries will not change even if more messages\r
+> > + # arrive in the thread. If there are no matched or unmatched\r
+> > + # messages, the corresponding query will be null (there is no\r
+> > + # query that matches nothing). (Added in schema version 2.)\r
+> > + query: [string|null, string|null],\r
+> > }\r
+> > \r
+> > notmuch reply schema\r
+> > diff --git a/notmuch-client.h b/notmuch-client.h\r
+> > index 8d986f4..1b14910 100644\r
+> > --- a/notmuch-client.h\r
+> > +++ b/notmuch-client.h\r
+> > @@ -138,7 +138,7 @@ chomp_newline (char *str)\r
+> > * this. New (required) map fields can be added without increasing\r
+> > * this.\r
+> > */\r
+> > -#define NOTMUCH_FORMAT_CUR 1\r
+> > +#define NOTMUCH_FORMAT_CUR 2\r
+> > /* The minimum supported structured output format version. Requests\r
+> > * for format versions below this will return an error. */\r
+> > #define NOTMUCH_FORMAT_MIN 1\r
+> > diff --git a/notmuch-search.c b/notmuch-search.c\r
+> > index d9d39ec..1d14651 100644\r
+> > --- a/notmuch-search.c\r
+> > +++ b/notmuch-search.c\r
+> > @@ -20,6 +20,7 @@\r
+> > \r
+> > #include "notmuch-client.h"\r
+> > #include "sprinter.h"\r
+> > +#include "string-util.h"\r
+> > \r
+> > typedef enum {\r
+> > OUTPUT_SUMMARY,\r
+> > @@ -46,6 +47,46 @@ sanitize_string (const void *ctx, const char *str)\r
+> > return out;\r
+> > }\r
+> > \r
+> > +/* Return two stable query strings that identify exactly the matched\r
+> > + * and unmatched messages currently in thread. If there are no\r
+> > + * matched or unmatched messages, the returned buffers will be\r
+> > + * NULL. */\r
+> > +static int\r
+> > +get_thread_query (notmuch_thread_t *thread,\r
+> > + char **matched_out, char **unmached_out)\r
+> > +{\r
+> > + notmuch_messages_t *messages;\r
+> > + char *escaped = NULL;\r
+> > + size_t escaped_len = 0;\r
+> > +\r
+> > + *matched_out = *unmached_out = NULL;\r
+> > +\r
+> > + for (messages = notmuch_thread_get_messages (thread);\r
+> > + notmuch_messages_valid (messages);\r
+> > + notmuch_messages_move_to_next (messages))\r
+> > + {\r
+> > + notmuch_message_t *message = notmuch_messages_get (messages);\r
+> > + const char *mid = notmuch_message_get_message_id (message);\r
+> > + /* Determine which query buffer to extend */\r
+> > + char **buf = notmuch_message_get_flag (\r
+> > + message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmached_out;\r
+> > + /* Allocate the query buffer is this is the first message */\r
+> > + if (!*buf && (*buf = talloc_strdup (thread, "")) == NULL)\r
+> > + return -1;\r
+> \r
+> I think it would improve clarity if you dropped the above...\r
+> \r
+> > + /* Add this message's id: query. Since "id" is an exclusive\r
+> > + * prefix, it is implicitly 'or'd together, so we only need to\r
+> > + * join queries with a space. */\r
+> > + if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)\r
+> > + return -1;\r
+> > + *buf = talloc_asprintf_append_buffer (\r
+> > + *buf, "%s%s", **buf ? " " : "", escaped);\r
+> \r
+> ...and turned this into:\r
+> \r
+> if (*buf)\r
+> *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);\r
+> else\r
+> *buf = talloc_strdup (thread, escaped);\r
+\r
+Much nicer!\r
+\r
+> Also one talloc less. Which brings me to the main worry:\r
+> performance. What's the impact?\r
+\r
+Seems to be about 1%-3% for CLI search (tested on the medium corpus).\r
+It's hard to measure what the effect on Emacs search is, though I\r
+would expect it to be similarly negligible. Some work I did several\r
+attempts at this ago suggests that this slows down tagging (though I\r
+doubt it would be noticeable for single threads), but I also found\r
+that switching to docid-based queries significantly sped things up:\r
+id:CAH-f9WsPj=1Eu=g3sOePJgCTBFs6HrLdLq18xMEnJ8aZ00yCEg@mail.gmail.com\r
+Actually, docid queries probably make tagging faster than it is *now*,\r
+but I didn't measure that when I did the experiments.\r
+\r
+> BR,\r
+> Jani.\r
+> \r
+> \r
+> > + if (!*buf)\r
+> > + return -1;\r
+> > + }\r
+> > + talloc_free (escaped);\r
+> > + return 0;\r
+> > +}\r
+> > +\r
+> > static int\r
+> > do_search_threads (sprinter_t *format,\r
+> > notmuch_query_t *query,\r
+> > @@ -131,6 +172,25 @@ do_search_threads (sprinter_t *format,\r
+> > format->string (format, authors);\r
+> > format->map_key (format, "subject");\r
+> > format->string (format, subject);\r
+> > + if (notmuch_format_version >= 2) {\r
+> > + char *matched_query, *unmatched_query;\r
+> > + if (get_thread_query (thread, &matched_query,\r
+> > + &unmatched_query) < 0) {\r
+> > + fprintf (stderr, "Out of memory\n");\r
+> > + return 1;\r
+> > + }\r
+> > + format->map_key (format, "query");\r
+> > + format->begin_list (format);\r
+> > + if (matched_query)\r
+> > + format->string (format, matched_query);\r
+> > + else\r
+> > + format->null (format);\r
+> > + if (unmatched_query)\r
+> > + format->string (format, unmatched_query);\r
+> > + else\r
+> > + format->null (format);\r
+> > + format->end (format);\r
+> > + }\r
+> > }\r
+> > \r
+> > talloc_free (ctx_quote);\r
+> > diff --git a/test/json b/test/json\r
+> > index b87b7f6..e07a290 100755\r
+> > --- a/test/json\r
+> > +++ b/test/json\r
+> > @@ -26,6 +26,7 @@ test_expect_equal_json "$output" "[{\"thread\": \"XXX\",\r
+> > \"total\": 1,\r
+> > \"authors\": \"Notmuch Test Suite\",\r
+> > \"subject\": \"json-search-subject\",\r
+> > + \"query\": [\"id:$gen_msg_id\", null],\r
+> > \"tags\": [\"inbox\",\r
+> > \"unread\"]}]"\r
+> > \r
+> > @@ -59,6 +60,7 @@ test_expect_equal_json "$output" "[{\"thread\": \"XXX\",\r
+> > \"total\": 1,\r
+> > \"authors\": \"Notmuch Test Suite\",\r
+> > \"subject\": \"json-search-utf8-body-sübjéct\",\r
+> > + \"query\": [\"id:$gen_msg_id\", null],\r
+> > \"tags\": [\"inbox\",\r
+> > \"unread\"]}]"\r
+> > \r
+> > diff --git a/test/missing-headers b/test/missing-headers\r
+> > index f14b878..43e861b 100755\r
+> > --- a/test/missing-headers\r
+> > +++ b/test/missing-headers\r
+> > @@ -43,7 +43,8 @@ test_expect_equal_json "$output" '\r
+> > ],\r
+> > "thread": "XXX",\r
+> > "timestamp": 978709437,\r
+> > - "total": 1\r
+> > + "total": 1,\r
+> > + "query": ["id:notmuch-sha1-7a6e4eac383ef958fcd3ebf2143db71b8ff01161", null]\r
+> > },\r
+> > {\r
+> > "authors": "Notmuch Test Suite",\r
+> > @@ -56,7 +57,8 @@ test_expect_equal_json "$output" '\r
+> > ],\r
+> > "thread": "XXX",\r
+> > "timestamp": 0,\r
+> > - "total": 1\r
+> > + "total": 1,\r
+> > + "query": ["id:notmuch-sha1-ca55943aff7a72baf2ab21fa74fab3d632401334", null]\r
+> > }\r
+> > ]'\r
+> > \r
+> > diff --git a/test/sexp b/test/sexp\r
+> > index 492a82f..be815e1 100755\r
+> > --- a/test/sexp\r
+> > +++ b/test/sexp\r
+> > @@ -19,7 +19,7 @@ test_expect_equal "$output" "((((:id \"${gen_msg_id}\" :match t :excluded nil :f\r
+> > test_begin_subtest "Search message: sexp"\r
+> > add_message "[subject]=\"sexp-search-subject\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"sexp-search-message\""\r
+> > output=$(notmuch search --format=sexp "sexp-search-message" | notmuch_search_sanitize)\r
+> > -test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-subject\" :tags (\"inbox\" \"unread\")))"\r
+> > +test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-subject\" :query (\"id:$gen_msg_id\" nil) :tags (\"inbox\" \"unread\")))"\r
+> > \r
+> > test_begin_subtest "Show message: sexp, utf-8"\r
+> > add_message "[subject]=\"sexp-show-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-show-méssage\""\r
+> > @@ -44,7 +44,7 @@ test_expect_equal "$output" "((((:id \"$id\" :match t :excluded nil :filename \"\r
+> > test_begin_subtest "Search message: sexp, utf-8"\r
+> > add_message "[subject]=\"sexp-search-utf8-body-sübjéct\"" "[date]=\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=\"jsön-search-méssage\""\r
+> > output=$(notmuch search --format=sexp "jsön-search-méssage" | notmuch_search_sanitize)\r
+> > -test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-utf8-body-sübjéct\" :tags (\"inbox\" \"unread\")))"\r
+> > +test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 946728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmuch Test Suite\" :subject \"sexp-search-utf8-body-sübjéct\" :query (\"id:$gen_msg_id\" nil) :tags (\"inbox\" \"unread\")))"\r
+> > \r
+> > \r
+> > test_done\r
+> >\r
+> > _______________________________________________\r
+> > notmuch mailing list\r
+> > notmuch@notmuchmail.org\r
+> > http://notmuchmail.org/mailman/listinfo/notmuch\r
+\r
+-- \r
+Austin Clements MIT/'06/PhD/CSAIL\r
+amdragon@mit.edu http://web.mit.edu/amdragon\r
+ Somewhere in the dream we call reality you will find me,\r
+ searching for the reality we call dreams.\r