1 /* regexp-fields.cc - field processor glue for regex supporting fields
3 * This file is part of notmuch.
5 * Copyright © 2015 Austin Clements
6 * Copyright © 2016 David Bremner
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses/ .
21 * Author: Austin Clements <aclements@csail.mit.edu>
22 * David Bremner <david@tethera.net>
25 #include "regexp-fields.h"
26 #include "notmuch-private.h"
27 #include "database-private.h"
30 compile_regex (regex_t ®exp, const char *str, std::string &msg)
32 int err = regcomp (®exp, str, REG_EXTENDED | REG_NOSUB);
35 size_t len = regerror (err, ®exp, NULL, 0);
36 char *buffer = new char[len];
37 msg = "Regexp error: ";
38 (void) regerror (err, ®exp, buffer, len);
39 msg.append (buffer, len);
42 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
44 return NOTMUCH_STATUS_SUCCESS;
47 RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string ®exp)
51 notmuch_status_t status = compile_regex (regexp_, regexp.c_str (), msg);
54 throw Xapian::QueryParserError (msg);
57 RegexpPostingSource::~RegexpPostingSource ()
63 RegexpPostingSource::init (const Xapian::Database &db)
66 it_ = db_.valuestream_begin (slot_);
67 end_ = db.valuestream_end (slot_);
72 RegexpPostingSource::get_termfreq_min () const
78 RegexpPostingSource::get_termfreq_est () const
80 return get_termfreq_max () / 2;
84 RegexpPostingSource::get_termfreq_max () const
86 return db_.get_value_freq (slot_);
90 RegexpPostingSource::get_docid () const
92 return it_.get_docid ();
96 RegexpPostingSource::at_end () const
102 RegexpPostingSource::next (unused (double min_wt))
104 if (started_ && ! at_end ())
108 for (; ! at_end (); ++it_) {
109 std::string value = *it_;
110 if (regexec (®exp_, value.c_str (), 0, NULL, 0) == 0)
116 RegexpPostingSource::skip_to (Xapian::docid did, unused (double min_wt))
120 for (; ! at_end (); ++it_) {
121 std::string value = *it_;
122 if (regexec (®exp_, value.c_str (), 0, NULL, 0) == 0)
128 RegexpPostingSource::check (Xapian::docid did, unused (double min_wt))
131 if (! it_.check (did) || at_end ())
133 return (regexec (®exp_, (*it_).c_str (), 0, NULL, 0) == 0);
136 static inline Xapian::valueno
137 _find_slot (std::string prefix)
139 if (prefix == "from")
140 return NOTMUCH_VALUE_FROM;
141 else if (prefix == "subject")
142 return NOTMUCH_VALUE_SUBJECT;
143 else if (prefix == "mid")
144 return NOTMUCH_VALUE_MESSAGE_ID;
146 return Xapian::BAD_VALUENO;
149 RegexpFieldProcessor::RegexpFieldProcessor (std::string field_,
150 notmuch_field_flag_t options_,
151 Xapian::QueryParser &parser_,
152 notmuch_database_t *notmuch_)
153 : slot (_find_slot (field_)),
155 term_prefix (_find_prefix (field_.c_str ())),
163 _notmuch_regexp_to_query (notmuch_database_t *notmuch, Xapian::valueno slot, std::string field,
164 std::string regexp_str,
165 Xapian::Query &output, std::string &msg)
168 notmuch_status_t status;
170 status = compile_regex (regexp, regexp_str.c_str (), msg);
172 _notmuch_database_log_append (notmuch, "error compiling regex %s", msg.c_str ());
176 if (slot == Xapian::BAD_VALUENO)
177 slot = _find_slot (field);
179 if (slot == Xapian::BAD_VALUENO) {
180 std::string term_prefix = _find_prefix (field.c_str ());
181 std::vector<std::string> terms;
183 for (Xapian::TermIterator it = notmuch->xapian_db->allterms_begin (term_prefix);
184 it != notmuch->xapian_db->allterms_end (); ++it) {
185 if (regexec (®exp, (*it).c_str () + term_prefix.size (),
187 terms.push_back (*it);
189 output = Xapian::Query (Xapian::Query::OP_OR, terms.begin (), terms.end ());
191 RegexpPostingSource *postings = new RegexpPostingSource (slot, regexp_str);
192 output = Xapian::Query (postings->release ());
194 return NOTMUCH_STATUS_SUCCESS;
198 RegexpFieldProcessor::operator() (const std::string & str)
201 if (options & NOTMUCH_FIELD_PROBABILISTIC) {
202 return Xapian::Query (Xapian::Query::OP_AND_NOT,
203 Xapian::Query::MatchAll,
204 Xapian::Query (Xapian::Query::OP_WILDCARD, term_prefix));
206 return Xapian::Query (term_prefix);
210 if (str.at (0) == '/') {
211 if (str.length () > 1 && str.at (str.size () - 1) == '/') {
213 std::string regexp_str = str.substr (1, str.size () - 2);
215 notmuch_status_t status;
217 status = _notmuch_regexp_to_query (notmuch, slot, field, regexp_str, query, msg);
219 throw Xapian::QueryParserError (msg);
222 throw Xapian::QueryParserError ("unmatched regex delimiter in '" + str + "'");
225 if (options & NOTMUCH_FIELD_PROBABILISTIC) {
226 /* TODO replace this with a nicer API level triggering of
227 * phrase parsing, when possible */
228 std::string query_str;
230 if ((str.at (0) != '(' || *str.rbegin () != ')') &&
231 (*str.rbegin () != '*' || str.find (' ') != std::string::npos))
232 query_str = '"' + str + '"';
236 return parser.parse_query (query_str, NOTMUCH_QUERY_PARSER_FLAGS, term_prefix);
239 std::string query_str;
242 if (str.length () > 1 && str.at (str.size () - 1) == '/')
243 query_str = str.substr (0, str.size () - 1);
247 term = term_prefix + query_str;
248 return Xapian::Query (term);