diff options
| author | David Bremner <david@tethera.net> | 2016-03-22 07:54:44 -0300 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2016-05-08 08:17:07 -0300 |
| commit | bbf6069252d31e6693ee99cce8bf4f9fab47e360 (patch) | |
| tree | f70aab9b342674e675ac3c5a3794f33b1b05cfae /lib | |
| parent | 1871be319a28d8bba8b368f6b056d2c8448afcf6 (diff) | |
lib: optionally support single argument date: queries
This relies on the FieldProcessor API, which is only present in xapian
>= 1.3.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/database-private.h | 3 | ||||
| -rw-r--r-- | lib/database.cc | 6 | ||||
| -rw-r--r-- | lib/parse-time-vrp.cc | 21 | ||||
| -rw-r--r-- | lib/parse-time-vrp.h | 5 |
4 files changed, 35 insertions, 0 deletions
diff --git a/lib/database-private.h b/lib/database-private.h index 3fb10f7a..e1962f43 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -176,6 +176,9 @@ struct _notmuch_database { Xapian::TermGenerator *term_gen; Xapian::ValueRangeProcessor *value_range_processor; Xapian::ValueRangeProcessor *date_range_processor; +#if HAVE_XAPIAN_FIELD_PROCESSOR + Xapian::FieldProcessor *date_field_processor; +#endif Xapian::ValueRangeProcessor *last_mod_range_processor; }; diff --git a/lib/database.cc b/lib/database.cc index c8c5e261..ebe019fb 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 03804cf5..b15b77c6 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 094c4f87..3bd12bf3 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 */ |
