Add a custom value range processor to enable date and time searches of the form date:since..until, where "since" and "until" are expressions understood by parse_time_string(). If "since" or "until" describes date/time at an accuracy of days or less, the values are rounded according to the accuracy, towards past for "since" and towards future for "until". For example, date:november..yesterday would match from the beginning of November until the end of yesterday. Expressions such as date:today..today means since the beginning of today until the end of today. Open-ended ranges are supported (since Xapian 1.2.1), i.e. you can specify date:..until or date:since.. to not limit the start or end date, respectively. CAVEATS: Xapian does not support spaces in range expressions. You can replace the spaces with '_' or (in most cases) '-' or (in some cases) leave the spaces out altogether. Entering date:expr without ".." (for example date:yesterday) won't work. You can achieve the expected result by duplicating the expr both sides of ".." (for example date:yesterday..yesterday). Signed-off-by: Jani Nikula <jani@nikula.org> --- lib/Makefile.local | 1 + lib/database-private.h | 1 + lib/database.cc | 4 ++++ lib/getdate-proc.cc | 34 ++++++++++++++++++++++++++++++++++ lib/getdate-proc.h | 21 +++++++++++++++++++++ 5 files changed, 61 insertions(+), 0 deletions(-) create mode 100644 lib/getdate-proc.cc create mode 100644 lib/getdate-proc.h diff --git a/lib/Makefile.local b/lib/Makefile.local index 803a284..7dd1f7d 100644 --- a/lib/Makefile.local +++ b/lib/Makefile.local @@ -59,6 +59,7 @@ libnotmuch_c_srcs = \ libnotmuch_cxx_srcs = \ $(dir)/database.cc \ + $(dir)/getdate-proc.cc \ $(dir)/directory.cc \ $(dir)/index.cc \ $(dir)/message.cc \ diff --git a/lib/database-private.h b/lib/database-private.h index 88532d5..ba13dc7 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -52,6 +52,7 @@ struct _notmuch_database { Xapian::QueryParser *query_parser; Xapian::TermGenerator *term_gen; Xapian::ValueRangeProcessor *value_range_processor; + Xapian::ValueRangeProcessor *getdate_proc; }; /* Return the list of terms from the given iterator matching a prefix. diff --git a/lib/database.cc b/lib/database.cc index c928d02..a3f8adb 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -19,6 +19,7 @@ */ #include "database-private.h" +#include "getdate-proc.h" #include <iostream> @@ -682,12 +683,14 @@ notmuch_database_open (const char *path, notmuch->term_gen = new Xapian::TermGenerator; notmuch->term_gen->set_stemmer (Xapian::Stem ("english")); notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP); + notmuch->getdate_proc = new GetDateValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP, "date:", true); notmuch->query_parser->set_default_op (Xapian::Query::OP_AND); notmuch->query_parser->set_database (*notmuch->xapian_db); notmuch->query_parser->set_stemmer (Xapian::Stem ("english")); notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME); notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor); + notmuch->query_parser->add_valuerangeprocessor (notmuch->getdate_proc); for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) { prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i]; @@ -729,6 +732,7 @@ notmuch_database_close (notmuch_database_t *notmuch) delete notmuch->query_parser; delete notmuch->xapian_db; delete notmuch->value_range_processor; + delete notmuch->getdate_proc; talloc_free (notmuch); } diff --git a/lib/getdate-proc.cc b/lib/getdate-proc.cc new file mode 100644 index 0000000..31f8f03 --- /dev/null +++ b/lib/getdate-proc.cc @@ -0,0 +1,34 @@ + +#include "database-private.h" +#include "getdate-proc.h" +#include "parse-time-string.h" + +/* see *ValueRangeProcessor in xapian-core/api/valuerangeproc.cc */ +Xapian::valueno +GetDateValueRangeProcessor::operator() (std::string &begin, std::string &end) +{ + time_t t, now; + + if (Xapian::StringValueRangeProcessor::operator() (begin, end) == Xapian::BAD_VALUENO) + return Xapian::BAD_VALUENO; + + /* use the same 'now' for begin and end */ + if (time (&now) == (time_t) -1) + return Xapian::BAD_VALUENO; + + if (!begin.empty ()) { + if (parse_time_string (begin.c_str (), &t, &now, PARSE_TIME_ROUND_DOWN)) + return Xapian::BAD_VALUENO; + + begin.assign (Xapian::sortable_serialise ((double) t)); + } + + if (!end.empty ()) { + if (parse_time_string (end.c_str (), &t, &now, PARSE_TIME_ROUND_UP)) + return Xapian::BAD_VALUENO; + + end.assign (Xapian::sortable_serialise ((double) t)); + } + + return valno; +} diff --git a/lib/getdate-proc.h b/lib/getdate-proc.h new file mode 100644 index 0000000..351d06e --- /dev/null +++ b/lib/getdate-proc.h @@ -0,0 +1,21 @@ + +#ifndef NOTMUCH_GETDATE_PROC_H +#define NOTMUCH_GETDATE_PROC_H + +#include <xapian.h> + +/* see *ValueRangeProcessor in xapian-core/include/xapian/queryparser.h */ +class GetDateValueRangeProcessor : public Xapian::StringValueRangeProcessor { +public: + GetDateValueRangeProcessor (Xapian::valueno slot_) + : StringValueRangeProcessor (slot_) { } + + GetDateValueRangeProcessor (Xapian::valueno slot_, + const std::string &str_, + bool prefix_ = true) + : StringValueRangeProcessor (slot_, str_, prefix_) { } + + Xapian::valueno operator() (std::string &begin, std::string &end); +}; + +#endif /* NOTMUCH_GETDATE_PROC_H */ -- 1.7.5.4