[Patch v2 13/13] lib: add support for named queries
authorDavid Bremner <david@tethera.net>
Sat, 26 Mar 2016 17:57:23 +0000 (14:57 +2100)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Aug 2016 23:21:25 +0000 (16:21 -0700)
3a/eb3f6de7fb98c76fa6e8f262aab9a65e0f2697 [new file with mode: 0644]

diff --git a/3a/eb3f6de7fb98c76fa6e8f262aab9a65e0f2697 b/3a/eb3f6de7fb98c76fa6e8f262aab9a65e0f2697
new file mode 100644 (file)
index 0000000..d6f1115
--- /dev/null
@@ -0,0 +1,266 @@
+Return-Path: <bremner@tesseract.cs.unb.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id B95B66DE0962\r
+ for <notmuch@notmuchmail.org>; Sat, 26 Mar 2016 11:24:44 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.02\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.02 tagged_above=-999 required=5 tests=[AWL=-0.021,\r
+  HEADER_FROM_DIFFERENT_DOMAINS=0.001] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id Q47M4LcfCaXq for <notmuch@notmuchmail.org>;\r
+ Sat, 26 Mar 2016 11:24:36 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 2B28A6DE02DA\r
+ for <notmuch@notmuchmail.org>; Sat, 26 Mar 2016 11:23:26 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1ajsTT-0000us-S4; Sat, 26 Mar 2016 13:58:11 -0400\r
+Received: (nullmailer pid 8830 invoked by uid 1000);\r
+ Sat, 26 Mar 2016 17:57:27 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v2 13/13] lib: add support for named queries\r
+Date: Sat, 26 Mar 2016 14:57:23 -0300\r
+Message-Id: <1459015043-8460-14-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.6.4\r
+In-Reply-To: <1459015043-8460-1-git-send-email-david@tethera.net>\r
+References: <1459015043-8460-1-git-send-email-david@tethera.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 26 Mar 2016 18:24:44 -0000\r
+\r
+This relies on the optional presense of xapian field processors, and the\r
+library config API.\r
+---\r
+ doc/man7/notmuch-search-terms.rst |  8 ++++++++\r
+ lib/Makefile.local                |  1 +\r
+ lib/database-private.h            |  1 +\r
+ lib/database.cc                   |  3 +++\r
+ lib/query-fp.cc                   | 42 +++++++++++++++++++++++++++++++++++++++\r
+ lib/query-fp.h                    | 42 +++++++++++++++++++++++++++++++++++++++\r
+ test/T600-named-queries.sh        | 17 ++++++++++++++++\r
+ 7 files changed, 114 insertions(+)\r
+ create mode 100644 lib/query-fp.cc\r
+ create mode 100644 lib/query-fp.h\r
+\r
+diff --git a/doc/man7/notmuch-search-terms.rst b/doc/man7/notmuch-search-terms.rst\r
+index 29e8b03..7474f53 100644\r
+--- a/doc/man7/notmuch-search-terms.rst\r
++++ b/doc/man7/notmuch-search-terms.rst\r
+@@ -56,6 +56,8 @@ indicate user-supplied values):\r
\r
+ -  lastmod:<initial-revision>..<final-revision>\r
\r
++-  query:<name>\r
++\r
+ The **from:** prefix is used to match the name or address of the sender\r
+ of an email message.\r
\r
+@@ -132,6 +134,11 @@ were added/removed or filenames changed).  This is usually used in\r
+ conjunction with the **--uuid** argument to **notmuch search**\r
+ to find messages that have changed since an earlier query.\r
\r
++The **query:** prefix allows queries to refer to previously saved\r
++queries added with **notmuch-config(1)**. Named queries are only\r
++available if notmuch is built with **Xapian Field Processors** (see\r
++below).\r
++\r
+ Operators\r
+ ---------\r
\r
+@@ -385,6 +392,7 @@ notmuch was built against a sufficiently recent version of Xapian by running\r
+ Currently the following features require field processor support:\r
\r
+ - non-range date queries, e.g. "date:today"\r
++- named queries e.g. "query:my_special_query"\r
\r
+ SEE ALSO\r
+ ========\r
+diff --git a/lib/Makefile.local b/lib/Makefile.local\r
+index 96899bf..fab1242 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -49,6 +49,7 @@ libnotmuch_cxx_srcs =                \\r
+       $(dir)/index.cc         \\r
+       $(dir)/message.cc       \\r
+       $(dir)/query.cc         \\r
++      $(dir)/query-fp.cc \\r
+       $(dir)/config.cc        \\r
+       $(dir)/thread.cc\r
\r
+diff --git a/lib/database-private.h b/lib/database-private.h\r
+index b5c1f90..5ab4001 100644\r
+--- a/lib/database-private.h\r
++++ b/lib/database-private.h\r
+@@ -182,6 +182,7 @@ struct _notmuch_database {\r
+     Xapian::ValueRangeProcessor *date_range_processor;\r
+ #if HAVE_XAPIAN_FIELD_PROCESSOR\r
+     Xapian::FieldProcessor *date_field_processor;\r
++    Xapian::FieldProcessor *query_field_processor;\r
+ #endif\r
+     Xapian::ValueRangeProcessor *last_mod_range_processor;\r
+ };\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 0e3c90c..8df5555 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -20,6 +20,7 @@\r
\r
+ #include "database-private.h"\r
+ #include "parse-time-vrp.h"\r
++#include "query-fp.h"\r
+ #include "string-util.h"\r
\r
+ #include <iostream>\r
+@@ -1005,6 +1006,8 @@ notmuch_database_open_verbose (const char *path,\r
+        * with a .. to the range processor */\r
+       notmuch->date_field_processor = new DateFieldProcessor();\r
+       notmuch->query_parser->add_boolean_prefix("date", notmuch->date_field_processor);\r
++      notmuch->query_field_processor = new QueryFieldProcessor (*notmuch->query_parser, notmuch);\r
++      notmuch->query_parser->add_boolean_prefix("query", notmuch->query_field_processor);\r
+ #endif\r
+       notmuch->last_mod_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_LAST_MOD, "lastmod:");\r
\r
+diff --git a/lib/query-fp.cc b/lib/query-fp.cc\r
+new file mode 100644\r
+index 0000000..ee5a306\r
+--- /dev/null\r
++++ b/lib/query-fp.cc\r
+@@ -0,0 +1,42 @@\r
++/* query-fp.cc -  "query:" field processor glue glue\r
++ *\r
++ * This file is part of notmuch.\r
++ *\r
++ * Copyright © 2016 David Bremner\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: David Bremner <david@tethera.net>\r
++ */\r
++\r
++#include "database-private.h"\r
++#include "query-fp.h"\r
++#include <iostream>\r
++\r
++#if HAVE_XAPIAN_FIELD_PROCESSOR\r
++\r
++Xapian::Query QueryFieldProcessor::operator()(const std::string & name) {\r
++    std::string key = "query." + name;\r
++    char *expansion;\r
++    notmuch_status_t status;\r
++\r
++    status = notmuch_database_get_config (notmuch, key.c_str(), &expansion);\r
++    if (status) {\r
++      throw Xapian::QueryParserError("error looking up key" + name);\r
++\r
++    }\r
++\r
++    return parser.parse_query (expansion, NOTMUCH_QUERY_PARSER_FLAGS);\r
++}\r
++#endif\r
+diff --git a/lib/query-fp.h b/lib/query-fp.h\r
+new file mode 100644\r
+index 0000000..67f8705\r
+--- /dev/null\r
++++ b/lib/query-fp.h\r
+@@ -0,0 +1,42 @@\r
++/* query-fp.h - query field processor glue\r
++ *\r
++ * This file is part of notmuch.\r
++ *\r
++ * Copyright © 2016 David Bremner\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: David Bremner <david@tethera.net>\r
++ */\r
++\r
++#ifndef NOTMUCH_QUERY_FP_H\r
++#define NOTMUCH_QUERY_FP_H\r
++\r
++#include <xapian.h>\r
++#include "notmuch.h"\r
++\r
++#if HAVE_XAPIAN_FIELD_PROCESSOR\r
++class QueryFieldProcessor : public Xapian::FieldProcessor {\r
++ protected:\r
++    Xapian::QueryParser &parser;\r
++    notmuch_database_t *notmuch;\r
++\r
++ public:\r
++    QueryFieldProcessor (Xapian::QueryParser &parser_, notmuch_database_t *notmuch_)\r
++      : parser(parser_), notmuch(notmuch_) { };\r
++\r
++    Xapian::Query operator()(const std::string & str);\r
++};\r
++#endif\r
++#endif /* NOTMUCH_QUERY_FP_H */\r
+diff --git a/test/T600-named-queries.sh b/test/T600-named-queries.sh\r
+index 0922620..f0ae24f 100755\r
+--- a/test/T600-named-queries.sh\r
++++ b/test/T600-named-queries.sh\r
+@@ -50,4 +50,21 @@ notmuch restore < BEFORE\r
+ notmuch dump | grep '^#@' > OUTPUT\r
+ test_expect_equal_file QUERIES.BEFORE OUTPUT\r
\r
++if [ $NOTMUCH_HAVE_XAPIAN_FIELD_PROCESSOR -eq 1 ]; then\r
++    test_begin_subtest "search named query"\r
++    notmuch search query:test > OUTPUT\r
++    notmuch search $QUERYSTR > EXPECTED\r
++    test_expect_equal_file EXPECTED OUTPUT\r
++\r
++    test_begin_subtest "search named query with other terms"\r
++    notmuch search query:test and subject:Maildir > OUTPUT\r
++    notmuch search $QUERYSTR and subject:Maildir > EXPECTED\r
++    test_expect_equal_file EXPECTED OUTPUT\r
++\r
++    test_begin_subtest "search nested named query"\r
++    notmuch search query:test2 > OUTPUT\r
++    notmuch search $QUERYSTR2 > EXPECTED\r
++    test_expect_equal_file EXPECTED OUTPUT\r
++fi\r
++\r
+ test_done\r
+-- \r
+2.6.4\r
+\r