Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id B28CA431FC2 for ; Sun, 19 Feb 2012 14:56:12 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vyvV3C-Aj58G for ; Sun, 19 Feb 2012 14:56:09 -0800 (PST) Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id E3E44431FC4 for ; Sun, 19 Feb 2012 14:56:03 -0800 (PST) Received: by mail-lpp01m010-f53.google.com with SMTP id d3so5943891lah.26 for ; Sun, 19 Feb 2012 14:56:03 -0800 (PST) Received-SPF: pass (google.com: domain of jani@nikula.org designates 10.112.9.40 as permitted sender) client-ip=10.112.9.40; Authentication-Results: mr.google.com; spf=pass (google.com: domain of jani@nikula.org designates 10.112.9.40 as permitted sender) smtp.mail=jani@nikula.org Received: from mr.google.com ([10.112.9.40]) by 10.112.9.40 with SMTP id w8mr6460826lba.103.1329692163562 (num_hops = 1); Sun, 19 Feb 2012 14:56:03 -0800 (PST) MIME-Version: 1.0 Received: by 10.112.9.40 with SMTP id w8mr5364160lba.103.1329692163395; Sun, 19 Feb 2012 14:56:03 -0800 (PST) Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi. [84.248.80.253]) by mx.google.com with ESMTPS id os5sm16890205lab.13.2012.02.19.14.56.01 (version=SSLv3 cipher=OTHER); Sun, 19 Feb 2012 14:56:02 -0800 (PST) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [RFC PATCH 2/2] lib: add date range search Date: Mon, 20 Feb 2012 00:55:52 +0200 Message-Id: X-Mailer: git-send-email 1.7.5.4 In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQmuB6fhLoAnO2mhYj8sv+c2RzNeuaYuciZ8N0BGETURxCsUrTg/kjocKBHFJiKmjTNKmaJR X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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, 19 Feb 2012 22:56:12 -0000 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 --- 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 @@ -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 + +/* 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