[RFC PATCH 2/2] lib: add date range search
authorJani Nikula <jani@nikula.org>
Sun, 19 Feb 2012 22:55:52 +0000 (00:55 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:44 +0000 (09:44 -0800)
0a/1a7023309ba5ab50f2ec058a47035438827121 [new file with mode: 0644]

diff --git a/0a/1a7023309ba5ab50f2ec058a47035438827121 b/0a/1a7023309ba5ab50f2ec058a47035438827121
new file mode 100644 (file)
index 0000000..15fbd3e
--- /dev/null
@@ -0,0 +1,232 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id B28CA431FC2\r
+       for <notmuch@notmuchmail.org>; Sun, 19 Feb 2012 14:56:12 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id vyvV3C-Aj58G for <notmuch@notmuchmail.org>;\r
+       Sun, 19 Feb 2012 14:56:09 -0800 (PST)\r
+Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com\r
+       [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id E3E44431FC4\r
+       for <notmuch@notmuchmail.org>; Sun, 19 Feb 2012 14:56:03 -0800 (PST)\r
+Received: by mail-lpp01m010-f53.google.com with SMTP id d3so5943891lah.26\r
+       for <notmuch@notmuchmail.org>; Sun, 19 Feb 2012 14:56:03 -0800 (PST)\r
+Received-SPF: pass (google.com: domain of jani@nikula.org designates\r
+       10.112.9.40 as permitted sender) client-ip=10.112.9.40; \r
+Authentication-Results: mr.google.com;\r
+       spf=pass (google.com: domain of jani@nikula.org\r
+       designates 10.112.9.40 as permitted sender)\r
+       smtp.mail=jani@nikula.org\r
+Received: from mr.google.com ([10.112.9.40])\r
+       by 10.112.9.40 with SMTP id w8mr6460826lba.103.1329692163562 (num_hops\r
+       = 1); Sun, 19 Feb 2012 14:56:03 -0800 (PST)\r
+MIME-Version: 1.0\r
+Received: by 10.112.9.40 with SMTP id w8mr5364160lba.103.1329692163395;\r
+       Sun, 19 Feb 2012 14:56:03 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
+       [84.248.80.253])\r
+       by mx.google.com with ESMTPS id os5sm16890205lab.13.2012.02.19.14.56.01\r
+       (version=SSLv3 cipher=OTHER); Sun, 19 Feb 2012 14:56:02 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 2/2] lib: add date range search\r
+Date: Mon, 20 Feb 2012 00:55:52 +0200\r
+Message-Id:\r
+ <c02a53f8e1d6b7d12f17b8b5e578885790a5ea46.1329689945.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1329689945.git.jani@nikula.org>\r
+References: <cover.1329689945.git.jani@nikula.org>\r
+In-Reply-To: <cover.1329689945.git.jani@nikula.org>\r
+References: <cover.1329689945.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQmuB6fhLoAnO2mhYj8sv+c2RzNeuaYuciZ8N0BGETURxCsUrTg/kjocKBHFJiKmjTNKmaJR\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 19 Feb 2012 22:56:12 -0000\r
+\r
+Add a custom value range processor to enable date and time searches of\r
+the form date:since..until, where "since" and "until" are expressions\r
+understood by parse_time_string().\r
+\r
+If "since" or "until" describes date/time at an accuracy of days or\r
+less, the values are rounded according to the accuracy, towards past\r
+for "since" and towards future for "until". For example,\r
+date:november..yesterday would match from the beginning of November\r
+until the end of yesterday. Expressions such as date:today..today\r
+means since the beginning of today until the end of today.\r
+\r
+Open-ended ranges are supported (since Xapian 1.2.1), i.e. you can\r
+specify date:..until or date:since.. to not limit the start or end\r
+date, respectively.\r
+\r
+CAVEATS:\r
+\r
+Xapian does not support spaces in range expressions. You can replace\r
+the spaces with '_' or (in most cases) '-' or (in some cases) leave\r
+the spaces out altogether.\r
+\r
+Entering date:expr without ".." (for example date:yesterday) won't\r
+work. You can achieve the expected result by duplicating the expr both\r
+sides of ".." (for example date:yesterday..yesterday).\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ lib/Makefile.local     |    1 +\r
+ lib/database-private.h |    1 +\r
+ lib/database.cc        |    4 ++++\r
+ lib/getdate-proc.cc    |   34 ++++++++++++++++++++++++++++++++++\r
+ lib/getdate-proc.h     |   21 +++++++++++++++++++++\r
+ 5 files changed, 61 insertions(+), 0 deletions(-)\r
+ create mode 100644 lib/getdate-proc.cc\r
+ create mode 100644 lib/getdate-proc.h\r
+\r
+diff --git a/lib/Makefile.local b/lib/Makefile.local\r
+index 803a284..7dd1f7d 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -59,6 +59,7 @@ libnotmuch_c_srcs =          \\r
+ \r
+ libnotmuch_cxx_srcs =         \\r
+       $(dir)/database.cc      \\r
++      $(dir)/getdate-proc.cc  \\r
+       $(dir)/directory.cc     \\r
+       $(dir)/index.cc         \\r
+       $(dir)/message.cc       \\r
+diff --git a/lib/database-private.h b/lib/database-private.h\r
+index 88532d5..ba13dc7 100644\r
+--- a/lib/database-private.h\r
++++ b/lib/database-private.h\r
+@@ -52,6 +52,7 @@ struct _notmuch_database {\r
+     Xapian::QueryParser *query_parser;\r
+     Xapian::TermGenerator *term_gen;\r
+     Xapian::ValueRangeProcessor *value_range_processor;\r
++    Xapian::ValueRangeProcessor *getdate_proc;\r
+ };\r
+ \r
+ /* Return the list of terms from the given iterator matching a prefix.\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index c928d02..a3f8adb 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -19,6 +19,7 @@\r
+  */\r
+ \r
+ #include "database-private.h"\r
++#include "getdate-proc.h"\r
+ \r
+ #include <iostream>\r
+ \r
+@@ -682,12 +683,14 @@ notmuch_database_open (const char *path,\r
+       notmuch->term_gen = new Xapian::TermGenerator;\r
+       notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));\r
+       notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);\r
++      notmuch->getdate_proc = new GetDateValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP, "date:", true);\r
+ \r
+       notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);\r
+       notmuch->query_parser->set_database (*notmuch->xapian_db);\r
+       notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));\r
+       notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);\r
+       notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor);\r
++      notmuch->query_parser->add_valuerangeprocessor (notmuch->getdate_proc);\r
+ \r
+       for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {\r
+           prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i];\r
+@@ -729,6 +732,7 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
+     delete notmuch->query_parser;\r
+     delete notmuch->xapian_db;\r
+     delete notmuch->value_range_processor;\r
++    delete notmuch->getdate_proc;\r
+     talloc_free (notmuch);\r
+ }\r
+ \r
+diff --git a/lib/getdate-proc.cc b/lib/getdate-proc.cc\r
+new file mode 100644\r
+index 0000000..31f8f03\r
+--- /dev/null\r
++++ b/lib/getdate-proc.cc\r
+@@ -0,0 +1,34 @@\r
++\r
++#include "database-private.h"\r
++#include "getdate-proc.h"\r
++#include "parse-time-string.h"\r
++\r
++/* see *ValueRangeProcessor in xapian-core/api/valuerangeproc.cc */\r
++Xapian::valueno\r
++GetDateValueRangeProcessor::operator() (std::string &begin, std::string &end)\r
++{\r
++    time_t t, now;\r
++\r
++    if (Xapian::StringValueRangeProcessor::operator() (begin, end) == Xapian::BAD_VALUENO)\r
++      return Xapian::BAD_VALUENO;\r
++\r
++    /* use the same 'now' for begin and end */\r
++    if (time (&now) == (time_t) -1)\r
++      return Xapian::BAD_VALUENO;\r
++\r
++    if (!begin.empty ()) {\r
++      if (parse_time_string (begin.c_str (), &t, &now, PARSE_TIME_ROUND_DOWN))\r
++          return Xapian::BAD_VALUENO;\r
++\r
++      begin.assign (Xapian::sortable_serialise ((double) t));\r
++    }\r
++\r
++    if (!end.empty ()) {\r
++      if (parse_time_string (end.c_str (), &t, &now, PARSE_TIME_ROUND_UP))\r
++          return Xapian::BAD_VALUENO;\r
++\r
++      end.assign (Xapian::sortable_serialise ((double) t));\r
++    }\r
++\r
++    return valno;\r
++}\r
+diff --git a/lib/getdate-proc.h b/lib/getdate-proc.h\r
+new file mode 100644\r
+index 0000000..351d06e\r
+--- /dev/null\r
++++ b/lib/getdate-proc.h\r
+@@ -0,0 +1,21 @@\r
++\r
++#ifndef NOTMUCH_GETDATE_PROC_H\r
++#define NOTMUCH_GETDATE_PROC_H\r
++\r
++#include <xapian.h>\r
++\r
++/* see *ValueRangeProcessor in xapian-core/include/xapian/queryparser.h */\r
++class GetDateValueRangeProcessor : public Xapian::StringValueRangeProcessor {\r
++public:\r
++    GetDateValueRangeProcessor (Xapian::valueno slot_)\r
++      : StringValueRangeProcessor (slot_) { }\r
++\r
++    GetDateValueRangeProcessor (Xapian::valueno slot_,\r
++                              const std::string &str_,\r
++                              bool prefix_ = true)\r
++      : StringValueRangeProcessor (slot_, str_, prefix_) { }\r
++\r
++    Xapian::valueno operator() (std::string &begin, std::string &end);\r
++};\r
++\r
++#endif /* NOTMUCH_GETDATE_PROC_H */\r
+-- \r
+1.7.5.4\r
+\r