unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jani Nikula <jani@nikula.org>
To: notmuch@notmuchmail.org
Subject: [RFC PATCH 2/2] lib: add date range search
Date: Mon, 20 Feb 2012 00:55:52 +0200	[thread overview]
Message-ID: <c02a53f8e1d6b7d12f17b8b5e578885790a5ea46.1329689945.git.jani@nikula.org> (raw)
In-Reply-To: <cover.1329689945.git.jani@nikula.org>
In-Reply-To: <cover.1329689945.git.jani@nikula.org>

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

  parent reply	other threads:[~2012-02-19 22:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-19 22:55 [RFC PATCH 0/2] natural language date range search Jani Nikula
2012-02-19 22:55 ` [RFC PATCH 1/2] lib: add date/time parser Jani Nikula
2012-02-26  8:45   ` Mark Walters
2012-02-26 20:39     ` Jani Nikula
2012-02-19 22:55 ` Jani Nikula [this message]
2012-02-25 15:05 ` [RFC PATCH 0/2] natural language date range search Tomi Ollila
2012-02-25 19:53   ` Jani Nikula
2012-02-27  7:45     ` Tomi Ollila

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c02a53f8e1d6b7d12f17b8b5e578885790a5ea46.1329689945.git.jani@nikula.org \
    --to=jani@nikula.org \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).