Re: [PATCH v2 2/3] cli: refactor "notmuch tag" query tagging into a separate function
authorJani Nikula <jani@nikula.org>
Mon, 26 Mar 2012 07:38:47 +0000 (07:38 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:44 +0000 (09:45 -0800)
54/bd89fd3aff18403a8df50fce7db300611bd16b [new file with mode: 0644]

diff --git a/54/bd89fd3aff18403a8df50fce7db300611bd16b b/54/bd89fd3aff18403a8df50fce7db300611bd16b
new file mode 100644 (file)
index 0000000..9a6486b
--- /dev/null
@@ -0,0 +1,307 @@
+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 199E4431FAF\r
+       for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 00:38:55 -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 lZu4Blej6gwZ for <notmuch@notmuchmail.org>;\r
+       Mon, 26 Mar 2012 00:38:53 -0700 (PDT)\r
+Received: from mail-qc0-f181.google.com (mail-qc0-f181.google.com\r
+       [209.85.216.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id BED14431FAE\r
+       for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 00:38:53 -0700 (PDT)\r
+Received: by qcsk26 with SMTP id k26so3922863qcs.26\r
+       for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 00:38:52 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=from:to:subject:in-reply-to:references:user-agent:date:message-id\r
+       :mime-version:content-type:x-gm-message-state;\r
+       bh=V7j4+ucUhifx3J1KTLdK4+EMoK6sy/GsEoEyPscaQLE=;\r
+       b=gUESWePjwYXxff94eBnJDvWZrKFDdZwMPJ6qLH4XP94i608mOMt133KC1Aqb71VTfu\r
+       MbVXRrTzKVwshxFERlZR+em4r4JluWMQWKeTSjPeaVo8choQGJLkZy56o17/ov/pG5/8\r
+       +m31WbLJ8nUuYqMV8ox8KbfjVZr7wtxf7vM70BdV9HxzMzF2IZy3922xlW+mJUdxcP9c\r
+       s+r4AIZM6Wt08iMjPc708OrkiSqPLfzG9oOMU756ZMpmoi6MGCspVDv6+Ck1F7RQQnFP\r
+       t/SM5AItaoqlxngyKy+X6xGThkYCHuO8rIZN0c0f7y+erHLIdTrXLIeN+488Q9GU8krS\r
+       Sk1A==\r
+Received: by 10.224.187.2 with SMTP id cu2mr26045500qab.88.1332747531994;\r
+       Mon, 26 Mar 2012 00:38:51 -0700 (PDT)\r
+Received: from localhost (nikula.org. [92.243.24.172])\r
+       by mx.google.com with ESMTPS id cv8sm358404qab.12.2012.03.26.00.38.49\r
+       (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 00:38:51 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Tomi Ollila <tomi.ollila@iki.fi>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v2 2/3] cli: refactor "notmuch tag" query tagging into a\r
+       separate function\r
+In-Reply-To: <m2398we5id.fsf@guru.guru-group.fi>\r
+References: <cover.1332702915.git.jani@nikula.org>\r
+       <584067da00dd9f04a7f3982b76fd4b4918e4fe61.1332702915.git.jani@nikula.org>\r
+       <m262dse7f0.fsf@guru.guru-group.fi>\r
+       <87limo2xpk.fsf@nikula.org>User-Agent: Notmuch/0.12+71~gdacf76b\r
+       (http://notmuchmail.org) Emacs/23.3.1\r
+       (x86_64-unknown-linux-gnu) <m2398we5id.fsf@guru.guru-group.fi>\r
+User-Agent: Notmuch/0.11.1+222~ga47a98c (http://notmuchmail.org) Emacs/23.1.1\r
+       (i686-pc-linux-gnu)\r
+Date: Mon, 26 Mar 2012 07:38:47 +0000\r
+Message-ID: <87ty1b4xrs.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQkemWZT0HI2+q2nD9ShizwmMfsODTc4VDhzWfcKaU6t+6L12Vf50nS3WmAXhXHd0PHJBjOd\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: Mon, 26 Mar 2012 07:38:55 -0000\r
+\r
+On Mon, 26 Mar 2012 00:26:50 +0300, Tomi Ollila <tomi.ollila@iki.fi> wrote:\r
+> Jani Nikula <jani@nikula.org> writes:\r
+> \r
+> > On Sun, 25 Mar 2012 23:45:39 +0300, Tomi Ollila <tomi.ollila@iki.fi> wrote:\r
+> >> Jani Nikula <jani@nikula.org> writes:\r
+> >> \r
+> >> > Refactor to make tagging code easier to reuse in the future. No\r
+> >> > functional changes.\r
+> >> >\r
+> >> > Signed-off-by: Jani Nikula <jani@nikula.org>\r
+> >> > ---\r
+> >> \r
+> >> tag_query() is pretty generic name for a function which (also) does\r
+> >> tagging operations (adds and removes tags based on tag_ops).\r
+> >\r
+> > Mmmh, if you think of "tag" as a verb, it fairly accurately describes\r
+> > what the function does: tag (messages matching this) query (according\r
+> > given arguments). I don't mind changing, but can't come up with anything\r
+> > better right now either. notmuch_{search,query}_change_tags()? Meh?\r
+> \r
+> Hmmm, yes... -- tag...query... ok, I can live with that if no-one\r
+> else get same impression I got first...\r
+> \r
+> >\r
+> >> If, however, tag_opts != NULL (as is needs to be) but tag_opts[0] == NULL\r
+> >> (now tag_query() would not do any tagging operations, but\r
+> >> optimize_tag_query would mangle query string ( '*' to '()' and \r
+> >> 'any_other' to '( any_other ) and ()' )\r
+> >\r
+> > I'm not sure I follow you. AFAICT the behaviour does not change from\r
+> > what it was before, apart from the tag addition and removal being mixed\r
+> > together.\r
+> \r
+> The thing is that notmuch_tag_command () check that there is at least\r
+> one tagging operation to be done, but tag_query () doesn't do such\r
+> checking...\r
+> \r
+> >> I.e. IMO the function name should be more spesific & the fact that this\r
+> >> function needs tag changing data in tag_ops array should be documented.\r
+> >\r
+> > Documentation good. :)\r
+> \r
+> ... therefore the requirement that there needs to tagging information\r
+> in tag_ops is in place.\r
+\r
+Actually, no. In the future (the follow-up patches are in my local\r
+tree...) tag_query() should work also when there are no tagging\r
+operations. I'll either need to fix tag_query() or _optimize_tag_query()\r
+to handle this gracefully. Thanks for spelling this out for me! :)\r
+\r
+> >> Minor thing in patch #1:\r
+> >> \r
+> >>  +            tag_ops[tag_ops_count].remove = argv[i][0] == '-';\r
+> >> \r
+> >> would be more clearer as:\r
+> >> \r
+> >>  +            tag_ops[tag_ops_count].remove = (argv[i][0] == '-');\r
+> >\r
+> > I usually don't add braces where they aren't needed, but can do here.\r
+> \r
+> Assigment is much lower in precedence than equality comparison -- but\r
+> as this is so seldom-used construct, humans read (with understanding)\r
+> the code faster with this added clarity.\r
+\r
+Sounds reasonable.\r
+\r
+BR,\r
+Jani.\r
+\r
+\r
+\r
+> \r
+> >> Everything else LGTM.\r
+> >\r
+> > Thanks for the review,\r
+> > Jani.\r
+> >\r
+> >> \r
+> >> Tomi\r
+> \r
+> Tomi\r
+> \r
+> >> \r
+> >> \r
+> >> >  notmuch-tag.c |  101 ++++++++++++++++++++++++++++++++-------------------------\r
+> >> >  1 files changed, 57 insertions(+), 44 deletions(-)\r
+> >> >\r
+> >> > diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+> >> > index c39b235..edbfdec 100644\r
+> >> > --- a/notmuch-tag.c\r
+> >> > +++ b/notmuch-tag.c\r
+> >> > @@ -106,6 +106,60 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+> >> >      return query_string;\r
+> >> >  }\r
+> >> >  \r
+> >> > +static int\r
+> >> > +tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
+> >> > +          tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags)\r
+> >> > +{\r
+> >> > +    notmuch_query_t *query;\r
+> >> > +    notmuch_messages_t *messages;\r
+> >> > +    notmuch_message_t *message;\r
+> >> > +    int i;\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, tag_ops);\r
+> >> > +    if (query_string == NULL) {\r
+> >> > +       fprintf (stderr, "Out of memory.\n");\r
+> >> > +       return 1;\r
+> >> > +    }\r
+> >> > +\r
+> >> > +    query = notmuch_query_create (notmuch, query_string);\r
+> >> > +    if (query == NULL) {\r
+> >> > +       fprintf (stderr, "Out of memory.\n");\r
+> >> > +       return 1;\r
+> >> > +    }\r
+> >> > +\r
+> >> > +    /* tagging is not interested in any special sort order */\r
+> >> > +    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
+> >> > +\r
+> >> > +    for (messages = notmuch_query_search_messages (query);\r
+> >> > +        notmuch_messages_valid (messages) && !interrupted;\r
+> >> > +        notmuch_messages_move_to_next (messages))\r
+> >> > +    {\r
+> >> > +       message = notmuch_messages_get (messages);\r
+> >> > +\r
+> >> > +       notmuch_message_freeze (message);\r
+> >> > +\r
+> >> > +       for (i = 0; tag_ops[i].tag; i++) {\r
+> >> > +           if (tag_ops[i].remove)\r
+> >> > +               notmuch_message_remove_tag (message, tag_ops[i].tag);\r
+> >> > +           else\r
+> >> > +               notmuch_message_add_tag (message, tag_ops[i].tag);\r
+> >> > +       }\r
+> >> > +\r
+> >> > +       notmuch_message_thaw (message);\r
+> >> > +\r
+> >> > +       if (synchronize_flags)\r
+> >> > +           notmuch_message_tags_to_maildir_flags (message);\r
+> >> > +\r
+> >> > +       notmuch_message_destroy (message);\r
+> >> > +    }\r
+> >> > +\r
+> >> > +    notmuch_query_destroy (query);\r
+> >> > +\r
+> >> > +    return interrupted;\r
+> >> > +}\r
+> >> > +\r
+> >> >  int\r
+> >> >  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> >> >  {\r
+> >> > @@ -114,12 +168,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> >> >      char *query_string;\r
+> >> >      notmuch_config_t *config;\r
+> >> >      notmuch_database_t *notmuch;\r
+> >> > -    notmuch_query_t *query;\r
+> >> > -    notmuch_messages_t *messages;\r
+> >> > -    notmuch_message_t *message;\r
+> >> >      struct sigaction action;\r
+> >> >      notmuch_bool_t synchronize_flags;\r
+> >> >      int i;\r
+> >> > +    int ret;\r
+> >> >  \r
+> >> >      /* Setup our handler for SIGINT */\r
+> >> >      memset (&action, 0, sizeof (struct sigaction));\r
+> >> > @@ -166,14 +218,6 @@ notmuch_tag_command (void *ctx, int argc, 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, tag_ops);\r
+> >> > -    if (query_string == NULL) {\r
+> >> > -       fprintf (stderr, "Out of memory.\n");\r
+> >> > -       return 1;\r
+> >> > -    }\r
+> >> > -\r
+> >> >      config = notmuch_config_open (ctx, NULL, NULL);\r
+> >> >      if (config == NULL)\r
+> >> >         return 1;\r
+> >> > @@ -185,40 +229,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
+> >> >  \r
+> >> >      synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
+> >> >  \r
+> >> > -    query = notmuch_query_create (notmuch, query_string);\r
+> >> > -    if (query == NULL) {\r
+> >> > -       fprintf (stderr, "Out of memory.\n");\r
+> >> > -       return 1;\r
+> >> > -    }\r
+> >> > -\r
+> >> > -    /* tagging is not interested in any special sort order */\r
+> >> > -    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
+> >> > +    ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
+> >> >  \r
+> >> > -    for (messages = notmuch_query_search_messages (query);\r
+> >> > -        notmuch_messages_valid (messages) && !interrupted;\r
+> >> > -        notmuch_messages_move_to_next (messages))\r
+> >> > -    {\r
+> >> > -       message = notmuch_messages_get (messages);\r
+> >> > -\r
+> >> > -       notmuch_message_freeze (message);\r
+> >> > -\r
+> >> > -       for (i = 0; tag_ops[i].tag; i++) {\r
+> >> > -           if (tag_ops[i].remove)\r
+> >> > -               notmuch_message_remove_tag (message, tag_ops[i].tag);\r
+> >> > -           else\r
+> >> > -               notmuch_message_add_tag (message, tag_ops[i].tag);\r
+> >> > -       }\r
+> >> > -\r
+> >> > -       notmuch_message_thaw (message);\r
+> >> > -\r
+> >> > -       if (synchronize_flags)\r
+> >> > -           notmuch_message_tags_to_maildir_flags (message);\r
+> >> > -\r
+> >> > -       notmuch_message_destroy (message);\r
+> >> > -    }\r
+> >> > -\r
+> >> > -    notmuch_query_destroy (query);\r
+> >> >      notmuch_database_close (notmuch);\r
+> >> >  \r
+> >> > -    return interrupted;\r
+> >> > +    return ret;\r
+> >> >  }\r
+> >> > -- \r
+> >> > 1.7.5.4\r
+> >> >\r
+> >> > _______________________________________________\r
+> >> > notmuch mailing list\r
+> >> > notmuch@notmuchmail.org\r
+> >> > http://notmuchmail.org/mailman/listinfo/notmuch\r
+> > _______________________________________________\r
+> > notmuch mailing list\r
+> > notmuch@notmuchmail.org\r
+> > http://notmuchmail.org/mailman/listinfo/notmuch\r