Re: [PATCH] tag: Automatically limit to messages whose tags will actually change.
authorAustin Clements <amdragon@MIT.EDU>
Wed, 9 Nov 2011 13:40:13 +0000 (08:40 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:40:02 +0000 (09:40 -0800)
84/6247a0af7688f26e832a7f89e8fed24cf8bf99 [new file with mode: 0644]

diff --git a/84/6247a0af7688f26e832a7f89e8fed24cf8bf99 b/84/6247a0af7688f26e832a7f89e8fed24cf8bf99
new file mode 100644 (file)
index 0000000..3e23e38
--- /dev/null
@@ -0,0 +1,245 @@
+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 A62E1429E26\r
+       for <notmuch@notmuchmail.org>; Wed,  9 Nov 2011 05:37:48 -0800 (PST)\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 bZcJ0LOkaKsv for <notmuch@notmuchmail.org>;\r
+       Wed,  9 Nov 2011 05:37:44 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU\r
+       [18.9.25.13])\r
+       by olra.theworths.org (Postfix) with ESMTP id B3D94431FD0\r
+       for <notmuch@notmuchmail.org>; Wed,  9 Nov 2011 05:37:44 -0800 (PST)\r
+X-AuditID: 1209190d-b7f726d0000008d1-0c-4eba822714a4\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id FE.05.02257.7228ABE4; Wed,  9 Nov 2011 08:37:43 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id pA9DbgqX024428; \r
+       Wed, 9 Nov 2011 08:37:43 -0500\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.6/8.12.4) with ESMTP id pA9DbfNU018625\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Wed, 9 Nov 2011 08:37:42 -0500 (EST)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1RO8Ny-0007KU-0D; Wed, 09 Nov 2011 08:40:14 -0500\r
+Date: Wed, 9 Nov 2011 08:40:13 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: Jani Nikula <jani@nikula.org>\r
+Subject: Re: [PATCH] tag: Automatically limit to messages whose tags will\r
+       actually change.\r
+Message-ID: <20111109134013.GK2658@mit.edu>\r
+References: <1320724523-23568-1-git-send-email-amdragon@mit.edu>\r
+       <87ty6d1y5x.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <87ty6d1y5x.fsf@nikula.org>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFuplleLIzCtJLcpLzFFi42IRYrdT11Vv2uVnsLZbyqJpurPF9ZszmR2Y\r
+       PG7df83u8WzVLeYApigum5TUnMyy1CJ9uwSujI1z/zMWrDSo+NDexNzA+F+5i5GTQ0LARGLq\r
+       lL2sELaYxIV769m6GLk4hAT2MUqsnHKFCcJZzygxafV5KOcEk8Tp9SvYIZwljBK/Nx1j6WLk\r
+       4GARUJFY08wDMopNQENi2/7ljCC2iICixOaT+8FsZgFpiW+/m5lAbGGBGIm933azgdi8AtoS\r
+       Mw6cZgcZIyQQJ/FpIStEWFDi5MwnLBCtWhI3/r1kAikBGbP8HwdImBNo08elbWBTRIEOmHJy\r
+       G9sERqFZSLpnIemehdC9gJF5FaNsSm6Vbm5iZk5xarJucXJiXl5qka6RXm5miV5qSukmRlBI\r
+       c0ry7mB8d1DpEKMAB6MSD++lop1+QqyJZcWVuYcYJTmYlER59Rp2+QnxJeWnVGYkFmfEF5Xm\r
+       pBYfYpTgYFYS4dWvBsrxpiRWVqUW5cOkpDlYlMR5C3c4+AkJpCeWpGanphakFsFkZTg4lCR4\r
+       ExqBGgWLUtNTK9Iyc0oQ0kwcnCDDeYCGvwdZzFtckJhbnJkOkT/FqMtx+s+lU4xCLHn5ealS\r
+       4ry1IIMEQIoySvPg5sBS0StGcaC3hHmjQap4gGkMbtIroCVMQEtUDcGWlCQipKQaGLn/Xvmh\r
+       LWorlrhV9rnLJIu9lyT0Twao5y08t5J/ZqTCO3ujF4YWLItLEm/fTdv5eunq1Te7+fgc/fx6\r
+       os2jlhqKHlFWTYhafvOp79R+Rw/PNx9n3vYsZb2UfHzxHuWZkzSyOF/biL52VSydtYqZT1sv\r
+       g0ddVclG+LOmtNrks+LXWVu+ORTsUWIpzkg01GIuKk4EAI/lqjAgAwAA\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 Nov 2011 13:37:48 -0000\r
+\r
+Quoth Jani Nikula on Nov 09 at  8:46 am:\r
+> \r
+> FWIW, I reviewed this and didn't find any obvious problems. A few\r
+> nitpicks below, though.\r
+> \r
+> BR,\r
+> Jani.\r
+> \r
+> On Mon,  7 Nov 2011 22:55:23 -0500, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> > This optimizes the user's tagging query to exclude messages that won't\r
+> > be affected by the tagging operation, saving computation and IO for\r
+> > redundant tagging operations.\r
+> > \r
+> > For example,\r
+> >   notmuch tag +notmuch to:notmuch@notmuchmail.org\r
+> > will now use the query\r
+> >   ( to:notmuch@notmuchmail.org ) and (not tag:"notmuch")\r
+> > \r
+> > In the past, we've often suggested that people do this exact\r
+> > transformation by hand for slow tagging operations.  This makes that\r
+> > unnecessary.\r
+> > ---\r
+> > I was about to implement this optimization in my initial tagging\r
+> > script, but then I figured, why not just do it in notmuch so we can\r
+> > stop telling people to do this by hand?\r
+> > \r
+> >  NEWS          |    9 ++++++\r
+> >  notmuch-tag.c |   76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+> >  2 files changed, 85 insertions(+), 0 deletions(-)\r
+> > \r
+> > diff --git a/NEWS b/NEWS\r
+> > index e00452a..9ca5e0c 100644\r
+> > --- a/NEWS\r
+> > +++ b/NEWS\r
+> > @@ -16,6 +16,15 @@ Add search terms to  "notmuch dump"\r
+> >    search/show/tag. The output file argument of dump is deprecated in\r
+> >    favour of using stdout.\r
+> >  \r
+> > +Optimizations\r
+> > +-------------\r
+> > +\r
+> > +Automatic tag query optimization\r
+> > +\r
+> > +  "notmuch tag" now automatically optimizes the user's query to\r
+> > +  exclude messages whose tags won't change.  In the past, we've\r
+> > +  suggested that people do this by hand; this is no longer necessary.\r
+> > +\r
+> >  Notmuch 0.9 (2011-10-01)\r
+> >  ========================\r
+> >  \r
+> > diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+> > index dded39e..62c4bf1 100644\r
+> > --- a/notmuch-tag.c\r
+> > +++ b/notmuch-tag.c\r
+> > @@ -30,6 +30,76 @@ handle_sigint (unused (int sig))\r
+> >      interrupted = 1;\r
+> >  }\r
+> >  \r
+> > +static char *\r
+> > +_escape_tag (char *buf, const char *tag)\r
+> > +{\r
+> > +    const char *in = tag;\r
+> > +    char *out = buf;\r
+> > +    /* Boolean terms surrounded by double quotes can contain any\r
+> > +     * character.  Double quotes are quoted by doubling them. */\r
+> > +    *(out++) = '"';\r
+> > +    while (*in) {\r
+> > +  if (*in == '"')\r
+> > +      *(out++) = '"';\r
+> > +  *(out++) = *(in++);\r
+> > +    }\r
+> > +    *(out++) = '"';\r
+> \r
+> The parenthesis are unnecessary for *p++.\r
+\r
+Removed.  I put these in out of paranoia, but I suppose it wouldn't be\r
+an lvalue if it parsed differently.\r
+\r
+> > +    *out = 0;\r
+> > +    return buf;\r
+> > +}\r
+> > +\r
+> > +static char *\r
+> > +_optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],\r
+> > +               int *add_tags, int add_tags_count,\r
+> > +               int *remove_tags, int remove_tags_count)\r
+> > +{\r
+> > +    /* This is subtler than it looks.  Xapian ignores the '-' operator\r
+> > +     * at the beginning both queries and parenthesized groups and,\r
+> > +     * furthermore, the presence of a '-' operator at the beginning of\r
+> > +     * a group can inhibit parsing of the previous operator.  Hence,\r
+> > +     * the user-provided query MUST appear first, but it is safe to\r
+> > +     * parenthesize and the exclusion part of the query must not use\r
+> > +     * the '-' operator (though the NOT operator is fine). */\r
+> > +\r
+> > +    char *escaped, *query_string;\r
+> > +    const char *join = "";\r
+> > +    int i;\r
+> > +    unsigned int max_tag_len = 0;\r
+> > +\r
+> > +    /* Allocate a buffer for escaping tags. */\r
+> > +    for (i = 0; i < add_tags_count; i++)\r
+> > +  if (strlen (argv[add_tags[i]] + 1) > max_tag_len)\r
+> > +      max_tag_len = strlen (argv[add_tags[i]] + 1);\r
+> > +    for (i = 0; i < remove_tags_count; i++)\r
+> > +  if (strlen (argv[remove_tags[i]] + 1) > max_tag_len)\r
+> > +      max_tag_len = strlen (argv[remove_tags[i]] + 1);\r
+> > +    escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);\r
+> \r
+> Perhaps a comment here or above _escape_tag() explaining the worst case\r
+> memory consumption of strlen(tag) * 2 + 3 for a tag of "s would be in\r
+> order.\r
+\r
+Definitely.  Done.\r
+\r
+> It's unrelated, but looking at the above also made me check something\r
+> I've suspected before: notmuch allows you to have empty or zero length\r
+> tags "", which is probably not intentional.\r
+> \r
+> There's no check for talloc failures here or below. But then there are\r
+> few checks for that in the cli in general. *shrug*.\r
+\r
+It's unfortunate that error handling obscures C code so much.  But\r
+there's no sense in not handling errors, so I fixed this.\r
+\r
+> > +\r
+> > +    /* Build the new query string */\r
+> > +    if (strcmp (orig_query_string, "*") == 0)\r
+> > +  query_string = talloc_strdup (ctx, "(");\r
+> > +    else\r
+> > +  query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
+> > +\r
+> > +    for (i = 0; i < add_tags_count; i++) {\r
+> > +  query_string = talloc_asprintf_append_buffer (\r
+> > +      query_string, "%snot tag:%s", join,\r
+> > +      _escape_tag (escaped, argv[add_tags[i]] + 1));\r
+> > +  join = " or ";\r
+> > +    }\r
+> > +    for (i = 0; i < remove_tags_count; i++) {\r
+> > +  query_string = talloc_asprintf_append_buffer (\r
+> > +      query_string, "%stag:%s", join,\r
+> > +      _escape_tag (escaped, argv[remove_tags[i]] + 1));\r
+> > +  join = " or ";\r
+> > +    }\r
+> > +\r
+> > +    query_string = talloc_strdup_append_buffer (query_string, ")");\r
+> > +\r
+> > +    talloc_free (escaped);\r
+> > +    return query_string;\r
+> > +}\r
+> > +\r
+> >  int\r
+> >  notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+> >  {\r
+> > @@ -93,6 +163,12 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+> >    return 1;\r
+> >      }\r
+> >  \r
+> > +    /* Optimize the query so it excludes messages that already have\r
+> > +     * the specified set of tags. */\r
+> > +    query_string = _optimize_tag_query (ctx, query_string, argv,\r
+> > +                                  add_tags, add_tags_count,\r
+> > +                                  remove_tags, remove_tags_count);\r
+> > +\r
+> >      config = notmuch_config_open (ctx, NULL, NULL);\r
+> >      if (config == NULL)\r
+> >    return 1;\r