--- /dev/null
+Return-Path: <jani@nikula.org>\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 343EC431FAF\r
+ for <notmuch@notmuchmail.org>; Wed, 9 Oct 2013 00:41:37 -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 2v91nAVV-QQ1 for <notmuch@notmuchmail.org>;\r
+ Wed, 9 Oct 2013 00:41:31 -0700 (PDT)\r
+Received: from mail-ea0-f182.google.com (mail-ea0-f182.google.com\r
+ [209.85.215.182]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id D12D0431FAE\r
+ for <notmuch@notmuchmail.org>; Wed, 9 Oct 2013 00:41:30 -0700 (PDT)\r
+Received: by mail-ea0-f182.google.com with SMTP id o10so182340eaj.41\r
+ for <notmuch@notmuchmail.org>; Wed, 09 Oct 2013 00:41:29 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:subject:in-reply-to:references\r
+ :user-agent:date:message-id:mime-version:content-type\r
+ :content-transfer-encoding;\r
+ bh=VryUUpM0hLEW2dkWppzpGgaza/4RZEWLuf3PF7f5Ltg=;\r
+ b=TNWI5zx1JVofAU3UUN+k1Q1N3aX26lsXFjZXSf38qZL6IioPMBEDsaZkoOsBQd56aF\r
+ Z3wbv8BE9uswAWi3fOzKAaxxJpcuAabqOZC+qULkFTRAIDrx+fKGiz6j7tWxpM3jgJvs\r
+ JP/QapekHkr3Mmt/W4pJO1l6wnBe3RQ+Eesp39dTg95JC5zMtgKX4ouIxx2Si7ayH7JZ\r
+ cjiIYuZCpic0DRZnnBpLPV43bZDiAM1kyI1GCleUp14M77B7nbaOQoqI5UaJddQrJy61\r
+ vQ3BkRP3JBiBOf6QxDxRSBXQNQyhH2128WGed25/+upCnpzIDuTjHWYHgOCUrwhuDfBV\r
+ V3wg==\r
+X-Gm-Message-State:\r
+ ALoCoQkylFmUfSS6zL6y7kE4HBK2jpFMPRUNmSyLvVvixKEEvYNDS/q2iy/wAHfAvrcI3gYSvnqS\r
+X-Received: by 10.14.199.200 with SMTP id x48mr1048932een.65.1381304489705;\r
+ Wed, 09 Oct 2013 00:41:29 -0700 (PDT)\r
+Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3])\r
+ by mx.google.com with ESMTPSA id\r
+ x47sm85546562eea.16.1969.12.31.16.00.00\r
+ (version=TLSv1.1 cipher=RC4-SHA bits=128/128);\r
+ Wed, 09 Oct 2013 00:41:28 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 08/11] search: Add stable queries to thread search results\r
+In-Reply-To: <1381185201-25197-9-git-send-email-amdragon@mit.edu>\r
+References: <1381185201-25197-1-git-send-email-amdragon@mit.edu>\r
+ <1381185201-25197-9-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.16+62~g9f2ae2e (http://notmuchmail.org) Emacs/23.2.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Wed, 09 Oct 2013 09:41:17 +0200\r
+Message-ID: <87fvsaao2q.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\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 07:41:37 -0000\r
+\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
+++++++\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=\r
+ 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
+>=20=20\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
+>=20=20\r
+> Common non-terminals\r
+> --------------------\r
+> @@ -145,7 +155,15 @@ thread_summary =3D {\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
+>=20=20\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
+>=20=20\r
+> #include "notmuch-client.h"\r
+> #include "sprinter.h"\r
+> +#include "string-util.h"\r
+>=20=20\r
+> typedef enum {\r
+> OUTPUT_SUMMARY,\r
+> @@ -46,6 +47,46 @@ sanitize_string (const void *ctx, const char *str)\r
+> return out;\r
+> }\r
+>=20=20\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 =3D NULL;\r
+> + size_t escaped_len =3D 0;\r
+> +\r
+> + *matched_out =3D *unmached_out =3D NULL;\r
+> +\r
+> + for (messages =3D notmuch_thread_get_messages (thread);\r
+> + notmuch_messages_valid (messages);\r
+> + notmuch_messages_move_to_next (messages))\r
+> + {\r
+> + notmuch_message_t *message =3D notmuch_messages_get (messages);\r
+> + const char *mid =3D notmuch_message_get_message_id (message);\r
+> + /* Determine which query buffer to extend */\r
+> + char **buf =3D 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 =3D talloc_strdup (thread, "")) =3D=3D 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 =3D talloc_asprintf_append_buffer (\r
+> + *buf, "%s%s", **buf ? " " : "", escaped);\r
+\r
+...and turned this into:\r
+\r
+ if (*buf)\r
+ *buf =3D talloc_asprintf_append_buffer (*buf, " %s", escaped);\r
+ else\r
+ *buf =3D talloc_strdup (thread, escaped);\r
+\r
+Also one talloc less. Which brings me to the main worry:\r
+performance. What's the impact?\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 >=3D 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
+>=20=20\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
+>=20=20\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=C3=BCbj=C3=A9ct\",\r
+> + \"query\": [\"id:$gen_msg_id\", null],\r
+> \"tags\": [\"inbox\",\r
+> \"unread\"]}]"\r
+>=20=20\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-7a6e4eac383ef958fcd3ebf2143db71b8ff01=\r
+161", 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-ca55943aff7a72baf2ab21fa74fab3d632401=\r
+334", null]\r
+> }\r
+> ]'\r
+>=20=20\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}\" =\r
+:match t :excluded nil :f\r
+> test_begin_subtest "Search message: sexp"\r
+> add_message "[subject]=3D\"sexp-search-subject\"" "[date]=3D\"Sat, 01 Ja=\r
+n 2000 12:00:00 -0000\"" "[body]=3D\"sexp-search-message\""\r
+> output=3D$(notmuch search --format=3Dsexp "sexp-search-message" | notmuc=\r
+h_search_sanitize)\r
+> -test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-subject\" :tags (\"inbox\" \"unread\=\r
+")))"\r
+> +test_expect_equal "$output" "((:thread \"0000000000000002\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-subject\" :query (\"id:$gen_msg_id\"=\r
+ nil) :tags (\"inbox\" \"unread\")))"\r
+>=20=20\r
+> test_begin_subtest "Show message: sexp, utf-8"\r
+> add_message "[subject]=3D\"sexp-show-utf8-body-s=C3=BCbj=C3=A9ct\"" "[da=\r
+te]=3D\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=3D\"js=C3=B6n-show-m=C3=\r
+=A9ssage\""\r
+> @@ -44,7 +44,7 @@ test_expect_equal "$output" "((((:id \"$id\" :match t :=\r
+excluded nil :filename \"\r
+> test_begin_subtest "Search message: sexp, utf-8"\r
+> add_message "[subject]=3D\"sexp-search-utf8-body-s=C3=BCbj=C3=A9ct\"" "[=\r
+date]=3D\"Sat, 01 Jan 2000 12:00:00 -0000\"" "[body]=3D\"js=C3=B6n-search-m=\r
+=C3=A9ssage\""\r
+> output=3D$(notmuch search --format=3Dsexp "js=C3=B6n-search-m=C3=A9ssage=\r
+" | notmuch_search_sanitize)\r
+> -test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-utf8-body-s=C3=BCbj=C3=A9ct\" :tags =\r
+(\"inbox\" \"unread\")))"\r
+> +test_expect_equal "$output" "((:thread \"0000000000000005\" :timestamp 9=\r
+46728000 :date_relative \"2000-01-01\" :matched 1 :total 1 :authors \"Notmu=\r
+ch Test Suite\" :subject \"sexp-search-utf8-body-s=C3=BCbj=C3=A9ct\" :query=\r
+ (\"id:$gen_msg_id\" nil) :tags (\"inbox\" \"unread\")))"\r
+>=20=20\r
+>=20=20\r
+> test_done\r
+> --=20\r
+> 1.8.4.rc3\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r