]> git.notmuchmail.org Git - notmuch/blob - lib/regexp-fields.cc
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / lib / regexp-fields.cc
1 /* regexp-fields.cc - field processor glue for regex supporting fields
2  *
3  * This file is part of notmuch.
4  *
5  * Copyright © 2015 Austin Clements
6  * Copyright © 2016 David Bremner
7  *
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.
12  *
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.
17  *
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/ .
20  *
21  * Author: Austin Clements <aclements@csail.mit.edu>
22  *                David Bremner <david@tethera.net>
23  */
24
25 #include "regexp-fields.h"
26 #include "notmuch-private.h"
27 #include "database-private.h"
28
29 notmuch_status_t
30 compile_regex (regex_t &regexp, const char *str, std::string &msg)
31 {
32     int err = regcomp (&regexp, str, REG_EXTENDED | REG_NOSUB);
33
34     if (err != 0) {
35         size_t len = regerror (err, &regexp, NULL, 0);
36         char *buffer = new char[len];
37         msg = "Regexp error: ";
38         (void) regerror (err, &regexp, buffer, len);
39         msg.append (buffer, len);
40         delete[] buffer;
41
42         return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
43     }
44     return NOTMUCH_STATUS_SUCCESS;
45 }
46
47 RegexpPostingSource::RegexpPostingSource (Xapian::valueno slot, const std::string &regexp)
48     : slot_ (slot)
49 {
50     std::string msg;
51     notmuch_status_t status = compile_regex (regexp_, regexp.c_str (), msg);
52
53     if (status)
54         throw Xapian::QueryParserError (msg);
55 }
56
57 RegexpPostingSource::~RegexpPostingSource ()
58 {
59     regfree (&regexp_);
60 }
61
62 void
63 RegexpPostingSource::init (const Xapian::Database &db)
64 {
65     db_ = db;
66     it_ = db_.valuestream_begin (slot_);
67     end_ = db.valuestream_end (slot_);
68     started_ = false;
69 }
70
71 Xapian::doccount
72 RegexpPostingSource::get_termfreq_min () const
73 {
74     return 0;
75 }
76
77 Xapian::doccount
78 RegexpPostingSource::get_termfreq_est () const
79 {
80     return get_termfreq_max () / 2;
81 }
82
83 Xapian::doccount
84 RegexpPostingSource::get_termfreq_max () const
85 {
86     return db_.get_value_freq (slot_);
87 }
88
89 Xapian::docid
90 RegexpPostingSource::get_docid () const
91 {
92     return it_.get_docid ();
93 }
94
95 bool
96 RegexpPostingSource::at_end () const
97 {
98     return it_ == end_;
99 }
100
101 void
102 RegexpPostingSource::next (unused (double min_wt))
103 {
104     if (started_ && ! at_end ())
105         ++it_;
106     started_ = true;
107
108     for (; ! at_end (); ++it_) {
109         std::string value = *it_;
110         if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)
111             break;
112     }
113 }
114
115 void
116 RegexpPostingSource::skip_to (Xapian::docid did, unused (double min_wt))
117 {
118     started_ = true;
119     it_.skip_to (did);
120     for (; ! at_end (); ++it_) {
121         std::string value = *it_;
122         if (regexec (&regexp_, value.c_str (), 0, NULL, 0) == 0)
123             break;
124     }
125 }
126
127 bool
128 RegexpPostingSource::check (Xapian::docid did, unused (double min_wt))
129 {
130     started_ = true;
131     if (! it_.check (did) || at_end ())
132         return false;
133     return (regexec (&regexp_, (*it_).c_str (), 0, NULL, 0) == 0);
134 }
135
136 static inline Xapian::valueno
137 _find_slot (std::string prefix)
138 {
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;
145     else
146         return Xapian::BAD_VALUENO;
147 }
148
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_)),
154     field (field_),
155     term_prefix (_find_prefix (field_.c_str ())),
156     options (options_),
157     parser (parser_),
158     notmuch (notmuch_)
159 {
160 };
161
162 notmuch_status_t
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)
166 {
167     regex_t regexp;
168     notmuch_status_t status;
169
170     status = compile_regex (regexp, regexp_str.c_str (), msg);
171     if (status) {
172         _notmuch_database_log_append (notmuch, "error compiling regex %s", msg.c_str ());
173         return status;
174     }
175
176     if (slot == Xapian::BAD_VALUENO)
177         slot = _find_slot (field);
178
179     if (slot == Xapian::BAD_VALUENO) {
180         std::string term_prefix = _find_prefix (field.c_str ());
181         std::vector<std::string> terms;
182
183         for (Xapian::TermIterator it = notmuch->xapian_db->allterms_begin (term_prefix);
184              it != notmuch->xapian_db->allterms_end (); ++it) {
185             if (regexec (&regexp, (*it).c_str () + term_prefix.size (),
186                          0, NULL, 0) == 0)
187                 terms.push_back (*it);
188         }
189         output = Xapian::Query (Xapian::Query::OP_OR, terms.begin (), terms.end ());
190     } else {
191         RegexpPostingSource *postings = new RegexpPostingSource (slot, regexp_str);
192         output = Xapian::Query (postings->release ());
193     }
194     return NOTMUCH_STATUS_SUCCESS;
195 }
196
197 Xapian::Query
198 RegexpFieldProcessor::operator() (const std::string & str)
199 {
200     if (str.empty ()) {
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));
205         } else {
206             return Xapian::Query (term_prefix);
207         }
208     }
209
210     if (str.at (0) == '/') {
211         if (str.length () > 1 && str.at (str.size () - 1) == '/') {
212             Xapian::Query query;
213             std::string regexp_str = str.substr (1, str.size () - 2);
214             std::string msg;
215             notmuch_status_t status;
216
217             status = _notmuch_regexp_to_query (notmuch, slot, field, regexp_str, query, msg);
218             if (status)
219                 throw Xapian::QueryParserError (msg);
220             return query;
221         } else {
222             throw Xapian::QueryParserError ("unmatched regex delimiter in '" + str + "'");
223         }
224     } else {
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;
229
230             if (*str.rbegin () != '*' || str.find (' ') != std::string::npos)
231                 query_str = '"' + str + '"';
232             else
233                 query_str = str;
234
235             return parser.parse_query (query_str, NOTMUCH_QUERY_PARSER_FLAGS, term_prefix);
236         } else {
237             /* Boolean prefix */
238             std::string term = term_prefix + str;
239             return Xapian::Query (term);
240         }
241     }
242 }