From a1814ff4143ecb2b7c524b2d65323c26b75a3450 Mon Sep 17 00:00:00 2001 From: David Bremner Date: Sun, 20 Mar 2016 22:38:24 +2100 Subject: [PATCH] [PATCH 3/3] lib: optionally support single argument date: queries --- 9d/d31802b5ab2b3bb410d42533b22529a3511f6e | 161 ++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 9d/d31802b5ab2b3bb410d42533b22529a3511f6e diff --git a/9d/d31802b5ab2b3bb410d42533b22529a3511f6e b/9d/d31802b5ab2b3bb410d42533b22529a3511f6e new file mode 100644 index 000000000..a5d9fc5f4 --- /dev/null +++ b/9d/d31802b5ab2b3bb410d42533b22529a3511f6e @@ -0,0 +1,161 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by arlo.cworth.org (Postfix) with ESMTP id CF1906DE00BD + for ; Sat, 19 Mar 2016 18:38:47 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: -0.03 +X-Spam-Level: +X-Spam-Status: No, score=-0.03 tagged_above=-999 required=5 tests=[AWL=-0.019, + SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01] autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id fg26qZN8khNY for ; + Sat, 19 Mar 2016 18:38:38 -0700 (PDT) +Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) + by arlo.cworth.org (Postfix) with ESMTPS id 0F2996DE0032 + for ; Sat, 19 Mar 2016 18:38:38 -0700 (PDT) +Received: from remotemail by fethera.tethera.net with local (Exim 4.84) + (envelope-from ) + id 1ahSKm-0001W0-3W; Sat, 19 Mar 2016 21:39:12 -0400 +Received: (nullmailer pid 17760 invoked by uid 1000); + Sun, 20 Mar 2016 01:38:32 -0000 +From: David Bremner +To: notmuch@notmuchmail.org +Subject: [PATCH 3/3] lib: optionally support single argument date: queries +Date: Sat, 19 Mar 2016 22:38:24 -0300 +Message-Id: <1458437904-17677-4-git-send-email-david@tethera.net> +X-Mailer: git-send-email 2.7.0 +In-Reply-To: <1458437904-17677-1-git-send-email-david@tethera.net> +References: <1458437904-17677-1-git-send-email-david@tethera.net> +MIME-Version: 1.0 +Content-Type: text/plain; charset=UTF-8 +Content-Transfer-Encoding: 8bit +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.20 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sun, 20 Mar 2016 01:38:47 -0000 + +This relies on the FieldProcessor API, which is only present in xapian +>= 1.3. +--- + lib/database-private.h | 4 ++++ + lib/database.cc | 6 ++++++ + lib/parse-time-vrp.cc | 21 +++++++++++++++++++++ + lib/parse-time-vrp.h | 5 +++++ + test/T500-search-date.sh | 6 ++++++ + 5 files changed, 42 insertions(+) + +diff --git a/lib/database-private.h b/lib/database-private.h +index 3fb10f7..fb2c8ec 100644 +--- a/lib/database-private.h ++++ b/lib/database-private.h +@@ -144,6 +144,7 @@ operator&=(_notmuch_features &a, _notmuch_features b) + return a; + } + ++class DateFieldProcessor; + struct _notmuch_database { + notmuch_bool_t exception_reported; + +@@ -176,6 +177,9 @@ struct _notmuch_database { + Xapian::TermGenerator *term_gen; + Xapian::ValueRangeProcessor *value_range_processor; + Xapian::ValueRangeProcessor *date_range_processor; ++#if HAVE_XAPIAN_FIELD_PROCESSOR ++ DateFieldProcessor *date_field_processor; ++#endif + Xapian::ValueRangeProcessor *last_mod_range_processor; + }; + +diff --git a/lib/database.cc b/lib/database.cc +index 3b342f1..0e3c90c 100644 +--- a/lib/database.cc ++++ b/lib/database.cc +@@ -1000,6 +1000,12 @@ notmuch_database_open_verbose (const char *path, + notmuch->term_gen->set_stemmer (Xapian::Stem ("english")); + notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP); + notmuch->date_range_processor = new ParseTimeValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP); ++#if HAVE_XAPIAN_FIELD_PROCESSOR ++ /* This currently relies on the query parser to pass anything ++ * with a .. to the range processor */ ++ notmuch->date_field_processor = new DateFieldProcessor(); ++ notmuch->query_parser->add_boolean_prefix("date", notmuch->date_field_processor); ++#endif + notmuch->last_mod_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_LAST_MOD, "lastmod:"); + + notmuch->query_parser->set_default_op (Xapian::Query::OP_AND); +diff --git a/lib/parse-time-vrp.cc b/lib/parse-time-vrp.cc +index 03804cf..b15b77c 100644 +--- a/lib/parse-time-vrp.cc ++++ b/lib/parse-time-vrp.cc +@@ -64,3 +64,24 @@ ParseTimeValueRangeProcessor::operator() (std::string &begin, std::string &end) + + return valno; + } ++ ++#if HAVE_XAPIAN_FIELD_PROCESSOR ++/* XXX TODO: is throwing an exception the right thing to do here? */ ++Xapian::Query DateFieldProcessor::operator()(const std::string & str) { ++ time_t from, to, now; ++ ++ /* Use the same 'now' for begin and end. */ ++ if (time (&now) == (time_t) -1) ++ throw Xapian::QueryParserError("Unable to get current time"); ++ ++ if (parse_time_string (str.c_str (), &from, &now, PARSE_TIME_ROUND_DOWN)) ++ throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'"); ++ ++ if (parse_time_string (str.c_str (), &to, &now, PARSE_TIME_ROUND_UP_INCLUSIVE)) ++ throw Xapian::QueryParserError ("Didn't understand date specification '" + str + "'"); ++ ++ return Xapian::Query(Xapian::Query::OP_AND, ++ Xapian::Query(Xapian::Query::OP_VALUE_GE, 0, Xapian::sortable_serialise ((double) from)), ++ Xapian::Query(Xapian::Query::OP_VALUE_LE, 0, Xapian::sortable_serialise ((double) to))); ++} ++#endif +diff --git a/lib/parse-time-vrp.h b/lib/parse-time-vrp.h +index 094c4f8..3bd12bf 100644 +--- a/lib/parse-time-vrp.h ++++ b/lib/parse-time-vrp.h +@@ -37,4 +37,9 @@ public: + Xapian::valueno operator() (std::string &begin, std::string &end); + }; + ++#if HAVE_XAPIAN_FIELD_PROCESSOR ++class DateFieldProcessor : public Xapian::FieldProcessor { ++ Xapian::Query operator()(const std::string & str); ++}; ++#endif + #endif /* NOTMUCH_PARSE_TIME_VRP_H */ +diff --git a/test/T500-search-date.sh b/test/T500-search-date.sh +index f5cea42..198a2e6 100755 +--- a/test/T500-search-date.sh ++++ b/test/T500-search-date.sh +@@ -12,6 +12,12 @@ test_begin_subtest "Absolute date range with 'same' operator" + output=$(notmuch search date:2010-12-16..! | notmuch_search_sanitize) + test_expect_equal "$output" "thread:XXX 2010-12-16 [1/1] Olivier Berger; Essai accentué (inbox unread)" + ++if [ "${NOTMUCH_HAVE_XAPIAN_FIELD_PROCESSOR}" = "1" ]; then ++ test_begin_subtest "Absolute date field" ++ output=$(notmuch search date:2010-12-16 | notmuch_search_sanitize) ++ test_expect_equal "$output" "thread:XXX 2010-12-16 [1/1] Olivier Berger; Essai accentué (inbox unread)" ++fi ++ + test_begin_subtest "Absolute time range with TZ" + notmuch search date:18-Nov-2009_02:19:26-0800..2009-11-18_04:49:52-06:00 | notmuch_search_sanitize > OUTPUT + cat <EXPECTED +-- +2.7.0 + -- 2.26.2