diff options
| author | David Bremner <david@tethera.net> | 2017-02-26 22:34:20 -0400 |
|---|---|---|
| committer | David Bremner <david@tethera.net> | 2017-03-03 17:46:48 -0400 |
| commit | 55524bb063c95ae51a1762eb0b1aacce6ca49223 (patch) | |
| tree | 06ec5e27a5d20ca917bbff7e9d732ab94cff0fc2 /lib | |
| parent | 31b8ce4558de69860c95bf319a0a162316dce6c6 (diff) | |
lib: regexp matching in 'subject' and 'from'
the idea is that you can run
% notmuch search subject:/<your-favourite-regexp>/
% notmuch search from:/<your-favourite-regexp>/
or
% notmuch search subject:"your usual phrase search"
% notmuch search from:"usual phrase search"
This feature is only available with recent Xapian, specifically
support for field processors is needed.
It should work with bindings, since it extends the query parser.
This is easy to extend for other value slots, but currently the only
value slots are date, message_id, from, subject, and last_mod. Date is
already searchable; message_id is left for a followup commit.
This was originally written by Austin Clements, and ported to Xapian
field processors (from Austin's custom query parser) by yours truly.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Makefile.local | 1 | ||||
| -rw-r--r-- | lib/database.cc | 9 | ||||
| -rw-r--r-- | lib/regexp-fields.cc | 149 | ||||
| -rw-r--r-- | lib/regexp-fields.h | 77 |
4 files changed, 233 insertions, 3 deletions
diff --git a/lib/Makefile.local b/lib/Makefile.local index b77e5780..cd92fc79 100644 --- a/lib/Makefile.local +++ b/lib/Makefile.local @@ -52,6 +52,7 @@ libnotmuch_cxx_srcs = \ $(dir)/query.cc \ $(dir)/query-fp.cc \ $(dir)/config.cc \ + $(dir)/regexp-fields.cc \ $(dir)/thread.cc libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o) diff --git a/lib/database.cc b/lib/database.cc index fa4c3116..573c9fe0 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -21,6 +21,7 @@ #include "database-private.h" #include "parse-time-vrp.h" #include "query-fp.h" +#include "regexp-fields.h" #include "string-util.h" #include <iostream> @@ -277,7 +278,8 @@ prefix_t prefix_table[] = { NOTMUCH_FIELD_PROCESSOR }, #endif { "from", "XFROM", NOTMUCH_FIELD_EXTERNAL | - NOTMUCH_FIELD_PROBABILISTIC }, + NOTMUCH_FIELD_PROBABILISTIC | + NOTMUCH_FIELD_PROCESSOR }, { "to", "XTO", NOTMUCH_FIELD_EXTERNAL | NOTMUCH_FIELD_PROBABILISTIC }, { "attachment", "XATTACHMENT", NOTMUCH_FIELD_EXTERNAL | @@ -285,7 +287,8 @@ prefix_t prefix_table[] = { { "mimetype", "XMIMETYPE", NOTMUCH_FIELD_EXTERNAL | NOTMUCH_FIELD_PROBABILISTIC }, { "subject", "XSUBJECT", NOTMUCH_FIELD_EXTERNAL | - NOTMUCH_FIELD_PROBABILISTIC }, + NOTMUCH_FIELD_PROBABILISTIC | + NOTMUCH_FIELD_PROCESSOR}, }; static void @@ -309,7 +312,7 @@ _setup_query_field (const prefix_t *prefix, notmuch_database_t *notmuch) else if (STRNCMP_LITERAL(prefix->name, "query") == 0) fp = (new QueryFieldProcessor (*notmuch->query_parser, notmuch))->release (); else - INTERNAL_ERROR("unsupported field processor prefix: %s\n", prefix->name); + fp = (new RegexpFieldProcessor (prefix->name, *notmuch->query_parser, notmuch))->release (); /* we treat all field-processor fields as boolean in order to get the raw input */ notmuch->query_parser->add_boolean_prefix (prefix->name, fp); diff --git a/lib/regexp-fields.cc b/lib/regexp-fields.cc new file mode 100644 index 00000000..9873af80 --- /dev/null +++ b/lib/regexp-fields.cc @@ -0,0 +1,149 @@ +/* regexp-fields.cc - field processor glue for regex supporting fields + * + * This file is part of notmuch. + * + * Copyright © 2015 Austin Clements + * Copyright © 2016 David Bremner + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses/ . + * + * Author: Austin Clements <aclements@csail.mit.edu> + * David Bremner <david@tethera.net> + */ + +#include "regexp-fields.h" +#include "notmuch-private.h" +#include "database-private.h" + +#if HAVE_XAPIAN_FIELD_PROCESSOR +static void +compile_regex (regex_t ®exp, const char *str) +{ + int err = regcomp (®exp, str, REG_EXTENDED | REG_NOSUB); + + if (err != 0) { + size_t len = regerror (err, ®exp, NULL, 0); + char *buffer = new char[len]; + std::string msg; + (void) regerror (err, ®exp, buffer, len); + msg.assign (buffer, len); + delete buffer; + + throw Xapian::QueryParserError (msg); + } +} + +RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string ®exp) + : slot_ (slot) +{ + compile_regex (regexp_, regexp.c_str ()); +} + +RegexpPostingSource::~RegexpPostingSource () +{ + regfree (®exp_); +} + +void +RegexpPostingSource::init (const Xapian::Database &db) +{ + db_ = db; + it_ = db_.valuestream_begin (slot_); + end_ = db.valuestream_end (slot_); + started_ = false; + + /* make sure we start on a matching value */ + while (!at_end() && regexec (®exp_, (*it_).c_str (), 0, NULL, 0) != 0) { + ++it_; + } +} + +Xapian::doccount +RegexpPostingSource::get_termfreq_min () const +{ + return 0; +} + +Xapian::doccount +RegexpPostingSource::get_termfreq_est () const +{ + return get_termfreq_max () / 2; +} + +Xapian::doccount +RegexpPostingSource::get_termfreq_max () const +{ + return db_.get_value_freq (slot_); +} + +Xapian::docid +RegexpPostingSource::get_docid () const +{ + return it_.get_docid (); +} + +bool +RegexpPostingSource::at_end () const +{ + return it_ == end_; +} + +void +RegexpPostingSource::next (unused (double min_wt)) +{ + if (started_ && ! at_end ()) + ++it_; + started_ = true; + + for (; ! at_end (); ++it_) { + std::string value = *it_; + if (regexec (®exp_, value.c_str (), 0, NULL, 0) == 0) + break; + } +} + +static inline Xapian::valueno _find_slot (std::string prefix) +{ + if (prefix == "from") + return NOTMUCH_VALUE_FROM; + else if (prefix == "subject") + return NOTMUCH_VALUE_SUBJECT; + else + throw Xapian::QueryParserError ("unsupported regexp field '" + prefix + "'"); +} + +RegexpFieldProcessor::RegexpFieldProcessor (std::string prefix, Xapian::QueryParser &parser_, notmuch_database_t *notmuch_) + : slot (_find_slot (prefix)), term_prefix (_find_prefix (prefix.c_str ())), + parser (parser_), notmuch (notmuch_) +{ +}; + +Xapian::Query +RegexpFieldProcessor::operator() (const std::string & str) +{ + if (str.at (0) == '/') { + if (str.at (str.size () - 1) == '/'){ + RegexpPostingSource *postings = new RegexpPostingSource (slot, str.substr(1,str.size () - 2)); + return Xapian::Query (postings->release ()); + } else { + throw Xapian::QueryParserError ("unmatched regex delimiter in '" + str + "'"); + } + } else { + /* TODO replace this with a nicer API level triggering of + * phrase parsing, when possible */ + std::string quoted='"' + str + '"'; + return parser.parse_query (quoted, NOTMUCH_QUERY_PARSER_FLAGS, term_prefix); + } +} +#endif diff --git a/lib/regexp-fields.h b/lib/regexp-fields.h new file mode 100644 index 00000000..bac11999 --- /dev/null +++ b/lib/regexp-fields.h @@ -0,0 +1,77 @@ +/* regex-fields.h - xapian glue for semi-bruteforce regexp search + * + * This file is part of notmuch. + * + * Copyright © 2015 Austin Clements + * Copyright © 2016 David Bremner + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses/ . + * + * Author: Austin Clements <aclements@csail.mit.edu> + * David Bremner <david@tethera.net> + */ + +#ifndef NOTMUCH_REGEXP_FIELDS_H +#define NOTMUCH_REGEXP_FIELDS_H +#if HAVE_XAPIAN_FIELD_PROCESSOR +#include <sys/types.h> +#include <regex.h> +#include "database-private.h" +#include "notmuch-private.h" + +/* A posting source that returns documents where a value matches a + * regexp. + */ +class RegexpPostingSource : public Xapian::PostingSource +{ + protected: + const Xapian::valueno slot_; + regex_t regexp_; + Xapian::Database db_; + bool started_; + Xapian::ValueIterator it_, end_; + +/* No copying */ + RegexpPostingSource (const RegexpPostingSource &); + RegexpPostingSource &operator= (const RegexpPostingSource &); + + public: + RegexpPostingSource (Xapian::valueno slot, const std::string ®exp); + ~RegexpPostingSource (); + void init (const Xapian::Database &db); + Xapian::doccount get_termfreq_min () const; + Xapian::doccount get_termfreq_est () const; + Xapian::doccount get_termfreq_max () const; + Xapian::docid get_docid () const; + bool at_end () const; + void next (unused (double min_wt)); +}; + + +class RegexpFieldProcessor : public Xapian::FieldProcessor { + protected: + Xapian::valueno slot; + std::string term_prefix; + Xapian::QueryParser &parser; + notmuch_database_t *notmuch; + + public: + RegexpFieldProcessor (std::string prefix, Xapian::QueryParser &parser_, notmuch_database_t *notmuch_); + + ~RegexpFieldProcessor () { }; + + Xapian::Query operator()(const std::string & str); +}; +#endif +#endif /* NOTMUCH_REGEXP_FIELDS_H */ |
